File size: 5,421 Bytes
4f69797 d1f468d e444e3b 9227ae5 e444e3b efe5a1a e444e3b 59c23dd e444e3b 9227ae5 a0025dc 9227ae5 3db7d02 385222f a0025dc 59c23dd 9227ae5 385222f e444e3b 9227ae5 3db7d02 59c23dd 9227ae5 3db7d02 b7ed881 3db7d02 7f078c2 e78b69f 3db7d02 9227ae5 a0025dc 716cd4d 59c23dd 6d1bed2 a0025dc 9227ae5 59c23dd 3db7d02 59c23dd efe5a1a e444e3b 37ab76b e444e3b a0025dc 9227ae5 59c23dd a0025dc 21ef542 6d1bed2 b7ed881 a0025dc b7ed881 a0025dc e444e3b a0025dc 4b5f2cf 21ef542 9227ae5 e444e3b 21ef542 3db7d02 a0025dc 3db7d02 d5ca01c a0025dc b7ed881 3db7d02 a0025dc 3db7d02 21ef542 385222f a0025dc e444e3b 59c23dd 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
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": "๋ค์ ํฌ์ผ๋ชฌ์ ์ด๋ฆ์ ๋ญ๊น์?", "answer": "{name}"}
def get_question_answer():
a = random.randint(0, 10)
b = random.randint(0, 10)
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="Dummy", 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() |