Spaces:
Sleeping
Sleeping
Update handler.py
Browse files- handler.py +34 -36
handler.py
CHANGED
@@ -1,24 +1,28 @@
|
|
1 |
from threading import Thread
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
3 |
import re
|
4 |
import time
|
5 |
import torch
|
6 |
|
7 |
-
template = """
|
8 |
<START>
|
9 |
-
{user_name}:
|
10 |
-
|
11 |
-
{user_name}:
|
12 |
-
|
13 |
-
{user_name}:
|
14 |
-
|
15 |
-
{user_name}:
|
16 |
-
|
17 |
-
{user_name}:
|
18 |
-
|
19 |
-
{user_name}:
|
|
|
|
|
20 |
<END>
|
21 |
-
|
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 =
|
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 |
-
|
46 |
-
|
47 |
-
inputs,
|
48 |
max_new_tokens = 50,
|
49 |
temperature = 0.5,
|
50 |
top_p = 0.9,
|
51 |
top_k = 0,
|
52 |
-
|
|
|
53 |
repetition_penalty = 1.1,
|
54 |
pad_token_id = 50256,
|
55 |
num_return_sequences = 1
|
56 |
)
|
57 |
-
|
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 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
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)
|