Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,39 +35,10 @@ def _load_model():
|
|
| 35 |
if _tokenizer is None or _model is None:
|
| 36 |
_tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, cache_dir=CACHE_DIR)
|
| 37 |
_model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID, cache_dir=CACHE_DIR)
|
| 38 |
-
_model.eval() #
|
| 39 |
# Warm-up
|
| 40 |
with torch.no_grad():
|
| 41 |
_ = _model(**_tokenizer(["warm up"], return_tensors="pt")).logits
|
| 42 |
-
# Log mapping once for verification
|
| 43 |
-
try:
|
| 44 |
-
print("id2label:", _model.config.id2label)
|
| 45 |
-
print("label2id:", _model.config.label2id)
|
| 46 |
-
except Exception:
|
| 47 |
-
pass
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def _label_for_index(idx: int) -> str:
|
| 51 |
-
cfg = getattr(_model, "config", None)
|
| 52 |
-
id2label = getattr(cfg, "id2label", None) if cfg else None
|
| 53 |
-
if isinstance(id2label, dict):
|
| 54 |
-
# Try int key first, then str key
|
| 55 |
-
if idx in id2label:
|
| 56 |
-
return id2label[idx]
|
| 57 |
-
sidx = str(idx)
|
| 58 |
-
if sidx in id2label:
|
| 59 |
-
return id2label[sidx]
|
| 60 |
-
# Fallback for 2-class default
|
| 61 |
-
default_map = {0: "LEGIT", 1: "PHISH"}
|
| 62 |
-
return default_map.get(idx, str(idx))
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def _probs_dict(probs_list):
|
| 66 |
-
# Map each index to its label name for readability
|
| 67 |
-
out = {}
|
| 68 |
-
for i, p in enumerate(probs_list):
|
| 69 |
-
out[_label_for_index(i)] = float(p)
|
| 70 |
-
return out
|
| 71 |
|
| 72 |
|
| 73 |
@app.get("/")
|
|
@@ -85,9 +56,10 @@ def root():
|
|
| 85 |
def labels():
|
| 86 |
_load_model()
|
| 87 |
cfg = getattr(_model, "config", None)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
@app.post("/predict")
|
|
@@ -101,17 +73,22 @@ def predict(payload: PredictPayload):
|
|
| 101 |
truncation=True,
|
| 102 |
max_length=512
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
pred_idx = int(torch.argmax(
|
| 108 |
-
pred_label = _label_for_index(pred_idx)
|
| 109 |
-
pred_score = float(probs[pred_idx])
|
| 110 |
except Exception as e:
|
| 111 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return {
|
| 114 |
-
"
|
| 115 |
-
"
|
| 116 |
-
"
|
|
|
|
|
|
|
| 117 |
}
|
|
|
|
| 35 |
if _tokenizer is None or _model is None:
|
| 36 |
_tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, cache_dir=CACHE_DIR)
|
| 37 |
_model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID, cache_dir=CACHE_DIR)
|
| 38 |
+
_model.eval() # inference mode
|
| 39 |
# Warm-up
|
| 40 |
with torch.no_grad():
|
| 41 |
_ = _model(**_tokenizer(["warm up"], return_tensors="pt")).logits
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
@app.get("/")
|
|
|
|
| 56 |
def labels():
|
| 57 |
_load_model()
|
| 58 |
cfg = getattr(_model, "config", None)
|
| 59 |
+
return {
|
| 60 |
+
"id2label": getattr(cfg, "id2label", {}) if cfg else {},
|
| 61 |
+
"label2id": getattr(cfg, "label2id", {}) if cfg else {},
|
| 62 |
+
}
|
| 63 |
|
| 64 |
|
| 65 |
@app.post("/predict")
|
|
|
|
| 73 |
truncation=True,
|
| 74 |
max_length=512
|
| 75 |
)
|
| 76 |
+
outputs = _model(**inputs)
|
| 77 |
+
logits = outputs.logits # [1, num_labels]
|
| 78 |
+
logits_list = logits[0].tolist()
|
| 79 |
+
pred_idx = int(torch.argmax(logits, dim=-1).item())
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 82 |
|
| 83 |
+
cfg = getattr(_model, "config", None)
|
| 84 |
+
id2label = getattr(cfg, "id2label", {}) if cfg else {}
|
| 85 |
+
# Try to fetch label from config without any remapping logic
|
| 86 |
+
pred_label = id2label.get(pred_idx, id2label.get(str(pred_idx), None))
|
| 87 |
+
|
| 88 |
return {
|
| 89 |
+
"logits": logits_list, # direct model output (authoritative)
|
| 90 |
+
"predicted_index": pred_idx, # argmax over logits
|
| 91 |
+
"predicted_label": pred_label, # from model config if available
|
| 92 |
+
"id2label": id2label,
|
| 93 |
+
"label2id": getattr(cfg, "label2id", {}) if cfg else {},
|
| 94 |
}
|