Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| import torch | |
| # Load smaller CodeT5 model (faster) | |
| model_name = "Salesforce/codet5-small" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
| # Main function | |
| def eternos_debugger(code, error): | |
| if not code.strip(): | |
| return "β Please provide some code." | |
| if not error.strip(): | |
| return "β Please provide the error message you encountered." | |
| # Smart prompting | |
| prompt = ( | |
| f"You are an expert Python debugger.\n" | |
| f"Given the buggy code and the error message, fix the code.\n\n" | |
| f"Code:\n{code}\n\n" | |
| f"Error:\n{error}\n\n" | |
| f"Corrected Code:" | |
| ) | |
| inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_length=512, | |
| num_beams=4, | |
| early_stopping=True, | |
| temperature=0.7, | |
| top_p=0.95 | |
| ) | |
| fixed_code = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return fixed_code.strip() | |
| # Gradio UI | |
| interface = gr.Interface( | |
| fn=eternos_debugger, | |
| inputs=[ | |
| gr.Textbox(label="π Buggy Code", lines=12, placeholder="Paste your Python code here..."), | |
| gr.Textbox(label="π¨ Error Message", lines=3, placeholder="Paste the error message you got...") | |
| ], | |
| outputs=gr.Code(label="β Suggested Fixed Code"), | |
| title="π οΈ Eternos: AI Code Debugger", | |
| description="Eternos uses a CodeT5 model to help debug and fix Python code. Provide your code and the error message to get a fix.", | |
| theme="soft", | |
| allow_flagging="never", | |
| examples=[ | |
| [ | |
| "def add_numbers(a, b)\n return a + b", | |
| "SyntaxError: expected ':'" | |
| ], | |
| [ | |
| "for i in range(5)\n print(i)", | |
| "SyntaxError: expected ':'" | |
| ], | |
| [ | |
| "def divide(a, b):\n return a / b\nprint(divide(4, 0))", | |
| "ZeroDivisionError: division by zero" | |
| ] | |
| ] | |
| ) | |
| interface.launch() | |