BlueDice commited on
Commit
8bde110
·
1 Parent(s): a474153

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +26 -27
handler.py CHANGED
@@ -1,5 +1,5 @@
1
- from transformers import AutoTokenizer
2
  from optimum.onnxruntime import ORTModelForCausalLM
 
3
  import re
4
  import time
5
  import torch
@@ -19,40 +19,25 @@ Alice Gate: Yeah, it's really fun. I'm lucky to be able to do this as a job.
19
  {user_name}: Definetly.
20
  <END>
21
  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!
22
- {user_input}
23
- Alice Gate:"""
24
 
25
- class EndpointHandler():
26
 
27
- def __init__(self, path = ""):
28
  self.tokenizer = AutoTokenizer.from_pretrained(path)
29
  self.model = ORTModelForCausalLM.from_pretrained(path)#provider = "CUDAExecutionProvider"
30
-
31
- def response(self, result, user_name):
32
- result = result.rsplit("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
33
- parsed_result = re.sub('\*.*?\*', '', result).strip()
34
- result = parsed_result if len(parsed_result) != 0 else result.replace("*","")
35
- result = " ".join(result.split())
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
  prompt = template.format(
48
  user_name = user_name,
49
  user_input = user_input
50
  )
51
- input_ids = self.tokenizer(
52
- prompt,
53
- return_tensors = "pt"
54
- ).to("cuda")
55
- generator = self.model.generate(
56
  input_ids["input_ids"],
57
  max_new_tokens = 50,
58
  temperature = 0.5,
@@ -62,4 +47,18 @@ class EndpointHandler():
62
  pad_token_id = 50256,
63
  num_return_sequences = 1
64
  )
65
- return self.response(self.tokenizer.decode(generator[0], skip_special_tokens=True), user_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from optimum.onnxruntime import ORTModelForCausalLM
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import re
4
  import time
5
  import torch
 
19
  {user_name}: Definetly.
20
  <END>
21
  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!
22
+ {user_input}"""
 
23
 
24
+ class SweetCommander():
25
 
26
+ def __init__(self, path="") -> None:
27
  self.tokenizer = AutoTokenizer.from_pretrained(path)
28
  self.model = ORTModelForCausalLM.from_pretrained(path)#provider = "CUDAExecutionProvider"
29
+ self.star_line = "***********************************************************"
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ def __call__(self, user_name, user_input):
32
+ t1 = time.time()
 
 
33
  prompt = template.format(
34
  user_name = user_name,
35
  user_input = user_input
36
  )
37
+ print(self.star_line)
38
+ print(prompt)
39
+ input_ids = self.tokenizer(prompt + "\nAlice Gate:", return_tensors = "pt")
40
+ encoded_output = self.model.generate(
 
41
  input_ids["input_ids"],
42
  max_new_tokens = 50,
43
  temperature = 0.5,
 
47
  pad_token_id = 50256,
48
  num_return_sequences = 1
49
  )
50
+ decoded_output = self.tokenizer.decode(encoded_output[0], skip_special_tokens = True).replace(prompt, "")
51
+ decoded_output = decoded_output.split("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
52
+ parsed_result = re.sub('\*.*?\*', '', decoded_output).strip()
53
+ if len(parsed_result) != 0: decoded_output = parsed_result
54
+ decoded_output = decoded_output.replace("*","")
55
+ decoded_output = " ".join(decoded_output.split())
56
+ try:
57
+ parsed_result = decoded_output[:[m.start() for m in re.finditer(r'[.!?]', decoded_output)][-1]+1]
58
+ if len(parsed_result) != 0: decoded_output = parsed_result
59
+ except Exception: pass
60
+ print(self.star_line)
61
+ print("Response:",decoded_output)
62
+ print("Eval time:",time.time()-t1)
63
+ print(self.star_line)
64
+ return decoded_output