Devops-hestabit commited on
Commit
9398f38
·
1 Parent(s): 6266c3b

Update handler.py

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