Update handler.py
Browse files- handler.py +16 -18
handler.py
CHANGED
@@ -3,26 +3,24 @@ import torch
|
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
4 |
|
5 |
class EndpointHandler():
|
6 |
-
def
|
7 |
-
|
8 |
-
self.
|
9 |
-
self.model =
|
10 |
-
self.model.generation_config = GenerationConfig.from_pretrained(model_name)
|
11 |
self.model.generation_config.pad_token_id = self.model.generation_config.eos_token_id
|
12 |
|
13 |
-
def
|
14 |
-
"""
|
15 |
-
data args:
|
16 |
-
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
17 |
-
kwargs
|
18 |
-
Return:
|
19 |
-
A :obj:`list` | `dict`: will be serialized and returned
|
20 |
-
"""
|
21 |
inputs = data.pop('inputs', data)
|
22 |
messages = [{"role": "user", "content": inputs}]
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
return [{"result": result}]
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
4 |
|
5 |
class EndpointHandler():
|
6 |
+
def __init__(self, path=""):
|
7 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
8 |
+
self.model = AutoModelForCausalLM.from_pretrained(path, torch_dtype=torch.bfloat16, device_map="auto")
|
9 |
+
self.model.generation_config = GenerationConfig.from_pretrained(path)
|
|
|
10 |
self.model.generation_config.pad_token_id = self.model.generation_config.eos_token_id
|
11 |
|
12 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
inputs = data.pop('inputs', data)
|
14 |
messages = [{"role": "user", "content": inputs}]
|
15 |
+
|
16 |
+
# Mesajları modelin anlayacağı formata dönüştürme
|
17 |
+
input_texts = [message["content"] for message in messages]
|
18 |
+
input_text = self.tokenizer.eos_token.join(input_texts)
|
19 |
+
input_ids = self.tokenizer.encode(input_text, return_tensors="pt")
|
20 |
+
|
21 |
+
# Modelden yanıt üretme
|
22 |
+
outputs = self.model.generate(input_ids.to(self.model.device), max_new_tokens=100)
|
23 |
+
|
24 |
+
# Üretilen yanıtı çözme
|
25 |
+
result = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
26 |
return [{"result": result}]
|