Divyashree-Nanjappa commited on
Commit
350dfeb
·
1 Parent(s): 304c5a2

Fixed bug detection API, added CORS and error handling

Browse files
Files changed (2) hide show
  1. app.py +9 -23
  2. requirements.txt.txt +0 -4
app.py CHANGED
@@ -1,10 +1,12 @@
1
  from flask import Flask, request, jsonify
2
- from transformers import RobertaTokenizer, RobertaForSequenceClassification
3
  import torch
 
4
 
5
  app = Flask(__name__)
 
6
 
7
- # Load CodeBERT Model for Bug Detection
8
  tokenizer = RobertaTokenizer.from_pretrained("microsoft/codebert-base")
9
  model = RobertaForSequenceClassification.from_pretrained("microsoft/codebert-base")
10
 
@@ -23,31 +25,15 @@ def detect_bug():
23
 
24
  # Tokenize and classify
25
  inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True)
26
- outputs = model(**inputs)
27
  prediction = torch.argmax(outputs.logits, dim=1).item()
28
 
29
  bug_status = "buggy" if prediction == 1 else "clean"
30
- return jsonify({"bug_status": bug_status})
31
-
32
- except Exception as e:
33
- return jsonify({"error": str(e)}), 500
34
-
35
- @app.route("/fix", methods=["POST"])
36
- def fix_code():
37
- try:
38
- data = request.get_json()
39
- code = data.get("code", "")
40
-
41
- if not code:
42
- return jsonify({"error": "No code provided"}), 400
43
-
44
- # Simple rule-based fix (replace common typos)
45
- fixed_code = code.replace("prnt", "print").replace("imprt", "import")
46
-
47
- return jsonify({"suggested_fix": fixed_code})
48
 
 
 
49
  except Exception as e:
50
- return jsonify({"error": str(e)}), 500
51
 
52
  if __name__ == "__main__":
53
- app.run(host="0.0.0.0", port=5000, debug=True)
 
1
  from flask import Flask, request, jsonify
2
+ from flask_cors import CORS # Fix CORS issue
3
  import torch
4
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification
5
 
6
  app = Flask(__name__)
7
+ CORS(app) # Enable CORS
8
 
9
+ # Load CodeBERT model
10
  tokenizer = RobertaTokenizer.from_pretrained("microsoft/codebert-base")
11
  model = RobertaForSequenceClassification.from_pretrained("microsoft/codebert-base")
12
 
 
25
 
26
  # Tokenize and classify
27
  inputs = tokenizer(code, return_tensors="pt", truncation=True, padding=True)
28
+ outputs = model(input_ids=inputs["input_ids"], attention_mask=inputs["attention_mask"])
29
  prediction = torch.argmax(outputs.logits, dim=1).item()
30
 
31
  bug_status = "buggy" if prediction == 1 else "clean"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ return jsonify({"status": bug_status})
34
+
35
  except Exception as e:
36
+ return jsonify({"error": str(e)}), 500 # Handle errors properly
37
 
38
  if __name__ == "__main__":
39
+ app.run(host="0.0.0.0", port=5000) # Ensure compatibility with Docker
requirements.txt.txt DELETED
@@ -1,4 +0,0 @@
1
- torch
2
- transformers
3
- flask
4
- flask-cors