Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
model_name = "Salesforce/codet5-small"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def eternos_debugger(code, error):
|
| 12 |
-
if not code.strip():
|
| 13 |
-
return "β Please
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Smart prompting
|
| 18 |
-
prompt = (
|
| 19 |
-
f"You are an expert Python debugger.\n"
|
| 20 |
-
f"Given the buggy code and the error message, fix the code.\n\n"
|
| 21 |
-
f"Code:\n{code}\n\n"
|
| 22 |
-
f"Error:\n{error}\n\n"
|
| 23 |
-
f"Corrected Code:"
|
| 24 |
-
)
|
| 25 |
|
| 26 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model.generate(
|
| 29 |
**inputs,
|
| 30 |
-
max_length=
|
| 31 |
-
num_beams=
|
| 32 |
early_stopping=True,
|
| 33 |
temperature=0.7,
|
| 34 |
-
top_p=0.95
|
| 35 |
)
|
| 36 |
-
|
| 37 |
fixed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
return fixed_code.strip()
|
| 39 |
|
|
@@ -45,24 +39,24 @@ interface = gr.Interface(
|
|
| 45 |
gr.Textbox(label="π¨ Error Message", lines=3, placeholder="Paste the error message you got...")
|
| 46 |
],
|
| 47 |
outputs=gr.Code(label="β
Suggested Fixed Code"),
|
| 48 |
-
title="
|
| 49 |
-
description="Eternos uses a CodeT5 model to
|
| 50 |
-
theme="soft",
|
| 51 |
-
allow_flagging="never",
|
| 52 |
examples=[
|
| 53 |
[
|
| 54 |
-
"def
|
| 55 |
"SyntaxError: expected ':'"
|
| 56 |
],
|
| 57 |
[
|
| 58 |
-
"
|
| 59 |
-
"
|
| 60 |
],
|
| 61 |
[
|
| 62 |
-
"def
|
| 63 |
-
"
|
| 64 |
]
|
| 65 |
-
]
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
+
# Load optimized model
|
| 6 |
model_name = "Salesforce/codet5-small"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Use GPU if available
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model.to(device)
|
| 13 |
+
|
| 14 |
+
# Eternos Fixer Function
|
| 15 |
def eternos_debugger(code, error):
|
| 16 |
+
if not code.strip() or not error.strip():
|
| 17 |
+
return "β Please enter both code and an error message."
|
| 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():
|
| 23 |
outputs = model.generate(
|
| 24 |
**inputs,
|
| 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 |
|
|
|
|
| 39 |
gr.Textbox(label="π¨ Error Message", lines=3, placeholder="Paste the error message you got...")
|
| 40 |
],
|
| 41 |
outputs=gr.Code(label="β
Suggested Fixed Code"),
|
| 42 |
+
title="βοΈ Eternos β AI Code Debugger",
|
| 43 |
+
description="Eternos uses a CodeT5-small model to analyze your Python code and error, then suggest a possible fix.",
|
|
|
|
|
|
|
| 44 |
examples=[
|
| 45 |
[
|
| 46 |
+
"def greet(name)\n print('Hello', name)",
|
| 47 |
"SyntaxError: expected ':'"
|
| 48 |
],
|
| 49 |
[
|
| 50 |
+
"def divide(a, b):\n return a / b\n\nprint(divide(5, 0))",
|
| 51 |
+
"ZeroDivisionError: division by zero"
|
| 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()
|