File size: 5,418 Bytes
4f69797
d1f468d
e444e3b
9227ae5
e444e3b
 
efe5a1a
e444e3b
59c23dd
e444e3b
9227ae5
a0025dc
c938c12
 
a0025dc
 
 
9227ae5
 
3db7d02
385222f
a0025dc
59c23dd
 
 
 
 
 
 
9227ae5
385222f
e444e3b
 
9227ae5
 
3db7d02
c938c12
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
 
c938c12
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": "๋‹ค์Œ ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์€ ๋ญ˜๊นŒ์š”?![]({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()