|
import os |
|
os.system("pip install gradio==3.47.1") |
|
import gradio as gr |
|
import pandas as pd |
|
import random |
|
import json |
|
import time |
|
|
|
USERS = ["June", "Sean", "Woojoo", "Taejoo", "Dummy"] |
|
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"} |
|
|
|
def get_question_answer(): |
|
a = random.randint(0, 5) |
|
b = random.randint(0, 6) |
|
c = a + b |
|
q = f"$${a} + {b} = \square$$" |
|
a = f"{c}" |
|
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. μ΄ ν΄μ¦ κ°μλ₯Ό μ ννμΈμ. |
|
|
|
## μ μν |
|
{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="Taejoo", label="μ¬μ©μ", info="λΉμ μ λꡬμ κ°μ?") |
|
quiz_count = gr.Radio([10, 20, 30], value=10, label="μ΄ ν΄μ¦ κ°μ", info="ν΄μ¦λ₯Ό λͺ κ° ν μμ μΈκ°μ?") |
|
with gr.Column(): |
|
with gr.Row(): |
|
play = gr.Button(value="ν΄μ¦ μμ", label="ν΄μ¦ μμ") |
|
skip = 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, request: gr.Request): |
|
message = message.strip() |
|
done = info[user]['done'] |
|
if done: |
|
if "ν΄μ¦μμ" == message.replace(" ", ""): |
|
q, a = get_question_answer() |
|
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() |
|
answer.value = a |
|
info[user]['score'] += 1 |
|
info[user]['count'] += 1 |
|
bot_message = f"πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€.\n- νμ¬ μ μ: {info[user]['score']}μ \n- μμ μκ°: {time.time() - info[user]['time']:4.3f}μ΄\nλΉμΉΈμ λ€μ΄κ° μ«μλ₯Ό λ§μΆ°μ£ΌμΈμ.{q}" |
|
|
|
elif "ν΄μ¦μ’
λ£" == message.replace(" ", ""): |
|
bot_message = f"ν΄μ¦λ₯Ό κ°μ μ’
λ£ν©λλ€." |
|
info[user]['done'] = True |
|
elif "λ¬Έμ λμ΄κ°κΈ°" == message: |
|
info[user]['count'] += 1 |
|
q, a = get_question_answer() |
|
answer.value = a |
|
bot_message = f"λ¬Έμ λ₯Ό λμ΄κ°λλ€. λ€μ λ¬Έμ μ
λλ€.\n{q}" |
|
else: |
|
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ." |
|
info[user]['score'] -= 0.1 |
|
|
|
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']) |
|
|
|
chat_history.append((message, bot_message)) |
|
|
|
leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True) |
|
lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board]) |
|
lbdf.index += 1 |
|
md = lbdf[['name', 'best_score', 'best_time']].to_markdown() |
|
return "", chat_history, MD.format(content=md) |
|
|
|
play.click(respond, |
|
inputs=[play, chatbot, user, quiz_count], |
|
outputs=[msg, chatbot, markdown]) |
|
skip.click(respond, |
|
inputs=[skip, chatbot, user, quiz_count], |
|
outputs=[msg, chatbot, markdown]) |
|
stop.click(respond, |
|
inputs=[stop, chatbot, user, quiz_count], |
|
outputs=[msg, chatbot, markdown]) |
|
def update_table(): |
|
leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True) |
|
lbdf = pd.DataFrame([dict(**a[1], name=a[0]) for a in leader_board]) |
|
lbdf.index += 1 |
|
md = lbdf[['name', 'best_score', 'best_time']].to_markdown() |
|
return MD.format(content=md) |
|
demo.load(update_table, |
|
inputs=None, |
|
outputs=markdown) |
|
msg.submit(respond, [msg, chatbot, user, quiz_count], [msg, chatbot, markdown]) |
|
|
|
demo.queue(concurrency_count=1) |
|
demo.launch() |