|
import os |
|
os.system("pip install gradio --upgrade") |
|
|
|
import gradio as gr |
|
from pyjosa.josa import Josa |
|
import random |
|
import json |
|
|
|
with open('pokemon.json', 'r') as f: |
|
pokemons = json.load(f) |
|
|
|
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"} |
|
with gr.Blocks() as demo: |
|
quiz_start = gr.State(value=False) |
|
answer = gr.State(value="") |
|
chatbot = gr.Chatbot(bubble_full_width=False) |
|
msg = gr.Textbox() |
|
clear = gr.ClearButton([msg, chatbot]) |
|
|
|
|
|
def respond(message, chat_history): |
|
if not quiz_start.value: |
|
chosen = random.choice(pokemons) |
|
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}" |
|
if "ν΄μ¦ μμ" == message: |
|
bot_message = "ν΄μ¦λ₯Ό μμν©λλ€.\n" + QUESTION_TEMPLATE["question"].format(img_url=img_url) |
|
quiz_start.value = True |
|
else: |
|
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ**μ΄λΌκ³ λ§μν΄μ£ΌμΈμ." |
|
else: |
|
trial = Josa.get_full_string(message, "λ") |
|
if answer.value == message: |
|
chosen = random.choice(pokemons) |
|
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}" |
|
bot_message = "μ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€." + QUESTION_TEMPLATE["question"].format(img_url=img_url) |
|
else: |
|
bot_message = f"{trial} μ λ΅μΌκΉμ? λ€μ νλ² μκ°ν΄λ³΄μΈμ." |
|
|
|
chat_history.append((message, bot_message)) |
|
return "", chat_history |
|
|
|
msg.submit(respond, [msg, chatbot], [msg, chatbot]) |
|
|
|
demo.launch() |
|
|
|
|