Update handler.py
Browse files- handler.py +30 -9
handler.py
CHANGED
@@ -1,9 +1,30 @@
|
|
1 |
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
)
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
2 |
+
import torch
|
3 |
+
|
4 |
+
class Gemma2Handler:
|
5 |
+
def __init__(self, model_dir):
|
6 |
+
# Load the tokenizer
|
7 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
8 |
+
|
9 |
+
# Load the model with the `ignore_mismatched_sizes` flag
|
10 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(
|
11 |
+
model_dir,
|
12 |
+
ignore_mismatched_sizes=True
|
13 |
+
)
|
14 |
+
|
15 |
+
# Initialize the pipeline
|
16 |
+
self.pipeline = pipeline(
|
17 |
+
"text-classification",
|
18 |
+
model=self.model,
|
19 |
+
tokenizer=self.tokenizer,
|
20 |
+
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
|
21 |
+
)
|
22 |
+
|
23 |
+
def __call__(self, inputs):
|
24 |
+
# Perform inference using the pipeline
|
25 |
+
predictions = self.pipeline(inputs)
|
26 |
+
return predictions
|
27 |
+
|
28 |
+
# Function to be called by Hugging Face Inference Toolkit
|
29 |
+
def get_pipeline(model_dir):
|
30 |
+
return Gemma2Handler(model_dir)
|