|
import os |
|
os.system("pip install gradio==3.46.0") |
|
import gradio as gr |
|
import pandas as pd |
|
import random |
|
import json |
|
import time |
|
|
|
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] |
|
} |
|
|
|
USERS = ["June", "Sean", "Woojoo", "Taejoo", "Dummy"] |
|
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'] |
|
answer.value = QUESTION_TEMPLATE['answer'].format(name=name) |
|
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) |
|
return q, a |
|
|
|
info = {u: {"done" : True, "score": 0, "count": 0, "best_score": 0, "best_time": float("inf"), "time": 0.0} for u in USERS} |
|
|
|
MD = """# ν¬μΌλͺ¬ ν΄μ¦ |
|
## μ¬μ©λ°©λ² |
|
|
|
1. μ¬μ©μλ₯Ό μ ννμΈμ. |
|
2. μ΄ ν΄μ¦ κ°μλ₯Ό μ ννμΈμ. |
|
3. ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ. |
|
4. ν¬μΌλͺ¬ νμ
μ μ ννμΈμ. |
|
|
|
## μ μν |
|
{content} |
|
""" |
|
|
|
with gr.Blocks() as demo: |
|
answer = gr.State(value="") |
|
with gr.Row(): |
|
with gr.Column(): |
|
markdown = gr.Markdown(MD.format(content='')) |
|
user = gr.Radio(USERS, value="Dummy", label="μ¬μ©μ", info="λΉμ μ λꡬμ κ°μ?") |
|
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="ν΄μ¦ μμ") |
|
stop = gr.Button(value="ν΄μ¦ μ’
λ£", label="ν΄μ¦ μ’
λ£") |
|
chatbot = gr.Chatbot(bubble_full_width=False, |
|
avatar_images=["https://huggingface.co/spaces/yoon-gu/pokemon-quiz/resolve/main/μ€λ°μ¬.png", |
|
"https://huggingface.co/spaces/yoon-gu/pokemon-quiz/resolve/main/κ³ λΌνλ.png"]) |
|
msg = gr.Textbox(placeholder="λ¬Έμ μ λ΅μ μ
λ ₯νμΈμ.", label="λ΅") |
|
|
|
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 = get_question_answer(pokemons_set) |
|
bot_message = f"ν΄μ¦λ₯Ό μμν©λλ€.\n{q}" |
|
answer.value = a |
|
info[user]['done'] = False |
|
info[user]['score'] = 0 |
|
info[user]['count'] = 0 |
|
info[user]['time'] = time.time() |
|
else: |
|
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ** λ²νΌμ λλ₯΄μΈμ." |
|
else: |
|
if answer.value == message: |
|
q, a = get_question_answer(pokemons_set) |
|
answer.value = a |
|
info[user]['score'] += 1 |
|
info[user]['count'] += 1 |
|
bot_message = f"πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€.\n{q}" |
|
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']) |
|
elif "ν΄μ¦μ’
λ£" == message.replace(" ", ""): |
|
bot_message = f"ν΄μ¦λ₯Ό κ°μ μ’
λ£ν©λλ€." |
|
info[user]['done'] = True |
|
else: |
|
hint1 = "" |
|
for x, y in zip(message, answer.value): |
|
if x == y: |
|
hint1 += x |
|
else: |
|
hint1 += "X" |
|
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ.\nννΈ: {hint1}" |
|
info[user]['score'] -= 0.1 |
|
|
|
chat_history.append((message, bot_message)) |
|
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()])) |
|
|
|
play.click(respond, |
|
inputs=[play, chatbot, user, quiz_count, generation, poke_types], |
|
outputs=[msg, chatbot, markdown]) |
|
stop.click(respond, |
|
inputs=[stop, chatbot, user, quiz_count, generation, poke_types], |
|
outputs=[msg, chatbot, markdown]) |
|
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()])), |
|
inputs=None, |
|
outputs=markdown) |
|
msg.submit(respond, [msg, chatbot, user, quiz_count, generation, poke_types], [msg, chatbot, markdown]) |
|
|
|
demo.queue(concurrency_count=1) |
|
demo.launch() |