svjack commited on
Commit
378c99a
1 Parent(s): 2b8be42

Create app.py

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