Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Load tokenizer and model once at startup
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("cybersectony/phishing-email-detection-distilbert_v2.4.1")
|
10 |
+
model = AutoModelForSequenceClassification.from_pretrained("cybersectony/phishing-email-detection-distilbert_v2.4.1")
|
11 |
+
|
12 |
+
# Define input schema
|
13 |
+
class EmailInput(BaseModel):
|
14 |
+
text: str
|
15 |
+
|
16 |
+
@app.post("/predict")
|
17 |
+
def predict(input: EmailInput):
|
18 |
+
inputs = tokenizer(input.text, return_tensors="pt", truncation=True, max_length=512)
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model(**inputs)
|
21 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
22 |
+
|
23 |
+
probs = predictions[0].tolist()
|
24 |
+
labels = {
|
25 |
+
"legitimate_email": probs[0],
|
26 |
+
"phishing_email": probs[1],
|
27 |
+
"legitimate_url": probs[2],
|
28 |
+
"phishing_url": probs[3]
|
29 |
+
}
|
30 |
+
max_label = max(labels.items(), key=lambda x: x[1])
|
31 |
+
|
32 |
+
return {
|
33 |
+
"prediction": max_label[0],
|
34 |
+
"confidence": round(max_label[1], 4),
|
35 |
+
"all_probabilities": labels
|
36 |
+
}
|