Update handler.py
Browse files- handler.py +20 -1
handler.py
CHANGED
@@ -11,8 +11,27 @@ class EndpointHandler:
|
|
11 |
# load the model
|
12 |
self.tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
13 |
self.model = AutoModelForCausalLM.from_pretrained(path, device_map="auto", torch_dtype=dtype, trust_remote_code=True)
|
|
|
|
|
14 |
|
15 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# ignoring parameters! Default to configs in generation_config.json.
|
17 |
messages = [{"role": "user", "content": data}]
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# load the model
|
12 |
self.tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
13 |
self.model = AutoModelForCausalLM.from_pretrained(path, device_map="auto", torch_dtype=dtype, trust_remote_code=True)
|
14 |
+
# create inference pipeline
|
15 |
+
self.pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
16 |
|
17 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
18 |
+
inputs = data.pop("inputs", data)
|
19 |
+
parameters = data.pop("parameters", None)
|
20 |
+
|
21 |
+
# pass inputs with all kwargs in data
|
22 |
+
if parameters is not None:
|
23 |
+
prediction = self.pipeline(inputs, **parameters)
|
24 |
+
else:
|
25 |
+
prediction = self.pipeline(inputs)
|
26 |
+
print("---start---")
|
27 |
+
print(prediction)
|
28 |
+
print("---end---")
|
29 |
+
|
30 |
# ignoring parameters! Default to configs in generation_config.json.
|
31 |
messages = [{"role": "user", "content": data}]
|
32 |
+
response = self.model.chat(self.tokenizer, messages)}
|
33 |
+
print("---start chat response---")
|
34 |
+
print(response)
|
35 |
+
print("---end chat response---")
|
36 |
+
|
37 |
+
return [{'generated_text': response}]
|