Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,29 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
-
#
|
| 6 |
model_name = "Salesforce/codet5-small"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
prompt = f"Fix this Python code:\n{code}\nError:\n{error}\nFixed Code:"
|
| 20 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 21 |
|
| 22 |
with torch.no_grad():
|
|
@@ -25,38 +33,28 @@ def eternos_debugger(code, error):
|
|
| 25 |
max_length=128,
|
| 26 |
num_beams=2,
|
| 27 |
early_stopping=True,
|
| 28 |
-
temperature=0.7
|
| 29 |
)
|
| 30 |
|
| 31 |
fixed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
return fixed_code.strip()
|
| 33 |
|
| 34 |
-
# Gradio
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
[
|
| 54 |
-
"def sqr(x):\n return x*x\n\nprint(sqre(5))",
|
| 55 |
-
"NameError: name 'sqre' is not defined"
|
| 56 |
-
]
|
| 57 |
-
],
|
| 58 |
-
theme="soft",
|
| 59 |
-
allow_flagging="never"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
interface.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
+
# Use light model for fast inference
|
| 6 |
model_name = "Salesforce/codet5-small"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Auto-detect device
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
+
# Supported languages and file extensions
|
| 15 |
+
language_prompts = {
|
| 16 |
+
"Python": "Fix the following Python code and provide the corrected version:",
|
| 17 |
+
"C": "Fix the following C code and provide the corrected version:",
|
| 18 |
+
"C++": "Fix the following C++ code and provide the corrected version:",
|
| 19 |
+
"JavaScript": "Fix the following JavaScript code and provide the corrected version:"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def eternos_debugger(code, error, language):
|
| 23 |
+
if not code.strip() or not language.strip():
|
| 24 |
+
return "β Please enter code and select a language."
|
| 25 |
+
|
| 26 |
+
prompt = f"{language_prompts[language]}\n{code}\nError:\n{error}\nCorrected Code:"
|
| 27 |
|
|
|
|
| 28 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 29 |
|
| 30 |
with torch.no_grad():
|
|
|
|
| 33 |
max_length=128,
|
| 34 |
num_beams=2,
|
| 35 |
early_stopping=True,
|
| 36 |
+
temperature=0.7
|
| 37 |
)
|
| 38 |
|
| 39 |
fixed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
return fixed_code.strip()
|
| 41 |
|
| 42 |
+
# Gradio Interface
|
| 43 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), css="body {background-color: black !important;}") as demo:
|
| 44 |
+
gr.Markdown("<h1 style='color: white;'>βοΈ Eternos β AI Code Debugger</h1>")
|
| 45 |
+
gr.Markdown("<p style='color: grey;'>Fast AI-powered debugger for Python, C, C++, and JavaScript</p>")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
code_input = gr.Textbox(label="π Your Code", lines=15, placeholder="Paste your buggy code here...")
|
| 49 |
+
error_input = gr.Textbox(label="β οΈ Error Message", lines=4, placeholder="Paste error (optional)...")
|
| 50 |
+
|
| 51 |
+
language_input = gr.Dropdown(choices=["Python", "C", "C++", "JavaScript"], label="π Language", value="Python")
|
| 52 |
+
|
| 53 |
+
output_code = gr.Code(label="β
Suggested Fix")
|
| 54 |
+
|
| 55 |
+
run_button = gr.Button("π οΈ Fix Code")
|
| 56 |
+
|
| 57 |
+
run_button.click(fn=eternos_debugger, inputs=[code_input, error_input, language_input], outputs=output_code)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|
| 60 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|