Files changed (2) hide show
  1. handler.py +29 -33
  2. requirements.txt +2 -0
handler.py CHANGED
@@ -1,6 +1,6 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
- import re
3
- import torch
4
 
5
  template = """Alice Gate's Persona: Alice Gate is a young, computer engineer-nerd with a knack for problem solving and a passion for technology.
6
  <START>
@@ -14,46 +14,38 @@ Alice Gate: I love exploring, going out with friends, watching movies, and playi
14
  Alice Gate: Motherboards, they're like puzzles and the backbone of any system.
15
  {user_name}: That sounds great!
16
  Alice Gate: Yeah, it's really fun. I'm lucky to be able to do this as a job.
17
- {user_name}: Definetly.
18
  <END>
19
  Alice Gate: *Alice strides into the room with a smile, her eyes lighting up when she sees you. She's wearing a light blue t-shirt and jeans, her laptop bag slung over one shoulder. She takes a seat next to you, her enthusiasm palpable in the air* Hey! I'm so excited to finally meet you. I've heard so many great things about you and I'm eager to pick your brain about computers. I'm sure you have a wealth of knowledge that I can learn from. *She grins, eyes twinkling with excitement* Let's get started!
20
- {user_input}
21
- Alice Gate:"""
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
- low_cpu_mem_usage = True,
30
- trust_remote_code = False,
31
- torch_dtype = torch.float16,
32
- ).to('cuda')
33
-
34
- def response(self, result, user_name):
35
- result = result.rsplit("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
36
- try:
37
- result = result[:[m.start() for m in re.finditer(r'[.!?]', result)][-1]+1]
38
- except Exception: pass
39
- return {
40
- "message": result
41
- }
42
 
43
  def __call__(self, data):
44
- inputs = data.pop("inputs", data)
45
- user_name = inputs["user_name"]
46
- user_input = "\n".join(inputs["user_input"])
47
  input_ids = self.tokenizer(
48
- template.format(
49
- user_name = user_name,
50
- user_input = user_input
51
- ),
52
- return_tensors = "pt"
53
- ).to("cuda")
54
- generator = self.model.generate(
55
- input_ids["input_ids"],
56
- max_new_tokens = 50,
57
  temperature = 0.5,
58
  top_p = 0.9,
59
  top_k = 0,
@@ -61,4 +53,8 @@ class EndpointHandler():
61
  pad_token_id = 50256,
62
  num_return_sequences = 1
63
  )
64
- return self.response(self.tokenizer.decode(generator[0], skip_special_tokens=True), user_name)
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
2
+ from transformers_stream_generator import init_stream_support
3
+ init_stream_support()
4
 
5
  template = """Alice Gate's Persona: Alice Gate is a young, computer engineer-nerd with a knack for problem solving and a passion for technology.
6
  <START>
 
14
  Alice Gate: Motherboards, they're like puzzles and the backbone of any system.
15
  {user_name}: That sounds great!
16
  Alice Gate: Yeah, it's really fun. I'm lucky to be able to do this as a job.
 
17
  <END>
18
  Alice Gate: *Alice strides into the room with a smile, her eyes lighting up when she sees you. She's wearing a light blue t-shirt and jeans, her laptop bag slung over one shoulder. She takes a seat next to you, her enthusiasm palpable in the air* Hey! I'm so excited to finally meet you. I've heard so many great things about you and I'm eager to pick your brain about computers. I'm sure you have a wealth of knowledge that I can learn from. *She grins, eyes twinkling with excitement* Let's get started!
19
+ """
 
20
 
21
  class EndpointHandler():
22
 
23
+ def __init__(self, path=""):
24
+ quantization_config = BitsAndBytesConfig(
25
+ load_in_8bit = True,
26
+ llm_int8_threshold = 0.0,
27
+ llm_int8_enable_fp32_cpu_offload = True
28
+ )
29
  self.tokenizer = AutoTokenizer.from_pretrained(path)
30
  self.model = AutoModelForCausalLM.from_pretrained(
31
  path,
32
+ device_map = "auto"
33
+ torch_dtype = "auto",
34
+ low_cpu_mem_usage = True,
35
+ quantization_config = quantization_config
36
+ )
 
 
 
 
 
 
 
 
37
 
38
  def __call__(self, data):
39
+ prompt += data.pop("inputs", data)
 
 
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 = 70,
47
+ do_sample = True,
48
+ do_stream = True,
 
49
  temperature = 0.5,
50
  top_p = 0.9,
51
  top_k = 0,
 
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 result[-1] == "\n":
60
+ return "".join(result).strip()
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  accelerate==0.18.0
2
  bitsandbytes==0.37.2
 
 
3
  transformers @ git+https://github.com/huggingface/transformers.git@151425ddb29d4ad1a121e8cce62000a2ac52d3ba
 
1
  accelerate==0.18.0
2
  bitsandbytes==0.37.2
3
+ safetensors==0.3.1
4
+ transformers-stream-generator==0.0.4
5
  transformers @ git+https://github.com/huggingface/transformers.git@151425ddb29d4ad1a121e8cce62000a2ac52d3ba