Tanveerooooooo commited on
Commit
2c5e1cd
Β·
verified Β·
1 Parent(s): 8f21ee7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -37
app.py CHANGED
@@ -2,21 +2,29 @@ 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():
@@ -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 UI
35
- interface = gr.Interface(
36
- fn=eternos_debugger,
37
- inputs=[
38
- gr.Textbox(label="🐞 Buggy Code", lines=12, placeholder="Paste your Python code here..."),
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()
 
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
+