-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (39 loc) · 1.49 KB
/
app.py
File metadata and controls
50 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, request, jsonify
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import os
app = Flask(__name__)
# Load model and tokenizer
MODEL_PATH = "phishing_url_detection_BERT"
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
model.eval()
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({"status": "healthy"}), 200
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.get_json()
if not data or 'url' not in data:
return jsonify({"error": "No URL provided"}), 400
url = data['url']
# Tokenize input
inputs = tokenizer(url, truncation=True, return_tensors="pt")
# Make prediction
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
prediction = torch.argmax(probabilities, dim=-1).item()
confidence = probabilities[0][prediction].item()
result = {
"url": url,
"prediction": "Safe" if prediction == 0 else "Not Safe",
"confidence": confidence
}
return jsonify(result), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)