svjack commited on
Commit
f581b20
1 Parent(s): 5be7513

Create app.py

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