Spaces:
Sleeping
Sleeping
Modify request type + take params from JSON body.
#1
by
Kareem94
- opened
main.py
CHANGED
@@ -8,6 +8,7 @@ app = Flask(__name__)
|
|
8 |
# Load model and tokenizer
|
9 |
def load_model():
|
10 |
# Load saved config and weights
|
|
|
11 |
checkpoint = torch.load("codebert_vulnerability_scorer.pth", map_location=torch.device('cpu'))
|
12 |
config = RobertaConfig.from_dict(checkpoint['config'])
|
13 |
|
@@ -19,6 +20,7 @@ def load_model():
|
|
19 |
|
20 |
# Load components
|
21 |
try:
|
|
|
22 |
tokenizer = RobertaTokenizer.from_pretrained("./tokenizer_vulnerability")
|
23 |
model = load_model()
|
24 |
print("Model and tokenizer loaded successfully!")
|
@@ -29,13 +31,14 @@ except Exception as e:
|
|
29 |
def home():
|
30 |
return request.url
|
31 |
|
32 |
-
@app.route("/predict")
|
33 |
def predict():
|
34 |
try:
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
# Tokenize input
|
41 |
inputs = tokenizer(
|
@@ -54,8 +57,7 @@ def predict():
|
|
54 |
score = torch.sigmoid(outputs.logits).item()
|
55 |
|
56 |
return jsonify({
|
57 |
-
"score": round(score, 4)
|
58 |
-
"given_code": code[:500] + "..." if len(code) > 500 else code
|
59 |
})
|
60 |
|
61 |
except Exception as e:
|
|
|
8 |
# Load model and tokenizer
|
9 |
def load_model():
|
10 |
# Load saved config and weights
|
11 |
+
# Updated to 'codebert_vulnerability_scorer.pth' as per user's request
|
12 |
checkpoint = torch.load("codebert_vulnerability_scorer.pth", map_location=torch.device('cpu'))
|
13 |
config = RobertaConfig.from_dict(checkpoint['config'])
|
14 |
|
|
|
20 |
|
21 |
# Load components
|
22 |
try:
|
23 |
+
# Updated to './tokenizer_vulnerability' as per user's request
|
24 |
tokenizer = RobertaTokenizer.from_pretrained("./tokenizer_vulnerability")
|
25 |
model = load_model()
|
26 |
print("Model and tokenizer loaded successfully!")
|
|
|
31 |
def home():
|
32 |
return request.url
|
33 |
|
34 |
+
@app.route("/predict", methods=["POST"])
|
35 |
def predict():
|
36 |
try:
|
37 |
+
data = request.get_json()
|
38 |
+
if not data or "code" not in data:
|
39 |
+
return jsonify({"error": "Missing 'code' in request body"}), 400
|
40 |
+
|
41 |
+
code = data["code"]
|
42 |
|
43 |
# Tokenize input
|
44 |
inputs = tokenizer(
|
|
|
57 |
score = torch.sigmoid(outputs.logits).item()
|
58 |
|
59 |
return jsonify({
|
60 |
+
"score": round(score, 4)
|
|
|
61 |
})
|
62 |
|
63 |
except Exception as e:
|