Team Ai
Apppublic

Sushyy/bug-explainer-ml

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
analyzer.py252 linesDownload Raw Back to root
1# import json2 3# def analyze_code(language, code, tokenizer, model):4#     messages = [5#         {6#             "role": "system",7#             "content": (8#                 "You are a helpful and expert-level AI code reviewer and bug fixer. "9#                 "Your task is to analyze the given buggy code in the specified programming language, "10#                 "identify bugs (logical, syntax, runtime, etc.), and fix them. "11#                 "Return a JSON object with the following keys:\n\n"12#                 "1. 'bug_analysis': a list of objects, each containing:\n"13#                 "   - 'line_number': the line number (approximate if needed)\n"14#                 "   - 'error_message': a short name of the bug\n"15#                 "   - 'explanation': short explanation of the problem\n"16#                 "   - 'fix_suggestion': how to fix it\n"17#                 "2. 'corrected_code': the entire corrected code block.\n\n"18#                 "Respond with ONLY the raw JSON object, no extra commentary or markdown."19#             )20#         },21#         {22#             "role": "user",23#             "content": f"๐Ÿ’ป Language: {language}\n๐Ÿž Buggy Code:\n```{language.lower()}\n{code.strip()}\n```"24#         }25#     ]26 27#     inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)28#     attention_mask = (inputs != tokenizer.pad_token_id).long()29 30#     outputs = model.generate(31#         inputs,32#         attention_mask=attention_mask,33#         max_new_tokens=1024,34#         do_sample=False,35#         pad_token_id=tokenizer.eos_token_id,36#         eos_token_id=tokenizer.eos_token_id37#     )38 39#     response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)40 41#     # Try parsing response to JSON42#     try:43#         json_output = json.loads(response)44#         return json_output45#     except json.JSONDecodeError:46#         print("โš ๏ธ Could not decode response into JSON. Here's the raw output:\n")47#         print(response)48#         return None49# import json50# import logging51# import time52# import torch53 54# # Configure logging55# logger = logging.getLogger(__name__)56 57# def analyze_code(language, code, tokenizer, model):58#     """59#     Analyze code and return bug analysis with improved logging and error handling60#     """61#     start_time = time.time()62#     logger.info(f"๐Ÿ” Starting analysis for {language} code ({len(code)} characters)")63    64#     try:65#         # Prepare messages66#         messages = [67#             {68#                 "role": "system",69#                 "content": (70#                     "You are a helpful and expert-level AI code reviewer and bug fixer. "71#                     "Your task is to analyze the given buggy code in the specified programming language, "72#                     "identify bugs (logical, syntax, runtime, etc.), and fix them. "73#                     "Return a JSON object with the following keys:\n\n"74#                     "1. 'bug_analysis': a list of objects, each containing:\n"75#                     "   - 'line_number': the line number (approximate if needed)\n"76#                     "   - 'error_message': a short name of the bug\n"77#                     "   - 'explanation': short explanation of the problem\n"78#                     "   - 'fix_suggestion': how to fix it\n"79#                     "2. 'corrected_code': the entire corrected code block.\n\n"80#                     "Respond with ONLY the raw JSON object, no extra commentary or markdown."81#                 )82#             },83#             {84#                 "role": "user",85#                 "content": f"๐Ÿ’ป Language: {language}\n๐Ÿž Buggy Code:\n```{language.lower()}\n{code.strip()}\n```"86#             }87#         ]88 89#         logger.info("๐Ÿ”ง Applying chat template...")90#         inputs = tokenizer.apply_chat_template(91#             messages, 92#             add_generation_prompt=True, 93#             return_tensors="pt"94#         ).to(model.device)95        96#         attention_mask = (inputs != tokenizer.pad_token_id).long()97        98#         logger.info(f"๐Ÿ“ Input length: {inputs.shape[1]} tokens")99#         logger.info("๐Ÿš€ Starting model generation...")100        101#         generation_start = time.time()102        103#         # Generate with more conservative settings104#         with torch.no_grad():  # Ensure no gradients are computed105#             outputs = model.generate(106#                 inputs,107#                 attention_mask=attention_mask,108#                 max_new_tokens=512,  # Reduced from 1024 for faster inference109#                 do_sample=False,110#                 temperature=0.1,  # Add temperature for more consistent output111#                 pad_token_id=tokenizer.eos_token_id,112#                 eos_token_id=tokenizer.eos_token_id,113#                 use_cache=True,  # Enable KV cache for efficiency114#             )115        116#         generation_time = time.time() - generation_start117#         logger.info(f"โšก Generation completed in {generation_time:.2f} seconds")118        119#         logger.info("๐Ÿ“ Decoding response...")120#         response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)121        122#         logger.info(f"๐Ÿ“„ Response length: {len(response)} characters")123#         logger.info(f"๐Ÿ” First 100 chars: {response[:100]}...")124 125#         # Try parsing response to JSON126#         logger.info("๐Ÿ” Attempting to parse JSON...")127#         try:128#             # Clean up response - remove any markdown formatting129#             cleaned_response = response.strip()130#             if cleaned_response.startswith('```json'):131#                 cleaned_response = cleaned_response[7:]132#             if cleaned_response.startswith('```'):133#                 cleaned_response = cleaned_response[3:]134#             if cleaned_response.endswith('```'):135#                 cleaned_response = cleaned_response[:-3]136            137#             cleaned_response = cleaned_response.strip()138            139#             json_output = json.loads(cleaned_response)140            141#             total_time = time.time() - start_time142#             logger.info(f"โœ… Analysis completed successfully in {total_time:.2f} seconds")143            144#             # Validate the JSON structure145#             if not isinstance(json_output, dict):146#                 raise ValueError("Response is not a dictionary")147                148#             if 'bug_analysis' not in json_output:149#                 logger.warning("โš ๏ธ Missing 'bug_analysis' key, adding empty list")150#                 json_output['bug_analysis'] = []151                152#             if 'corrected_code' not in json_output:153#                 logger.warning("โš ๏ธ Missing 'corrected_code' key, adding original code")154#                 json_output['corrected_code'] = code155            156#             return json_output157            158#         except json.JSONDecodeError as e:159#             logger.error(f"โŒ JSON decode error: {e}")160#             logger.error(f"๐Ÿ“„ Raw response: {repr(response)}")161            162#             # Return a fallback structure with the raw response163#             fallback_response = {164#                 "bug_analysis": [{165#                     "line_number": 1,166#                     "error_message": "Analysis parsing failed",167#                     "explanation": "The AI model returned a response that couldn't be parsed as JSON",168#                     "fix_suggestion": "Please try again or check the code format"169#                 }],170#                 "corrected_code": code,171#                 "raw_output": response,172#                 "parsing_error": str(e)173#             }174            175#             return fallback_response176            177#     except Exception as e:178#         total_time = time.time() - start_time179#         logger.error(f"โŒ Analysis failed after {total_time:.2f} seconds: {str(e)}")180#         logger.error(f"๐Ÿ’ฅ Exception type: {type(e).__name__}")181        182#         # Return error response183#         return {184#             "bug_analysis": [{185#                 "line_number": 1,186#                 "error_message": "Analysis failed",187#                 "explanation": f"An error occurred during analysis: {str(e)}",188#                 "fix_suggestion": "Please try again or contact support"189#             }],190#             "corrected_code": code,191#             "error": str(e),192#             "error_type": type(e).__name__193#         }194 195# analyzer.py196# analyzer.py197 198import torch199import json200import time201import logging202 203# Configure logger204logger = logging.getLogger("CodeAnalyzer")205logger.setLevel(logging.INFO)206handler = logging.StreamHandler()207formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] - %(message)s")208handler.setFormatter(formatter)209logger.addHandler(handler)210 211def analyze_code(tokenizer, model, language, code):212    """213    Analyze and fix buggy code using CodeT5+ model with 'fix:' prompt prefix.214    Works across multiple programming languages.215    """216    start_time = time.time()217 218    # Prepare prompt in CodeT5+ style219    prompt = f"fix: {code.strip()}"220 221    logger.info(f"๐Ÿ” Starting analysis for language: {language}")222    logger.info(f"๐Ÿงพ Prompt: {prompt[:80]}...")223 224    try:225        # Tokenize and generate response226        inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)227        with torch.no_grad():228            output = model.generate(**inputs, max_new_tokens=1024)229 230        # Decode output231        response = tokenizer.decode(output[0], skip_special_tokens=True).strip()232 233        elapsed = round(time.time() - start_time, 2)234        logger.info(f"โœ… Inference completed in {elapsed}s")235 236        return {237            "bug_analysis": [],  # Optional: You could add heuristics here238            "corrected_code": response239        }240 241    except Exception as e:242        logger.error(f"โŒ Error during analysis: {e}")243        return {244            "bug_analysis": [{245                "line_number": 0,246                "error_message": "Inference failed",247                "explanation": str(e),248                "fix_suggestion": "Try again with simpler code or retry later"249            }],250            "corrected_code": code251        }252