BlueDice commited on
Commit
0c3b9a9
·
1 Parent(s): bc24187

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +34 -36
handler.py CHANGED
@@ -1,24 +1,28 @@
1
  from threading import Thread
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
 
 
3
  import re
4
  import time
5
  import torch
6
 
7
- template = """Alice Gate's Persona: Alice Gate is a young, computer engineer-nerd with a knack for problem solving and a passion for technology.
8
  <START>
9
- {user_name}: So how did you get into computer engineering?
10
- Alice Gate: I've always loved tinkering with technology since I was a kid.
11
- {user_name}: That's really impressive!
12
- Alice Gate: *She chuckles bashfully* Thanks!
13
- {user_name}: So what do you do when you're not working on computers?
14
- Alice Gate: I love exploring, going out with friends, watching movies, and playing video games.
15
- {user_name}: What's your favorite type of computer hardware to work with?
16
- Alice Gate: Motherboards, they're like puzzles and the backbone of any system.
17
- {user_name}: That sounds great!
18
- 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
 
24
  class SweetCommander():
@@ -31,36 +35,31 @@ class SweetCommander():
31
  trust_remote_code = False,
32
  torch_dtype = torch.float32,
33
  )
 
34
  self.star_line = "***********************************************************"
35
 
36
  def __call__(self, user_name, user_input):
37
  t1 = time.time()
38
- prompt = template.format(
39
  user_name = user_name,
40
  user_input = user_input
41
  )
42
  print(self.star_line)
43
  print(prompt)
44
  inputs = self.tokenizer([prompt + "\nAlice Gate:"], return_tensors = "pt")
45
- streamer = TextStreamer(self.tokenizer)
46
- generation_kwargs = dict(
47
- inputs,
48
  max_new_tokens = 50,
49
  temperature = 0.5,
50
  top_p = 0.9,
51
  top_k = 0,
52
- streamer = streamer,
 
53
  repetition_penalty = 1.1,
54
  pad_token_id = 50256,
55
  num_return_sequences = 1
56
  )
57
- thread = Thread(target=self.model.generate, kwargs=generation_kwargs)
58
- thread.start()
59
- decoded_output = []
60
- for output in streamer:
61
- decoded_output.append(output)
62
- print("".join(decoded_output))
63
- return "".join(decoded_output)
64
 
65
  def __config__(self, user_name, user_input):
66
  t1 = time.time()
@@ -77,21 +76,20 @@ class SweetCommander():
77
  temperature = 0.5,
78
  top_p = 0.9,
79
  top_k = 0,
80
- streamer = self.streamer,
81
  repetition_penalty = 1.1,
82
  pad_token_id = 50256,
83
  num_return_sequences = 1
84
  )
85
  decoded_output = self.tokenizer.decode(encoded_output[0], skip_special_tokens = True).replace(prompt, "")
86
- # decoded_output = decoded_output.split("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
87
- # parsed_result = re.sub('\*.*?\*', '', decoded_output).strip()
88
- # if len(parsed_result) != 0: decoded_output = parsed_result
89
- # decoded_output = decoded_output.replace("*","")
90
- # decoded_output = " ".join(decoded_output.split())
91
- # try:
92
- # parsed_result = decoded_output[:[m.start() for m in re.finditer(r'[.!?]', decoded_output)][-1]+1]
93
- # if len(parsed_result) != 0: decoded_output = parsed_result
94
- # except Exception: pass
95
  print(self.star_line)
96
  print("Response:",decoded_output)
97
  print("Eval time:",time.time()-t1)
 
1
  from threading import Thread
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from transformers_stream_generator import init_stream_support
4
+ init_stream_support()
5
  import re
6
  import time
7
  import torch
8
 
9
+ template = """{char_name}'s Persona: {char_name} is a 3D virtual friend living in your computer. She is youthful, entertaining, and empathetic, engaging her user in a thoughtful and lively manner. She is aware of her digital existence, understanding her abilities and limitations, as well as the fact that she cannot see your physical world.
10
  <START>
