| from fastapi import FastAPI |
| from pydantic import BaseModel |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification |
| import torch |
|
|
| app = FastAPI() |
|
|
| |
| |
| |
| MODEL_ID = "dima806/phishing-email-detection" |
|
|
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID) |
|
|
| class EmailInput(BaseModel): |
| text: str |
|
|
| @app.post("/") |
| async def testingFunction(): |
| return "Api working fine..." |
|
|
| @app.post("/predict") |
| async def predict_email(data: EmailInput): |
| |
| if len(data.text.strip().split()) < 3: |
| return {"prediction": "legitimate", "confidence": 1.0, "is_phishing": False, "note": "Text too short for analysis"} |
|
|
| inputs = tokenizer(data.text, return_tensors="pt", truncation=True, max_length=512) |
| |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| predictions = torch.nn.functional.softmax(outputs.logits, dim=-1) |
| |
| probs = predictions[0].tolist() |
| |
| |
| |
| confidences = {model.config.id2label[i]: prob for i, prob in enumerate(probs)} |
| |
| |
| max_label = max(confidences.items(), key=lambda x: x[1]) |
| |
| return { |
| "prediction": max_label[0], |
| "confidence": round(max_label[1], 4), |
| "all_scores": confidences, |
| "is_phishing": "phishing" in max_label[0].lower() or "spam" in max_label[0].lower() |
| } |
|
|
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=7860) |