Spaces:
Sleeping
Sleeping
Update models.py
Browse files
models.py
CHANGED
@@ -1,20 +1,32 @@
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
import torch
|
|
|
3 |
|
4 |
def load_model():
|
5 |
model_path = "sathish2352/email-classifier-model"
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
7 |
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
|
|
8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
model.to(device)
|
10 |
model.eval()
|
|
|
11 |
return tokenizer, model, device
|
12 |
|
13 |
def classify_email(text, tokenizer, model, device):
|
14 |
inputs = tokenizer(text, return_tensors="pt", max_length=256, padding="max_length", truncation=True)
|
15 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
|
|
16 |
with torch.no_grad():
|
17 |
logits = model(**inputs).logits
|
|
|
18 |
label_map = {0: "Incident", 1: "Request", 2: "Change", 3: "Problem"}
|
19 |
pred = torch.argmax(logits, dim=1).item()
|
|
|
20 |
return label_map[pred]
|
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
import torch
|
3 |
+
import os
|
4 |
|
5 |
def load_model():
|
6 |
model_path = "sathish2352/email-classifier-model"
|
7 |
+
|
8 |
+
# Set HF_HOME to use a writable cache dir
|
9 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
10 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface/transformers"
|
11 |
+
os.makedirs("/tmp/huggingface/transformers", exist_ok=True)
|
12 |
+
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
14 |
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
15 |
+
|
16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
model.to(device)
|
18 |
model.eval()
|
19 |
+
|
20 |
return tokenizer, model, device
|
21 |
|
22 |
def classify_email(text, tokenizer, model, device):
|
23 |
inputs = tokenizer(text, return_tensors="pt", max_length=256, padding="max_length", truncation=True)
|
24 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
25 |
+
|
26 |
with torch.no_grad():
|
27 |
logits = model(**inputs).logits
|
28 |
+
|
29 |
label_map = {0: "Incident", 1: "Request", 2: "Change", 3: "Problem"}
|
30 |
pred = torch.argmax(logits, dim=1).item()
|
31 |
+
|
32 |
return label_map[pred]
|