ahmetmete commited on
Commit
af33494
·
1 Parent(s): 428d607

Update handler.py

Browse files
Files changed (1) hide show
  1. 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 _init_(self, path=""):
7
- model_name = "deepseek-ai/deepseek-llm-67b-chat"
8
- self.tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- self.model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
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 _call_(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
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
- input_tensor = self.tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
24
- outputs = self.model.generate(input_tensor.to(self.model.device), max_new_tokens=100)
25
- result = self.tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
26
- # pseudo
27
- # self.model(input)
 
 
 
 
 
 
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}]