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