Yoon-gu Hwang commited on
Commit
78309aa
β€’
1 Parent(s): b7d38a9
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -5,6 +5,8 @@ import random
5
  import json
6
  import time
7
  import wandb
 
 
8
 
9
  wandb.login(key=os.environ['WANDB_KEY'])
10
  run = wandb.init(project="pokemon-quiz", entity="yoon-gu")
@@ -26,7 +28,6 @@ GEN_RANGE = {
26
  "9μ„ΈλŒ€": [906, 1017]
27
  }
28
 
29
- USERS = ["June", "Sean", "Woojoo", "Taejoo", "Dummy"]
30
  QUESTION_TEMPLATE = {"question": "λ‹€μŒ 포켓λͺ¬μ˜ 이름은 λ­˜κΉŒμš”?![]({img_url})", "answer": "{name}"}
31
 
32
  def get_question_answer(pokemons_set):
@@ -38,15 +39,22 @@ def get_question_answer(pokemons_set):
38
  a = QUESTION_TEMPLATE['answer'].format(name=name)
39
  return q, a
40
 
 
 
 
 
 
41
  try:
42
  folder = run.use_artifact("settings:latest").download()
 
 
43
  with open(os.path.join(folder, "info.json"), "r") as f:
44
  info = json.load(f)
45
  except:
46
- info = {u: {"done" : True,
47
- "score": 0, "count": 0,
48
- "best_score": 0, "best_time": float("inf"),
49
- "time": 0.0, "comment": ""} for u in USERS}
50
 
51
  MD = """# 포켓λͺ¬ ν€΄μ¦ˆ
52
  ## 곡뢀 방법
@@ -62,14 +70,14 @@ MD = """# 포켓λͺ¬ ν€΄μ¦ˆ
62
  4. 포켓λͺ¬ νƒ€μž…μ„ μ„ νƒν•˜μ„Έμš”.
63
 
64
  ## 점수판
