MrD05 commited on
Commit
89f8d10
·
1 Parent(s): 17d05c4

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +35 -35
handler.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
  from transformers_stream_generator import init_stream_support
3
  import re
4
  init_stream_support()
@@ -22,45 +22,45 @@ Alice Gate: *Alice strides into the room with a smile, her eyes lighting up when
22
 
23
  class EndpointHandler():
24
 
25
- def __init__(self, path = "."):
 
 
26
  self.tokenizer = AutoTokenizer.from_pretrained(path)
27
  self.model = AutoModelForCausalLM.from_pretrained(
28
  path,
29
  device_map = "auto",
30
- load_in_8bit = True
 
 
31
  )
32
 
33
  def __call__(self, data):
34
  inputs = data.pop("inputs", data)
35
- try:
36
- prompt = template.format(
37
- user_name = inputs["user_name"],
38
- user_input = "\n".join(inputs["user_input"])
39
- )
40
- input_ids = self.tokenizer(
41
- prompt,
42
- return_tensors="pt"
43
- ) .input_ids
44
- stream_generator = self.model.generate(
45
- input_ids,
46
- max_new_tokens = 50,
47
- do_sample = True,
48
- do_stream = True,
49
- temperature = 0.5,
50
- top_p = 0.9,
51
- top_k = 0,
52
- repetition_penalty = 1.1,
53
- pad_token_id = 50256,
54
- num_return_sequences = 1
55
- )
56
- result = []
57
- for token in stream_generator:
58
- result.append(self.tokenizer.decode(token))
59
- if len(result) != 1 and result[-1] == "\n":
60
- return {
61
- "message": " ".join(filter(None, re.sub("\*.*?\*", "", "".join(result).strip()).split()))
62
- }
63
- except Exception as e:
64
- return {
65
- "error": str(e)
66
- }
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2
  from transformers_stream_generator import init_stream_support
3
  import re
4
  init_stream_support()
 
22
 
23
  class EndpointHandler():
24
 
25
+ def __init__(self, path = ""):
26
+ path = ""
27
+ # quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload = True)
28
  self.tokenizer = AutoTokenizer.from_pretrained(path)
29
  self.model = AutoModelForCausalLM.from_pretrained(
30
  path,
31
  device_map = "auto",
32
+ load_in_8bit = True,
33
+ torch_dtype = "auto",
34
+ low_cpu_mem_usage = True
35
  )
36
 
37
  def __call__(self, data):
38
  inputs = data.pop("inputs", data)
39
+ prompt = template.format(
40
+ user_name = inputs["user_name"],
41
+ user_input = "\n".join(inputs["user_input"])
42
+ )
43
+ input_ids = self.tokenizer(
44
+ prompt,
45
+ return_tensors = "pt"
46
+ ).input_ids
47
+ stream_generator = self.model.generate(
48
+ input_ids,
49
+ max_new_tokens = 50,
50
+ do_sample = True,
51
+ do_stream = True,
52
+ temperature = 0.5,
53
+ top_p = 0.9,
54
+ top_k = 0,
55
+ repetition_penalty = 1.1,
56
+ pad_token_id = 50256,
57
+ num_return_sequences = 1
58
+ )
59
+ result = []
60
+ for token in stream_generator:
61
+ result.append(self.tokenizer.decode(token))
62
+ response = "".join(result).strip()
63
+ if len(response) != 0 and result[-1] == "\n":
64
+ return {
65
+ "message": " ".join(filter(None, re.sub("\*.*?\*", "", response).split()))
66
+ }