Spaces:
Runtime error
Runtime error
Yoon-gu Hwang
commited on
Commit
β’
78309aa
1
Parent(s):
b7d38a9
update
Browse files
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 =
|
47 |
-
|
48 |
-
|
49 |
-
|
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 |
-
|
|
|
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[['μ΄λ¦', 'μ΅κ³ μ μ', 'μκ°κΈ°λ‘', 'ν΄μ¦μ ν']]
|
165 |
-
return "", chat_history,
|
166 |
|
167 |
play.click(respond,
|
168 |
inputs=[play, chatbot, user, quiz_count, generation, poke_types],
|
169 |
-
outputs=[msg, chatbot,
|
170 |
skip.click(respond,
|
171 |
inputs=[skip, chatbot, user, quiz_count, generation, poke_types],
|
172 |
-
outputs=[msg, chatbot,
|
173 |
stop.click(respond,
|
174 |
inputs=[stop, chatbot, user, quiz_count, generation, poke_types],
|
175 |
-
outputs=[msg, chatbot,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
182 |
-
|
|
|
183 |
demo.load(update_table,
|
184 |
inputs=None,
|
185 |
-
outputs=
|
186 |
-
msg.submit(respond, [msg, chatbot, user, quiz_count, generation, poke_types], [msg, chatbot,
|
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()
|