Upload handler.py with huggingface_hub
Browse files- handler.py +22 -41
handler.py
CHANGED
|
@@ -1,50 +1,31 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
class EndpointHandler:
|
| 6 |
def __init__(self, path=""):
|
| 7 |
-
|
| 8 |
-
Initializes the handler by loading the T5Gemma model and tokenizer.
|
| 9 |
-
trust_remote_code=True is essential for new architectures.
|
| 10 |
-
"""
|
| 11 |
-
self.tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 12 |
self.model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 13 |
-
path,
|
| 14 |
-
torch_dtype=torch.bfloat16
|
| 15 |
-
trust_remote_code=True
|
| 16 |
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
def __call__(self, data):
|
| 20 |
-
""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if isinstance(inputs_text, str):
|
| 30 |
-
inputs_text = [inputs_text]
|
| 31 |
-
|
| 32 |
-
# Create the chat message structure that apply_chat_template expects
|
| 33 |
-
messages_list = [[{"role": "user", "content": text}] for text in inputs_text]
|
| 34 |
-
|
| 35 |
-
# Apply the model's specific chat template to format the input correctly
|
| 36 |
-
# The tokenizer handles padding for batched inputs automatically.
|
| 37 |
-
input_ids = [
|
| 38 |
-
self.tokenizer.apply_chat_template(
|
| 39 |
-
messages, add_generation_prompt=True, return_tensors="pt"
|
| 40 |
-
) for messages in messages_list
|
| 41 |
-
]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
return
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
| 2 |
import torch
|
| 3 |
|
| 4 |
class EndpointHandler:
|
| 5 |
def __init__(self, path=""):
|
| 6 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
self.model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 8 |
+
path,
|
| 9 |
+
torch_dtype=torch.bfloat16
|
|
|
|
| 10 |
)
|
| 11 |
+
|
|
|
|
| 12 |
def __call__(self, data):
|
| 13 |
+
inputs = data.pop("inputs", data)
|
| 14 |
+
messages = [{"role": "user", "content": inputs}]
|
| 15 |
+
|
| 16 |
+
input_ids = self.tokenizer.apply_chat_template(
|
| 17 |
+
messages,
|
| 18 |
+
add_generation_prompt=True,
|
| 19 |
+
return_tensors="pt"
|
| 20 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
outputs = self.model.generate(
|
| 23 |
+
input_ids,
|
| 24 |
+
max_new_tokens=1024,
|
| 25 |
+
temperature=0.1,
|
| 26 |
+
do_sample=True
|
| 27 |
+
)
|
| 28 |
|
| 29 |
+
return {
|
| 30 |
+
"generated_text": self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
+
}
|