venky2k1 commited on
Commit
983d3b8
·
2 Parent(s): 055d938 22b22ed

Resolved merge conflicts from Hugging Face

Browse files
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python as the base image
2
+ FROM python:3.9
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Copy project files
8
+ COPY . /app
9
+
10
+ # Install dependencies
11
+ RUN pip install flask torch transformers
12
+
13
+ # Expose the API port
14
+ EXPOSE 5000
15
+
16
+ # Start Flask server
17
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,3 +1,14 @@
1
  # Bug Fixer
2
-
3
  Bug Fixer is a Gradio-based web tool that uses the CodeT5+ model to automatically detect and fix bugs in Python code. Built using Hugging Face Transformers and deployable via Hugging Face Spaces.
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Bug Fixer
 
2
  Bug Fixer is a Gradio-based web tool that uses the CodeT5+ model to automatically detect and fix bugs in Python code. Built using Hugging Face Transformers and deployable via Hugging Face Spaces.
3
+ ---
4
+ title: Bug Fixer - CodeT5
5
+ emoji: 🧠
6
+ colorFrom: pink
7
+ colorTo: purple
8
+ sdk: gradio
9
+ sdk_version: 4.33.0
10
+ app_file: app_ui.py
11
+ pinned: false
12
+ ---
13
+
14
+
__pycache__/bug_detector.cpython-313.pyc DELETED
Binary file (977 Bytes)
 
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from bug_detector import fix_code
3
 
@@ -10,3 +11,44 @@ gr.Interface(
10
  outputs=gr.Textbox(label="Suggested Fixed Code"),
11
  title="Code Fixer using CodeT5+"
12
  ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <<<<<<< HEAD
2
  import gradio as gr
3
  from bug_detector import fix_code
4
 
 
11
  outputs=gr.Textbox(label="Suggested Fixed Code"),
12
  title="Code Fixer using CodeT5+"
13
  ).launch()
14
+ =======
15
+ from flask import Flask, request, jsonify
16
+ from flask_cors import CORS # Fix CORS issue
17
+ import torch
18
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification
19
+
20
+ app = Flask(__name__)
21
+ CORS(app) # Enable CORS
22
+
23
+ # Load CodeBERT model
24
+ tokenizer = RobertaTokenizer.from_pretrained("microsoft/codebert-base")
25
+ model = RobertaForSequenceClassification.from_pretrained("microsoft/codebert-base")
26
+
27
+ @app.route("/")
28
+ def home():
29
+ return "Bug Detection and Fixing API is running!"
30
+
31
+ @app.route("/detect", methods=["POST"])
32
+ def detect_bug():
33
+ try:
34
+ data = request.get_json()
35
+ code = data.get("code", "")
36
+
37
+ if not code:
38
+ return jsonify({"error": "No code provided"}), 400
39
+
40
+ # Tokenize and classify
41
+ inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True)
42
+ outputs = model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
43
+ prediction = torch.argmax(outputs.logits, dim=1).item()
44
+
45
+ bug_status = "buggy" if prediction == 1 else "clean"
46
+
47
+ return jsonify({"status": bug_status})
48
+
49
+ except Exception as e:
50
+ return jsonify({"error": str(e)}), 500 # Handle errors properly
51
+
52
+ if __name__ == "__main__":
53
+ app.run(host="0.0.0.0", port=5000) # Ensure compatibility with Docker
54
+ >>>>>>> 22b22edd4386cff48f5ad4c4325e1f8524238b52
app_ui.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
bug-fixer ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit 0469b937d3a712bd7bcdcf362bed99e60ad7e37b
bug_detector.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import torch
3
 
@@ -12,3 +13,24 @@ def fix_code(code):
12
  output = model.generate(inputs["input_ids"], max_length=256)
13
  fixed_code = tokenizer.decode(output[0], skip_special_tokens=True)
14
  return fixed_code.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <<<<<<< HEAD
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
 
13
  output = model.generate(inputs["input_ids"], max_length=256)
14
  fixed_code = tokenizer.decode(output[0], skip_special_tokens=True)
15
  return fixed_code.strip()
16
+ =======
17
+ import torch
18
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
19
+
20
+ # Load pre-trained CodeBERT model
21
+ model = AutoModelForSequenceClassification.from_pretrained("microsoft/codebert-base", num_labels=2)
22
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
23
+
24
+ def detect_bug(code):
25
+ inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True)
26
+ outputs = model(**inputs)
27
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
28
+ return "buggy" if probabilities[0][1] > probabilities[0][0] else "correct"
29
+
30
+ # Optional test
31
+ if __name__ == "__main__":
32
+ sample = "def multiply(a, b): return a + b"
33
+ print(detect_bug(sample))
34
+ #detects if there's a bug in code
35
+
36
+ >>>>>>> 22b22edd4386cff48f5ad4c4325e1f8524238b52
fix_generator.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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))
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ torch
4
+ transformers
5
+ gradio