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/resolve/main/images/No_0001_이상해씨.png", "https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0155_브케인.png"]) msg = gr.Textbox(placeholder="문제의 답을 입력하세요.", label="답") def respond(message, chat_history, user, quiz_count, gen, types, request: gr.Request): 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()