|
from typing import Dict, Any, List |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
import torch |
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
|
|
self.model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.float16) |
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
data args: |
|
inputs (str): The text input or prompts for the model |
|
Return: |
|
A list containing the generated responses. |
|
""" |
|
|
|
inputs = data.get("inputs", "") |
|
if not inputs: |
|
return [{"error": "No input provided"}] |
|
|
|
|
|
tokens = self.tokenizer(inputs, return_tensors="pt").to(torch.float16) |
|
output_tokens = self.model.generate(**tokens) |
|
|
|
|
|
output_text = self.tokenizer.decode(output_tokens[0], skip_special_tokens=True) |
|
|
|
|
|
return [{"generated_text": output_text}] |
|
|