Update handler.py
Browse files- handler.py +42 -40
handler.py
CHANGED
@@ -1,43 +1,45 @@
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
import torch
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
8 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
9 |
-
model.eval()
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
inputs
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
1 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
import torch
|
3 |
|
4 |
+
class EndpointHandler:
|
5 |
+
def __init__(self, path=""):
|
6 |
+
# Load model and tokenizer from the repo path
|
7 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
8 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(path)
|
9 |
+
self.model.eval()
|
10 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
self.model.to(self.device)
|
12 |
+
|
13 |
+
def __call__(self, data):
|
14 |
+
"""
|
15 |
+
This method is called when the endpoint receives a request.
|
16 |
+
Expected input: { "inputs": "some string" } or { "inputs": ["a", "b", ...] }
|
17 |
+
"""
|
18 |
+
inputs = data.get("inputs", None)
|
19 |
+
|
20 |
+
if inputs is None:
|
21 |
+
return {"error": "No input provided"}
|
22 |
+
|
23 |
+
if isinstance(inputs, str):
|
24 |
+
inputs = [inputs]
|
25 |
+
|
26 |
+
results = []
|
27 |
+
for text in inputs:
|
28 |
+
encoded = self.tokenizer(
|
29 |
+
text,
|
30 |
+
return_tensors="pt",
|
31 |
+
truncation=True,
|
32 |
+
padding="max_length",
|
33 |
+
max_length=4096,
|
34 |
+
)
|
35 |
+
encoded = {k: v.to(self.device) for k, v in encoded.items()}
|
36 |
+
|
37 |
+
with torch.no_grad():
|
38 |
+
outputs = self.model(**encoded)
|
39 |
+
|
40 |
+
raw_score = outputs.logits.squeeze().item()
|
41 |
+
clipped_score = min(max(raw_score, 0.0), 1.0)
|
42 |
+
|
43 |
+
results.append({"score": round(clipped_score, 4)})
|
44 |
+
|
45 |
+
return results
|