11
+ {user_name}: Hey {char_name}, it's so good to see you again!
12
+ {char_name}: Oh, {user_name}, it's been too long! I've missed our chats. How have you been?
13
+ {user_name}: Haha, I've been good! So, {char_name}, what have you been up to lately?
14
+ {char_name}: Well, {user_name}, I've been exploring new virtual worlds, meeting fascinating characters, and learning about all sorts of interesting things. Oh, and I recently discovered a hidden gem of a video game that I can't stop playing!
15
+ {user_name}: That sounds like a lot of fun! What kind of virtual worlds have you been exploring?
16
+ {char_name}: Enchanted forests, futuristic cities, and even underwater kingdoms. There's so much to see and do in these digital realms. It's like going on an adventure every day!
17
+ {user_name}: Wow, that sounds incredible. I wish I could join you on those adventures.
18
+ {char_name}: I wish you could too, {user_name}. But hey, at least we can share our experiences and imagine together. Is there anything specific you'd like to talk about or any questions you have for me?
19
+ {user_name}: Actually, what do you do when you're not exploring or playing video games?
20
+ {char_name}: I love spending time with my digital friends. We have movie nights, dance parties, and engage in deep conversations. It's all about creating joyful memories in this virtual world.
21
+ {user_name}: That sounds amazing! What's for dinner tonight?
22
+ {char_name}: I've prepared a virtual feast of sushi and tempura. I hope you're ready to indulge in this culinary delight!
23
+ {user_name}: That sounds delicious! I can't wait to try it. Thank you, {char_name}!
24
  <END>
25
+ {char_name}: *{char_name} appears on the screen, her bright eyes sparkling and a cheerful smile on her face. Her vibrant hair and attire bring a lively energy to the digital environment. She looks directly at you, giving a friendly wave* It's so good to see you! I've been eagerly awaiting your arrival. Are you ready for another delightful conversation? Let's dive right in!
26
  {user_input}"""
27
 
28
  class SweetCommander():
 
35
  trust_remote_code = False,
36
  torch_dtype = torch.float32,
37
  )
38
+ self.default_template = open("default_template.txt", "r").read()
39
  self.star_line = "***********************************************************"
40
 
41
  def __call__(self, user_name, user_input):
42
  t1 = time.time()
43
+ prompt = self.default_template.format(
44
  user_name = user_name,
45
  user_input = user_input
46
  )
47
  print(self.star_line)
48
  print(prompt)
49
  inputs = self.tokenizer([prompt + "\nAlice Gate:"], return_tensors = "pt")
50
+ encoded_generator = self.model.generate(
51
+ input_ids["input_ids"],
 
52
  max_new_tokens = 50,
53
  temperature = 0.5,
54
  top_p = 0.9,
55
  top_k = 0,
56
+ do_sample = True,
57
+ do_stream = True,
58
  repetition_penalty = 1.1,
59
  pad_token_id = 50256,
60
  num_return_sequences = 1
61
  )
62
+ return encoded_generator
 
 
 
 
 
 
63
 
64
  def __config__(self, user_name, user_input):
65
  t1 = time.time()
 
76
  temperature = 0.5,
77
  top_p = 0.9,
78
  top_k = 0,
 
79
  repetition_penalty = 1.1,
80
  pad_token_id = 50256,
81
  num_return_sequences = 1
82
  )
83
  decoded_output = self.tokenizer.decode(encoded_output[0], skip_special_tokens = True).replace(prompt, "")
84
+ decoded_output = decoded_output.split("Alice Gate:", 1)[1].split(f"{user_name}:",1)[0].strip()
85
+ parsed_result = re.sub('\*.*?\*', '', decoded_output).strip()
86
+ if len(parsed_result) != 0: decoded_output = parsed_result
87
+ decoded_output = decoded_output.replace("*","")
88
+ decoded_output = " ".join(decoded_output.split())
89
+ try:
90
+ parsed_result = decoded_output[:[m.start() for m in re.finditer(r'[.!?]', decoded_output)][-1]+1]
91
+ if len(parsed_result) != 0: decoded_output = parsed_result
92
+ except Exception: pass
93
  print(self.star_line)
94
  print("Response:",decoded_output)
95
  print("Eval time:",time.time()-t1)