|
from typing import Dict, List, Any |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
device = "cuda" |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
|
|
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-1.5B-Instruct") |
|
model = AutoModelForCausalLM.from_pretrained( |
|
"Qwen/Qwen2-1.5B-Instruct", |
|
torch_dtype="auto", |
|
device_map="auto" |
|
) |
|
|
|
self.pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
def __call__(self, data: Any) -> List[List[Dict[str, float]]]: |
|
inputs = data.pop("inputs", data) |
|
parameters = data.pop("parameters", None) |
|
|
|
|
|
if parameters is not None: |
|
prediction = self.pipeline(inputs, **parameters) |
|
else: |
|
prediction = self.pipeline(inputs) |
|
|
|
|
|
return prediction |
|
|
|
|
|
if __name__ == "__main__": |
|
handler = EndpointHandler() |
|
data = { |
|
"inputs": "Hello, how can I", |
|
"parameters": {"max_length": 50, "num_return_sequences": 1} |
|
} |
|
result = handler(data) |
|
print(result) |
|
|