Download inference_example.py from patelkrish2727/jarvis-email: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/patelkrish2727/jarvis-email/resolve/main/inference_example.py
- Command line
-
hf download hf://patelkrish2727/jarvis-email/inference_example.py
-
curl -L -o inference_example.py https://huggingface.co/patelkrish2727/jarvis-email/resolve/main/inference_example.py
2.09 kB
| """Minimal inference against local files or HF Hub repo.""" | |
| import pickle | |
| import torch | |
| from transformers import AutoTokenizer | |
| from modeling_jarvis import JarvisEmailIntelligence | |
| REPO = "patelkrish2727/jarvis-email" | |
| BASE = "microsoft/deberta-v3-small" | |
| with open("label_encoders.pkl", "rb") as f: | |
| encoders = pickle.load(f) | |
| model = JarvisEmailIntelligence( | |
| model_name=BASE, | |
| n_category=len(encoders['category'].classes_), | |
| n_intent=len(encoders['intent'].classes_), | |
| n_actionability=len(encoders['actionability'].classes_), | |
| n_threat_verdict=len(encoders['threat_verdict'].classes_), | |
| n_attack_vector=len(encoders['attack_vector'].classes_), | |
| ) | |
| state = torch.load("pytorch_model.bin", map_location="cpu", weights_only=True) | |
| model.load_state_dict(state) | |
| model.eval() | |
| tok = AutoTokenizer.from_pretrained(BASE) | |
| def classify(subject: str, body: str): | |
| text = f"Subject: {subject}\n\nBody: {body}" | |
| e = tok(text, truncation=True, padding='max_length', max_length=384, return_tensors='pt') | |
| with torch.no_grad(): | |
| p = model(e['input_ids'], e['attention_mask']) | |
| def top(k): | |
| import torch as t | |
| probs = t.softmax(p[k], dim=-1).flatten() | |
| i = int(probs.argmax()) | |
| return encoders[k].classes_[i], float(probs[i]) | |
| import torch as t | |
| sig = lambda x: float(t.sigmoid(x).item()) | |
| return { | |
| "category": top("category"), | |
| "intent": top("intent"), | |
| "actionability": top("actionability"), | |
| "threat_verdict": top("threat_verdict"), | |
| "attack_vector": top("attack_vector"), | |
| "requires_response": sig(p["requires_response"]), | |
| "urgency_manipulation": sig(p["urgency_manipulation"]), | |
| "display_name_mismatch": sig(p["display_name_mismatch"]), | |
| "importance": float(p["importance_score"].item()), | |
| "urgency": float(p["urgency_score"].item()), | |
| "threat": float(p["threat_score"].item()), | |
| "confidence": float(p["confidence"].item()), | |
| } | |
| if __name__ == "__main__": | |
| print(classify("3 Days Left: Unstop PPI", "Hi, apply now for PPI opportunities")) | |