Tanveerooooooo commited on
Commit
7235cb4
Β·
verified Β·
1 Parent(s): ace22c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -64
app.py CHANGED
@@ -1,77 +1,53 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
- # Load StarCoder model
6
- model_name = "bigcode/starcoder"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
  model.to(device)
11
 
12
- # Prompts per language
13
  language_prompts = {
14
- "Python": "### Fix Python code:\n### Buggy code:\n",
15
- "C": "### Fix C code:\n### Buggy code:\n",
16
- "C++": "### Fix C++ code:\n### Buggy code:\n",
17
- "JavaScript": "### Fix JavaScript code:\n### Buggy code:\n"
18
  }
19
 
20
- # Debugging function
21
  def eternos_debugger(code, error, language):
22
  if not code.strip():
23
- return "❌ Please provide code."
24
- prompt = f"{language_prompts[language]}{code}\n### Error:\n{error}\n### Corrected code:\n"
25
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=8192).to(device)
26
- outputs = model.generate(
27
- **inputs,
28
- max_new_tokens=256,
29
- temperature=0.2,
30
- top_p=0.95,
31
- do_sample=False,
32
- pad_token_id=tokenizer.eos_token_id
33
- )
34
- resp = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
35
- return resp.strip()
36
-
37
- # Gradio UI
38
- with gr.Blocks(css="""
39
- #logo-container {
40
- display: flex;
41
- justify-content: center;
42
- margin-top: 10px;
43
- }
44
- .title-text {
45
- text-align: center;
46
- font-size: 28px;
47
- font-weight: bold;
48
- color: #222;
49
- margin-top: 10px;
50
- margin-bottom: 30px;
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()