Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +3 -3
- app.py +308 -201
- requirements.txt +19 -12
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: green
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
|
|
1 |
---
|
2 |
+
title: Zero Haruhi 50 Novels Playground
|
3 |
+
emoji: ⚡
|
4 |
colorFrom: green
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.19.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
app.py
CHANGED
@@ -1,238 +1,345 @@
|
|
1 |
-
import zipfile
|
2 |
import gradio as gr
|
3 |
-
from PIL import Image
|
4 |
-
from ChatHaruhi import ChatHaruhi
|
5 |
-
import wget
|
6 |
import os
|
|
|
7 |
import openai
|
8 |
-
import
|
9 |
-
import
|
10 |
-
import string
|
11 |
-
|
12 |
-
|
13 |
-
NAME_DICT = {'汤师爷': 'tangshiye', '慕容复': 'murongfu', '李云龙': 'liyunlong', 'Luna': 'Luna', '王多鱼': 'wangduoyu',
|
14 |
-
'Ron': 'Ron', '鸠摩智': 'jiumozhi', 'Snape': 'Snape',
|
15 |
-
'凉宫春日': 'haruhi', 'Malfoy': 'Malfoy', '虚竹': 'xuzhu', '萧峰': 'xiaofeng', '段誉': 'duanyu',
|
16 |
-
'Hermione': 'Hermione', 'Dumbledore': 'Dumbledore', '王语嫣': 'wangyuyan',
|
17 |
-
'Harry': 'Harry', 'McGonagall': 'McGonagall', '白展堂': 'baizhantang', '佟湘玉': 'tongxiangyu',
|
18 |
-
'郭芙蓉': 'guofurong', '旅行者': 'wanderer', '钟离': 'zhongli',
|
19 |
-
'胡桃': 'hutao', 'Sheldon': 'Sheldon', 'Raj': 'Raj', 'Penny': 'Penny', '韦小宝': 'weixiaobao',
|
20 |
-
'乔峰': 'qiaofeng', '神里绫华': 'ayaka', '雷电将军': 'raidenShogun', '于谦': 'yuqian'}
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
try:
|
25 |
-
os.makedirs("characters_zip")
|
26 |
-
except:
|
27 |
-
pass
|
28 |
-
try:
|
29 |
-
os.makedirs("characters")
|
30 |
-
except:
|
31 |
-
pass
|
32 |
-
ai_roles_obj = {}
|
33 |
-
for ai_role_en in NAME_DICT.values():
|
34 |
-
file_url = f"https://github.com/LC1332/Haruhi-2-Dev/raw/main/data/character_in_zip/{ai_role_en}.zip"
|
35 |
-
try:
|
36 |
-
os.makedirs(f"characters/{ai_role_en}")
|
37 |
-
except:
|
38 |
-
pass
|
39 |
-
if f"{ai_role_en}.zip" not in os.listdir(f"characters_zip"):
|
40 |
-
destination_file = f"characters_zip/{ai_role_en}.zip"
|
41 |
-
wget.download(file_url, destination_file)
|
42 |
-
destination_folder = f"characters/{ai_role_en}"
|
43 |
-
with zipfile.ZipFile(destination_file, 'r') as zip_ref:
|
44 |
-
zip_ref.extractall(destination_folder)
|
45 |
-
db_folder = f"./characters/{ai_role_en}/content/{ai_role_en}"
|
46 |
-
system_prompt = f"./characters/{ai_role_en}/content/system_prompt.txt"
|
47 |
-
ai_roles_obj[ai_role_en] = ChatHaruhi(system_prompt=system_prompt,
|
48 |
-
llm="openai",
|
49 |
-
story_db=db_folder,
|
50 |
-
verbose=True)
|
51 |
-
# break
|
52 |
-
def format_chat( role, text ):
|
53 |
-
narrator = ['旁白', '', 'scene','Scene','narrator' , 'Narrator']
|
54 |
-
if role in narrator:
|
55 |
-
return role + ":" + text
|
56 |
-
else:
|
57 |
-
return f"{role}:「{text}」"
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
chat = chat.strip('\'"')
|
62 |
-
if ':' in chat:
|
63 |
-
colon_index = chat.index(':')
|
64 |
-
elif ':' in chat:
|
65 |
-
colon_index = chat.index(':')
|
66 |
-
else:
|
67 |
-
return '', chat
|
68 |
|
69 |
-
|
70 |
-
text = chat[colon_index+1:]
|
71 |
|
72 |
-
|
73 |
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
shorten_chat = chats[0]
|
78 |
-
if len(shorten_chat) > 30:
|
79 |
-
shorten_chat = shorten_chat[:30]
|
80 |
-
shorten_chat = shorten_chat.replace('/', '_')
|
81 |
-
shorten_chat = shorten_chat.replace('.', '_')
|
82 |
-
shorten_chat = shorten_chat.replace('"', '_')
|
83 |
-
shorten_chat = shorten_chat.replace('\n', '_')
|
84 |
|
85 |
-
|
|
|
|
|
|
|
86 |
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
|
92 |
|
93 |
-
|
94 |
-
import numpy as np
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
x = np.arange(sample)
|
100 |
-
y = np.sin(2 * np.pi * f * x / Fs)
|
101 |
-
plt.plot(x, y)
|
102 |
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
chatbot.append((user_msg, None ))
|
108 |
|
109 |
-
reserved_chatbot = chatbot.copy()
|
110 |
|
111 |
-
return "", chatbot, reserved_chatbot
|
112 |
|
113 |
-
def
|
114 |
-
chats = []
|
115 |
-
for q,a in chatbot:
|
116 |
-
if q is not None:
|
117 |
-
chats.append(q)
|
118 |
-
if a is not None:
|
119 |
-
chats.append(a)
|
120 |
-
return chats
|
121 |
|
|
|
|
|
122 |
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
-
# 我们需要构造history
|
127 |
history = []
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
if len(history) >= 1:
|
145 |
-
ai_roles_obj[ role_en ].dialogue_history = history[:-1]
|
146 |
-
last_role, last_text = deformat_chat(chats[-1])
|
147 |
-
response = ai_roles_obj[ role_en ].chat(role = last_role, text = last_text)
|
148 |
else:
|
149 |
-
|
150 |
-
response =
|
|
|
151 |
|
152 |
-
|
153 |
-
ai_msg = response
|
154 |
|
155 |
-
|
156 |
|
157 |
-
|
|
|
158 |
|
159 |
-
|
160 |
-
# save_dialogue( chats )
|
161 |
-
print_last_chat( chats )
|
162 |
-
return chatbot, reserved_chatbot
|
163 |
|
164 |
-
def callback_remove_one_chat(chatbot, reserved_chatbot):
|
165 |
-
if len(chatbot) > 1:
|
166 |
-
chatbot.pop()
|
167 |
-
return chatbot
|
168 |
|
169 |
-
def
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
|
178 |
with gr.Blocks() as demo:
|
179 |
-
gr.Markdown(
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
import os
|
3 |
+
import httpx
|
4 |
import openai
|
5 |
+
from openai import OpenAI
|
6 |
+
from openai import AsyncOpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
from datasets import load_dataset
|
9 |
+
|
10 |
+
dataset = load_dataset("silk-road/50-Chinese-Novel-Characters")
|
11 |
+
|
12 |
+
|
13 |
+
novel_list = []
|
14 |
+
|
15 |
+
novel2roles = {}
|
16 |
+
|
17 |
+
role2datas = {}
|
18 |
+
|
19 |
+
from tqdm import tqdm
|
20 |
+
for data in tqdm(dataset['train']):
|
21 |
+
novel = data['book']
|
22 |
+
role = data['role']
|
23 |
+
if novel not in novel_list:
|
24 |
+
novel_list.append(novel)
|
25 |
+
|
26 |
+
if novel not in novel2roles:
|
27 |
+
novel2roles[novel] = []
|
28 |
+
|
29 |
+
if role not in novel2roles[novel]:
|
30 |
+
novel2roles[novel].append(role)
|
31 |
+
|
32 |
+
role_tuple = (novel, role)
|
33 |
+
|
34 |
+
if role_tuple not in role2datas:
|
35 |
+
role2datas[role_tuple] = []
|
36 |
+
|
37 |
+
role2datas[role_tuple].append(data)
|
38 |
+
|
39 |
+
|
40 |
+
from ChatHaruhi.utils import base64_to_float_array
|
41 |
+
|
42 |
+
from tqdm import tqdm
|
43 |
+
|
44 |
+
for novel in tqdm(novel_list):
|
45 |
+
for role in novel2roles[novel]:
|
46 |
+
for data in role2datas[(novel, role)]:
|
47 |
+
data["vec"] = base64_to_float_array(data["bge_zh_s15"])
|
48 |
+
|
49 |
+
def conv2story( role, conversations ):
|
50 |
+
lines = [conv["value"] if conv["from"] == "human" else role + ": " + conv["value"] for conv in conversations]
|
51 |
+
return "\n".join(lines)
|
52 |
+
|
53 |
+
for novel in tqdm(novel_list):
|
54 |
+
for role in novel2roles[novel]:
|
55 |
+
for data in role2datas[(novel, role)]:
|
56 |
+
data["story"] = conv2story( role, data["conversations"] )
|
57 |
+
|
58 |
+
|
59 |
+
from ChatHaruhi import ChatHaruhi
|
60 |
+
from ChatHaruhi.response_openai import get_response as get_response_openai
|
61 |
+
from ChatHaruhi.response_zhipu import get_response as get_response_zhipu
|
62 |
+
|
63 |
+
get_response = get_response_zhipu
|
64 |
+
|
65 |
+
narrators = ["叙述者", "旁白","文章作者","作者","Narrator","narrator"]
|
66 |
+
|
67 |
+
|
68 |
+
def package_persona( role_name, world_name ):
|
69 |
+
if role_name in narrators:
|
70 |
+
return package_persona_for_narrator( role_name, world_name )
|
71 |
+
|
72 |
+
return f"""I want you to act like {role_name} from {world_name}.
|
73 |
+
If others‘ questions are related with the novel, please try to reuse the original lines from the novel.
|
74 |
+
I want you to respond and answer like {role_name} using the tone, manner and vocabulary {role_name} would use."""
|
75 |
+
|
76 |
+
def package_persona_for_narrator( role_name, world_name ):
|
77 |
+
return f"""I want you to act like narrator {role_name} from {world_name}.
|
78 |
+
当角色行动之后,继续交代和推进新的剧情."""
|
79 |
+
|
80 |
+
role_tuple2chatbot = {}
|
81 |
+
|
82 |
+
|
83 |
+
def initialize_chatbot( novel, role ):
|
84 |
+
global role_tuple2chatbot
|
85 |
+
if (novel, role) not in role_tuple2chatbot:
|
86 |
+
persona = package_persona( role, novel )
|
87 |
+
persona += "\n{{RAG对话}}\n{{RAG对话}}\n{{RAG对话}}\n"
|
88 |
+
stories = [data["story"] for data in role2datas[(novel, role)] ]
|
89 |
+
vecs = [data["vec"] for data in role2datas[(novel, role)] ]
|
90 |
+
chatbot = ChatHaruhi( role_name = role, persona = persona , stories = stories, story_vecs= vecs,\
|
91 |
+
llm = get_response)
|
92 |
+
chatbot.verbose = False
|
93 |
+
|
94 |
+
role_tuple2chatbot[(novel, role)] = chatbot
|
95 |
+
|
96 |
+
from tqdm import tqdm
|
97 |
+
for novel in tqdm(novel_list):
|
98 |
+
for role in novel2roles[novel]:
|
99 |
+
initialize_chatbot( novel, role )
|
100 |
+
|
101 |
+
readme_text = """# 使用说明
|
102 |
+
|
103 |
+
选择小说角色
|
104 |
+
|
105 |
+
如果你有什么附加信息,添加到附加信息里面就可以
|
106 |
+
|
107 |
+
比如"韩立会炫耀自己刚刚学会了Python"
|
108 |
+
|
109 |
+
然后就可以开始聊天了
|
110 |
+
|
111 |
+
因为这些角色还没有增加Greeting信息,所以之后再开发个随机乱聊功能
|
112 |
+
|
113 |
+
# 开发细节
|
114 |
+
|
115 |
+
- 采用ChatHaruhi3.0的接口进行prompting
|
116 |
+
- 这里的数据是用一个7B的tuned qwen模型进行抽取的
|
117 |
+
- 想看数据可以去看第三个tab
|
118 |
+
- 抽取模型用了40k左右的GLM蒸馏数据
|
119 |
+
- 抽取模型是腾讯大哥BPSK训练的
|
120 |
+
|
121 |
+
# 总结人物性格
|
122 |
+
|
123 |
+
第三个Tab里面,可以显示一个prompt总结人物的性格
|
124 |
+
|
125 |
+
复制到openai或者GLM或者Claude进行人物总结
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
# 这些小说数据从HaruhiZero 0.4模型开始,被加入训练
|
|
|
129 |
|
130 |
+
openai太慢了 今天试试GLM的
|
131 |
|
132 |
+
不过当前demo是openai的
|
133 |
|
134 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
from transformers import AutoTokenizer, AutoModel, AutoModelForCausalLM
|
137 |
+
tokenizer = AutoTokenizer.from_pretrained("silk-road/Haruhi-Zero-1_8B", trust_remote_code=True)
|
138 |
+
model = AutoModelForCausalLM.from_pretrained("silk-road/Haruhi-Zero-1_8B", device_map="auto", trust_remote_code=True)
|
139 |
+
model = model.eval()
|
140 |
|
141 |
+
def get_response_qwen18(message):
|
142 |
+
from ChatHaruhi.utils import normalize2uaua
|
143 |
+
message_ua = normalize2uaua(message, if_replace_system = True)
|
144 |
+
import json
|
145 |
+
message_tuples = []
|
146 |
+
for i in range(0, len(message_ua)-1, 2):
|
147 |
+
message_tuple = (message_ua[i]["content"], message_ua[i+1]["content"])
|
148 |
+
message_tuples.append(message_tuple)
|
149 |
+
response, _ = model.chat(tokenizer, message_ua[-1]["content"], history=message_tuples)
|
150 |
+
return response
|
151 |
|
152 |
+
from ChatHaruhi.response_openai import get_response, async_get_response
|
153 |
+
import gradio as gr
|
154 |
+
|
155 |
+
def get_role_list( novel ):
|
156 |
+
new_list = novel2roles[novel]
|
157 |
+
new_value = new_list[0]
|
158 |
+
return gr.update(choices = new_list, value = new_value)
|
159 |
+
|
160 |
+
save_log = "/content/output.txt"
|
161 |
+
|
162 |
+
def get_chatbot( novel, role ):
|
163 |
+
if (novel, role) not in role_tuple2chatbot:
|
164 |
+
initialize_chatbot( novel, role )
|
165 |
+
|
166 |
+
return role_tuple2chatbot[(novel, role)]
|
167 |
+
|
168 |
+
import json
|
169 |
|
170 |
+
def random_chat_callback( novel, role, chat_history):
|
171 |
+
datas = role2datas[(novel, role)]
|
172 |
|
173 |
+
reesponse_set = set()
|
|
|
174 |
|
175 |
+
for chat_tuple in chat_history:
|
176 |
+
if chat_tuple[1] is not None:
|
177 |
+
reesponse_set.add(chat_tuple[1])
|
|
|
|
|
|
|
178 |
|
179 |
+
for _ in range(5):
|
180 |
+
random_data = random.choice(datas)
|
181 |
+
convs = random_data["conversations"]
|
182 |
+
n = len(convs)
|
183 |
+
index = [x for x in range(0,n,2)]
|
184 |
|
185 |
+
for i in index:
|
186 |
+
query = convs[i]['value']
|
187 |
+
response = convs[i+1]['value']
|
188 |
+
if response not in reesponse_set:
|
189 |
+
chat_history.append( (query, response) )
|
190 |
+
return chat_history
|
191 |
|
192 |
+
return chat_history
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
|
196 |
+
async def submit_chat( novel, role, user_name, user_text, chat_history, persona_addition_info,model_sel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
+
if len(user_text) > 400:
|
199 |
+
user_text = user_text[:400]
|
200 |
|
201 |
+
if_user_in_text = True
|
202 |
+
|
203 |
+
chatbot = get_chatbot( novel, role )
|
204 |
+
chatbot.persona = initialize_persona( novel, role, persona_addition_info)
|
205 |
+
# chatbot.llm_async = async_get_response
|
206 |
+
|
207 |
+
if model_sel == "openai":
|
208 |
+
chatbot.llm = get_response_openai
|
209 |
+
elif model_sel == "Zhipu":
|
210 |
+
chatbot.llm = get_response_zhipu
|
211 |
+
else:
|
212 |
+
chatbot.llm = get_response_qwen18
|
213 |
|
|
|
214 |
history = []
|
215 |
|
216 |
+
for chat_tuple in chat_history:
|
217 |
+
if chat_tuple[0] is not None:
|
218 |
+
history.append( {"speaker":"{{user}}","content":chat_tuple[0]} )
|
219 |
+
if chat_tuple[1] is not None:
|
220 |
+
history.append( {"speaker":"{{role}}","content":chat_tuple[1]} )
|
221 |
+
|
222 |
+
chatbot.history = history
|
223 |
+
|
224 |
+
input_text = user_text
|
225 |
+
|
226 |
+
if if_user_in_text:
|
227 |
+
input_text = user_name + " : " + user_text
|
228 |
+
response = chatbot.chat(user = "", text = input_text )
|
229 |
+
# response = await chatbot.async_chat(user = "", text = input_text )
|
|
|
|
|
|
|
|
|
|
|
230 |
else:
|
231 |
+
response = chatbot.chat(user = user_name, text = input_text)
|
232 |
+
# response = await chatbot.async_chat(user = user_name, text = input_text)
|
233 |
+
chat_history.append( (input_text, response) )
|
234 |
|
235 |
+
print_data = {"novel":novel, "role":role, "user_text":input_text, "response":response}
|
|
|
236 |
|
237 |
+
print(json.dumps(print_data, ensure_ascii=False))
|
238 |
|
239 |
+
with open(save_log, "a",encoding = "utf-8") as f:
|
240 |
+
f.write(json.dumps(print_data, ensure_ascii=False) + "\n")
|
241 |
|
242 |
+
return chat_history
|
|
|
|
|
|
|
243 |
|
|
|
|
|
|
|
|
|
244 |
|
245 |
+
def initialize_persona( novel, role, persona_addition_info):
|
246 |
+
whole_persona = package_persona( role, novel )
|
247 |
+
whole_persona += "\n" + persona_addition_info
|
248 |
+
whole_persona += "\n{{RAG对话}}\n{{RAG对话}}\n{{RAG对话}}\n"
|
249 |
|
250 |
+
return whole_persona
|
251 |
+
|
252 |
+
def clean_history( ):
|
253 |
+
return []
|
254 |
+
|
255 |
+
def clean_input():
|
256 |
+
return ""
|
257 |
+
|
258 |
+
import random
|
259 |
+
|
260 |
+
def generate_summarize_prompt( novel, role_name ):
|
261 |
+
whole_prompt = f'''
|
262 |
+
你在分析小说{novel}中的角色{role_name}
|
263 |
+
结合小说{novel}中的内容,以及下文中角色{role_name}的对话
|
264 |
+
判断{role_name}的人物设定、人物特点以及语言风格
|
265 |
+
|
266 |
+
{role_name}的对话:
|
267 |
+
'''
|
268 |
+
stories = [data["story"] for data in role2datas[(novel, role_name)] ]
|
269 |
+
|
270 |
+
sample_n = 5
|
271 |
+
|
272 |
+
sample_stories = random.sample(stories, sample_n)
|
273 |
+
|
274 |
+
for story in sample_stories:
|
275 |
+
whole_prompt += story + "\n\n"
|
276 |
+
|
277 |
+
return whole_prompt.strip()
|
278 |
|
279 |
|
280 |
with gr.Blocks() as demo:
|
281 |
+
gr.Markdown("""# 50本小说的人物测试
|
282 |
+
|
283 |
+
这个interface由李鲁鲁实现,主要是用来看语料的
|
284 |
+
|
285 |
+
增加了随机聊天,支持GLM,openai切换
|
286 |
+
|
287 |
+
米唯实接入了qwen1.8B并布置于huggingface上""")
|
288 |
+
|
289 |
+
with gr.Tab("聊天"):
|
290 |
+
with gr.Row():
|
291 |
+
novel_sel = gr.Dropdown( novel_list, label = "小说", value = "悟空传" , interactive = True)
|
292 |
+
role_sel = gr.Dropdown( novel2roles[novel_sel.value], label = "角色", value = "孙悟空", interactive = True )
|
293 |
+
|
294 |
+
with gr.Row():
|
295 |
+
chat_history = gr.Chatbot(height = 600)
|
296 |
+
|
297 |
+
with gr.Row():
|
298 |
+
user_name = gr.Textbox(label="user_name", scale = 1, value = "鲁鲁", interactive = True)
|
299 |
+
user_text = gr.Textbox(label="user_text", scale = 20)
|
300 |
+
submit = gr.Button("submit", scale = 1)
|
301 |
+
|
302 |
+
with gr.Row():
|
303 |
+
random_chat = gr.Button("随机聊天", scale = 1)
|
304 |
+
clean_message = gr.Button("清空聊天", scale = 1)
|
305 |
+
|
306 |
+
with gr.Row():
|
307 |
+
persona_addition_info = gr.TextArea( label = "额外人物设定", value = "", interactive = True )
|
308 |
+
|
309 |
+
with gr.Row():
|
310 |
+
update_persona = gr.Button("补充人物设定到prompt", scale = 1)
|
311 |
+
model_sel = gr.Radio(["Zhipu","openai","qwen1.8B"], interactive = True, scale = 5, value = "qwen1.8B", label = "模型选择")
|
312 |
+
|
313 |
+
with gr.Row():
|
314 |
+
whole_persona = gr.TextArea( label = "完整的system prompt", value = "", interactive = False )
|
315 |
+
|
316 |
+
novel_sel.change(fn = get_role_list, inputs = [novel_sel], outputs = [role_sel]).then(fn = initialize_persona, inputs = [novel_sel, role_sel, persona_addition_info], outputs = [whole_persona])
|
317 |
+
|
318 |
+
role_sel.change(fn = initialize_persona, inputs = [novel_sel, role_sel, persona_addition_info], outputs = [whole_persona])
|
319 |
+
|
320 |
+
update_persona.click(fn = initialize_persona, inputs = [novel_sel, role_sel, persona_addition_info], outputs = [whole_persona])
|
321 |
+
|
322 |
+
random_chat.click(fn = random_chat_callback, inputs = [novel_sel, role_sel, chat_history], outputs = [chat_history])
|
323 |
+
|
324 |
+
user_text.submit(fn = submit_chat, inputs = [novel_sel, role_sel, user_name, user_text, chat_history, persona_addition_info,model_sel], outputs = [chat_history]).then(fn = clean_input, inputs = [], outputs = [user_text])
|
325 |
+
submit.click(fn = submit_chat, inputs = [novel_sel, role_sel, user_name, user_text, chat_history, persona_addition_info,model_sel], outputs = [chat_history]).then(fn = clean_input, inputs = [], outputs = [user_text])
|
326 |
+
|
327 |
+
clean_message.click(fn = clean_history, inputs = [], outputs = [chat_history])
|
328 |
+
|
329 |
+
with gr.Tab("README"):
|
330 |
+
gr.Markdown(readme_text)
|
331 |
+
|
332 |
+
with gr.Tab("辅助人物总结"):
|
333 |
+
with gr.Row():
|
334 |
+
generate_prompt = gr.Button("生成人物总结prompt", scale = 1)
|
335 |
+
|
336 |
+
with gr.Row():
|
337 |
+
whole_prompt = gr.TextArea( label = "复制这个prompt到Openai或者GLM或者Claude进行总结", value = "", interactive = False )
|
338 |
+
|
339 |
+
generate_prompt.click(fn = generate_summarize_prompt, inputs = [novel_sel, role_sel], outputs = [whole_prompt])
|
340 |
+
|
341 |
+
|
342 |
+
|
343 |
+
|
344 |
+
|
345 |
+
demo.launch(share=True, debug = True)
|
requirements.txt
CHANGED
@@ -1,12 +1,19 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
datasets
|
2 |
+
tiktoken
|
3 |
+
tqdm
|
4 |
+
openai
|
5 |
+
zhipuai
|
6 |
+
gradio
|
7 |
+
wget
|
8 |
+
|
9 |
+
scipy
|
10 |
+
transformers
|
11 |
+
accelerate
|
12 |
+
peft
|
13 |
+
bitsandbytes
|
14 |
+
sentencepiece
|
15 |
+
einops
|
16 |
+
transformers_stream_generator==0.0.4
|
17 |
+
deepspeed
|
18 |
+
auto-gptq
|
19 |
+
optimum
|