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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#### pip install gradio==3.50.2
|
2 |
+
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
import json
|
7 |
+
import re
|
8 |
+
from functools import partial
|
9 |
+
|
10 |
+
import jieba
|
11 |
+
def repeat_to_one_f(x):
|
12 |
+
req = None
|
13 |
+
for token in jieba.lcut(x):
|
14 |
+
#print("req :", req)
|
15 |
+
|
16 |
+
if len(set(token)) == 1:
|
17 |
+
token = token[0]
|
18 |
+
if req is None:
|
19 |
+
req = token
|
20 |
+
else:
|
21 |
+
|
22 |
+
if token in req:
|
23 |
+
continue
|
24 |
+
else:
|
25 |
+
while req.endswith(token[0]):
|
26 |
+
token = token[1:]
|
27 |
+
req = req + token
|
28 |
+
return req.strip()
|
29 |
+
|
30 |
+
def repeat_to_one_fb(x):
|
31 |
+
return sorted(map(repeat_to_one_f, [x, "".join(jieba.lcut(x)[::-1])]),
|
32 |
+
key = len
|
33 |
+
)[0]
|
34 |
+
|
35 |
+
repeat_to_one = repeat_to_one_fb
|
36 |
+
|
37 |
+
from huggingface_hub import snapshot_download
|
38 |
+
|
39 |
+
if not os.path.exists("genshin-impact-character"):
|
40 |
+
path = snapshot_download(
|
41 |
+
repo_id="svjack/genshin-impact-character",
|
42 |
+
repo_type="dataset",
|
43 |
+
local_dir="genshin-impact-character",
|
44 |
+
local_dir_use_symlinks = False
|
45 |
+
)
|
46 |
+
|
47 |
+
if not os.path.exists("genshin_impact_character_glm6b_base_ggml"):
|
48 |
+
path = snapshot_download(
|
49 |
+
repo_id="svjack/genshin_impact_character_glm6b_base_ggml",
|
50 |
+
repo_type="model",
|
51 |
+
local_dir="genshin_impact_character_glm6b_base_ggml",
|
52 |
+
local_dir_use_symlinks = False
|
53 |
+
)
|
54 |
+
|
55 |
+
info_df = pd.read_csv("genshin-impact-character/genshin_impact_background_settings_constrained.csv")
|
56 |
+
info_df["info"] = info_df["info"].map(eval)
|
57 |
+
|
58 |
+
with open("genshin-impact-character/genshin_impact_character_setting.json", "r") as f:
|
59 |
+
character_setting_total_dict = json.load(f)
|
60 |
+
|
61 |
+
req_dict = {}
|
62 |
+
for k, v_dict in character_setting_total_dict.items():
|
63 |
+
req_dict[k] = {}
|
64 |
+
for kk, vv in v_dict.items():
|
65 |
+
if kk != "元素力":
|
66 |
+
req_dict[k][kk] = vv
|
67 |
+
character_setting_total_dict = req_dict
|
68 |
+
|
69 |
+
def get_character_background_list(info_dict):
|
70 |
+
text = []
|
71 |
+
if "角色详细" in info_dict["描述"]:
|
72 |
+
text.append(info_dict["描述"]["角色详细"])
|
73 |
+
if "更多描述" in info_dict["描述"]:
|
74 |
+
text.append(info_dict["描述"]["更多描述"])
|
75 |
+
return list(map(lambda x: x.replace(" ", "").replace("\n\n", "\n"), text))
|
76 |
+
def get_character_background(info_dict):
|
77 |
+
return "\n".join(get_character_background_list(info_dict))
|
78 |
+
|
79 |
+
pd.DataFrame(
|
80 |
+
pd.Series(character_setting_total_dict.values()).map(
|
81 |
+
lambda x: {
|
82 |
+
"性别": x['性别'],
|
83 |
+
"国籍": x["国籍"]
|
84 |
+
}
|
85 |
+
).values.tolist()).apply(lambda x: set(x), axis = 0).to_dict()
|
86 |
+
|
87 |
+
|
88 |
+
character_setting_total_dist_dict = {
|
89 |
+
'姓名': "",
|
90 |
+
'性别': {'少女女性', '少年男性', '成年女性', '成年男性'},
|
91 |
+
'国籍': {'枫丹', '璃月', '稻妻', '至冬', '蒙德', '须弥'},
|
92 |
+
'身份': "",
|
93 |
+
'性格特征': "",
|
94 |
+
'角色介绍': "",
|
95 |
+
}
|
96 |
+
|
97 |
+
def get_character_setting_total_dict(name):
|
98 |
+
from copy import deepcopy
|
99 |
+
req = deepcopy(character_setting_total_dist_dict)
|
100 |
+
if name in character_setting_total_dict:
|
101 |
+
for k, v in character_setting_total_dict[name].items():
|
102 |
+
req[k] = v
|
103 |
+
info_dict = dict(info_df[["title", "info"]].values.tolist())[name]
|
104 |
+
req["角色介绍"] = get_character_background(info_dict)
|
105 |
+
req["姓名"] = name
|
106 |
+
return req
|
107 |
+
|
108 |
+
prompt_format_dict = {
|
109 |
+
"Basic_Info": ["性别", "国籍", "身份", "性格特征"],
|
110 |
+
|
111 |
+
"两人同属{}": ["国籍"],
|
112 |
+
"{}来自{},{}来自{}。": ["姓名", "国籍", "姓名", "国籍"],
|
113 |
+
|
114 |
+
"下面是{}的一些基本信息\n{}": ["姓名", "Basic_Info"],
|
115 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"],
|
116 |
+
|
117 |
+
"续写下面的角色介绍,下面是角色介绍的开头。{}是{}。{}": ["姓名", "身份", "Text"],
|
118 |
+
"续写下面的角色故事,下面是角色故事的开头。{}是{}。{}": ["姓名", "身份", "Text"],
|
119 |
+
"续写下面获得神之眼的过程,下面是开头。{}是{}。{}": ["姓名", "身份", "Text"],
|
120 |
+
"{}给你写了一封信,信主题是{},信的内容是这样的。": ["姓名", "Text"],
|
121 |
+
|
122 |
+
"{}在进行有关{}的聊天时会说什么?": ["姓名", "Text"],
|
123 |
+
"{}在{}的时候会说什么?": ["姓名", "Text"],
|
124 |
+
"{}在{}时会说什么?": ["姓名", "Text"],
|
125 |
+
"关于{},{}会说什么?": ["Text", "姓名"],
|
126 |
+
"当你想要了解{}时": ["姓名"],
|
127 |
+
|
128 |
+
"关于{},{}会说什么?": ["姓名", "姓名"],
|
129 |
+
"从{}那里,可以获得哪些关于{}的信息?": ["姓名", "姓名"]
|
130 |
+
}
|
131 |
+
|
132 |
+
def single_character_prompt_func(name,
|
133 |
+
used_prompt_format_dict,
|
134 |
+
character_setting_rewrite_dict = {},
|
135 |
+
Text = "",
|
136 |
+
):
|
137 |
+
assert type(used_prompt_format_dict) == type({})
|
138 |
+
assert type(character_setting_rewrite_dict) == type({})
|
139 |
+
character_setting_total_dict = get_character_setting_total_dict(name)
|
140 |
+
for k, v in character_setting_rewrite_dict.items():
|
141 |
+
if k in character_setting_total_dict:
|
142 |
+
character_setting_total_dict[k] = v
|
143 |
+
key = list(used_prompt_format_dict.keys())[0]
|
144 |
+
assert key in prompt_format_dict
|
145 |
+
if key == "Basic_Info":
|
146 |
+
return "\n".join(
|
147 |
+
map(lambda k: "{}:{}".format(k, character_setting_total_dict[k]), prompt_format_dict[key])
|
148 |
+
)
|
149 |
+
elif key == "两人同属{}":
|
150 |
+
return "两人同属{}".format(character_setting_total_dict["国籍"])
|
151 |
+
elif key == "下面是{}的一些基本信息\n{}":
|
152 |
+
return "下面是{}的一些基本信息\n{}".format(name,
|
153 |
+
single_character_prompt_func(name,
|
154 |
+
{
|
155 |
+
"Basic_Info": ["性别", "国籍", "身份", "性格特征"]
|
156 |
+
},
|
157 |
+
character_setting_rewrite_dict
|
158 |
+
)
|
159 |
+
)
|
160 |
+
elif key == "下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}":
|
161 |
+
return "下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}".format(
|
162 |
+
name,
|
163 |
+
single_character_prompt_func(name,
|
164 |
+
{
|
165 |
+
"Basic_Info": ["性别", "国籍", "身份", "性格特征"]
|
166 |
+
},
|
167 |
+
character_setting_rewrite_dict
|
168 |
+
),
|
169 |
+
character_setting_total_dict["角色介绍"]
|
170 |
+
)
|
171 |
+
elif key == "续写下面的角色介绍,下面是角色介绍的开头。{}是{}。{}":
|
172 |
+
return "续写下面的角色介绍,下面是角色介绍的开头。{}是{}。{}".format(
|
173 |
+
name,
|
174 |
+
character_setting_total_dict["身份"],
|
175 |
+
Text
|
176 |
+
)
|
177 |
+
elif key == "续写下面的角色故事,下面是角色故事的开头。{}是{}。{}":
|
178 |
+
return "续写下面的角色故事,下面是角色介绍的开头。{}是{}。{}".format(
|
179 |
+
name,
|
180 |
+
character_setting_total_dict["身份"],
|
181 |
+
Text
|
182 |
+
)
|
183 |
+
elif key == "续写下面获得神之眼的过程,下面是开头。{}是{}。{}":
|
184 |
+
return "续写下面获得神之眼的过程,下面是开头。{}是{}。{}".format(
|
185 |
+
name,
|
186 |
+
character_setting_total_dict["身份"],
|
187 |
+
Text
|
188 |
+
)
|
189 |
+
elif key == "{}给你写了一封信,信主题是{},信的内容是这样的。":
|
190 |
+
return "{}给你写了一封信,信主题是{},信的内容是这样的。".format(
|
191 |
+
name,
|
192 |
+
Text
|
193 |
+
)
|
194 |
+
elif key == "{}在进行有关{}的聊天时会说什么?":
|
195 |
+
return "{}在进行有关{}的聊天时会说什么?".format(
|
196 |
+
name,
|
197 |
+
Text
|
198 |
+
)
|
199 |
+
elif key == "{}在{}的时候会说什么?":
|
200 |
+
return "{}在{}的时候会说什么?".format(
|
201 |
+
name,
|
202 |
+
Text
|
203 |
+
)
|
204 |
+
elif key == "{}在{}时会说什么?":
|
205 |
+
return "{}在{}时会说什么?".format(
|
206 |
+
name,
|
207 |
+
Text
|
208 |
+
)
|
209 |
+
elif key == "关于{},{}会说什么?":
|
210 |
+
return "关于{},{}会说什么?".format(
|
211 |
+
Text,
|
212 |
+
name,
|
213 |
+
)
|
214 |
+
elif key == "当你想要了解{}时":
|
215 |
+
return "当你想要了解{}时".format(
|
216 |
+
name,
|
217 |
+
)
|
218 |
+
return 1 / 0
|
219 |
+
|
220 |
+
def two_character_prompt_func(
|
221 |
+
name_1,
|
222 |
+
name_2,
|
223 |
+
used_prompt_format_dict,
|
224 |
+
character_setting_rewrite_dict_1 = {},
|
225 |
+
character_setting_rewrite_dict_2 = {},
|
226 |
+
):
|
227 |
+
assert type(character_setting_rewrite_dict_1) == type({})
|
228 |
+
character_setting_total_dict_1 = get_character_setting_total_dict(name_1)
|
229 |
+
for k, v in character_setting_rewrite_dict_1.items():
|
230 |
+
if k in character_setting_total_dict_1:
|
231 |
+
character_setting_total_dict_1[k] = v
|
232 |
+
character_setting_total_dict_2 = get_character_setting_total_dict(name_2)
|
233 |
+
for k, v in character_setting_rewrite_dict_2.items():
|
234 |
+
if k in character_setting_total_dict_2:
|
235 |
+
character_setting_total_dict_2[k] = v
|
236 |
+
key = list(used_prompt_format_dict.keys())[0]
|
237 |
+
assert key in prompt_format_dict
|
238 |
+
if key == "关于{},{}会说什么?":
|
239 |
+
return "关于{},{}会说什么?".format(name_1, name_2)
|
240 |
+
elif key == "从{}那里,可以获得哪些关于{}的信息?":
|
241 |
+
return "从{}那里,可以获得哪些关于{}的信息?".format(name_1, name_2)
|
242 |
+
elif key == "{}来自{},{}来自{}。":
|
243 |
+
return "{}来自{},{}来自{}。".format(name_1, character_setting_total_dict_1["国籍"],
|
244 |
+
name_2, character_setting_total_dict_2["国籍"],
|
245 |
+
)
|
246 |
+
return 1 / 0
|
247 |
+
|
248 |
+
def main_single_character_prompt_func(name,
|
249 |
+
used_prompt_format_dict,
|
250 |
+
character_setting_rewrite_dict = {},
|
251 |
+
Text = "",
|
252 |
+
):
|
253 |
+
key = list(used_prompt_format_dict.keys())[0]
|
254 |
+
assert key in prompt_format_dict
|
255 |
+
if key == "续写下面的角色介绍,下面是角色介绍的开头。{}是{}。{}":
|
256 |
+
task_prompt = single_character_prompt_func(
|
257 |
+
name,
|
258 |
+
used_prompt_format_dict,
|
259 |
+
character_setting_rewrite_dict,
|
260 |
+
Text
|
261 |
+
)
|
262 |
+
info_prompt = single_character_prompt_func(
|
263 |
+
name,
|
264 |
+
{
|
265 |
+
"��面是{}的一些基本信息\n{}": ["姓名", "Basic_Info"]
|
266 |
+
},
|
267 |
+
character_setting_rewrite_dict,
|
268 |
+
Text
|
269 |
+
)
|
270 |
+
elif key == "续写下面的角色故事,下面是角色故事的开头。{}是{}。{}":
|
271 |
+
task_prompt = single_character_prompt_func(
|
272 |
+
name,
|
273 |
+
used_prompt_format_dict,
|
274 |
+
character_setting_rewrite_dict,
|
275 |
+
Text
|
276 |
+
)
|
277 |
+
info_prompt = single_character_prompt_func(
|
278 |
+
name,
|
279 |
+
{
|
280 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
281 |
+
},
|
282 |
+
character_setting_rewrite_dict,
|
283 |
+
Text
|
284 |
+
)
|
285 |
+
|
286 |
+
elif key == "续写下面获得神之眼的过程,下面是开头。{}是{}。{}":
|
287 |
+
task_prompt = single_character_prompt_func(
|
288 |
+
name,
|
289 |
+
used_prompt_format_dict,
|
290 |
+
character_setting_rewrite_dict,
|
291 |
+
Text
|
292 |
+
)
|
293 |
+
info_prompt = single_character_prompt_func(
|
294 |
+
name,
|
295 |
+
{
|
296 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
297 |
+
},
|
298 |
+
character_setting_rewrite_dict,
|
299 |
+
Text
|
300 |
+
)
|
301 |
+
|
302 |
+
elif key == "{}给你写了一封信,信主题是{},信的内容是这样的。":
|
303 |
+
task_prompt = single_character_prompt_func(
|
304 |
+
name,
|
305 |
+
used_prompt_format_dict,
|
306 |
+
character_setting_rewrite_dict,
|
307 |
+
Text
|
308 |
+
)
|
309 |
+
info_prompt = single_character_prompt_func(
|
310 |
+
name,
|
311 |
+
{
|
312 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
313 |
+
},
|
314 |
+
character_setting_rewrite_dict,
|
315 |
+
Text
|
316 |
+
)
|
317 |
+
elif key == "{}在进行有关{}的聊天时会说什么?":
|
318 |
+
task_prompt = single_character_prompt_func(
|
319 |
+
name,
|
320 |
+
used_prompt_format_dict,
|
321 |
+
character_setting_rewrite_dict,
|
322 |
+
Text
|
323 |
+
)
|
324 |
+
info_prompt = single_character_prompt_func(
|
325 |
+
name,
|
326 |
+
{
|
327 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
328 |
+
},
|
329 |
+
character_setting_rewrite_dict,
|
330 |
+
Text
|
331 |
+
)
|
332 |
+
elif key == "{}在{}的时候会说什么?":
|
333 |
+
task_prompt = single_character_prompt_func(
|
334 |
+
name,
|
335 |
+
used_prompt_format_dict,
|
336 |
+
character_setting_rewrite_dict,
|
337 |
+
Text
|
338 |
+
)
|
339 |
+
info_prompt = single_character_prompt_func(
|
340 |
+
name,
|
341 |
+
{
|
342 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
343 |
+
},
|
344 |
+
character_setting_rewrite_dict,
|
345 |
+
Text
|
346 |
+
)
|
347 |
+
elif key == "{}在{}时会说什么?":
|
348 |
+
task_prompt = single_character_prompt_func(
|
349 |
+
name,
|
350 |
+
used_prompt_format_dict,
|
351 |
+
character_setting_rewrite_dict,
|
352 |
+
Text
|
353 |
+
)
|
354 |
+
info_prompt = single_character_prompt_func(
|
355 |
+
name,
|
356 |
+
{
|
357 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
358 |
+
},
|
359 |
+
character_setting_rewrite_dict,
|
360 |
+
Text
|
361 |
+
)
|
362 |
+
elif key == "关于{},{}会说什么?":
|
363 |
+
task_prompt = single_character_prompt_func(
|
364 |
+
name,
|
365 |
+
used_prompt_format_dict,
|
366 |
+
character_setting_rewrite_dict,
|
367 |
+
Text
|
368 |
+
)
|
369 |
+
info_prompt = single_character_prompt_func(
|
370 |
+
name,
|
371 |
+
{
|
372 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
373 |
+
},
|
374 |
+
character_setting_rewrite_dict,
|
375 |
+
Text
|
376 |
+
)
|
377 |
+
elif key == "当你想要了解{}时":
|
378 |
+
task_prompt = single_character_prompt_func(
|
379 |
+
name,
|
380 |
+
used_prompt_format_dict,
|
381 |
+
character_setting_rewrite_dict,
|
382 |
+
Text
|
383 |
+
)
|
384 |
+
info_prompt = single_character_prompt_func(
|
385 |
+
name,
|
386 |
+
{
|
387 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
388 |
+
},
|
389 |
+
character_setting_rewrite_dict,
|
390 |
+
Text
|
391 |
+
)
|
392 |
+
return task_prompt, info_prompt
|
393 |
+
|
394 |
+
def main_two_character_prompt_func(
|
395 |
+
name_1,
|
396 |
+
name_2,
|
397 |
+
used_prompt_format_dict,
|
398 |
+
character_setting_rewrite_dict_1 = {},
|
399 |
+
character_setting_rewrite_dict_2 = {},
|
400 |
+
):
|
401 |
+
task_prompt = two_character_prompt_func(
|
402 |
+
name_1,
|
403 |
+
name_2,
|
404 |
+
used_prompt_format_dict,
|
405 |
+
character_setting_rewrite_dict_1,
|
406 |
+
character_setting_rewrite_dict_2)
|
407 |
+
info_prompt_1 = single_character_prompt_func(
|
408 |
+
name_1,
|
409 |
+
{
|
410 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
411 |
+
},
|
412 |
+
character_setting_rewrite_dict_1,
|
413 |
+
)
|
414 |
+
info_prompt_2 = single_character_prompt_func(
|
415 |
+
name_2,
|
416 |
+
{
|
417 |
+
"下面是{}的一些基本信息\n{}\n这些是一段角色介绍\n{}": ["姓名", "Basic_Info", "角色介绍"]
|
418 |
+
},
|
419 |
+
character_setting_rewrite_dict_2,
|
420 |
+
)
|
421 |
+
character_setting_total_dict_1 = get_character_setting_total_dict(name_1)
|
422 |
+
for k, v in character_setting_rewrite_dict_1.items():
|
423 |
+
if k in character_setting_total_dict_1:
|
424 |
+
character_setting_total_dict_1[k] = v
|
425 |
+
character_setting_total_dict_2 = get_character_setting_total_dict(name_2)
|
426 |
+
for k, v in character_setting_rewrite_dict_2.items():
|
427 |
+
if k in character_setting_total_dict_2:
|
428 |
+
character_setting_total_dict_2[k] = v
|
429 |
+
|
430 |
+
country_prompt = ""
|
431 |
+
same_country = character_setting_total_dict_1["国籍"] == character_setting_total_dict_2["国籍"]
|
432 |
+
if same_country:
|
433 |
+
country_prompt = single_character_prompt_func(
|
434 |
+
name_1,
|
435 |
+
{
|
436 |
+
"两人同属{}": ["国籍"]
|
437 |
+
},
|
438 |
+
character_setting_rewrite_dict_1,
|
439 |
+
)
|
440 |
+
else:
|
441 |
+
country_prompt = two_character_prompt_func(
|
442 |
+
name_1,
|
443 |
+
name_2,
|
444 |
+
{
|
445 |
+
"{}来自{},{}来自{}。": ["姓名", "国籍", "姓名", "国籍"]
|
446 |
+
},
|
447 |
+
character_setting_rewrite_dict_1,
|
448 |
+
character_setting_rewrite_dict_2,
|
449 |
+
)
|
450 |
+
info_prompt = "\n".join(
|
451 |
+
[info_prompt_1, info_prompt_2, country_prompt]
|
452 |
+
)
|
453 |
+
return task_prompt, info_prompt
|
454 |
+
|
455 |
+
def main_single_character_prompt_func_cls(
|
456 |
+
name,
|
457 |
+
task,
|
458 |
+
character_setting_rewrite_dict = {},
|
459 |
+
Text = "",
|
460 |
+
):
|
461 |
+
assert task in ["介绍", "故事", "信", "聊天", "时候", "关于", "了解"]
|
462 |
+
if task == "介绍":
|
463 |
+
return main_single_character_prompt_func(
|
464 |
+
name,
|
465 |
+
{
|
466 |
+
"续写下面的角色介绍,下面是角色介绍的开头。{}是{}。{}": ["姓名", "身份", "Text"],
|
467 |
+
},
|
468 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
469 |
+
Text = Text,
|
470 |
+
)
|
471 |
+
elif task == "故事":
|
472 |
+
return main_single_character_prompt_func(
|
473 |
+
name,
|
474 |
+
{
|
475 |
+
"续写下面的角色故事,下面是角色故事的开头。{}是{}。{}": ["姓名", "身份", "Text"],
|
476 |
+
},
|
477 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
478 |
+
Text = Text,
|
479 |
+
)
|
480 |
+
elif task == "神之眼":
|
481 |
+
return main_single_character_prompt_func(
|
482 |
+
name,
|
483 |
+
{
|
484 |
+
"续写下面获得神之眼的过程,下面是开头。{}是{}。{}": ["姓名", "身份", "Text"],
|
485 |
+
},
|
486 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
487 |
+
Text = Text,
|
488 |
+
)
|
489 |
+
elif task == "信":
|
490 |
+
return main_single_character_prompt_func(
|
491 |
+
name,
|
492 |
+
{
|
493 |
+
"{}给你写了一封信,信主题是{},信的内容是这样的。": ["姓名", "Text"],
|
494 |
+
},
|
495 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
496 |
+
Text = Text,
|
497 |
+
)
|
498 |
+
elif task == "聊天":
|
499 |
+
return main_single_character_prompt_func(
|
500 |
+
name,
|
501 |
+
{
|
502 |
+
"{}在进行有关{}的聊天时会说什么?": ["姓名", "Text"],
|
503 |
+
},
|
504 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
505 |
+
Text = Text,
|
506 |
+
)
|
507 |
+
elif task == "时候":
|
508 |
+
return main_single_character_prompt_func(
|
509 |
+
name,
|
510 |
+
{
|
511 |
+
"{}在{}的时候会说什么?": ["姓名", "Text"],
|
512 |
+
},
|
513 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
514 |
+
Text = Text,
|
515 |
+
)
|
516 |
+
elif task == "关于":
|
517 |
+
return main_single_character_prompt_func(
|
518 |
+
name,
|
519 |
+
{
|
520 |
+
"关于{},{}会说什么?": ["Text", "姓名"],
|
521 |
+
},
|
522 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
523 |
+
Text = Text,
|
524 |
+
)
|
525 |
+
elif task == "了解":
|
526 |
+
return main_single_character_prompt_func(
|
527 |
+
name,
|
528 |
+
{
|
529 |
+
"当你想要了解{}时": ["姓名"],
|
530 |
+
},
|
531 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
532 |
+
Text = Text,
|
533 |
+
)
|
534 |
+
return 1 / 0
|
535 |
+
|
536 |
+
def main_two_character_prompt_func_cls(
|
537 |
+
name_1,
|
538 |
+
name_2,
|
539 |
+
task,
|
540 |
+
character_setting_rewrite_dict_1 = {},
|
541 |
+
character_setting_rewrite_dict_2 = {},
|
542 |
+
):
|
543 |
+
assert task in ["会说什么", "哪些信息"]
|
544 |
+
if task == "会说什么":
|
545 |
+
return main_two_character_prompt_func(
|
546 |
+
name_1,
|
547 |
+
name_2,
|
548 |
+
{
|
549 |
+
"关于{},{}会说什么?": ["姓名", "姓名"],
|
550 |
+
},
|
551 |
+
character_setting_rewrite_dict_1,
|
552 |
+
character_setting_rewrite_dict_2
|
553 |
+
)
|
554 |
+
elif task == "哪些信息":
|
555 |
+
return main_two_character_prompt_func(
|
556 |
+
name_1,
|
557 |
+
name_2,
|
558 |
+
{
|
559 |
+
"从{}那里,可以获得哪些关于{}的信息?": ["姓名", "姓名"]
|
560 |
+
},
|
561 |
+
character_setting_rewrite_dict_1,
|
562 |
+
character_setting_rewrite_dict_2
|
563 |
+
)
|
564 |
+
return 1 / 0
|
565 |
+
|
566 |
+
character_setting_total_dist_dict = {
|
567 |
+
'姓名': "",
|
568 |
+
'性别': {'少女女性', '少年男性', '成年女性', '成年男性'},
|
569 |
+
'国籍': {'枫丹', '璃月', '稻妻', '至冬', '蒙德', '须弥'},
|
570 |
+
'身份': "",
|
571 |
+
'性格特征': "",
|
572 |
+
'角色介绍': "",
|
573 |
+
}
|
574 |
+
|
575 |
+
all_single_task = ["介绍", "故事", "信", "聊天", "时候", "关于", "了解"]
|
576 |
+
all_two_task = ["会说什么", "哪些信息"]
|
577 |
+
|
578 |
+
all_genders = ['少女女性', '少年男性', '成年女性', '成年男性']
|
579 |
+
all_countries = ['蒙德','璃月', '稻妻', '须弥','枫丹', '至冬']
|
580 |
+
|
581 |
+
def change_single_name(single_name):
|
582 |
+
if hasattr(single_name, "value"):
|
583 |
+
single_name_ = single_name.value
|
584 |
+
else:
|
585 |
+
single_name_ = single_name
|
586 |
+
character_setting_total_dict = get_character_setting_total_dict(single_name)
|
587 |
+
character_setting_total_dict = dict(map(lambda t2: (t2[0] ,t2[1] if type(t2[1]) == type("") else ""),
|
588 |
+
character_setting_total_dict.items()))
|
589 |
+
return character_setting_total_dict["姓名"], \
|
590 |
+
gr.Dropdown.update(value = character_setting_total_dict["性别"], choices = all_genders), \
|
591 |
+
gr.Dropdown.update(value = character_setting_total_dict["国籍"], choices = all_countries), \
|
592 |
+
character_setting_total_dict["身份"], \
|
593 |
+
character_setting_total_dict["性格特征"], character_setting_total_dict["角色介绍"]
|
594 |
+
|
595 |
+
def get_single_prompt(
|
596 |
+
single_name, select_gender, select_country, single_identity, single_disposition,
|
597 |
+
select_task, Text, single_introduction
|
598 |
+
):
|
599 |
+
if hasattr(single_name, "value"):
|
600 |
+
single_name_ = single_name.value
|
601 |
+
else:
|
602 |
+
single_name_ = single_name
|
603 |
+
if hasattr(select_gender, "value"):
|
604 |
+
select_gender_ = select_gender.value
|
605 |
+
else:
|
606 |
+
select_gender_ = select_gender
|
607 |
+
if hasattr(select_country, "value"):
|
608 |
+
select_country_ = select_country.value
|
609 |
+
else:
|
610 |
+
select_country_ = select_country
|
611 |
+
if hasattr(single_identity, "value"):
|
612 |
+
single_identity_ = single_identity.value
|
613 |
+
else:
|
614 |
+
single_identity_ = single_identity
|
615 |
+
if hasattr(single_disposition, "value"):
|
616 |
+
single_disposition_ = single_disposition.value
|
617 |
+
else:
|
618 |
+
single_disposition_ = single_disposition
|
619 |
+
if hasattr(select_task, "value"):
|
620 |
+
select_task_ = select_task.value
|
621 |
+
else:
|
622 |
+
select_task_ = select_task
|
623 |
+
if hasattr(Text, "value"):
|
624 |
+
Text_ = Text.value
|
625 |
+
else:
|
626 |
+
Text_ = Text
|
627 |
+
if hasattr(single_introduction, "value"):
|
628 |
+
single_introduction_ = single_introduction.value
|
629 |
+
else:
|
630 |
+
single_introduction_ = single_introduction
|
631 |
+
character_setting_rewrite_dict = {
|
632 |
+
'姓名': single_name_,
|
633 |
+
'性别': select_gender_,
|
634 |
+
'国籍': select_country_,
|
635 |
+
'身份': single_identity_,
|
636 |
+
'性格特征': single_disposition_,
|
637 |
+
'角色介绍': single_introduction_,
|
638 |
+
}
|
639 |
+
a, b = main_single_character_prompt_func_cls(
|
640 |
+
single_name_,
|
641 |
+
select_task_,
|
642 |
+
character_setting_rewrite_dict = character_setting_rewrite_dict,
|
643 |
+
Text = Text,
|
644 |
+
)
|
645 |
+
#a = a.replace("?", "").replace("?", "")
|
646 |
+
a = a.replace("?", "?")
|
647 |
+
return "\n".join([b, a])
|
648 |
+
|
649 |
+
def get_two_prompt(
|
650 |
+
single_name_1, select_gender_1, select_country_1, single_identity_1, single_disposition_1,
|
651 |
+
single_introduction_1,
|
652 |
+
single_name_2, select_gender_2, select_country_2, single_identity_2, single_disposition_2,
|
653 |
+
single_introduction_2, two_task,
|
654 |
+
):
|
655 |
+
assert two_task in ["会说什么", "哪些信息"]
|
656 |
+
if hasattr(single_name_1, "value"):
|
657 |
+
single_name_1_ = single_name_1.value
|
658 |
+
else:
|
659 |
+
single_name_1_ = single_name_1
|
660 |
+
if hasattr(select_gender_1, "value"):
|
661 |
+
select_gender_1_ = select_gender_1.value
|
662 |
+
else:
|
663 |
+
select_gender_1_ = select_gender_1
|
664 |
+
if hasattr(select_country_1, "value"):
|
665 |
+
select_country_1_ = select_country_1.value
|
666 |
+
else:
|
667 |
+
select_country_1_ = select_country_1
|
668 |
+
if hasattr(single_identity_1, "value"):
|
669 |
+
single_identity_1_ = single_identity_1.value
|
670 |
+
else:
|
671 |
+
single_identity_1_ = single_identity_1
|
672 |
+
if hasattr(single_disposition_1, "value"):
|
673 |
+
single_disposition_1_ = single_disposition_1.value
|
674 |
+
else:
|
675 |
+
single_disposition_1_ = single_disposition_1
|
676 |
+
if hasattr(single_introduction_1, "value"):
|
677 |
+
single_introduction_1_ = single_introduction_1.value
|
678 |
+
else:
|
679 |
+
single_introduction_1_ = single_introduction_1
|
680 |
+
|
681 |
+
if hasattr(single_name_2, "value"):
|
682 |
+
single_name_2_ = single_name_2.value
|
683 |
+
else:
|
684 |
+
single_name_2_ = single_name_2
|
685 |
+
if hasattr(select_gender_2, "value"):
|
686 |
+
select_gender_2_ = select_gender_2.value
|
687 |
+
else:
|
688 |
+
select_gender_2_ = select_gender_2
|
689 |
+
if hasattr(select_country_2, "value"):
|
690 |
+
select_country_2_ = select_country_2.value
|
691 |
+
else:
|
692 |
+
select_country_2_ = select_country_2
|
693 |
+
if hasattr(single_identity_2, "value"):
|
694 |
+
single_identity_2_ = single_identity_2.value
|
695 |
+
else:
|
696 |
+
single_identity_2_ = single_identity_2
|
697 |
+
if hasattr(single_disposition_2, "value"):
|
698 |
+
single_disposition_2_ = single_disposition_2.value
|
699 |
+
else:
|
700 |
+
single_disposition_2_ = single_disposition_2
|
701 |
+
if hasattr(single_introduction_2, "value"):
|
702 |
+
single_introduction_2_ = single_introduction_2.value
|
703 |
+
else:
|
704 |
+
single_introduction_2_ = single_introduction_2
|
705 |
+
character_setting_rewrite_dict_1 = {
|
706 |
+
'姓名': single_name_1_,
|
707 |
+
'性别': select_gender_1_,
|
708 |
+
'国籍': select_country_1_,
|
709 |
+
'身份': single_identity_1_,
|
710 |
+
'性格特征': single_disposition_1_,
|
711 |
+
'角色介绍': single_introduction_1_,
|
712 |
+
}
|
713 |
+
character_setting_rewrite_dict_2 = {
|
714 |
+
'姓名': single_name_2_,
|
715 |
+
'性别': select_gender_2_,
|
716 |
+
'国籍': select_country_2_,
|
717 |
+
'身份': single_identity_2_,
|
718 |
+
'性格特征': single_disposition_2_,
|
719 |
+
'角色介绍': single_introduction_2_,
|
720 |
+
}
|
721 |
+
|
722 |
+
a, b = main_two_character_prompt_func_cls(
|
723 |
+
single_name_1_,
|
724 |
+
single_name_2_,
|
725 |
+
two_task,
|
726 |
+
character_setting_rewrite_dict_1 = character_setting_rewrite_dict_1,
|
727 |
+
character_setting_rewrite_dict_2 = character_setting_rewrite_dict_2,
|
728 |
+
)
|
729 |
+
#a = a.replace("?", "").replace("?", "")
|
730 |
+
a = a.replace("?", "?")
|
731 |
+
return "\n".join([b, a])
|
732 |
+
|
733 |
+
import chatglm_cpp
|
734 |
+
from pathlib import Path
|
735 |
+
import pandas as pd
|
736 |
+
|
737 |
+
import re
|
738 |
+
def retrieve_sent_split(sent,
|
739 |
+
stops_split_pattern = "|".join(map(lambda x: r"\{}".format(x),
|
740 |
+
",." + ",。" + ":" + "n"))
|
741 |
+
):
|
742 |
+
if not sent.strip():
|
743 |
+
return []
|
744 |
+
|
745 |
+
split_list = re.split(stops_split_pattern, sent)
|
746 |
+
split_list = list(filter(lambda x: x.strip() ,split_list))
|
747 |
+
return split_list
|
748 |
+
|
749 |
+
def stop_criteria(sent, min_sub_len = 4):
|
750 |
+
#### chunk rec stop
|
751 |
+
split_list = retrieve_sent_split(sent)
|
752 |
+
split_list = list(filter(lambda x: len(x) >= min_sub_len,split_list))
|
753 |
+
if split_list:
|
754 |
+
if pd.Series(split_list).value_counts().max() >= 2:
|
755 |
+
print("stop in : {}".format(sent))
|
756 |
+
return "stop"
|
757 |
+
#### row rec stop
|
758 |
+
if list(filter(lambda x: x ,map(lambda x: x.strip(),sent.split("\n")))) and pd.Series(list(filter(lambda x: x ,map(lambda x: x.strip(),sent.split("\n"))))).value_counts().max() >= 2:
|
759 |
+
return "stop"
|
760 |
+
return "continue"
|
761 |
+
|
762 |
+
#model_file_path = "chatglm.cpp/chatglm3-base-inst-6500-sm-ggml.bin"
|
763 |
+
#model_file_path = "chatglm3-base-inst-6500-sm-ggml.bin"
|
764 |
+
model_file_path = "genshin_impact_character_glm6b_base_ggml/chatglm3-base-inst-6500-sm-ggml.bin"
|
765 |
+
print("load {}".format(model_file_path))
|
766 |
+
chatglm_llm = chatglm_cpp.Pipeline(Path(model_file_path))
|
767 |
+
|
768 |
+
def repeat_cmp_process(x, ratio_threshold = 0.3):
|
769 |
+
l = x.split("\n")
|
770 |
+
l = list(filter(lambda y: y.strip(), l))
|
771 |
+
req = []
|
772 |
+
for ele in l:
|
773 |
+
one_ele = repeat_to_one(ele)
|
774 |
+
if ele.strip() and (len(one_ele) / len(ele)) <= ratio_threshold:
|
775 |
+
req.append(one_ele)
|
776 |
+
else:
|
777 |
+
req.append(ele)
|
778 |
+
return "\n".join(req)
|
779 |
+
|
780 |
+
def text_process_before_yield(x, add_repeat_process = True):
|
781 |
+
import re
|
782 |
+
x = x.strip()
|
783 |
+
if len(x.split("\n")) <= 1:
|
784 |
+
#return repeat_to_one_fb(x)
|
785 |
+
if add_repeat_process:
|
786 |
+
return repeat_cmp_process(x)
|
787 |
+
return x
|
788 |
+
zh_list = re.findall(u"[\u4e00-\u9fa5]+" ,x)
|
789 |
+
if zh_list:
|
790 |
+
last_zh = zh_list[-1]
|
791 |
+
l = list(map(lambda y: y.strip() ,x.split("\n")))
|
792 |
+
l_rev = l[::-1]
|
793 |
+
l_rev_collect = []
|
794 |
+
find_it = False
|
795 |
+
for ele in l_rev:
|
796 |
+
if not ele.endswith(last_zh):
|
797 |
+
find_it = True
|
798 |
+
else:
|
799 |
+
pass
|
800 |
+
if find_it:
|
801 |
+
l_rev_collect.append(ele)
|
802 |
+
l_collect = l_rev_collect[::-1]
|
803 |
+
#print(l_collect)
|
804 |
+
req = "\n".join(l_collect)
|
805 |
+
'''
|
806 |
+
zh_list = re.findall(u"[\u4e00-\u9fa5]+" ,x)
|
807 |
+
if zh_list:
|
808 |
+
req = req[req.find(zh_list[0]):]
|
809 |
+
'''
|
810 |
+
#return repeat_to_one_fb(req)
|
811 |
+
if add_repeat_process:
|
812 |
+
return repeat_cmp_process(req)
|
813 |
+
return req
|
814 |
+
return ""
|
815 |
+
|
816 |
+
def chat_messages(message, history = [], chatglm_llm = chatglm_llm,
|
817 |
+
max_length = 128, show_process = True,
|
818 |
+
temperature = 0.5, top_p = 0.95, top_k = 40, do_sample = True,
|
819 |
+
):
|
820 |
+
#print("message :")
|
821 |
+
#print(message)
|
822 |
+
flatten_history = []
|
823 |
+
for a, b in history:
|
824 |
+
flatten_history.append(
|
825 |
+
chatglm_cpp.ChatMessage(role="user", content=a)
|
826 |
+
)
|
827 |
+
flatten_history.append(
|
828 |
+
chatglm_cpp.ChatMessage(role="assistant", content=b)
|
829 |
+
)
|
830 |
+
|
831 |
+
streamer = chatglm_llm.chat(
|
832 |
+
flatten_history + [
|
833 |
+
chatglm_cpp.ChatMessage(role="user", content=message)
|
834 |
+
], do_sample=do_sample,
|
835 |
+
stream = True,
|
836 |
+
max_length = 5120,
|
837 |
+
temperature = temperature,
|
838 |
+
top_p = top_p,
|
839 |
+
top_k = top_k,
|
840 |
+
)
|
841 |
+
|
842 |
+
response = ""
|
843 |
+
for new_text in streamer:
|
844 |
+
response += new_text.content
|
845 |
+
if show_process:
|
846 |
+
#print(response)
|
847 |
+
#from IPython.display import clear_output
|
848 |
+
#clear_output(wait=True)
|
849 |
+
pass
|
850 |
+
if len(response) >= max_length:
|
851 |
+
break
|
852 |
+
if stop_criteria(response) == "stop":
|
853 |
+
break
|
854 |
+
#print(text_process_before_yield(response))
|
855 |
+
yield text_process_before_yield(response)
|
856 |
+
|
857 |
+
def process_text(x):
|
858 |
+
rp_list = ["[/INST]","/INST]","[/INST","/INST","[/INST>","INST","[/CHARACTER]"]
|
859 |
+
for ele in rp_list:
|
860 |
+
x = x.replace(ele, " ")
|
861 |
+
return x
|
862 |
+
|
863 |
+
def run_single(
|
864 |
+
single_name, select_gender, select_country, single_identity, single_disposition,
|
865 |
+
select_task, Text, single_introduction,
|
866 |
+
gen_times, max_length, top_p, temperature):
|
867 |
+
prompt = get_single_prompt(
|
868 |
+
single_name, select_gender, select_country, single_identity, single_disposition,
|
869 |
+
select_task, Text, single_introduction
|
870 |
+
)
|
871 |
+
req = []
|
872 |
+
for i in range(gen_times):
|
873 |
+
for ele in chat_messages(prompt,
|
874 |
+
max_length = max_length,
|
875 |
+
top_p = top_p,
|
876 |
+
temperature = temperature
|
877 |
+
):
|
878 |
+
pass
|
879 |
+
#yield ele
|
880 |
+
req.append(ele)
|
881 |
+
#print(req)
|
882 |
+
req = sorted(set(filter(lambda x: x.strip(), req)), key = lambda y: -1 * len(y))
|
883 |
+
req = "\n\n".join(map(lambda t2: "结果{}:\n{}".format(t2[0], t2[1]), enumerate(req)))
|
884 |
+
req = process_text(req)
|
885 |
+
return req
|
886 |
+
|
887 |
+
def run_two(
|
888 |
+
single_name_1, select_gender_1, select_country_1, single_identity_1, single_disposition_1,
|
889 |
+
single_introduction_1,
|
890 |
+
single_name_2, select_gender_2, select_country_2, single_identity_2, single_disposition_2,
|
891 |
+
single_introduction_2,
|
892 |
+
gen_times, max_length, top_p, temperature):
|
893 |
+
two_prompt = partial(get_two_prompt, two_task = "哪些信息")(
|
894 |
+
single_name_1, select_gender_1, select_country_1, single_identity_1, single_disposition_1,
|
895 |
+
single_introduction_1,
|
896 |
+
single_name_2, select_gender_2, select_country_2, single_identity_2, single_disposition_2,
|
897 |
+
single_introduction_2
|
898 |
+
)
|
899 |
+
req = []
|
900 |
+
for i in range(gen_times):
|
901 |
+
for ele in chat_messages(two_prompt,
|
902 |
+
max_length = max_length,
|
903 |
+
top_p = top_p,
|
904 |
+
temperature = temperature
|
905 |
+
):
|
906 |
+
pass
|
907 |
+
#yield ele
|
908 |
+
req.append(ele)
|
909 |
+
#print(req)
|
910 |
+
req = sorted(set(filter(lambda x: x.strip(), req)), key = lambda y: -1 * len(y))
|
911 |
+
req = "\n\n".join(map(lambda t2: "结果{}:\n{}".format(t2[0], t2[1]), enumerate(req)))
|
912 |
+
req = process_text(req)
|
913 |
+
return req
|
914 |
+
|
915 |
+
all_single_task = ["介绍", "故事", "信", "聊天", "时候", "关于", "了解"]
|
916 |
+
all_two_task = ["会说什么", "哪些信息"]
|
917 |
+
|
918 |
+
with gr.Blocks() as demo:
|
919 |
+
title = gr.HTML(
|
920 |
+
"""<h1 align="center"> <font size="+3"> Genshin Impact Character ChatGLM3 Instruction 📚 </font> </h1>""",
|
921 |
+
elem_id="title",
|
922 |
+
)
|
923 |
+
|
924 |
+
with gr.Tab("单个角色任务指令"):
|
925 |
+
with gr.Row():
|
926 |
+
with gr.Column(0.5):
|
927 |
+
with gr.Row():
|
928 |
+
select_name = gr.Dropdown(label="选择角色",
|
929 |
+
choices= [""] + list(character_setting_total_dict.keys()),
|
930 |
+
info = "可选择。原神沉玉谷前的内建角色",
|
931 |
+
value="")
|
932 |
+
single_name = gr.Text(label = "角色姓名",
|
933 |
+
info = "可编辑。角色姓名会重写选择角色,用这个选项可以新建角色",
|
934 |
+
interactive = True)
|
935 |
+
with gr.Row():
|
936 |
+
select_gender = gr.Dropdown(label="性别",
|
937 |
+
choices=all_genders,
|
938 |
+
info = "可选择",
|
939 |
+
value=all_genders[0], interactive = True)
|
940 |
+
select_country = gr.Dropdown(label="国籍",
|
941 |
+
choices=all_countries,
|
942 |
+
info = "可选择",
|
943 |
+
value=all_countries[0], interactive = True)
|
944 |
+
#with gr.Column():
|
945 |
+
single_identity = gr.Text(label = "身份", info = "可编辑", interactive = True)
|
946 |
+
single_disposition = gr.Text(label = "性格特征", info = "可编辑", interactive = True)
|
947 |
+
select_task = gr.Dropdown(label="选择任务",
|
948 |
+
choices=all_single_task,
|
949 |
+
info = "可选择",
|
950 |
+
value=all_single_task[0], interactive = True)
|
951 |
+
Text = gr.Text(label = "任务追加信息", interactive = True, lines = 4,
|
952 |
+
info = "可编辑。这些信息除了‘了解’任务外不应该为空。对于不同任务填入的值不同。"
|
953 |
+
"(介绍->前缀 故事->前缀 信->主题 聊天->主题 时候->事件 关于->看法 了解->了解角色自身)"
|
954 |
+
)
|
955 |
+
|
956 |
+
with gr.Column(0.5):
|
957 |
+
single_introduction = gr.Text(label = "角色介绍", info = "可编辑",
|
958 |
+
interactive = True, lines = 36)
|
959 |
+
|
960 |
+
with gr.Row():
|
961 |
+
single_prompt_run_button = gr.Button("得到任务结果")
|
962 |
+
output = gr.Text(label = "任务生成结果", info = "可编辑", lines = 2, scale = 5.0)
|
963 |
+
|
964 |
+
with gr.Tab("两个角色看法指令"):
|
965 |
+
with gr.Row():
|
966 |
+
with gr.Column(0.5):
|
967 |
+
with gr.Row():
|
968 |
+
select_name_1 = gr.Dropdown(label="选择角色",
|
969 |
+
choices= [""] + list(character_setting_total_dict.keys()),
|
970 |
+
info = "可选择。原神沉玉谷前的内建角色",
|
971 |
+
value="")
|
972 |
+
single_name_1 = gr.Text(label = "角色姓名",
|
973 |
+
info = "可编辑。角色姓名会重写选择角色,用这个选项可以新建角色",
|
974 |
+
interactive = True)
|
975 |
+
with gr.Row():
|
976 |
+
select_gender_1 = gr.Dropdown(label="性别",
|
977 |
+
choices=all_genders,
|
978 |
+
info = "可选择",
|
979 |
+
value=all_genders[0], interactive = True)
|
980 |
+
select_country_1 = gr.Dropdown(label="国籍",
|
981 |
+
choices=all_countries,
|
982 |
+
info = "可选择",
|
983 |
+
value=all_countries[0], interactive = True)
|
984 |
+
#with gr.Column():
|
985 |
+
single_identity_1 = gr.Text(label = "身份", info = "可编辑", interactive = True)
|
986 |
+
single_disposition_1 = gr.Text(label = "性格特征", info = "可编辑", interactive = True)
|
987 |
+
single_introduction_1 = gr.Text(label = "角色介绍", info = "可编辑",
|
988 |
+
interactive = True, lines = 36)
|
989 |
+
|
990 |
+
with gr.Column(0.5):
|
991 |
+
with gr.Row():
|
992 |
+
select_name_2 = gr.Dropdown(label="选择角色",
|
993 |
+
choices= [""] + list(character_setting_total_dict.keys()),
|
994 |
+
info = "可选择。原神沉玉谷前的内建角色",
|
995 |
+
value="")
|
996 |
+
single_name_2 = gr.Text(label = "角色姓名",
|
997 |
+
info = "可编辑。角色姓名会重写选择角色,用这个选项可以新建角色",
|
998 |
+
interactive = True)
|
999 |
+
with gr.Row():
|
1000 |
+
select_gender_2 = gr.Dropdown(label="性别",
|
1001 |
+
choices=all_genders,
|
1002 |
+
info = "可选择",
|
1003 |
+
value=all_genders[0], interactive = True)
|
1004 |
+
select_country_2 = gr.Dropdown(label="国籍",
|
1005 |
+
choices=all_countries,
|
1006 |
+
info = "可选择",
|
1007 |
+
value=all_countries[0], interactive = True)
|
1008 |
+
#with gr.Column():
|
1009 |
+
single_identity_2 = gr.Text(label = "身份", info = "可编辑", interactive = True)
|
1010 |
+
single_disposition_2 = gr.Text(label = "性格特征", info = "可编辑", interactive = True)
|
1011 |
+
single_introduction_2 = gr.Text(label = "角色介绍", info = "可编辑",
|
1012 |
+
interactive = True, lines = 36)
|
1013 |
+
|
1014 |
+
with gr.Row():
|
1015 |
+
two_prompt_run_button = gr.Button("得到角色间看法")
|
1016 |
+
two_output = gr.Text(label = "角色间看法结果", info = "可编辑", lines = 2, scale = 5.0)
|
1017 |
+
|
1018 |
+
with gr.Row():
|
1019 |
+
gen_times = gr.Slider(1, 10, value=3, step=1.0, label="Generate Num", interactive=True)
|
1020 |
+
max_length = gr.Slider(0, 32768, value=512, step=1.0, label="Maximum length", interactive=True)
|
1021 |
+
top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
|
1022 |
+
temperature = gr.Slider(0.01, 1, value=0.6, step=0.01, label="Temperature", interactive=True)
|
1023 |
+
|
1024 |
+
with gr.Row():
|
1025 |
+
gr.Examples(
|
1026 |
+
[
|
1027 |
+
["行秋", "介绍"],
|
1028 |
+
],
|
1029 |
+
inputs = [select_name, select_task],
|
1030 |
+
label = "单个角色任务指令例子"
|
1031 |
+
)
|
1032 |
+
with gr.Row():
|
1033 |
+
gr.Examples(
|
1034 |
+
[
|
1035 |
+
["行秋", "故事", "一天行秋在绝云间练剑。"],
|
1036 |
+
["柯莱", "信", "鸟语花香"],
|
1037 |
+
["魈", "聊天", "美味的杏仁豆腐"],
|
1038 |
+
#["刻晴", "时候", "谈到璃月民生"],
|
1039 |
+
["凝光", "时候", "品尝璃月香茗"],
|
1040 |
+
["可莉", "关于", "如何制造蹦蹦炸���"],
|
1041 |
+
["北斗", "了解", ""],
|
1042 |
+
],
|
1043 |
+
inputs = [select_name, select_task, Text],
|
1044 |
+
label = "单个角色任务指令例子"
|
1045 |
+
)
|
1046 |
+
with gr.Row():
|
1047 |
+
gr.Examples(
|
1048 |
+
[
|
1049 |
+
["大慈树王", "故事", "大慈树王到须弥沙漠上播种,并跟沙漠的统治者赤王交朋友。",
|
1050 |
+
"成年女性", "须弥", "须弥的统治者",
|
1051 |
+
"爱民如子,带领雨林的人民战胜灾厄",
|
1052 |
+
'''
|
1053 |
+
草神之所以也被称之为“智慧之神”,正是因为她的意识连接着世界之树。在须弥人眼里,她是智慧的化身、仁慈与无所不能的象征,但她却在几百年前的灾难中消失了。
|
1054 |
+
在漫长的历史当中,须弥历经浩劫,种种险情都被大慈树王一一化解。大慈树王创造出雨林,使得须弥人能获得安宁的生活。而须弥最初的教令院,便是由长久地去追随大慈树王的学者组成,他们各司其职,协助大慈树王管理须弥。最能理解大慈树王的力量和哲思的,只能是教令院了。
|
1055 |
+
'''
|
1056 |
+
],
|
1057 |
+
["大慈树王", "关于", "教令院",
|
1058 |
+
"成年女性", "须弥", "须弥的统治者",
|
1059 |
+
"爱民如子,带领雨林的人民战胜灾厄",
|
1060 |
+
'''
|
1061 |
+
草神之所以也被称之为“智慧之神”,正是因为她的意识连接着世界之树。在须弥人眼里,她是智慧的化身、仁慈与无所不能的象征,但她却在几百年前的灾难中消失了。
|
1062 |
+
在漫长的历史当中,须弥历经浩劫,种种险情都被大慈树王一一化解。大慈树王创造出雨林,使得须弥人能获得安宁的生活。而须弥最初的教令院,便是由长久地去追随大慈树王的学者组成,他们各司其职,协助大慈树王管理须弥。最能理解大慈树王的力量和哲思的,只能是教令院了。
|
1063 |
+
'''
|
1064 |
+
],
|
1065 |
+
],
|
1066 |
+
inputs = [single_name, select_task, Text, select_gender, select_country, single_identity,
|
1067 |
+
single_disposition, single_introduction
|
1068 |
+
],
|
1069 |
+
label = "单个角色任务指令例子"
|
1070 |
+
)
|
1071 |
+
|
1072 |
+
with gr.Row():
|
1073 |
+
gr.Examples(
|
1074 |
+
[
|
1075 |
+
["芙宁娜", "那维莱特"],
|
1076 |
+
],
|
1077 |
+
inputs = [select_name_1, select_name_2],
|
1078 |
+
label = "两个角色看法指令例子"
|
1079 |
+
)
|
1080 |
+
|
1081 |
+
with gr.Row():
|
1082 |
+
gr.Examples(
|
1083 |
+
[
|
1084 |
+
["提纳里", "大慈树王",
|
1085 |
+
"成年女性", "须弥", "须弥的统治者",
|
1086 |
+
"爱民如子,带领雨林的人民战胜灾厄",
|
1087 |
+
'''
|
1088 |
+
草神之所以也被称之为“智慧之神”,正是因为她的意识连接着世界之树。在须弥人眼里,她是智慧的化身、仁慈与无所不能的象征,但她却在几百年前的灾难中消失了。
|
1089 |
+
在漫长的历史当中,须弥历经浩劫,种种险情都被大慈树王一一化解。大慈树王创造出雨林,使得须弥人能获得安宁的生活。而须弥最初的教令院,便是由长久地去追随大慈树王的学者组成,他们各司其职,协助大慈树王管理须弥。最能理解大慈树王的力量和哲思的,只能是教令院了。
|
1090 |
+
'''
|
1091 |
+
],
|
1092 |
+
],
|
1093 |
+
inputs = [select_name_1, single_name_2,
|
1094 |
+
select_gender_2, select_country_2, single_identity_2,
|
1095 |
+
single_disposition_2, single_introduction_2
|
1096 |
+
],
|
1097 |
+
label = "两个角色看法指令例子"
|
1098 |
+
)
|
1099 |
+
|
1100 |
+
select_name.change(
|
1101 |
+
lambda x: x, select_name, single_name
|
1102 |
+
)
|
1103 |
+
|
1104 |
+
select_name_1.change(
|
1105 |
+
lambda x: x, select_name_1, single_name_1
|
1106 |
+
)
|
1107 |
+
|
1108 |
+
select_name_2.change(
|
1109 |
+
lambda x: x, select_name_2, single_name_2
|
1110 |
+
)
|
1111 |
+
|
1112 |
+
select_name.change(
|
1113 |
+
change_single_name, select_name,
|
1114 |
+
[single_name, select_gender, select_country,
|
1115 |
+
single_identity, single_disposition, single_introduction
|
1116 |
+
]
|
1117 |
+
)
|
1118 |
+
|
1119 |
+
select_name_1.change(
|
1120 |
+
change_single_name, select_name_1,
|
1121 |
+
[single_name_1, select_gender_1, select_country_1,
|
1122 |
+
single_identity_1, single_disposition_1, single_introduction_1
|
1123 |
+
]
|
1124 |
+
)
|
1125 |
+
|
1126 |
+
select_name_2.change(
|
1127 |
+
change_single_name, select_name_2,
|
1128 |
+
[single_name_2, select_gender_2, select_country_2,
|
1129 |
+
single_identity_2, single_disposition_2, single_introduction_2
|
1130 |
+
]
|
1131 |
+
)
|
1132 |
+
|
1133 |
+
single_prompt_run_button.click(run_single, [
|
1134 |
+
single_name, select_gender, select_country, single_identity, single_disposition,
|
1135 |
+
select_task, Text, single_introduction,
|
1136 |
+
gen_times, max_length, top_p, temperature
|
1137 |
+
], output)
|
1138 |
+
two_prompt_run_button.click(run_two, [
|
1139 |
+
single_name_1, select_gender_1, select_country_1, single_identity_1, single_disposition_1,
|
1140 |
+
single_introduction_1,
|
1141 |
+
single_name_2, select_gender_2, select_country_2, single_identity_2, single_disposition_2,
|
1142 |
+
single_introduction_2,
|
1143 |
+
gen_times, max_length, top_p, temperature], two_output)
|
1144 |
+
|
1145 |
+
#demo.launch("0.0.0.0", show_api=False, share = True)
|
1146 |
+
demo.queue(max_size=4, concurrency_count=1).launch(debug=True, show_api=False, share = True)
|