Spaces:
Sleeping
Sleeping
added app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from preditormodels import PhishingPredictor
|
| 2 |
+
import re
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from pydantic import BaseModel, field_validator
|
| 5 |
+
from config import XGB_MODEL_PATH, BERT_MODEL_PATH
|
| 6 |
+
class EmailRequest(BaseModel):
|
| 7 |
+
subject: str
|
| 8 |
+
body: str
|
| 9 |
+
|
| 10 |
+
@field_validator("subject", "body", mode="before")
|
| 11 |
+
@classmethod
|
| 12 |
+
def clean_text(cls, v):
|
| 13 |
+
if isinstance(v, str):
|
| 14 |
+
# Remove illegal ASCII control characters
|
| 15 |
+
v = re.sub(r"[\x00-\x08\x0B\x0C\x0E-\x1F]", "", v)
|
| 16 |
+
return v
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# FastAPI App
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
app = FastAPI(title="Phishing Detection API")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Load models once
|
| 26 |
+
predictor = PhishingPredictor(BERT_MODEL_PATH, XGB_MODEL_PATH)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@app.get("/")
|
| 30 |
+
def read_root():
|
| 31 |
+
return {"message": "Phishing Detection API is running."}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@app.post("/predict")
|
| 35 |
+
async def get_prediction(email: EmailRequest):
|
| 36 |
+
"""
|
| 37 |
+
Predict whether an email is phishing or legitimate.
|
| 38 |
+
"""
|
| 39 |
+
try:
|
| 40 |
+
return predictor.predict(email.subject, email.body)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Run Server
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
import uvicorn
|
| 50 |
+
uvicorn.run(app, host="127.0.0.1", port=8500)
|