Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
import pandas as pd | |
import random | |
import json | |
import time | |
import wandb | |
from copy import deepcopy | |
from collections import defaultdict | |
wandb.login(key=os.environ['WANDB_KEY']) | |
run = wandb.init(project="pokemon-quiz", entity="yoon-gu") | |
with open('pokemon.json', 'r') as f: | |
pokemons = json.load(f) | |
pokemons_types = ["λͺ¨λ νμ "] + sorted(set([t for poke in pokemons for t in poke['types']])) | |
df = pd.DataFrame(pokemons) | |
GEN_RANGE = { | |
"λͺ¨λ μΈλ": [1, 1017], | |
"1μΈλ": [1, 151], | |
"2μΈλ": [152, 251], | |
"3μΈλ": [252, 386], | |
"4μΈλ": [387, 493], | |
"5μΈλ": [494, 649], | |
"6μΈλ": [650, 721], | |
"7μΈλ": [722, 809], | |
"8μΈλ": [810, 905], | |
"9μΈλ": [906, 1017] | |
} | |
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"} | |
def get_question_answer(pokemons_set): | |
chosen = random.choice(pokemons_set) | |
name = chosen['name'].replace("β", "μμ»·").replace("β", "μμ»·") | |
image_path = chosen['image_path'] | |
img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}" | |
q = QUESTION_TEMPLATE["question"].format(img_url=img_url) | |
a = QUESTION_TEMPLATE['answer'].format(name=name) | |
candidates = random.choices([poke['name'] for poke in pokemons_set], k=3) | |
candidates.append(a) | |
random.shuffle(candidates) | |
random_buttons = [gr.Button(value=f"{name}") for name in candidates] | |
return q, a, random_buttons | |
initial_info = {"done" : True, | |
"score": 0, "count": 0, | |
"best_score": 0, "best_time": float("inf"), | |
"time": 0.0, "comment": "", | |
"history": []} | |
try: | |
folder = run.use_artifact("settings:latest").download() | |
with open(os.path.join(folder, "users.json"), "r") as f: | |
USERS = json.load(f) | |
with open(os.path.join(folder, "info.json"), "r") as f: | |
info = json.load(f) | |
except: | |
info = defaultdict(lambda : deepcopy(initial_info)) | |
USERS = ["κΉμν", "κΉμ°μ£Ό", "Anonymous"] | |
for user in USERS: | |
print(info[user]) | |
MD = """# ν¬μΌλͺ¬ ν΄μ¦ | |
## κ³΅λΆ λ°©λ² | |
μλ ν¬μΌλͺ¬ λκ°μ λ³΄κ³ κ³΅λΆνλ©΄ λμμ΄ λ©λλ€. | |
- https://huggingface.co/spaces/yoon-gu/pokemon | |
## μ¬μ©λ°©λ² | |
1. μ¬μ©μλ₯Ό μ ννμΈμ. | |
2. μ΄ ν΄μ¦ κ°μλ₯Ό μ ννμΈμ. | |
3. ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ. | |
4. ν¬μΌλͺ¬ νμ μ μ ννμΈμ. | |
## μ μν | |
""" | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
markdown = gr.Markdown(MD.format(content='')) | |
leader_board = gr.DataFrame(wrap=True, row_count=10) | |
user = gr.Dropdown(USERS, value="Anonymous", label="μ¬μ©μ", info="λΉμ μ λꡬμ κ°μ?", allow_custom_value=True) | |
quiz_count = gr.Radio([10, 20, 30], value=10, label="μ΄ ν΄μ¦ κ°μ", info="ν΄μ¦λ₯Ό λͺ κ° ν μμ μΈκ°μ?") | |
with gr.Column(): | |
with gr.Row(): | |
generation = gr.Dropdown( | |
[f"{k}μΈλ" for k in range(1, 10)] + ["λͺ¨λ μΈλ"], | |
value="λͺ¨λ μΈλ", | |
label="ν¬μΌλͺ¬ μΈλ", | |
info="μνλ ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ." | |
) | |
poke_types = gr.Dropdown( | |
pokemons_types, value="λͺ¨λ νμ ", | |
label="ν¬μΌλͺ¬ νμ ", | |
info="μνλ ν¬μΌλͺ¬ νμ μ μ ννμΈμ." | |
) | |
with gr.Row(): | |
play = gr.Button(value="ν΄μ¦ μμ", label="ν΄μ¦ μμ") | |
skip = gr.Button(value="λ¬Έμ λμ΄κ°κΈ°", label="λ¬Έμ μ€ν΅") | |
chatbot = gr.Chatbot(bubble_full_width=False, | |
avatar_images=["https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0001_μ΄μν΄μ¨.png", | |
"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0155_λΈμΌμΈ.png"]) | |
with gr.Row(): | |
button1 = gr.Button(value="1λ²") | |
button2 = gr.Button(value="2λ²") | |
button3 = gr.Button(value="3λ²") | |
button4 = gr.Button(value="4λ²") | |
def respond(message, chat_history, user, quiz_count, gen, types, request: gr.Request): | |
message = message.strip() | |
done = info[user]['done'] | |
start, end = GEN_RANGE[gen] | |
sdf = df[start:end] | |
pokemons_set = sdf[sdf['types'].apply(lambda x: (types in x)) | (types == "λͺ¨λ νμ ")] | |
pokemons_set = pokemons_set.to_dict("records") | |
if done: | |
if "ν΄μ¦μμ" == message.replace(" ", ""): | |
q, a, random_buttons = get_question_answer(pokemons_set) | |
bot_message = f"ν΄μ¦λ₯Ό μμν©λλ€.\n{q}" | |
info[user]['answer'] = a | |
info[user]['done'] = False | |
info[user]['score'] = 0 | |
info[user]['count'] = 0 | |
info[user]['time'] = time.time() | |
else: | |
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ** λ²νΌμ λλ₯΄μΈμ." | |
else: | |
if info[user]['answer'] == message: | |
q, a, random_buttons = get_question_answer(pokemons_set) | |
info[user]['answer'] = a | |
info[user]['score'] += 1 | |
info[user]['count'] += 1 | |
bot_message = f"πμ λ΅μ λλ€! λ€μ λ¬Έμ μ λλ€.\n{q}\n- νμ¬ μ μ: {info[user]['score']}μ \n- μμ μκ°: {time.time() - info[user]['time']:4.3f}μ΄" | |
elif "ν¬κΈ°νκΈ°" == message.replace(" ", ""): | |
bot_message = f"ν΄μ¦λ₯Ό κ°μ μ’ λ£ν©λλ€." | |
info[user]['done'] = True | |
elif "λ¬Έμ λμ΄κ°κΈ°" == message: | |
info[user]['count'] += 1 | |
q, a = get_question_answer(pokemons_set) | |
info[user]['answer'] = a | |
bot_message = f"λ¬Έμ λ₯Ό λμ΄κ°λλ€. λ€μλ¬Έμ μ λλ€.\n{q}" | |
else: | |
hint1 = "" | |
for i, y in enumerate(info[user]['answer']): | |
if i < len(message): | |
if message[i] == y: | |
hint1 += y | |
else: | |
hint1 += "X" | |
else: | |
hint1 += "X" | |
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ.\nννΈ: {hint1}" | |
info[user]['score'] -= 0.1 | |
bot_message += f"\n\nμ΄ {quiz_count}κ° λ¬Έμ μ€ {info[user]['count']+1}λ²μ§Έ λ¬Έμ μ§ν μ€μ λλ€." | |
if quiz_count == info[user]['count']: | |
bot_message = f"λͺ¨λ ν΄μ¦λ₯Ό λ€ νμμ΅λλ€. μ μλ {info[user]['score']:3.1f}μ μ λλ€." | |
info[user]['done'] = True | |
if info[user]['score'] >= info[user]['best_score']: | |
info[user]['best_score'] = info[user]['score'] | |
info[user]['best_time'] = min(time.time() - info[user]['time'], info[user]['best_time']) | |
info[user]['best_time'] = round(info[user]['best_time'], 2) | |
info[user]['comment'] = f"{gen.replace(' ', '')}+{types.replace(' ', '')}" | |
with open("info.json", "w") as f: | |
json.dump(info, f, indent=4) | |
with open("users.json", "w") as f: | |
json.dump(USERS, f, indent=4) | |
artifact = wandb.Artifact("settings", type="info") | |
artifact.add_file("info.json") | |
artifact.add_file("users.json") | |
run.log_artifact(artifact) | |
info[user]['history'].append((message, bot_message)) | |
leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True) | |
lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board]) | |
lbdf.rename(columns={'name': "μ΄λ¦", 'best_score': "μ΅κ³ μ μ", 'best_time': "μκ°κΈ°λ‘", 'comment': "ν΄μ¦μ ν"}, inplace=True) | |
lbdf.index += 1 | |
md = lbdf[['μ΄λ¦', 'μ΅κ³ μ μ', 'μκ°κΈ°λ‘', 'ν΄μ¦μ ν']] | |
if info[user]['done']: | |
btn = gr.Button(value="ν΄μ¦ μμ", label="ν΄μ¦ μμ") | |
else: | |
btn = gr.Button(value="ν¬κΈ°νκΈ°", label="ν΄μ¦ μ€λ¨") | |
return btn, info[user]['history'], md, *random_buttons | |
play.click(respond, | |
inputs=[play, chatbot, user, quiz_count, generation, poke_types], | |
outputs=[play, chatbot, leader_board, button1, button2, button3, button4]) | |
skip.click(respond, | |
inputs=[skip, chatbot, user, quiz_count, generation, poke_types], | |
outputs=[play, chatbot, leader_board, button1, button2, button3, button4]) | |
for btn in [button1, button2, button3, button4]: | |
btn.click(respond, | |
inputs=[btn, chatbot, user, quiz_count, generation, poke_types], | |
outputs=[play, chatbot, leader_board, button1, button2, button3, button4]) | |
def rs_change(rs): | |
global USERS | |
if rs not in USERS: | |
USERS += [rs] | |
new_dropdown = gr.Dropdown(sorted(USERS), value=rs, label="μ¬μ©μ", info="λΉμ μ λꡬμ κ°μ?", allow_custom_value=True) | |
return new_dropdown | |
user.blur(rs_change, user, user) | |
def update_table(user): | |
leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True) | |
lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board]) | |
lbdf.rename(columns={'name': "μ΄λ¦", 'best_score': "μ΅κ³ μ μ", 'best_time': "μκ°κΈ°λ‘", 'comment': "ν΄μ¦μ ν"}, inplace=True) | |
lbdf.index += 1 | |
md = lbdf[['μ΄λ¦', 'μ΅κ³ μ μ', 'μκ°κΈ°λ‘', 'ν΄μ¦μ ν']] | |
if info[user]['done']: | |
btn = gr.Button(value="ν΄μ¦ μμ", label="ν΄μ¦ μμ") | |
else: | |
btn = gr.Button(value="ν¬κΈ°νκΈ°", label="ν΄μ¦ μ€λ¨") | |
return btn, md, gr.Dropdown(USERS, value=user, label="μ¬μ©μ", info="λΉμ μ λꡬμ κ°μ?", allow_custom_value=True), info[user]['history'] | |
demo.load(update_table, | |
inputs=user, | |
outputs=[play, leader_board, user, chatbot]) | |
demo.queue(concurrency_count=3) | |
demo.launch() |