svjack commited on
Commit
ee861fb
·
verified ·
1 Parent(s): 9b48eb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -6
app.py CHANGED
@@ -98,6 +98,37 @@ def repeat_to_one_fb(x):
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"):
@@ -735,7 +766,10 @@ def get_single_prompt(
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,
@@ -819,7 +853,10 @@ def get_two_prompt(
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
@@ -983,6 +1020,7 @@ def run_single(
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(
@@ -991,6 +1029,14 @@ def run_two(
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,
@@ -1006,11 +1052,24 @@ def run_two(
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 = ["介绍", "故事", "信", "聊天", "时候", "关于", "了解"]
@@ -1131,7 +1190,7 @@ with gr.Blocks() as demo:
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)
@@ -1141,7 +1200,7 @@ with gr.Blocks() as demo:
1141
  """<h2 align="center"> <font size="+0"> 例子(这里面涉及推荐选择的需要从左面或上面的角色图片集里面点按选取) </font> </h2>""",
1142
  elem_id="title",
1143
  )
1144
-
1145
  with gr.Row():
1146
  gr.Examples(
1147
  [
@@ -1154,11 +1213,11 @@ with gr.Blocks() as demo:
1154
  with gr.Row():
1155
  gr.Examples(
1156
  [
1157
- ["这里推荐从左面选择:行秋" ,"故事", "一天行秋在绝云间练剑。"],
1158
  ["这里推荐从左面选择:柯莱" ,"信", "鸟语花香"],
1159
  ["这里推荐从左面选择:魈" ,"聊天", "美味��杏仁豆腐"],
1160
  ["这里推荐从左面选择:凝光" ,"时候", "品尝璃月香茗"],
1161
  ["这里推荐从左面选择:可莉" ,"关于", "如何制造蹦蹦炸弹"],
 
1162
  ["这里推荐从左面选择:北斗" ,"了解", ""],
1163
  ],
1164
  inputs = [single_name ,select_task, Text],
 
98
 
99
  repeat_to_one = repeat_to_one_fb
100
 
101
+ def process_row(x, names):
102
+ assert type(names) == type([])
103
+ x = x.strip()
104
+ if not x:
105
+ return x
106
+ first_char = x[0]
107
+ if first_char in map(lambda y: y[0], names):
108
+ return x
109
+ if first_char in [":", ":"]:
110
+ return x[1:]
111
+ if first_char in "".join(names):
112
+ req = {}
113
+ for name in names:
114
+ merged = name
115
+ find_it = False
116
+ for ele in jieba.lcut(x):
117
+ if merged.endswith(ele) and not find_it:
118
+ continue
119
+ else:
120
+ find_it = True
121
+ merged += ele
122
+ if find_it:
123
+ req[name] = merged
124
+ #print(req)
125
+ return sorted(req.items(), key = lambda t2: len(t2[1][len(t2[0]):]))[0][1]
126
+ return x
127
+
128
+ def process_info(x, maintain_chars = ",.。,;::;??\n——"):
129
+ req = re.findall(u"[0-9\u4e00-\u9fa5{}]+".format(maintain_chars) ,x)
130
+ return "".join(req)
131
+
132
  from huggingface_hub import snapshot_download
133
 
134
  if not os.path.exists("genshin-impact-character"):
 
766
  )
767
  #a = a.replace("?", "").replace("?", "")
768
  a = a.replace("?", "?")
769
+ req = "\n".join([b, a])
770
+ req = process_info(req)
771
+ return req
772
+ #return "\n".join([b, a])
773
 
774
  def get_two_prompt(
775
  single_name_1, select_gender_1, select_country_1, single_identity_1, single_disposition_1,
 
853
  )
854
  #a = a.replace("?", "").replace("?", "")
855
  a = a.replace("?", "?")
856
+ req = "\n".join([b, a])
857
+ req = process_info(req)
858
+ return req
859
+ #return "\n".join([b, a])
860
 
861
  import chatglm_cpp
862
  from pathlib import Path
 
1020
 
1021
  req = "\n\n".join(map(lambda t2: "结果{}:\n{}".format(t2[0], t2[1]), enumerate(req)))
1022
  req = process_text(req)
1023
+ req = process_info(req)
1024
  return req
1025
 
1026
  def run_two(
 
1029
  single_name_2, select_gender_2, select_country_2, single_identity_2, single_disposition_2,
1030
  single_introduction_2,
1031
  gen_times, max_length, top_p, temperature):
1032
+ if hasattr(single_name_1, "value"):
1033
+ single_name_1_ = single_name_1.value
1034
+ else:
1035
+ single_name_1_ = single_name_1
1036
+ if hasattr(single_name_2, "value"):
1037
+ single_name_2_ = single_name_2.value
1038
+ else:
1039
+ single_name_2_ = single_name_2
1040
  two_prompt = partial(get_two_prompt, two_task = "哪些信息")(
1041
  single_name_1, select_gender_1, select_country_1, single_identity_1, single_disposition_1,
1042
  single_introduction_1,
 
1052
  ):
1053
  pass
1054
  #yield ele
1055
+ ele = process_row(ele, [single_name_1_, single_name_2_]).strip()
1056
  req.append(ele)
1057
  #print(req)
1058
  req = sorted(set(filter(lambda x: x.strip(), req)), key = lambda y: -1 * len(y))
1059
+ req = "\n\n".join(map(lambda t2: "结果{}:\n{}".format(t2[0], t2[1].strip()), enumerate(req)))
1060
  req = process_text(req)
1061
+ req = process_info(req)
1062
+ l = req.split("\n\n")
1063
+ def process_head(y):
1064
+ ll = y.split("\n")
1065
+ if len(ll) >= 2:
1066
+ if len(ll[1].strip()) == 1:
1067
+ return "\n".join([ll[0]] + ll[2:]).strip()
1068
+ else:
1069
+ return y.strip()
1070
+ else:
1071
+ return y.strip()
1072
+ req = "\n\n".join(map(process_head, l))
1073
  return req
1074
 
1075
  all_single_task = ["介绍", "故事", "信", "聊天", "时候", "关于", "了解"]
 
1190
  two_output = gr.Text(label = "角色间看法结果", info = "可编辑", lines = 2, scale = 5.0)
1191
 
1192
  with gr.Row():
1193
+ gen_times = gr.Slider(1, 10, value=10, step=1.0, label="Generate Num", interactive=True)
1194
  max_length = gr.Slider(0, 32768, value=512, step=1.0, label="Maximum length", interactive=True)
1195
  top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
1196
  temperature = gr.Slider(0.01, 1, value=0.6, step=0.01, label="Temperature", interactive=True)
 
1200
  """<h2 align="center"> <font size="+0"> 例子(这里面涉及推荐选择的需要从左面或上面的角色图片集里面点按选取) </font> </h2>""",
1201
  elem_id="title",
1202
  )
1203
+
1204
  with gr.Row():
1205
  gr.Examples(
1206
  [
 
1213
  with gr.Row():
1214
  gr.Examples(
1215
  [
 
1216
  ["这里推荐从左面选择:柯莱" ,"信", "鸟语花香"],
1217
  ["这里推荐从左面选择:魈" ,"聊天", "美味��杏仁豆腐"],
1218
  ["这里推荐从左面选择:凝光" ,"时候", "品尝璃月香茗"],
1219
  ["这里推荐从左面选择:可莉" ,"关于", "如何制造蹦蹦炸弹"],
1220
+ ["这里推荐从左面选择:行秋" ,"故事", "一天行秋在绝云间练剑。"],
1221
  ["这里推荐从左面选择:北斗" ,"了解", ""],
1222
  ],
1223
  inputs = [single_name ,select_task, Text],