import gradio as gr import pyjosa 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(value="퀴즈 시작") clear = gr.ClearButton([msg, chatbot]) def respond(message, chat_history): if "퀴즈 시작" == message: bot_message = "퀴즈를 시작합니다.\n" + QUESTION_TEMPLATE["question"].format(img_url=img_url) quiz_start.value = True 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: 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: try: trial = Josa.get_full_string(message, "는") except pyjosa.exceptions.NotHangleException: trial = f"{message}!?" bot_message = f"***{trial}*** 정답일까요? 🧐 다시 한번 생각해보세요." chat_history.append((message, bot_message)) return "", chat_history msg.submit(respond, [msg, chatbot], [msg, chatbot]) demo.queue(concurrency_count=3) demo.launch()