65
- {content}
66
  """
67
 
68
  with gr.Blocks() as demo:
69
  with gr.Row():
70
  with gr.Column():
71
  markdown = gr.Markdown(MD.format(content=''))
72
- user = gr.Radio(USERS, value="Dummy", label="μ‚¬μš©μž", info="당신은 λˆ„κ΅¬μ‹ κ°€μš”?")
 
73
  quiz_count = gr.Radio([10, 20, 30], value=10, label="총 ν€΄μ¦ˆ 개수", info="ν€΄μ¦ˆλ₯Ό λͺ‡ 개 ν’€ μ˜ˆμ •μΈκ°€μš”?")
74
  with gr.Column():
75
  with gr.Row():
@@ -140,7 +148,7 @@ with gr.Blocks() as demo:
140
  bot_message = f"***{message}***!? 🧐 λ‹€μ‹œ ν•œλ²ˆ μƒκ°ν•΄λ³΄μ„Έμš”.\n힌트: {hint1}"
141
  info[user]['score'] -= 0.1
142
 
143
- bot_message += f"\n총 {quiz_count}개 문제 쀑 {info[user]['count']}번째 문제 진행 μ€‘μž…λ‹ˆλ‹€."
144
 
145
  if quiz_count == info[user]['count']:
146
  bot_message = f"λͺ¨λ“  ν€΄μ¦ˆλ₯Ό λ‹€ ν’€μ—ˆμŠ΅λ‹ˆλ‹€. μ μˆ˜λŠ” {info[user]['score']:3.1f}점 μž…λ‹ˆλ‹€."
@@ -148,11 +156,15 @@ with gr.Blocks() as demo:
148
  if info[user]['score'] >= info[user]['best_score']:
149
  info[user]['best_score'] = info[user]['score']
150
  info[user]['best_time'] = min(time.time() - info[user]['time'], info[user]['best_time'])
 
151
  info[user]['comment'] = f"{gen}μ„ΈλŒ€+{types}"
152
  with open("info.json", "w") as f:
153
  json.dump(info, f, indent=4)
 
 
154
  artifact = wandb.Artifact("settings", type="info")
155
  artifact.add_file("info.json")
 
156
  run.log_artifact(artifact)
157
 
158
  chat_history.append((message, bot_message))
@@ -161,29 +173,39 @@ with gr.Blocks() as demo:
161
  lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board])
162
  lbdf.rename(columns={'name': "이름", 'best_score': "졜고점수", 'best_time': "μ‹œκ°„κΈ°λ‘", 'comment': "ν€΄μ¦ˆμœ ν˜•"}, inplace=True)
163
  lbdf.index += 1
164
- md = lbdf[['이름', '졜고점수', 'μ‹œκ°„κΈ°λ‘', 'ν€΄μ¦ˆμœ ν˜•']].to_markdown()
165
- return "", chat_history, MD.format(content=md)
166
 
167
  play.click(respond,
168
  inputs=[play, chatbot, user, quiz_count, generation, poke_types],
169
- outputs=[msg, chatbot, markdown])
170
  skip.click(respond,
171
  inputs=[skip, chatbot, user, quiz_count, generation, poke_types],
172
- outputs=[msg, chatbot, markdown])
173
  stop.click(respond,
174
  inputs=[stop, chatbot, user, quiz_count, generation, poke_types],
175
- outputs=[msg, chatbot, markdown])
 
 
 
 
 
 
 
 
 
176
  def update_table():
177
  leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True)
178
  lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board])
179
  lbdf.rename(columns={'name': "이름", 'best_score': "졜고점수", 'best_time': "μ‹œκ°„κΈ°λ‘", 'comment': "ν€΄μ¦ˆμœ ν˜•"}, inplace=True)
180
  lbdf.index += 1
181
- md = lbdf[['이름', '졜고점수', 'μ‹œκ°„κΈ°λ‘', 'ν€΄μ¦ˆμœ ν˜•']].to_markdown()
182
- return MD.format(content=md)
 
183
  demo.load(update_table,
184
  inputs=None,
185
- outputs=markdown)
186
- msg.submit(respond, [msg, chatbot, user, quiz_count, generation, poke_types], [msg, chatbot, markdown])
187
 
188
  demo.queue(concurrency_count=1)
189
  demo.launch()
 
5
  import json
6
  import time
7
  import wandb
8
+ from copy import deepcopy
9
+ from collections import defaultdict
10
 
11
  wandb.login(key=os.environ['WANDB_KEY'])
12
  run = wandb.init(project="pokemon-quiz", entity="yoon-gu")
 
28
  "9μ„ΈλŒ€": [906, 1017]
29
  }
30
 
 
31
  QUESTION_TEMPLATE = {"question": "λ‹€μŒ 포켓λͺ¬μ˜ 이름은 λ­˜κΉŒμš”?![]({img_url})", "answer": "{name}"}
32
 
33
  def get_question_answer(pokemons_set):
 
39
  a = QUESTION_TEMPLATE['answer'].format(name=name)
40
  return q, a
41
 
42
+ initial_info = {"done" : True,
43
+ "score": 0, "count": 0,
44
+ "best_score": 0, "best_time": float("inf"),
45
+ "time": 0.0, "comment": ""}
46
+
47
  try:
48
  folder = run.use_artifact("settings:latest").download()
49
+ with open(os.path.join(folder, "users.json"), "r") as f:
50
+ USERS = json.load(f)
51
  with open(os.path.join(folder, "info.json"), "r") as f:
52
  info = json.load(f)
53
  except:
54
+ info = defaultdict(lambda : deepcopy(initial_info))
55
+ USERS = ["κΉ€μ„œν˜„", "κΉ€μš°μ£Ό", "Anonymous"]
56
+ for user in USERS:
57
+ print(info[user])
58
 
59
  MD = """# 포켓λͺ¬ ν€΄μ¦ˆ
60
  ## 곡뢀 방법
 
70
  4. 포켓λͺ¬ νƒ€μž…μ„ μ„ νƒν•˜μ„Έμš”.
71
 
72
  ## 점수판
 
