Yoon-gu Hwang commited on
Commit
ccdda9e
1 Parent(s): 4272589

객관식으로 변경

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -37,7 +37,12 @@ def get_question_answer(pokemons_set):
37
  img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
38
  q = QUESTION_TEMPLATE["question"].format(img_url=img_url)
39
  a = QUESTION_TEMPLATE['answer'].format(name=name)
40
- return q, a
 
 
 
 
 
41
 
42
  initial_info = {"done" : True,
43
  "score": 0, "count": 0,
@@ -99,7 +104,11 @@ with gr.Blocks() as demo:
99
  chatbot = gr.Chatbot(bubble_full_width=False,
100
  avatar_images=["https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0001_이상해씨.png",
101
  "https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0155_브케인.png"])
102
- msg = gr.Textbox(placeholder="문제의 답을 입력하세요.", label="답")
 
 
 
 
103
 
104
  def respond(message, chat_history, user, quiz_count, gen, types, request: gr.Request):
105
  message = message.strip()
@@ -110,7 +119,7 @@ with gr.Blocks() as demo:
110
  pokemons_set = pokemons_set.to_dict("records")
111
  if done:
112
  if "퀴즈시작" == message.replace(" ", ""):
113
- q, a = get_question_answer(pokemons_set)
114
  bot_message = f"퀴즈를 시작합니다.\n{q}"
115
  info[user]['answer'] = a
116
  info[user]['done'] = False
@@ -121,7 +130,7 @@ with gr.Blocks() as demo:
121
  bot_message = "퀴즈를 시작하고 싶으시면, **퀴즈 시작** 버튼을 누르세요."
122
  else:
123
  if info[user]['answer'] == message:
124
- q, a = get_question_answer(pokemons_set)
125
  info[user]['answer'] = a
126
  info[user]['score'] += 1
127
  info[user]['count'] += 1
@@ -167,7 +176,7 @@ with gr.Blocks() as demo:
167
  artifact.add_file("users.json")
168
  run.log_artifact(artifact)
169
 
170
- chat_history.append((message, bot_message))
171
 
172
  leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True)
173
  lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board])
@@ -178,15 +187,21 @@ with gr.Blocks() as demo:
178
  btn = gr.Button(value="퀴즈 시작", label="퀴즈 시작")
179
  else:
180
  btn = gr.Button(value="포기하기", label="퀴즈 중단")
181
- info[user]['history'] = chat_history
182
- return btn, "", chat_history, md
 
183
 
184
  play.click(respond,
185
  inputs=[play, chatbot, user, quiz_count, generation, poke_types],
186
- outputs=[play, msg, chatbot, leader_board])
187
  skip.click(respond,
188
  inputs=[skip, chatbot, user, quiz_count, generation, poke_types],
189
- outputs=[play, msg, chatbot, leader_board])
 
 
 
 
 
190
 
191
  def rs_change(rs):
192
  global USERS
@@ -213,7 +228,6 @@ with gr.Blocks() as demo:
213
  demo.load(update_table,
214
  inputs=user,
215
  outputs=[play, leader_board, user, chatbot])
216
- msg.submit(respond, [msg, chatbot, user, quiz_count, generation, poke_types], [play, msg, chatbot, leader_board])
217
 
218
- demo.queue(concurrency_count=1)
219
  demo.launch()
 
37
  img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
38
  q = QUESTION_TEMPLATE["question"].format(img_url=img_url)
39
  a = QUESTION_TEMPLATE['answer'].format(name=name)
40
+
41
+ candidates = random.choices([poke['name'] for poke in pokemons_set], k=3)
42
+ candidates.append(a)
43
+ random.shuffle(candidates)
44
+ random_buttons = [gr.Button(value=f"{name}") for name in candidates]
45
+ return q, a, random_buttons
46
 
47
  initial_info = {"done" : True,
48
  "score": 0, "count": 0,
 
104
  chatbot = gr.Chatbot(bubble_full_width=False,
105
  avatar_images=["https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0001_이상해씨.png",
106
  "https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0155_브케인.png"])
107
+ with gr.Row():
108
+ button1 = gr.Button(value="1번")
109
+ button2 = gr.Button(value="2번")
110
+ button3 = gr.Button(value="3번")
111
+ button4 = gr.Button(value="4번")
112
 
113
  def respond(message, chat_history, user, quiz_count, gen, types, request: gr.Request):
114
  message = message.strip()
 
119
  pokemons_set = pokemons_set.to_dict("records")
120
  if done:
121
  if "퀴즈시작" == message.replace(" ", ""):
122
+ q, a, random_buttons = get_question_answer(pokemons_set)
123
  bot_message = f"퀴즈를 시작합니다.\n{q}"
124
  info[user]['answer'] = a
125
  info[user]['done'] = False
 
130
  bot_message = "퀴즈를 시작하고 싶으시면, **퀴즈 시작** 버튼을 누르세요."
131
  else:
132
  if info[user]['answer'] == message:
133
+ q, a, random_buttons = get_question_answer(pokemons_set)
134
  info[user]['answer'] = a
135
  info[user]['score'] += 1
136
  info[user]['count'] += 1
 
176
  artifact.add_file("users.json")
177
  run.log_artifact(artifact)
178
 
179
+ info[user]['history'].append((message, bot_message))
180
 
181
  leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True)
182
  lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board])
 
187
  btn = gr.Button(value="퀴즈 시작", label="퀴즈 시작")
188
  else:
189
  btn = gr.Button(value="포기하기", label="퀴즈 중단")
190
+
191
+
192
+ return btn, info[user]['history'], md, *random_buttons
193
 
194
  play.click(respond,
195
  inputs=[play, chatbot, user, quiz_count, generation, poke_types],
196
+ outputs=[play, chatbot, leader_board, button1, button2, button3, button4])
197
  skip.click(respond,
198
  inputs=[skip, chatbot, user, quiz_count, generation, poke_types],
199
+ outputs=[play, chatbot, leader_board, button1, button2, button3, button4])
200
+
201
+ for btn in [button1, button2, button3, button4]:
202
+ btn.click(respond,
203
+ inputs=[btn, chatbot, user, quiz_count, generation, poke_types],
204
+ outputs=[play, chatbot, leader_board, button1, button2, button3, button4])
205
 
206
  def rs_change(rs):
207
  global USERS
 
228
  demo.load(update_table,
229
  inputs=user,
230
  outputs=[play, leader_board, user, chatbot])
 
231
 
232
+ demo.queue(concurrency_count=3)
233
  demo.launch()