venky2k1 commited on
Commit
d57024f
·
1 Parent(s): 7337ffe

Final working bug fixer with fixed detection and suggestion

Browse files
Files changed (4) hide show
  1. README.md +3 -4
  2. app_ui.py +19 -13
  3. bug_detector.py +11 -2
  4. fix_generator.py +18 -5
README.md CHANGED
@@ -1,11 +1,10 @@
1
  ---
2
  title: Bug Fixer
3
- emoji: 🐞
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.17.0
8
  app_file: app_ui.py
9
  pinned: false
10
- ---
11
- # Bug Fixer
 
1
  ---
2
  title: Bug Fixer
3
+ emoji: \ud83d\udc1e
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
+ ---
 
app_ui.py CHANGED
@@ -2,20 +2,26 @@ import gradio as gr
2
  from bug_detector import detect_bug
3
  from fix_generator import generate_fix
4
 
5
- def process_code(code):
6
  bug_status = detect_bug(code)
7
- if bug_status == "buggy":
8
- fixed = generate_fix(code)
9
- return fixed
10
- return "Code looks correct!"
11
 
12
- iface = gr.Interface(
13
- fn=process_code,
14
- inputs=gr.Textbox(lines=10, label="Enter Buggy Code"),
15
- outputs=gr.Textbox(label="Suggested Fix"),
16
- title="Bug Detection and Fixing Tool",
17
- description="Enter your Python code to detect and fix simple bugs."
18
- )
19
 
20
- iface.launch()
 
 
 
 
 
21
 
 
 
 
 
 
 
 
 
 
 
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 = "\ud83d\udd27 Bug Detection and Fixing Tool"
 
 
 
 
 
 
11
 
12
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
13
+ gr.Markdown("""
14
+ # \ud83d\udd27 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()
27
+ #for the gradio user interface
bug_detector.py CHANGED
@@ -1,11 +1,20 @@
1
  import torch
2
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
 
 
4
  model = AutoModelForSequenceClassification.from_pretrained("microsoft/codebert-base", num_labels=2)
5
  tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
6
 
7
  def detect_bug(code):
8
  inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True)
9
  outputs = model(**inputs)
10
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
11
- return "buggy" if probs[0][1] > probs[0][0] else "correct"
 
 
 
 
 
 
 
 
 
1
  import torch
2
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
 
4
+ # Load pre-trained CodeBERT model
5
  model = AutoModelForSequenceClassification.from_pretrained("microsoft/codebert-base", num_labels=2)
6
  tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
7
 
8
  def detect_bug(code):
9
  inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True)
10
  outputs = model(**inputs)
11
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
12
+ return "buggy" if probabilities[0][1] > probabilities[0][0] else "correct"
13
+
14
+ # Optional test
15
+ if __name__ == "__main__":
16
+ sample = "def multiply(a, b): return a + b"
17
+ print(detect_bug(sample))
18
+
19
+ #detects if there's a bug in code
20
+
fix_generator.py CHANGED
@@ -1,6 +1,19 @@
1
  def generate_fix(code):
2
- # Dummy fix: add colon if missing, fix print
3
- fixed_code = code.replace("print ", "print(")
4
- if "print(" in fixed_code and not fixed_code.strip().endswith(")"):
5
- fixed_code += ")"
6
- return fixed_code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Optional test
13
+ if __name__ == "__main__":
14
+ buggy = "a = input()\nprint a"
15
+ print(generate_fix(buggy))
16
+
17
+
18
+
19
+ #genrate the fixed version of code using an ML Model