Ippoboi commited on
Commit
977ba08
·
verified ·
1 Parent(s): f821219

Upload example.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. example.py +78 -0
example.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Inference example for the MiniLM email classifier ONNX model.
3
+
4
+ Usage:
5
+ pip install onnxruntime transformers
6
+ python example.py
7
+ """
8
+
9
+ import numpy as np
10
+ import onnxruntime as ort
11
+ from transformers import AutoTokenizer
12
+
13
+ CATEGORIES = ["ALERT", "NEWSLETTER", "PERSONAL", "SOCIAL", "TRANSACTION"]
14
+ REPO_ID = "Ippoboi/minilmail-classifier"
15
+
16
+
17
+ def classify_email(
18
+ session: ort.InferenceSession,
19
+ tokenizer: AutoTokenizer,
20
+ subject: str,
21
+ body: str,
22
+ action_threshold: float = 0.5,
23
+ ) -> dict:
24
+ """Classify an email and return category + action prediction."""
25
+ text = f"Subject: {subject}\n\nBody: {body}"
26
+ inputs = tokenizer(text, return_tensors="np", max_length=256, truncation=True)
27
+
28
+ cat_probs, act_prob = session.run(
29
+ ["category_probs", "action_prob"],
30
+ {
31
+ "input_ids": inputs["input_ids"].astype(np.int64),
32
+ "attention_mask": inputs["attention_mask"].astype(np.int64),
33
+ "token_type_ids": np.zeros_like(inputs["input_ids"], dtype=np.int64),
34
+ },
35
+ )
36
+
37
+ category_idx = int(np.argmax(cat_probs[0]))
38
+ return {
39
+ "category": CATEGORIES[category_idx],
40
+ "confidence": float(cat_probs[0][category_idx]),
41
+ "action_required": float(act_prob[0][0]) > action_threshold,
42
+ "action_probability": float(act_prob[0][0]),
43
+ "all_probabilities": {
44
+ cat: float(prob) for cat, prob in zip(CATEGORIES, cat_probs[0])
45
+ },
46
+ }
47
+
48
+
49
+ def main():
50
+ from huggingface_hub import hf_hub_download
51
+
52
+ # Download model and tokenizer
53
+ model_path = hf_hub_download(REPO_ID, "model.onnx")
54
+ tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
55
+ session = ort.InferenceSession(model_path)
56
+
57
+ # Example emails
58
+ emails = [
59
+ ("Your order has shipped", "Your order #12345 is on its way and will arrive by Monday."),
60
+ ("Meeting tomorrow", "Hey, can we reschedule our 2pm meeting to 3pm? Let me know."),
61
+ ("Weekly Newsletter", "Check out our latest deals! 50% off everything this weekend."),
62
+ ("Security Alert", "A new device logged into your account from San Francisco, CA."),
63
+ ("LinkedIn: New connection", "John Doe wants to connect with you on LinkedIn."),
64
+ ]
65
+
66
+ print("=" * 60)
67
+ print("MiniLM Email Classifier")
68
+ print("=" * 60)
69
+
70
+ for subject, body in emails:
71
+ result = classify_email(session, tokenizer, subject, body)
72
+ action = "ACTION" if result["action_required"] else "NO_ACTION"
73
+ print(f"\n Subject: {subject}")
74
+ print(f" → {result['category']} ({result['confidence']:.1%}) | {action} ({result['action_probability']:.1%})")
75
+
76
+
77
+ if __name__ == "__main__":
78
+ main()