mkw18 commited on
Commit
9d6aea0
·
1 Parent(s): 495ab3c
Files changed (1) hide show
  1. app.py +83 -88
app.py CHANGED
@@ -64,14 +64,14 @@ def showInput(input, chatbot):
64
  return chatbot
65
 
66
 
67
- def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history):
68
  start = time.time()
69
  chatbot.append((parse_text(input), ""))
70
  messages1 = messages[:10].copy()
71
  if len(known) > 0:
72
- messages1 += [{"role": 'user', "content": f"{' '.join(known)}\n请回答是或否或无关。"}, {"role": "assistant", "content": '是。'}, {"role": 'user', "content": f"{input}\n请回答是或否或无关。"}]
73
  else:
74
- messages1 += [{"role": 'user', "content": f"{input}\n请回答是或否或无关。"}]
75
  print(f"Init: {time.time() - start}")
76
  messages.append({"role": 'user', "content": input})
77
  llm = True
@@ -82,11 +82,11 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
82
  key = key.strip()
83
  if key == '':
84
  continue
85
- if ' ' in key:
86
- key = key.split(' ')[1]
87
- bleu = sentence_bleu([key], input.replace('?', '。'), weights=(1, 0, 0, 0))
88
- if bleu > 0.85:
89
- response = '这是汤面中已有的信息,请提一个新问题。'
90
  llm = False
91
  break
92
  print(f"Filter story: {time.time() - start}")
@@ -95,16 +95,14 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
95
  key = key.strip()
96
  if key == '':
97
  continue
98
- if ' ' in key:
99
- key = key.split(' ')[1]
100
- bleu = sentence_bleu([key], input.replace('', '。'), weights=(1, 0, 0, 0))
101
- if bleu > 0.85:
102
- response = '这是已经提问过的内容,请提一个新问题。'
103
  llm = False
104
  break
105
  print(f"Filter history: {time.time() - start}")
106
  if llm:
107
- history.append(input.replace('?', '。'))
108
  data = {'predict': messages1, 'idx': idx, 'isfinished': False, 'answer': answer}
109
  print(f"Start Request 1: {time.time() - start}")
110
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
@@ -115,12 +113,13 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
115
  completion = openai.ChatCompletion.create(
116
  model="gpt-3.5-turbo",
117
  messages=messages1,
 
118
  )
119
  response=completion.choices[0].message.content.strip()
120
  print(f"Request openai 1: {time.time() - start}")
121
  relevant = False
122
- if response.startswith("是"):
123
- decl_msg = [{"role": "user", "content": f"请将以下内容转述为陈述句,并简化为一句话:\n{input}"}]
124
  data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
125
  print(f"Start Request 2: {time.time() - start}")
126
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
@@ -131,12 +130,13 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
131
  summary = openai.ChatCompletion.create(
132
  model="gpt-3.5-turbo",
133
  messages=decl_msg,
 
134
  )
135
  summary = summary.choices[0].message.content.strip()
136
  print(f"Request openai 2: {time.time() - start}")
137
  relevant = True
138
- elif response.startswith("不是") or response.startswith("否"):
139
- decl_msg = [{"role": "user", "content": f"请将以下内容取反义然后转述为陈述句,并简化为一句话:\n{input}"}]
140
  data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
141
  print(f"Start Request 2: {time.time() - start}")
142
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
@@ -146,7 +146,8 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
146
  else:
147
  summary = openai.ChatCompletion.create(
148
  model="gpt-3.5-turbo",
149
- messages=decl_msg
 
150
  )
151
  summary = summary.choices[0].message.content.strip()
152
  print(f"Request openai 2: {time.time() - start}")
@@ -156,7 +157,7 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
156
  known.append(summary)
157
  reasoning.append(summary)
158
  if len(reasoning) >= 2:
