File size: 3,309 Bytes
4f69797 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 9227ae5 e444e3b 039d478 9227ae5 e444e3b 039d478 55c3782 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import os
os.system("pip install gradio==3.46.0")
import gradio as gr
import pandas as pd
import random
import json
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']
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
MD = """# ν¬μΌλͺ¬ ν΄μ¦
"""
with gr.Blocks() as demo:
quiz_start = gr.State(value=False)
score = gr.State(value=0)
answer = gr.State(value="")
with gr.Row():
with gr.Column():
gr.Markdown(MD)
with gr.Column():
with gr.Row():
generation = gr.Dropdown(
[f"{k}μΈλ" for k in range(1, 10)] + ["λͺ¨λ μΈλ"], value="1μΈλ", label="ν¬μΌλͺ¬ μΈλ", info="μνλ ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ."
)
poke_types = gr.Dropdown(
pokemons_types, value="λͺ¨λ νμ
", label="ν¬μΌλͺ¬ νμ
", info="μνλ ν¬μΌλͺ¬ νμ
μ μ ννμΈμ."
)
chatbot = gr.Chatbot(bubble_full_width=False)
msg = gr.Textbox(value="ν΄μ¦ μμ")
clear = gr.ClearButton([msg, chatbot, quiz_start])
def respond(gen, types, message, chat_history, request: gr.Request):
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 not quiz_start.value:
if "ν΄μ¦ μμ" == message:
q, a = get_question_answer(pokemons_set)
bot_message = f"ν΄μ¦λ₯Ό μμν©λλ€.\n{q}"
answer.value = a
quiz_start.value = True
else:
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ**μ΄λΌκ³ λ§μν΄μ£ΌμΈμ."
else:
if answer.value == message:
q, a = get_question_answer(pokemons_set)
answer.value = a
bot_message = f"πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€.\n{q}"
else:
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ."
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond, [generation, poke_types, msg, chatbot], [msg, chatbot])
demo.queue(concurrency_count=3)
demo.launch() |