Update src/api.py
Browse files- src/api.py +48 -36
src/api.py
CHANGED
|
@@ -1,36 +1,48 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
model.eval()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# api.py
|
| 2 |
+
from fastapi import FastAPI, Query
|
| 3 |
+
import uvicorn
|
| 4 |
+
import pickle, torch, random, os
|
| 5 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 6 |
+
from collections import defaultdict
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Load model
|
| 11 |
+
label_encoder = pickle.load(open("best_model/label_encoder.pkl", "rb"))
|
| 12 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
| 13 |
+
model = BertForSequenceClassification.from_pretrained(
|
| 14 |
+
"best_model", num_labels=len(label_encoder.classes_)
|
| 15 |
+
)
|
| 16 |
+
model.eval()
|
| 17 |
+
|
| 18 |
+
def predict_disease(symptoms_text):
|
| 19 |
+
symptoms = [s.strip() for s in symptoms_text.split(",") if s.strip()]
|
| 20 |
+
agg_probs = defaultdict(float)
|
| 21 |
+
for _ in range(10):
|
| 22 |
+
random.shuffle(symptoms)
|
| 23 |
+
inputs = tokenizer(", ".join(symptoms), return_tensors="pt",
|
| 24 |
+
truncation=True, padding=True, max_length=128)
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model(**inputs)
|
| 27 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1).squeeze()
|
| 28 |
+
for i, p in enumerate(probs):
|
| 29 |
+
agg_probs[i] += p.item()
|
| 30 |
+
for k in agg_probs:
|
| 31 |
+
agg_probs[k] /= 10
|
| 32 |
+
return sorted(
|
| 33 |
+
[{"disease": label_encoder.classes_[i], "probability": p}
|
| 34 |
+
for i, p in agg_probs.items()],
|
| 35 |
+
key=lambda x: x["probability"], reverse=True
|
| 36 |
+
)[:3]
|
| 37 |
+
|
| 38 |
+
@app.get("/")
|
| 39 |
+
def api(symptoms: str = Query(...)):
|
| 40 |
+
results = predict_disease(symptoms)
|
| 41 |
+
return {
|
| 42 |
+
"status": "success",
|
| 43 |
+
"input": symptoms,
|
| 44 |
+
"predictions": results
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|