File size: 1,252 Bytes
c1cb360 6d33a5e fe371ad 1aeb34c 6d33a5e b47e2d8 c1cb360 b47e2d8 6d33a5e e8628b3 6d33a5e e126c73 b3aebd1 6d33a5e 1aeb34c 6d33a5e 1e592e3 6d33a5e 1e592e3 6d33a5e b47e2d8 eff3ac4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from typing import Dict, List, Any
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
device = "cuda"
class EndpointHandler:
def __init__(self, path=""):
# load the model
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-1.5B-Instruct")
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2-1.5B-Instruct",
torch_dtype="auto",
device_map="auto"
)
# create inference pipeline
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)
# pass inputs with all kwargs in data
if parameters is not None:
prediction = self.pipeline(inputs, **parameters)
else:
prediction = self.pipeline(inputs)
# postprocess the prediction
return prediction
# Example usage
if __name__ == "__main__":
handler = EndpointHandler()
data = {
"inputs": "Hello, how can I",
"parameters": {"max_length": 50, "num_return_sequences": 1}
}
result = handler(data)
print(result)
|