Update handler.py
Browse files- handler.py +28 -19
handler.py
CHANGED
@@ -1,25 +1,34 @@
|
|
1 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
import torch
|
3 |
|
4 |
-
class
|
5 |
-
def __init__(self):
|
6 |
-
|
7 |
-
self.model = None
|
8 |
-
|
9 |
-
def initialize(self, model_dir):
|
10 |
-
# Load the tokenizer and model
|
11 |
self.tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
12 |
self.model = AutoModelForCausalLM.from_pretrained(model_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
1 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
import torch
|
3 |
|
4 |
+
class EndpointHandler:
|
5 |
+
def __init__(self, model_dir):
|
6 |
+
# Load tokenizer and model during initialization
|
|
|
|
|
|
|
|
|
7 |
self.tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
8 |
self.model = AutoModelForCausalLM.from_pretrained(model_dir)
|
9 |
+
|
10 |
+
def __call__(self, data):
|
11 |
+
"""
|
12 |
+
This method processes input data and generates output.
|
13 |
+
:param data: Input data, usually a dictionary with 'inputs' key.
|
14 |
+
"""
|
15 |
+
# Extract input prompt
|
16 |
+
inputs = data.get("inputs", "")
|
17 |
+
if not inputs:
|
18 |
+
return {"error": "No input provided"}
|
19 |
|
20 |
+
# Preprocess input
|
21 |
+
encoded_inputs = self.tokenizer(inputs, return_tensors="pt", padding=True)
|
22 |
+
|
23 |
+
# Generate output
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = self.model.generate(
|
26 |
+
**encoded_inputs,
|
27 |
+
max_length=200,
|
28 |
+
temperature=0.7,
|
29 |
+
do_sample=True
|
30 |
+
)
|
31 |
+
|
32 |
+
# Decode and return response
|
33 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
return {"generated_text": response}
|