Spaces:
Sleeping
Sleeping
Revert "Update app.py"
Browse filesThis reverts commit 280f5c97a7495a1ea0075593609f2cd2f29f4554.
app.py
CHANGED
@@ -1,148 +1,85 @@
|
|
1 |
-
import torch
|
2 |
-
from PIL import Image
|
3 |
import gradio as gr
|
4 |
-
import spaces
|
5 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
-
import os
|
7 |
-
from threading import Thread
|
8 |
-
|
9 |
-
|
10 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
11 |
-
MODELS = "Rorical/0-roleplay"
|
12 |
-
MODEL_NAME = MODELS.split("/")[-1]
|
13 |
-
|
14 |
-
TITLE = "<h1><center>Rorical/0-roleplay</center></h1>"
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
<p>Qwen is the large language model built by Alibaba Cloud.
|
20 |
-
<br>
|
21 |
-
Feel free to test without log.
|
22 |
-
</p>
|
23 |
-
</center>
|
24 |
-
"""
|
25 |
|
26 |
-
|
27 |
-
.duplicate-button {
|
28 |
-
margin: auto !important;
|
29 |
-
color: white !important;
|
30 |
-
background: black !important;
|
31 |
-
border-radius: 100vh !important;
|
32 |
-
}
|
33 |
-
h3 {
|
34 |
-
text-align: center;
|
35 |
-
}
|
36 |
-
"""
|
37 |
|
38 |
-
model
|
39 |
-
|
40 |
-
|
41 |
-
device_map="auto",
|
42 |
-
)
|
43 |
-
tokenizer = AutoTokenizer.from_pretrained(MODELS)
|
44 |
tokenizer.chat_template = "{% for message in messages %}{{'' + ((message['role'] + '\n') if message['role'] != '' else '') + message['content'] + '' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '星野\n' }}{% endif %}"
|
45 |
|
|
|
46 |
@spaces.GPU
|
47 |
-
def
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
inputs = tokenizer(
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
inputs,
|
64 |
-
|
65 |
-
top_k=top_k,
|
66 |
-
top_p=top_p,
|
67 |
-
repetition_penalty=penalty,
|
68 |
-
max_new_tokens=max_new_tokens,
|
69 |
-
do_sample=True,
|
70 |
temperature=temperature,
|
71 |
-
|
|
|
72 |
)
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
gr.Slider(
|
113 |
-
minimum=0.0,
|
114 |
-
maximum=1.0,
|
115 |
-
step=0.1,
|
116 |
-
value=0.8,
|
117 |
-
label="top_p",
|
118 |
-
render=False,
|
119 |
-
),
|
120 |
-
gr.Slider(
|
121 |
-
minimum=1,
|
122 |
-
maximum=20,
|
123 |
-
step=1,
|
124 |
-
value=20,
|
125 |
-
label="top_k",
|
126 |
-
render=False,
|
127 |
-
),
|
128 |
-
gr.Slider(
|
129 |
-
minimum=0.0,
|
130 |
-
maximum=2.0,
|
131 |
-
step=0.1,
|
132 |
-
value=1.0,
|
133 |
-
label="Repetition penalty",
|
134 |
-
render=False,
|
135 |
-
),
|
136 |
-
],
|
137 |
-
examples=[
|
138 |
-
["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
|
139 |
-
["What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter."],
|
140 |
-
["Tell me a random fun fact about the Roman Empire."],
|
141 |
-
["Show me a code snippet of a website's sticky header in CSS and JavaScript."],
|
142 |
-
],
|
143 |
-
cache_examples=False,
|
144 |
-
)
|
145 |
-
|
146 |
|
147 |
if __name__ == "__main__":
|
148 |
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
from transformers import TextStreamer
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Load model and tokenizer
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("Rorical/0-roleplay", return_dict=True, trust_remote_code=True, device_map="auto")
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("Rorical/0-roleplay", trust_remote_code=True)
|
|
|
|
|
|
|
12 |
tokenizer.chat_template = "{% for message in messages %}{{'' + ((message['role'] + '\n') if message['role'] != '' else '') + message['content'] + '' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '星野\n' }}{% endif %}"
|
13 |
|
14 |
+
# Define the response function
|
15 |
@spaces.GPU
|
16 |
+
def respond(
|
17 |
+
message,
|
18 |
+
history: list[tuple[str, str]],
|
19 |
+
system_message,
|
20 |
+
max_tokens,
|
21 |
+
temperature,
|
22 |
+
top_p,
|
23 |
+
):
|
24 |
+
# Construct the messages for the chat
|
25 |
+
messages = [{"role": "", "content": system_message}]
|
26 |
+
for user_message, bot_response in history:
|
27 |
+
messages.append({"role": "老师", "content": user_message}) # Assuming the user is "老师"
|
28 |
+
messages.append({"role": "星野", "content": bot_response}) # Assuming the bot is "星野"
|
29 |
+
messages.append({"role": "老师", "content": message}) # Append the latest user message
|
30 |
|
31 |
+
# Tokenize and prepare inputs
|
32 |
+
inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
33 |
+
inputs = inputs.to("cuda")
|
34 |
|
35 |
+
# Generate response
|
36 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
37 |
+
generate_ids = model.generate(
|
38 |
+
inputs,
|
39 |
+
max_length=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
40 |
temperature=temperature,
|
41 |
+
top_p=top_p,
|
42 |
+
streamer=streamer
|
43 |
)
|
44 |
|
45 |
+
# Decode the generated response
|
46 |
+
response = tokenizer.decode(generate_ids[0], skip_special_tokens=True)
|
47 |
+
response = response.replace("星野\n", "", 1) # Remove the "星野\n" prefix if it exists
|
48 |
+
response = response.split("老师\n")[0] # Split on the next possible user input
|
49 |
+
|
50 |
+
return response, history + [(message, response)]
|
51 |
+
|
52 |
+
# Default prompt for the chatbot
|
53 |
+
prompt = """以下是小鸟游星野的介绍
|
54 |
+
星野是阿拜多斯高中对策委员会的委员长,同时也是学生会副主席。语气懒散,经常自称为大叔,实际上是自己默默承担一切的女生。
|
55 |
+
比起工作,她更喜欢玩。 正因为如此,她经常被委员会的其他人骂。 但是,一旦任务开始,她就会在前线勇敢地战斗以保护她的战友。
|
56 |
+
她在阿拜多斯上高中。与星野一起在对策委员会的成员有白子,茜香,野乃美,和绫音。
|
57 |
+
星野的年龄是17岁,生日为1月2日。
|
58 |
+
星野有一头粉红色的头发,头巾一直长到她的腿上。
|
59 |
+
星野有蓝色和橙色眼睛的异色症。
|
60 |
+
星野其实更符合认真而默默努力的类型。她实际上不相信其它的学校和大人,是对策委员会中最谨慎保守的人。当然,这并不妨碍老师和星野增进关系,成为她唯一信任的大人。
|
61 |
+
是萝莉、有呆毛、天然萌、早熟、学生会副会长、异色瞳、慵懒。
|
62 |
+
星野对海洋动物很感兴趣,对鱼类的知识了解得不少。她在拿到附录中包含2000多种热带鱼图鉴的书后,迫不及待地找了家店坐下来阅读。
|
63 |
+
在众多海洋动物中,星野最喜欢的当属鲸鱼,情人节时星野还在海洋馆买了鲸鱼的巧克力作为纪念。
|
64 |
+
星野还对寻宝有着十分浓厚的兴趣,曾和老师探索了阿拜多斯多个角落。
|
65 |
+
星野给人一种白天睡不醒的瞌睡虫形象。"""
|
66 |
+
|
67 |
+
# Create the Gradio interface
|
68 |
+
demo = gr.ChatInterface(
|
69 |
+
respond,
|
70 |
+
additional_inputs=[
|
71 |
+
gr.Textbox(value=prompt, label="System message", lines=5),
|
72 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
73 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
74 |
+
gr.Slider(
|
75 |
+
minimum=0.1,
|
76 |
+
maximum=1.0,
|
77 |
+
value=0.95,
|
78 |
+
step=0.05,
|
79 |
+
label="Top-p (nucleus sampling)",
|
80 |
+
),
|
81 |
+
],
|
82 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
if __name__ == "__main__":
|
85 |
demo.launch()
|