159
- simp_msg = [{"role": "user", "content": f"请将以下内容简化为一句话:\n{' '.join(reasoning)}"}]
160
  data = {'predict': simp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
161
  print(f"Start Request 3: {time.time() - start}")
162
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
@@ -167,6 +168,7 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
167
  merge = openai.ChatCompletion.create(
168
  model="gpt-3.5-turbo",
169
  messages=simp_msg,
 
170
  )
171
  merge = merge.choices[0].message.content.strip()
172
  print(f"Request openai 3: {time.time() - start}")
@@ -176,13 +178,13 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
176
  key = key.strip()
177
  if key == '':
178
  continue
179
- if ' ' in key:
180
- key1 = key.split(' ')[1]
181
  else:
182
  key1 = key
183
- if len(input.replace('?', '')) < len(key1):
184
  continue
185
- comp_msg = [{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
186
  data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
187
  print(f"Start Request 4: {time.time() - start}")
188
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
@@ -192,54 +194,21 @@ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known,
192
  else:
193
  compare = openai.ChatCompletion.create(
194
  model="gpt-3.5-turbo",
195
- messages=comp_msg
 
196
  )
197
  compare = compare.choices[0].message.content.strip()
198
  print(f"Request openai 4: {time.time() - start}")
199
- if compare.startswith('是'):
200
- vote = 1
201
- comp_msg += [{"role": "assistant", "content": "是"},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
202
- data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
203
- print(f"Start Request 5: {time.time() - start}")
204
- completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
205
- print(f"Request 5: {time.time() - start}")
206
- if completion.status_code == 200:
207
- compare = str(completion.content, encoding="utf-8")
208
- else:
209
- compare = openai.ChatCompletion.create(
210
- model="gpt-3.5-turbo",
211
- messages=comp_msg,
212
- )
213
- compare = compare.choices[0].message.content.strip()
214
- print(f"Request openai 5: {time.time() - start}")
215
- if compare.startswith('是'):
216
- vote += 1
217
- comp_msg += [{"role": "assistant", "content": compare},{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}"}]
218
- data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
219
- print(f"Start Request 6: {time.time() - start}")
220
- completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
221
- print(f"Request 6: {time.time() - start}")
222
- if completion.status_code == 200:
223
- compare = str(completion.content, encoding="utf-8")
224
- else:
225
- compare = openai.ChatCompletion.create(
226
- model="gpt-3.5-turbo",
227
- messages=comp_msg,
228
- )
229
- compare = compare.choices[0].message.content.strip()
230
- print(f"Request openai 6: {time.time() - start}")
231
- if compare.startswith('是'):
232
- vote += 1
233
- if vote >= 2:
234
- bingo += 1
235
- answer_key.remove(key)
236
- print(key)
237
- reasoning = []
238
- break
239
- print(f"Finish compare: {time.time() - start}")
240
  if bingo >= len(answer_key):
241
  finished = True
242
- response += f'恭喜你猜到了汤底,汤底是:{answer}\n点击"再来一局"按钮开始下一局游戏。'
243
  print(f"Finish bingo: {time.time() - start}")
244
  messages.append({"role": "assistant", "content": response})
245
  data = {'predict': messages, 'idx': idx, 'isfinished': finished, 'answer': answer}
@@ -254,7 +223,7 @@ def reset_user_input():
254
  return gr.update(value='')
255
 
256
 
257
- def reset_state(request: gr.Request):
258
  global host_cnt
259
  host = request.client.host
260
  if not host in host_cnt:
@@ -264,7 +233,7 @@ def reset_state(request: gr.Request):
264
  if elapse < 10:
265
  time.sleep(10-elapse)
266
  host_cnt[host] = time.time()
267
- data = {'refresh': ''}
268
  data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
269
  data = json.loads(str(data, encoding="utf-8"))
270
  chatbot = data['chatbot']
@@ -273,30 +242,58 @@ def reset_state(request: gr.Request):
273
  story_key = data['story_key']
274
  answer_key = data['answer_key']
275
  idx = data['idx']
276
- return chatbot, messages, gr.update(value=""), gr.update(value="显示答案"), answer, idx, gr.update(value=data['story'].strip()), False, story_key, answer_key, [], 0, [], []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
 
278
 
279
  def show_hide_answer(answer, show_ans):
280
  if show_ans:
281
  show_ans = False
282
- return gr.update(value=""), gr.update(value="显示答案"), show_ans
283
  else:
284
  show_ans = True
285
- return gr.update(value=answer), gr.update(value="隐藏答案"), show_ans
286
 
287
 
288
  host_cnt = {}
289
 
290
  with gr.Blocks() as demo:
291
- gr.HTML("""<h1 align="center">海龟汤</h1>""")
 
 
 
 
292
 
293
  with gr.Row():
294
- rule = gr.Textbox(label='规则', value='海龟汤是一个推理类游戏,游戏开始时会给出一段隐去关键信息的叙述,即汤面,玩家根据汤面推理,提出能够通过“是”或“否”来回答的问题,通过提问不同可能性,缩小真相的范围,直到最终猜到真相(即汤底)的关键信息。玩家可以点击“再来一局”按钮随机一场新的游戏,点击“显示答案”可查看汤底。', lines=1, max_lines=3).style(container=False)
295
- chatbot = gr.Chatbot([(None, '点击“再来一局”开始游戏')])
296
  messages = gr.State([])
297
- answer = gr.State('点击“再来一局”开始游戏')
298
  idx = gr.State(0)
299
  show_ans = gr.State(False)
 
300
  known = gr.State([])
301
  story_key = gr.State([])
302
  answer_key = gr.State([])
@@ -306,32 +303,30 @@ with gr.Blocks() as demo:
306
 
307
  with gr.Row():
308
  with gr.Column(scale=4):
309
- question = gr.Textbox(label='汤面', value='点击“再来一局”开始游戏',
310
  lines=1, max_lines=3).style(container=False)
311
  with gr.Row():
312
- user_input = gr.Textbox(show_label=False, placeholder="请输入你的猜测...", lines=1, max_lines=3).style(
313
  container=False)
314
  with gr.Row():
315
  with gr.Column(scale=2):
316
- submitBtn = gr.Button("发送", variant="primary")
317
  with gr.Column(scale=2):
318
- emptyBtn = gr.Button("再来一局")
319
  with gr.Column(scale=1):
320
  answer_output = gr.Textbox(show_label=False, lines=6, max_lines=6).style(
321
  container=False)
322
- answerBtn = gr.Button("显示答案")
323
 
324
 
325
- # submitBtn.click(showInput, [user_input, chatbot], [chatbot])
326
- user_input.submit(predict, [user_input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history], [chatbot, messages, known, bingo, reasoning, history],
327
- show_progress=True)
328
  user_input.submit(reset_user_input, [], [user_input])
329
- submitBtn.click(predict, [user_input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history], [chatbot, messages, known, bingo, reasoning, history],
330
- show_progress=True)
331
  submitBtn.click(reset_user_input, [], [user_input])
332
 
333
- emptyBtn.click(reset_state, outputs=[chatbot, messages, answer_output, answerBtn, answer, idx, question, show_ans, story_key, answer_key, known, bingo, reasoning, history], show_progress=True)
334
 
335
- answerBtn.click(show_hide_answer, [answer, show_ans], outputs=[answer_output, answerBtn, show_ans])
336
 
337
  demo.queue().launch()
 
64
  return chatbot
65
 
66
 
67
+ def predict(input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history, zh):
68
  start = time.time()
69
  chatbot.append((parse_text(input), ""))
70
  messages1 = messages[:10].copy()
71
  if len(known) > 0:
72
+ messages1 += [{"role": 'user', "content": f"{' '.join(known)}\n请回答是或否或无关。"}, {"role": "assistant", "content": '是。'}, {"role": 'user', "content": f"{input}\n请回答是或否或无关。"}] if zh else [{"role": 'user', "content": f"{' '.join(known)}\nPlease answer with \"yes\", \"no\", or \"irrelevant\"."}, {"role": "assistant", "content": 'Yes.'}, {"role": 'user', "content": f"{input}\nPlease answer with \"yes\", \"no\", or \"irrelevant\"."}]
73
  else:
74
+ messages1 += [{"role": 'user', "content": f"{input}\n请回答是或否或无关。"}] if zh else [{"role": 'user', "content": f"{input}\nPlease answer with \"yes\", \"no\", or \"irrelevant\"."}]
75
  print(f"Init: {time.time() - start}")
76
  messages.append({"role": 'user', "content": input})
77
  llm = True
 
82
  key = key.strip()
83
  if key == '':
84
  continue
85
+ if key[1] == '.' or key[2] == '.' or key[0] == '-':
86
+ key = ' '.join(key.split(' ')[1:])
87
+ bleu = sentence_bleu([key], input.replace('?', '。').replace('?', '.'), weights=(1, 0, 0, 0))
88
+ if bleu >= 0.85:
89
+ response = '这是汤面中已有的信息,请提一个新问题。' if zh else 'This Information is already in the story, please ask a new question.'
90
  llm = False
91
  break
92
  print(f"Filter story: {time.time() - start}")
 
95
  key = key.strip()
96
  if key == '':
97
  continue
98
+ bleu = sentence_bleu([key], input.replace('?', '。').replace('?', '.'), weights=(1, 0, 0, 0))
99
+ if bleu >= 0.95:
100
+ response = '这是已经提问过的内容,请提一个新问题。' if zh else 'This is a question that has already been asked. Please ask a new question.'
 
 
101
  llm = False
102
  break
103
  print(f"Filter history: {time.time() - start}")
104
  if llm:
105
+ history.append(input.replace('?', '。').replace('?', '.'))
106
  data = {'predict': messages1, 'idx': idx, 'isfinished': False, 'answer': answer}
107
  print(f"Start Request 1: {time.time() - start}")
108
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
 
113
  completion = openai.ChatCompletion.create(
114
  model="gpt-3.5-turbo",
115
  messages=messages1,
116
+ temperature=0
117
  )
118
  response=completion.choices[0].message.content.strip()
119
  print(f"Request openai 1: {time.time() - start}")
120
  relevant = False
121
+ if response.startswith("是") or response.startswith("Yes") or response.startswith("yes"):
122
+ decl_msg = [{"role": "user", "content": f"请将以下内容转述为陈述句,并简化为一句话:\n{input}" if zh else f"Please restate the following content as a declarative sentence and simplify it into one sentence:\n{input}"}]
123
  data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
124
  print(f"Start Request 2: {time.time() - start}")
125
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
 
130
  summary = openai.ChatCompletion.create(
131
  model="gpt-3.5-turbo",
132
  messages=decl_msg,
133
+ temperature=0
134
  )
135
  summary = summary.choices[0].message.content.strip()
136
  print(f"Request openai 2: {time.time() - start}")
137
  relevant = True
138
+ elif response.startswith("不是") or response.startswith("否") or response.startswith("No") or response.startswith("no"):
139
+ decl_msg = [{"role": "user", "content": f"请将以下内容取反义然后转述为陈述句,并简化为一句话:\n{input}" if zh else f"Please restate the following content as a declarative sentence by using the opposite meaning and then simplify it into one sentence:\n{input}"}]
140
  data = {'predict': decl_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
141
  print(f"Start Request 2: {time.time() - start}")
142
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
 
146
  else:
147
  summary = openai.ChatCompletion.create(
148
  model="gpt-3.5-turbo",
149
+ messages=decl_msg,
150
+ temperature=0
151
  )
152
  summary = summary.choices[0].message.content.strip()
153
  print(f"Request openai 2: {time.time() - start}")
 
157
  known.append(summary)
158
  reasoning.append(summary)
159
  if len(reasoning) >= 2:
160
+ simp_msg = [{"role": "user", "content": f"请将以下内容简化为一句话:\n{' '.join(reasoning)}" if zh else f"Please simplify the following content into one sentence:\n{' '.join(reasoning)}"}]
161
  data = {'predict': simp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
162
  print(f"Start Request 3: {time.time() - start}")
163
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
 
168
  merge = openai.ChatCompletion.create(
169
  model="gpt-3.5-turbo",
170
  messages=simp_msg,
171
+ temperature=0
172
  )
173
  merge = merge.choices[0].message.content.strip()
174
  print(f"Request openai 3: {time.time() - start}")
 
178
  key = key.strip()
179
  if key == '':
180
  continue
181
+ if key[1] == '.' or key[2] == '.' or key[0] == '-':
182
+ key1 = ' '.join(key.split(' ')[1:])
183
  else:
184
  key1 = key
185
+ if len(merge) < len(key1):
186
  continue
187
+ comp_msg = [{"role": "user", "content": f"请对比第一句话和第二句话之间的信息,判断第二句话是否完整地概括了第一句话的全部信息,包括关键细节和描述。请用是或否回答。\n第一句话:{key1}\n第二句话:{merge}" if zh else f"Please compare the information between Sentence 1 and Sentence 2 to determine if Sentence 2 contains all the information in Sentence 1, including key details and descriptions. Please answer with \"yes\" or \"no\".\nSentence 1: {key1}\nSentence 2: {merge}"}]
188
  data = {'predict': comp_msg, 'idx': idx, 'isfinished': False, 'answer': answer}
189
  print(f"Start Request 4: {time.time() - start}")
190
  completion=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
 
194
  else:
195
  compare = openai.ChatCompletion.create(
196
  model="gpt-3.5-turbo",
197
+ messages=comp_msg,
198
+ temperature=0
199
  )
200
  compare = compare.choices[0].message.content.strip()
201
  print(f"Request openai 4: {time.time() - start}")
202
+ if compare.startswith('是') or compare.startswith('Yes') or compare.startswith('yes'):
203
+ bingo += 1
204
+ answer_key.remove(key)
205
+ print(key)
206
+ reasoning = []
207
+ break
208
+ print(f"Finish compare: {time.time() - start}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  if bingo >= len(answer_key):
210
  finished = True
211
+ response += f'恭喜你猜到了汤底,汤底是:{answer}\n点击"再来一局"按钮开始下一局游戏。' if zh else f'Congratulations! You have guessed the truth, the truth is: {answer}\nClick the "New Game" button for another game.'
212
  print(f"Finish bingo: {time.time() - start}")
213
  messages.append({"role": "assistant", "content": response})
214
  data = {'predict': messages, 'idx': idx, 'isfinished': finished, 'answer': answer}
 
223
  return gr.update(value='')
224
 
225
 
226
+ def reset_state(zh, request: gr.Request):
227
  global host_cnt
228
  host = request.client.host
229
  if not host in host_cnt:
 
233
  if elapse < 10:
234
  time.sleep(10-elapse)
235
  host_cnt[host] = time.time()
236
+ data = {'refresh': zh}
237
  data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
238
  data = json.loads(str(data, encoding="utf-8"))
239
  chatbot = data['chatbot']
 
242
  story_key = data['story_key']
243
  answer_key = data['answer_key']
244
  idx = data['idx']
245
+ return chatbot, messages, gr.update(value=""), gr.update(value="显示答案") if zh else gr.update(value="Show Answer"), answer, idx, gr.update(value=data['story'].strip()), False, story_key, answer_key, [], 0, [], [], zh
246
+
247
+
248
+ def zh_en_trans(zh, request: gr.Request):
249
+ global host_cnt
250
+ host = request.client.host
251
+ if not host in host_cnt:
252
+ host_cnt[host] = time.time()
253
+ else:
254
+ elapse = time.time()-host_cnt[host]
255
+ if elapse < 10:
256
+ time.sleep(10-elapse)
257
+ host_cnt[host] = time.time()
258
+ zh = not zh
259
+ data = {'refresh': zh}
260
+ data=requests.post(os.environ.get("URL"), data=json.dumps(data, ensure_ascii=False).encode('utf-8')).content
261
+ data = json.loads(str(data, encoding="utf-8"))
262
+ chatbot = data['chatbot']
263
+ messages = data['messages']
264
+ answer = data['answer']
265
+ story_key = data['story_key']
266
+ answer_key = data['answer_key']
267
+ idx = data['idx']
268
+ return chatbot, messages, gr.update(value=""), gr.update(value="显示答案") if zh else gr.update(value="Show Answer"), answer, idx, gr.update(value=data['story'].strip()), False, story_key, answer_key, [], 0, [], [], zh, gr.update(value="English") if zh else gr.update(value="中文"), gr.update(value="发送") if zh else gr.update(value="Submit"), gr.update(value="再来一局") if zh else gr.update(value="New Game"), gr.update(value="海龟汤是一个推理类游戏,游戏开始时会给出一段隐去关键信息的叙述,即汤面,玩家根据汤面推理,提出能够通过“是”或“否”来回答的问题,通过提问不同可能性,缩小真相的范围,直到最终猜到真相(即汤底)的关键信息。玩家可以点击“再来一局”按钮随机一场新的游戏,点击“显示答案”可查看汤底。") if zh else gr.update(value="Lateral Thinking Puzzle is a deductive reasoning game. At the beginning of the game, players receive a narrative with key information concealed, referred to as the \"story\". Based on the story, players deduce and ask questions that can be answered with \"yes\" or \"no\" to narrow down different possibilities and ultimately guess the key information, known as the \"truth\". Players can click the \"New Game\" button to start a new random game or click the \"Show Answer\" button to reveal the truth.")
269
 
270
 
271
  def show_hide_answer(answer, show_ans):
272
  if show_ans:
273
  show_ans = False
274
+ return gr.update(value=""), gr.update(value="显示答案") if zh else gr.update(value="Show Answer"), show_ans
275
  else:
276
  show_ans = True
277
+ return gr.update(value=answer), gr.update(value="隐藏答案") if zh else gr.update(value="Hide Answer"), show_ans
278
 
279
 
280
  host_cnt = {}
281
 
282
  with gr.Blocks() as demo:
283
+ with gr.Row():
284
+ with gr.Column(scale=4):
285
+ gr.HTML("""<h1 align="center">Lateral Thinking Puzzle</h1>""")
286
+ with gr.Column(scale=1):
287
+ zh_enBtn = gr.Button("中文", variant="primary")
288
 
289
  with gr.Row():
290
+ rule = gr.Textbox(label='Rules', value='Lateral Thinking Puzzle is a deductive reasoning game. At the beginning of the game, players receive a narrative with key information concealed, referred to as the \"story\". Based on the story, players deduce and ask questions that can be answered with \"yes\" or \"no\" to narrow down different possibilities and ultimately guess the key information, known as the \"truth\". Players can click the \"New Game\" button to start a new random game or click the \"Show Answer\" button to reveal the truth.', lines=1, max_lines=3).style(container=False)
291
+ chatbot = gr.Chatbot([(None, 'Click the \"New Game\" button to get started.')])
292
  messages = gr.State([])
293
+ answer = gr.State('Click the \"New Game\" button to get started.')
294
  idx = gr.State(0)
295
  show_ans = gr.State(False)
296
+ zh = gr.State(True)
297
  known = gr.State([])
298
  story_key = gr.State([])
299
  answer_key = gr.State([])
 
303
 
304
  with gr.Row():
305
  with gr.Column(scale=4):
306
+ question = gr.Textbox(label='Story', value='Click the \"New Game\" button to get started.',
307
  lines=1, max_lines=3).style(container=False)
308
  with gr.Row():
309
+ user_input = gr.Textbox(show_label=False, placeholder="Input your question...", lines=1, max_lines=3).style(
310
  container=False)
311
  with gr.Row():
312
  with gr.Column(scale=2):
313
+ submitBtn = gr.Button("Submit", variant="primary")
314
  with gr.Column(scale=2):
315
+ emptyBtn = gr.Button("New Game")
316
  with gr.Column(scale=1):
317
  answer_output = gr.Textbox(show_label=False, lines=6, max_lines=6).style(
318
  container=False)
319
+ answerBtn = gr.Button("Show Answer")
320
 
321
 
322
+ zh_enBtn.click(zh_en_trans, [zh], [chatbot, messages, answer_output, answerBtn, answer, idx, question, show_ans, story_key, answer_key, known, bingo, reasoning, history, zh, zh_enBtn, submitBtn, emptyBtn, rule], show_progress=True)
323
+ user_input.submit(predict, [user_input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history, zh], [chatbot, messages, known, bingo, reasoning, history], show_progress=True)
 
324
  user_input.submit(reset_user_input, [], [user_input])
325
+ submitBtn.click(predict, [user_input, chatbot, messages, idx, answer, story_key, answer_key, known, bingo, reasoning, history, zh], [chatbot, messages, known, bingo, reasoning, history], show_progress=True)
 
326
  submitBtn.click(reset_user_input, [], [user_input])
327
 
328
+ emptyBtn.click(reset_state, [zh], [chatbot, messages, answer_output, answerBtn, answer, idx, question, show_ans, story_key, answer_key, known, bingo, reasoning, history, zh], show_progress=True)
329
 
330
+ answerBtn.click(show_hide_answer, [answer, show_ans], outputs=[answer_output, answerBtn, show_ans], show_progress=True)
331
 
332
  demo.queue().launch()