Update handler.py
Browse files- handler.py +36 -60
handler.py
CHANGED
|
@@ -1,62 +1,38 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
import torch
|
| 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 |
-
def preprocess(self, requests):
|
| 32 |
-
"""Convert raw text into model-ready inputs"""
|
| 33 |
-
inputs = []
|
| 34 |
-
for req in requests:
|
| 35 |
-
text = req.get("data") or req.get("body")
|
| 36 |
-
if isinstance(text, (bytes, bytearray)):
|
| 37 |
-
text = text.decode("utf-8")
|
| 38 |
-
tokens = self.tokenizer(
|
| 39 |
-
text,
|
| 40 |
-
padding="max_length",
|
| 41 |
-
truncation=True,
|
| 42 |
-
max_length=512,
|
| 43 |
-
return_tensors="pt"
|
| 44 |
-
)
|
| 45 |
-
tokens = {k: v.to(self.device) for k, v in tokens.items()}
|
| 46 |
-
inputs.append(tokens)
|
| 47 |
-
return inputs
|
| 48 |
-
|
| 49 |
-
def inference(self, inputs):
|
| 50 |
-
"""Run forward pass and return clipped regression output"""
|
| 51 |
-
results = []
|
| 52 |
with torch.no_grad():
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def postprocess(self, inference_output):
|
| 61 |
-
"""Convert scores to response-friendly format"""
|
| 62 |
-
return [{"score": float(out)} for out in inference_output]
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
import torch
|
| 3 |
+
|
| 4 |
+
# Load model and tokenizer once at module level
|
| 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(texts):
|
| 15 |
+
"""Hugging Face expects a `predict()` function for custom handlers"""
|
| 16 |
+
if isinstance(texts, str):
|
| 17 |
+
texts = [texts] # Ensure batch input
|
| 18 |
+
|
| 19 |
+
results = []
|
| 20 |
+
for text in texts:
|
| 21 |
+
# Tokenize
|
| 22 |
+
inputs = tokenizer(
|
| 23 |
+
text,
|
| 24 |
+
return_tensors="pt",
|
| 25 |
+
truncation=True,
|
| 26 |
+
padding="max_length",
|
| 27 |
+
max_length=512
|
| 28 |
+
)
|
| 29 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 30 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
with torch.no_grad():
|
| 32 |
+
outputs = model(**inputs)
|
| 33 |
+
score = outputs.logits.squeeze().item()
|
| 34 |
+
clipped_score = min(max(score, 0.0), 1.0)
|
| 35 |
+
|
| 36 |
+
results.append({"score": clipped_score})
|
| 37 |
+
|
| 38 |
+
return results
|
|
|
|
|
|
|
|
|