Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoTokenizer,
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
model =
|
| 9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
model.to(device)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
language_prompts = {
|
| 14 |
-
"Python": "
|
| 15 |
-
"C": "
|
| 16 |
-
"C++": "
|
| 17 |
-
"JavaScript": "
|
| 18 |
}
|
| 19 |
|
| 20 |
-
#
|
| 21 |
def eternos_debugger(code, error, language):
|
| 22 |
if not code.strip():
|
| 23 |
-
return "β Please
|
| 24 |
-
prompt =
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
.gr-button {
|
| 53 |
-
background-color: #2F80ED !important;
|
| 54 |
-
color: white !important;
|
| 55 |
-
font-weight: bold;
|
| 56 |
-
border-radius: 8px;
|
| 57 |
-
padding: 10px 16px;
|
| 58 |
-
}
|
| 59 |
-
""") as demo:
|
| 60 |
-
with gr.Column():
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
gr.Markdown("<div class='title-text'> Eternos Beta β A Coder's Friend </div>")
|
| 64 |
-
|
| 65 |
-
with gr.Row():
|
| 66 |
-
language_input = gr.Dropdown(["Python", "C", "C++", "JavaScript"], label="π Language", value="Python")
|
| 67 |
-
|
| 68 |
-
with gr.Row():
|
| 69 |
-
code_input = gr.Textbox(label="π Your Code", lines=12, placeholder="Paste or write buggy code here...")
|
| 70 |
-
error_input = gr.Textbox(label="β οΈ Error Message (optional)", lines=4, placeholder="Paste the error traceback here (optional)")
|
| 71 |
-
|
| 72 |
-
run_btn = gr.Button("π§ Fix Code")
|
| 73 |
-
output_code = gr.Code(label="β
Suggested Fix")
|
| 74 |
-
|
| 75 |
-
run_btn.click(fn=eternos_debugger, inputs=[code_input, error_input, language_input], outputs=output_code)
|
| 76 |
|
| 77 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
+
# Load CodeT5+ model and tokenizer
|
| 6 |
+
model_id = "Salesforce/codet5p-770m"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
model.to(device)
|
| 11 |
|
| 12 |
+
# Prompt templates for supported languages
|
| 13 |
language_prompts = {
|
| 14 |
+
"Python": "Fix the following Python code:\n",
|
| 15 |
+
"C": "Fix the following C code:\n",
|
| 16 |
+
"C++": "Fix the following C++ code:\n",
|
| 17 |
+
"JavaScript": "Fix the following JavaScript code:\n"
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# Debug function
|
| 21 |
def eternos_debugger(code, error, language):
|
| 22 |
if not code.strip():
|
| 23 |
+
return "β Please enter some code to debug."
|
| 24 |
+
prompt = language_prompts[language] + code + "\nError:\n" + error + "\nFixed Code:\n"
|
| 25 |
+
|
| 26 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(device)
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model.generate(
|
| 29 |
+
**inputs,
|
| 30 |
+
max_new_tokens=256,
|
| 31 |
+
temperature=0.3,
|
| 32 |
+
top_p=0.9,
|
| 33 |
+
do_sample=True
|
| 34 |
+
)
|
| 35 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
+
return result.strip()
|
| 37 |
+
|
| 38 |
+
# Gradio Interface
|
| 39 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 40 |
+
gr.Markdown("## Eternos β A Coder,s Friend")
|
| 41 |
+
gr.Markdown("Supports Python, C, C++, JavaScript β Powered by CodeT5+")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
code_input = gr.Textbox(label="π¨βπ» Your Code", lines=14, placeholder="Paste your buggy code here...")
|
| 45 |
+
error_input = gr.Textbox(label="β οΈ Error Message (optional)", lines=4)
|
| 46 |
+
|
| 47 |
+
language_input = gr.Dropdown(["Python", "C", "C++", "JavaScript"], label="π Language", value="Python")
|
| 48 |
+
output_code = gr.Code(label="β
Suggested Fix")
|
| 49 |
+
run_btn = gr.Button("π Debug Code")
|
| 50 |
+
|
| 51 |
+
run_btn.click(fn=eternos_debugger, inputs=[code_input, error_input, language_input], outputs=output_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
demo.launch()
|