venky2k1 commited on
Commit
22b22ed
Β·
1 Parent(s): 6e3deff

Upgraded to CodeT5 for full code bug fixing

Browse files
Files changed (4) hide show
  1. README.md +6 -5
  2. app_ui.py +11 -20
  3. fix_generator.py +7 -11
  4. full_code_fixer.py +18 -0
README.md CHANGED
@@ -1,10 +1,11 @@
1
  ---
2
- title: Bug Fixer
3
- emoji: "πŸ”§"
4
- colorFrom: red
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 4.33.0
8
  app_file: app_ui.py
9
  pinned: false
10
- ---
 
 
1
  ---
2
+ title: Bug Fixer - CodeT5
3
+ emoji: 🧠
4
+ colorFrom: pink
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 4.33.0
8
  app_file: app_ui.py
9
  pinned: false
10
+ ---
11
+ s
app_ui.py CHANGED
@@ -1,27 +1,18 @@
1
  import gradio as gr
2
- from bug_detector import detect_bug
3
- from fix_generator import generate_fix
4
 
5
- def bug_fixer(code):
6
- bug_status = detect_bug(code)
7
- fixed_code = generate_fix(code)
8
- return bug_status, fixed_code
9
 
10
- title = "πŸ”§ Bug Detection and Fixing Tool"
 
 
 
 
11
 
12
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
13
- gr.Markdown("""
14
- # πŸ”§ Bug Detection and Fixing Tool
15
- **Final Year Project - Guided by: Prof. Alakananda K P**
16
- **Team: Akanksha K P and Divyashree N**
17
- """)
18
 
19
- code_input = gr.Textbox(lines=8, label="Enter Buggy Code")
20
- bug_output = gr.Textbox(label="Detected Bug")
21
- fix_output = gr.Textbox(label="Suggested Fix")
22
- btn = gr.Button("Detect and Fix")
23
 
24
- btn.click(fn=bug_fixer, inputs=code_input, outputs=[bug_output, fix_output])
25
-
26
- demo.launch(share=True)
27
  #for the gradio user interface
 
1
  import gradio as gr
2
+ from full_code_fixer import fix_code
 
3
 
4
+ def fix_script(code):
5
+ fixed = fix_code(code)
6
+ return "Analyzed", fixed
 
7
 
8
+ with gr.Blocks() as demo:
9
+ gr.Markdown("## πŸ› οΈ Bug Detection and Fixing Tool - Full Script Version")
10
+ code_input = gr.Textbox(lines=20, label="Paste Your Python Code Here")
11
+ bug_status = gr.Textbox(label="Status")
12
+ fixed_code = gr.Textbox(lines=20, label="Suggested Fix")
13
 
14
+ gr.Button("Detect and Fix").click(fix_script, inputs=code_input, outputs=[bug_status, fixed_code])
 
 
 
 
 
15
 
16
+ demo.launch()
 
 
 
17
 
 
 
 
18
  #for the gradio user interface
fix_generator.py CHANGED
@@ -1,20 +1,16 @@
1
  def generate_fix(code):
2
- lines = code.split("\n")
3
  fixed_lines = []
4
  for line in lines:
5
- line = line.strip()
6
- if line.startswith("print ") and not line.startswith("print("):
7
- content = line[6:].strip()
8
- fixed_lines.append(f"print({content})")
 
 
9
  else:
10
  fixed_lines.append(line)
11
  return "\n".join(fixed_lines)
12
 
13
- # Optional test
14
- if __name__ == "__main__":
15
- buggy = "a = input()\nprint a"
16
- print(generate_fix(buggy))
17
-
18
-
19
 
20
  #genrate the fixed version of code using an ML Model
 
1
  def generate_fix(code):
2
+ lines = code.split('\n')
3
  fixed_lines = []
4
  for line in lines:
5
+ stripped = line.strip()
6
+ # Fix print statements missing parentheses
7
+ if stripped.startswith("print ") and "(" not in stripped:
8
+ expression = stripped[6:]
9
+ fixed_line = f"print({expression})"
10
+ fixed_lines.append(fixed_line)
11
  else:
12
  fixed_lines.append(line)
13
  return "\n".join(fixed_lines)
14
 
 
 
 
 
 
 
15
 
16
  #genrate the fixed version of code using an ML Model
full_code_fixer.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # full_code_fixer.py
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+
4
+ model_name = "Salesforce/codet5-base"
5
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
6
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
7
+
8
+ def fix_code(code):
9
+ input_text = f"fix Python: {code}"
10
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
11
+ outputs = model.generate(**inputs, max_length=512, num_beams=4, early_stopping=True)
12
+ fixed_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
13
+ return fixed_code
14
+
15
+ # Optional test
16
+ if __name__ == "__main__":
17
+ buggy_code = "def add(a,b):\n return a*b" # wrong logic
18
+ print(fix_code(buggy_code))