73
  """
74
 
75
  with gr.Blocks() as demo:
76
  with gr.Row():
77
  with gr.Column():
78
  markdown = gr.Markdown(MD.format(content=''))
79
+ leader_board = gr.DataFrame()
80
+ user = gr.Dropdown(USERS, value="Anonymous", label="μ‚¬μš©μž", info="당신은 λˆ„κ΅¬μ‹ κ°€μš”?", allow_custom_value=True)
81
  quiz_count = gr.Radio([10, 20, 30], value=10, label="총 ν€΄μ¦ˆ 개수", info="ν€΄μ¦ˆλ₯Ό λͺ‡ 개 ν’€ μ˜ˆμ •μΈκ°€μš”?")
82
  with gr.Column():
83
  with gr.Row():
 
148
  bot_message = f"***{message}***!? 🧐 λ‹€μ‹œ ν•œλ²ˆ μƒκ°ν•΄λ³΄μ„Έμš”.\n힌트: {hint1}"
149
  info[user]['score'] -= 0.1
150
 
151
+ bot_message += f"\n\n총 {quiz_count}개 문제 쀑 {info[user]['count']}번째 문제 진행 μ€‘μž…λ‹ˆλ‹€."
152
 
153
  if quiz_count == info[user]['count']:
154
  bot_message = f"λͺ¨λ“  ν€΄μ¦ˆλ₯Ό λ‹€ ν’€μ—ˆμŠ΅λ‹ˆλ‹€. μ μˆ˜λŠ” {info[user]['score']:3.1f}점 μž…λ‹ˆλ‹€."
 
156
  if info[user]['score'] >= info[user]['best_score']:
157
  info[user]['best_score'] = info[user]['score']
158
  info[user]['best_time'] = min(time.time() - info[user]['time'], info[user]['best_time'])
159
+ info[user]['best_time'] = round(info[user]['best_time'], 2)
160
  info[user]['comment'] = f"{gen}μ„ΈλŒ€+{types}"
161
  with open("info.json", "w") as f:
162
  json.dump(info, f, indent=4)
163
+ with open("users.json", "w") as f:
164
+ json.dump(USERS, f, indent=4)
165
  artifact = wandb.Artifact("settings", type="info")
166
  artifact.add_file("info.json")
167
+ artifact.add_file("users.json")
168
  run.log_artifact(artifact)
169
 
170
  chat_history.append((message, bot_message))
 
173
  lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board])
174
  lbdf.rename(columns={'name': "이름", 'best_score': "졜고점수", 'best_time': "μ‹œκ°„κΈ°λ‘", 'comment': "ν€΄μ¦ˆμœ ν˜•"}, inplace=True)
175
  lbdf.index += 1
176
+ md = lbdf[['이름', '졜고점수', 'μ‹œκ°„κΈ°λ‘', 'ν€΄μ¦ˆμœ ν˜•']]
177
+ return "", chat_history, md
178
 
179
  play.click(respond,
180
  inputs=[play, chatbot, user, quiz_count, generation, poke_types],
181
+ outputs=[msg, chatbot, leader_board])
182
  skip.click(respond,
183
  inputs=[skip, chatbot, user, quiz_count, generation, poke_types],
184
+ outputs=[msg, chatbot, leader_board])
185
  stop.click(respond,
186
  inputs=[stop, chatbot, user, quiz_count, generation, poke_types],
187
+ outputs=[msg, chatbot, leader_board])
188
+
189
+ def rs_change(rs):
190
+ global USERS
191
+ if rs not in USERS:
192
+ USERS += [rs]
193
+ return gr.update(choices=sorted(USERS), value=rs)
194
+
195
+ user.blur(rs_change, user, user)
196
+
197
  def update_table():
198
  leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True)
199
  lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board])
200
  lbdf.rename(columns={'name': "이름", 'best_score': "졜고점수", 'best_time': "μ‹œκ°„κΈ°λ‘", 'comment': "ν€΄μ¦ˆμœ ν˜•"}, inplace=True)
201
  lbdf.index += 1
202
+ print(len(lbdf))
203
+ md = lbdf[['이름', '졜고점수', 'μ‹œκ°„κΈ°λ‘', 'ν€΄μ¦ˆμœ ν˜•']]
204
+ return md
205
  demo.load(update_table,
206
  inputs=None,
207
+ outputs=leader_board)
208
+ msg.submit(respond, [msg, chatbot, user, quiz_count, generation, poke_types], [msg, chatbot, leader_board])
209
 
210
  demo.queue(concurrency_count=1)
211
  demo.launch()