File size: 2,093 Bytes
f69ee2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""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"))