Update handler.py
Browse files- handler.py +22 -17
handler.py
CHANGED
|
@@ -1,38 +1,43 @@
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
import torch
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
-
def predict(
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
results = []
|
| 20 |
-
for text in
|
| 21 |
-
|
| 22 |
-
inputs = tokenizer(
|
| 23 |
text,
|
| 24 |
return_tensors="pt",
|
| 25 |
truncation=True,
|
| 26 |
padding="max_length",
|
| 27 |
-
max_length=
|
| 28 |
)
|
| 29 |
-
|
| 30 |
|
| 31 |
with torch.no_grad():
|
| 32 |
-
outputs = model(**
|
| 33 |
-
score = outputs.logits.squeeze().item()
|
| 34 |
-
clipped_score = min(max(score, 0.0), 1.0)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
return results
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
import torch
|
| 3 |
|
| 4 |
+
# Load once when the endpoint starts
|
| 5 |
+
model_name = "open-paws/text_performance_prediction_longform"
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
+
def predict(inputs):
|
| 15 |
+
"""
|
| 16 |
+
Hugging Face Inference Endpoints will call this function.
|
| 17 |
+
`inputs` can be a single string or a list of strings.
|
| 18 |
+
"""
|
| 19 |
+
if isinstance(inputs, str):
|
| 20 |
+
inputs = [inputs]
|
| 21 |
|
| 22 |
results = []
|
| 23 |
+
for text in inputs:
|
| 24 |
+
encoded = tokenizer(
|
|
|
|
| 25 |
text,
|
| 26 |
return_tensors="pt",
|
| 27 |
truncation=True,
|
| 28 |
padding="max_length",
|
| 29 |
+
max_length=4096,
|
| 30 |
)
|
| 31 |
+
encoded = {k: v.to(device) for k, v in encoded.items()}
|
| 32 |
|
| 33 |
with torch.no_grad():
|
| 34 |
+
outputs = model(**encoded)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
raw_score = outputs.logits.squeeze().item()
|
| 37 |
+
clipped_score = min(max(raw_score, 0.0), 1.0)
|
| 38 |
+
|
| 39 |
+
results.append({
|
| 40 |
+
"score": round(clipped_score, 4),
|
| 41 |
+
})
|
| 42 |
|
| 43 |
return results
|