Create custom_handler.py
Browse files- custom_handler.py +17 -0
custom_handler.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
2 |
+
import torch
|
3 |
+
|
4 |
+
class CustomHandler:
|
5 |
+
def __init__(self, model_name_or_path):
|
6 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
|
7 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_name_or_path)
|
8 |
+
self.model.eval() # Set the model to evaluation mode
|
9 |
+
|
10 |
+
def __call__(self, inputs):
|
11 |
+
# Preprocess the inputs
|
12 |
+
tokens = self.tokenizer(inputs, return_tensors='pt')
|
13 |
+
# Perform inference
|
14 |
+
with torch.no_grad():
|
15 |
+
outputs = self.model(**tokens)
|
16 |
+
# Postprocess the outputs
|
17 |
+
return outputs
|