3v324v23 commited on
Commit
8aa09c6
·
1 Parent(s): 7e6b19f

improved UI

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -33,9 +33,14 @@ huggingface_embeddings= HuggingFaceEmbeddings(
33
  # %%
34
  persist_directory = 'chroma/'
35
  vectordb = Chroma(embedding_function=huggingface_embeddings,persist_directory=persist_directory)
36
- print(vectordb._collection.count())
37
 
38
  # %%
 
 
 
 
 
39
  # helper functions for prompt processing for this LLM
40
 
41
  def preprocess(text):
@@ -70,9 +75,12 @@ def helper_rag(text):
70
  return context
71
 
72
  # helper function for prompt
73
- def helper_text(text_input,radio=None):
74
  chinese_type = "simplified"
75
 
 
 
 
76
  if hanzidentifier.is_traditional(text_input):
77
  text_input = chinese_converter.to_simplified(text_input)
78
  chinese_type = "traditional"
@@ -81,7 +89,7 @@ def helper_text(text_input,radio=None):
81
 
82
  if not any(c in text_input for c in ["猜", "打"]):
83
  warning = "请给一个提示,提示格式,例子:猜一水果,打一字。"
84
- if chinese_type == "traditional" or radio == "繁體中文":
85
  warning = chinese_converter.to_traditional(warning)
86
  return warning
87
 
@@ -101,8 +109,9 @@ def helper_text(text_input,radio=None):
101
 
102
 
103
  # Gradio function for configure the language of UI
104
- def change_language(radio,text_input,text_output,clear_btn,submit_btn,markdown,
105
- markdown_msg1, markdown_msg2):
 
106
  if radio == "简体中文":
107
  index = 0
108
  text_input_update=gr.Textbox.update(value = chinese_converter.to_simplified(text_input), label = text_input_label[index])
@@ -136,8 +145,10 @@ def change_language(radio,text_input,text_output,clear_btn,submit_btn,markdown,
136
  clear_btn_update = gr.ClearButton.update(value = clear_label[index])
137
  submit_btn_update = gr.Button.update(value = submit_label[index])
138
 
 
 
139
  return [text_input_update,text_output_update,clear_btn_update,submit_btn_update,markdown_update,
140
- markdown_msg1_update ,markdown_msg2_update]
141
 
142
 
143
  def clear_text():
@@ -148,11 +159,6 @@ def clear_text():
148
 
149
 
150
  # %%
151
- text_input_label=["谜面","謎面","Riddle"]
152
- text_output_label=["谜底","謎底","Answer"]
153
-
154
- clear_label = ["清除","清除","Clear"]
155
- submit_label = ["提交","提交","Submit"]
156
 
157
  # css = """
158
  # #markdown { background-image: url("file/data/DSC_0105.jpg");
@@ -162,6 +168,7 @@ submit_label = ["提交","提交","Submit"]
162
 
163
  with gr.Blocks() as demo:
164
  index = 0
 
165
  example_list = [
166
  ["小家伙穿黄袍,花丛中把房造。飞到西来飞到东,人人夸他爱劳动。(猜一动物)"],
167
  ["一物生来身穿三百多件衣,每天脱一件,年底剩张皮。(猜一物品)"],
@@ -176,6 +183,7 @@ with gr.Blocks() as demo:
176
  # Chinese Lantern Riddles Solver with LLM
177
  ## 用语言大模型来猜灯谜
178
  """,elem_id="markdown")
 
179
  with gr.Row():
180
  with gr.Column():
181
  text_input = gr.Textbox(label=text_input_label[index],
@@ -196,11 +204,11 @@ with gr.Blocks() as demo:
196
  )
197
  markdown_msg1 = gr.Markdown(
198
  """
199
- 灯谜是中华文化特色文娱活动,自北宋盛行。每年逢正月十五元宵节,或是八月十五中秋节,将谜语贴在花灯上,让大家可一起猜谜。
200
 
201
  Lantern riddle is a traditional Chinese cultural activity. Being popular since the Song Dynasty (960-1276), it \
202
- is held in the Lantern Festival (15th day of the first lunar month) or the Mid-Autumn Festival (15th day of \
203
- the eighth lunar month). When people are viewing the flower lanterns, they can guess the riddles on the lanterns together.
204
 
205
 
206
  """
@@ -247,18 +255,19 @@ with gr.Blocks() as demo:
247
 
248
 
249
 
250
- submit_btn.click(fn=helper_text, inputs=[text_input,radio], outputs=text_output)
251
 
252
  clear_btn.click(fn=clear_text,outputs=[text_input,text_output])
253
- radio.change(fn=change_language,inputs=[radio,text_input,text_output,clear_btn,submit_btn,
254
  markdown, markdown_msg1,markdown_msg2],
255
  outputs=[text_input,text_output,clear_btn,submit_btn,
256
- markdown, markdown_msg1,markdown_msg2])
257
 
258
  #demo = gr.Interface(fn=helper_text, inputs=text_input, outputs=text_output,
259
  # flagging_options=["Inappropriate"],allow_flagging="never",
260
  # title="aaa",description="aaa",article="aaa")
261
- demo.launch()
 
262
 
263
  # %%
264
 
 
33
  # %%
34
  persist_directory = 'chroma/'
35
  vectordb = Chroma(embedding_function=huggingface_embeddings,persist_directory=persist_directory)
36
+ print(f"Vector count: {vectordb._collection.count()}")
37
 
38
  # %%
39
+ text_input_label=["谜面","謎面","Riddle"]
40
+ text_output_label=["谜底","謎底","Answer"]
41
+
42
+ clear_label = ["清除","清除","Clear"]
43
+ submit_label = ["提交","提交","Submit"]
44
  # helper functions for prompt processing for this LLM
45
 
46
  def preprocess(text):
 
75
  return context
76
 
77
  # helper function for prompt
78
+ def helper_text(text_input,lang=None):
79
  chinese_type = "simplified"
80
 
81
+ if lang == "繁體中文" or lang == "traditional":
82
+ chinese_type = "traditional"
83
+
84
  if hanzidentifier.is_traditional(text_input):
85
  text_input = chinese_converter.to_simplified(text_input)
86
  chinese_type = "traditional"
 
89
 
90
  if not any(c in text_input for c in ["猜", "打"]):
91
  warning = "请给一个提示,提示格式,例子:猜一水果,打一字。"
92
+ if chinese_type == "traditional":
93
  warning = chinese_converter.to_traditional(warning)
94
  return warning
95
 
 
109
 
110
 
111
  # Gradio function for configure the language of UI
112
+ def change_language(radio,text_input,text_output,markdown,
113
+ markdown_msg1, markdown_msg2,language):
114
+
115
  if radio == "简体中文":
116
  index = 0
117
  text_input_update=gr.Textbox.update(value = chinese_converter.to_simplified(text_input), label = text_input_label[index])
 
145
  clear_btn_update = gr.ClearButton.update(value = clear_label[index])
146
  submit_btn_update = gr.Button.update(value = submit_label[index])
147
 
148
+ language = radio
149
+
150
  return [text_input_update,text_output_update,clear_btn_update,submit_btn_update,markdown_update,
151
+ markdown_msg1_update, markdown_msg2_update,language]
152
 
153
 
154
  def clear_text():
 
159
 
160
 
161
  # %%
 
 
 
 
 
162
 
163
  # css = """
164
  # #markdown { background-image: url("file/data/DSC_0105.jpg");
 
168
 
169
  with gr.Blocks() as demo:
170
  index = 0
171
+ language = gr.State()
172
  example_list = [
173
  ["小家伙穿黄袍,花丛中把房造。飞到西来飞到东,人人夸他爱劳动。(猜一动物)"],
174
  ["一物生来身穿三百多件衣,每天脱一件,年底剩张皮。(猜一物品)"],
 
183
  # Chinese Lantern Riddles Solver with LLM
184
  ## 用语言大模型来猜灯谜
185
  """,elem_id="markdown")
186
+
187
  with gr.Row():
188
  with gr.Column():
189
  text_input = gr.Textbox(label=text_input_label[index],
 
204
  )
205
  markdown_msg1 = gr.Markdown(
206
  """
207
+ 灯谜是中华文化特色文娱活动,自北宋盛行。每年逢正月十五元宵节,将谜语贴在花灯上,让大家可一起猜谜。
208
 
209
  Lantern riddle is a traditional Chinese cultural activity. Being popular since the Song Dynasty (960-1276), it \
210
+ is held in the Lantern Festival (15th day of the first lunar month). \
211
+ When people are viewing the flower lanterns, they can guess the riddles on the lanterns together.
212
 
213
 
214
  """
 
255
 
256
 
257
 
258
+ submit_btn.click(fn=helper_text, inputs=[text_input,radio], outputs=text_output, api_name="answer-the-riddle")
259
 
260
  clear_btn.click(fn=clear_text,outputs=[text_input,text_output])
261
+ radio.change(fn=change_language,inputs=[radio,text_input,text_output,
262
  markdown, markdown_msg1,markdown_msg2],
263
  outputs=[text_input,text_output,clear_btn,submit_btn,
264
+ markdown, markdown_msg1,markdown_msg2,language])
265
 
266
  #demo = gr.Interface(fn=helper_text, inputs=text_input, outputs=text_output,
267
  # flagging_options=["Inappropriate"],allow_flagging="never",
268
  # title="aaa",description="aaa",article="aaa")
269
+ demo.queue(api_open=False)
270
+ demo.launch(show_api=False)
271
 
272
  # %%
273