Spaces:
Running
Running
kovacsvi
commited on
Commit
·
f17fb84
1
Parent(s):
caa0374
jit test...
Browse files- interfaces/cap.py +18 -8
interfaces/cap.py
CHANGED
@@ -85,18 +85,28 @@ def build_huggingface_path(language: str, domain: str):
|
|
85 |
|
86 |
def predict(text, model_id, tokenizer_id):
|
87 |
device = torch.device("cpu")
|
88 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_id, device_map="auto", token=HF_TOKEN).to(device)
|
89 |
-
tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
padding="do_not_pad",
|
95 |
-
return_tensors="pt").to(device)
|
96 |
model.eval()
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
with torch.no_grad():
|
99 |
-
logits = model(
|
|
|
100 |
release_model(model, model_id)
|
101 |
|
102 |
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy().flatten()
|
|
|
85 |
|
86 |
def predict(text, model_id, tokenizer_id):
|
87 |
device = torch.device("cpu")
|
|
|
|
|
88 |
|
89 |
+
# Load JIT-traced model
|
90 |
+
jit_model_path = f"/data/jit_models/{model_id.replace('/', '_')}.pt"
|
91 |
+
model = torch.jit.load(jit_model_path).to(device)
|
|
|
|
|
92 |
model.eval()
|
93 |
|
94 |
+
# Load tokenizer (still regular HF)
|
95 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
|
96 |
+
|
97 |
+
# Tokenize input
|
98 |
+
inputs = tokenizer(
|
99 |
+
text,
|
100 |
+
max_length=256,
|
101 |
+
truncation=True,
|
102 |
+
padding="do_not_pad",
|
103 |
+
return_tensors="pt"
|
104 |
+
)
|
105 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
106 |
+
|
107 |
with torch.no_grad():
|
108 |
+
logits = model(inputs["input_ids"], inputs["attention_mask"]).logits
|
109 |
+
|
110 |
release_model(model, model_id)
|
111 |
|
112 |
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy().flatten()
|