Yoon-gu Hwang commited on
Commit
3db7d02
1 Parent(s): ac136ad

업데이트

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -28,7 +28,7 @@ QUESTION_TEMPLATE = {"question": "다음 포켓몬의 이름은 뭘까요?![]({i
28
 
29
  def get_question_answer(pokemons_set):
30
  chosen = random.choice(pokemons_set)
31
- name = chosen['name']
32
  image_path = chosen['image_path']
33
  answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
34
  img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
@@ -36,7 +36,7 @@ def get_question_answer(pokemons_set):
36
  a = QUESTION_TEMPLATE['answer'].format(name=name)
37
  return q, a
38
 
39
- info = {u: {"done" : True, "score": 0, "count": 0, "best": 0, "time": 0.0} for u in USERS}
40
 
41
  MD = """# 포켓몬 퀴즈
42
  ## 사용방법
@@ -54,7 +54,7 @@ with gr.Blocks() as demo:
54
  answer = gr.State(value="")
55
  with gr.Row():
56
  with gr.Column():
57
- markdown = gr.Markdown(MD.format(content='\n'.join([f"- {u}({v['score']:3.1f}점, {v['time']:3.1f}초)" for u, v in info.items()])))
58
  user = gr.Radio(USERS, value="Dummy", label="사용자", info="당신은 누구신가요?")
59
  quiz_count = gr.Radio([10, 20, 30], value=10, label="총 퀴즈 개수", info="퀴즈를 몇 개 풀 예정인가요?")
60
  with gr.Column():
@@ -70,10 +70,13 @@ with gr.Blocks() as demo:
70
  label="포켓몬 타입",
71
  info="원하는 포켓몬 타입을 선택하세요."
72
  )
 
 
 
73
  chatbot = gr.Chatbot(bubble_full_width=False)
74
- msg = gr.Textbox(value="퀴즈 시작", label="답")
75
 
76
- def respond(user, quiz_count, gen, types, message, chat_history, request: gr.Request):
77
  done = info[user]['done']
78
  start, end = GEN_RANGE[gen]
79
  sdf = df[start:end]
@@ -85,6 +88,7 @@ with gr.Blocks() as demo:
85
  bot_message = f"퀴즈를 시작합니다.\n{q}"
86
  answer.value = a
87
  info[user]['done'] = False
 
88
  info[user]['count'] = 0
89
  info[user]['time'] = time.time()
90
  else:
@@ -99,7 +103,9 @@ with gr.Blocks() as demo:
99
  if quiz_count == info[user]['count']:
100
  bot_message = f"모든 퀴즈를 다 풀었습니다. 점수는 {info[user]['score']:3.1f}점 입니다."
101
  info[user]['done'] = True
102
- info[user]['time'] = time.time() - info[user]['time']
 
 
103
  elif "퀴즈종료" == message.replace(" ", ""):
104
  bot_message = f"퀴즈를 강제 종료합니다."
105
  info[user]['done'] = True
@@ -108,12 +114,18 @@ with gr.Blocks() as demo:
108
  info[user]['score'] -= 0.1
109
 
110
  chat_history.append((message, bot_message))
111
- return "", chat_history, MD.format(content='\n'.join([f"- {u}({v['score']:3.1f}점, {v['time']:3.1f}초)" for u, v in info.items()]))
112
-
113
- demo.load(lambda : MD.format(content='\n'.join([f"- {u}({v['score']:3.1f}점)" for u, v in info.items()])),
 
 
 
 
 
 
114
  inputs=None,
115
  outputs=markdown)
116
- msg.submit(respond, [user, quiz_count, generation, poke_types, msg, chatbot], [msg, chatbot, markdown])
117
 
118
  demo.queue(concurrency_count=1)
119
  demo.launch()
 
28
 
29
  def get_question_answer(pokemons_set):
30
  chosen = random.choice(pokemons_set)
31
+ name = chosen['name'].replace("♀", "암컷").replace("♂", "수컷")
32
  image_path = chosen['image_path']
33
  answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
34
  img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
 
36
  a = QUESTION_TEMPLATE['answer'].format(name=name)
37
  return q, a
38
 
39
+ info = {u: {"done" : True, "score": 0, "count": 0, "best_score": 0, "best_time": float("inf"), "time": 0.0} for u in USERS}
40
 
41
  MD = """# 포켓몬 퀴즈
42
  ## 사용방법
 
54
  answer = gr.State(value="")
55
  with gr.Row():
56
  with gr.Column():
57
+ markdown = gr.Markdown(MD.format(content=''))
58
  user = gr.Radio(USERS, value="Dummy", label="사용자", info="당신은 누구신가요?")
59
  quiz_count = gr.Radio([10, 20, 30], value=10, label="총 퀴즈 개수", info="퀴즈를 몇 개 풀 예정인가요?")
60
  with gr.Column():
 
70
  label="포켓몬 타입",
71
  info="원하는 포켓몬 타입을 선택하세요."
72
  )
73
+ with gr.Row():
74
+ play = gr.Button(value="퀴즈 시작", label="퀴즈 시작")
75
+ stop = gr.Button(value="퀴즈 종료", label="퀴즈 종료")
76
  chatbot = gr.Chatbot(bubble_full_width=False)
77
+ msg = gr.Textbox(placeholder="문제의 답을 입력하세요.", label="답")
78
 
79
+ def respond(message, chat_history, user, quiz_count, gen, types, request: gr.Request):
80
  done = info[user]['done']
81
  start, end = GEN_RANGE[gen]
82
  sdf = df[start:end]
 
88
  bot_message = f"퀴즈를 시작합니다.\n{q}"
89
  answer.value = a
90
  info[user]['done'] = False
91
+ info[user]['score'] = 0
92
  info[user]['count'] = 0
93
  info[user]['time'] = time.time()
94
  else:
 
103
  if quiz_count == info[user]['count']:
104
  bot_message = f"모든 퀴즈를 다 풀었습니다. 점수는 {info[user]['score']:3.1f}점 입니다."
105
  info[user]['done'] = True
106
+ if info[user]['score'] >= info[user]['best_score']:
107
+ info[user]['best_score'] = info[user]['score']
108
+ info[user]['best_time'] = min(time.time() - info[user]['time'], info[user]['best_time'])
109
  elif "퀴즈종료" == message.replace(" ", ""):
110
  bot_message = f"퀴즈를 강제 종료합니다."
111
  info[user]['done'] = True
 
114
  info[user]['score'] -= 0.1
115
 
116
  chat_history.append((message, bot_message))
117
+ return "", chat_history, MD.format(content='\n'.join([f"- {u}({v['best_score']:3.1f}점, {v['best_time']:3.1f}초)" for u, v in info.items()]))
118
+
119
+ play.click(respond,
120
+ inputs=[play, chatbot, user, quiz_count, generation, poke_types],
121
+ outputs=[msg, chatbot, markdown])
122
+ stop.click(respond,
123
+ inputs=[stop, chatbot, user, quiz_count, generation, poke_types],
124
+ outputs=[msg, chatbot, markdown])
125
+ demo.load(lambda : MD.format(content='\n'.join([f"- {u}({v['best_score']:3.1f}점, {v['best_time']:3.1f}초)" for u, v in info.items()])),
126
  inputs=None,
127
  outputs=markdown)
128
+ msg.submit(respond, [msg, chatbot, user, quiz_count, generation, poke_types], [msg, chatbot, markdown])
129
 
130
  demo.queue(concurrency_count=1)
131
  demo.launch()