Update app.py
Browse files
app.py
CHANGED
@@ -6,50 +6,24 @@ import random
|
|
6 |
import json
|
7 |
import time
|
8 |
|
9 |
-
with open('pokemon.json', 'r') as f:
|
10 |
-
pokemons = json.load(f)
|
11 |
-
pokemons_types = ["๋ชจ๋ ํ์
"] + sorted(set([t for poke in pokemons for t in poke['types']]))
|
12 |
-
df = pd.DataFrame(pokemons)
|
13 |
-
GEN_RANGE = {
|
14 |
-
"๋ชจ๋ ์ธ๋": [1, 1017],
|
15 |
-
"1์ธ๋": [1, 151],
|
16 |
-
"2์ธ๋": [152, 251],
|
17 |
-
"3์ธ๋": [252, 386],
|
18 |
-
"4์ธ๋": [387, 493],
|
19 |
-
"5์ธ๋": [494, 649],
|
20 |
-
"6์ธ๋": [650, 721],
|
21 |
-
"7์ธ๋": [722, 809],
|
22 |
-
"8์ธ๋": [810, 905],
|
23 |
-
"9์ธ๋": [906, 1017]
|
24 |
-
}
|
25 |
-
|
26 |
USERS = ["June", "Sean", "Woojoo", "Taejoo", "Dummy"]
|
27 |
QUESTION_TEMPLATE = {"question": "๋ค์ ํฌ์ผ๋ชฌ์ ์ด๋ฆ์ ๋ญ๊น์?![]({img_url})", "answer": "{name}"}
|
28 |
|
29 |
-
def get_question_answer(
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
q = QUESTION_TEMPLATE["question"].format(img_url=img_url)
|
36 |
-
a = QUESTION_TEMPLATE['answer'].format(name=name)
|
37 |
return q, a
|
38 |
|
39 |
info = {u: {"done" : True, "score": 0, "count": 0, "best_score": 0, "best_time": float("inf"), "time": 0.0} for u in USERS}
|
40 |
|
41 |
-
MD = """#
|
42 |
-
## ๊ณต๋ถ ๋ฐฉ๋ฒ
|
43 |
-
|
44 |
-
์๋ ํฌ์ผ๋ชฌ ๋๊ฐ์ ๋ณด๊ณ ๊ณต๋ถํ๋ฉด ๋์์ด ๋ฉ๋๋ค.
|
45 |
-
- https://huggingface.co/spaces/yoon-gu/pokemon
|
46 |
-
|
47 |
## ์ฌ์ฉ๋ฐฉ๋ฒ
|
48 |
|
49 |
1. ์ฌ์ฉ์๋ฅผ ์ ํํ์ธ์.
|
50 |
2. ์ด ํด์ฆ ๊ฐ์๋ฅผ ์ ํํ์ธ์.
|
51 |
-
3. ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ์ธ์.
|
52 |
-
4. ํฌ์ผ๋ชฌ ํ์
์ ์ ํํ์ธ์.
|
53 |
|
54 |
## ์ ์ํ
|
55 |
{content}
|
@@ -63,18 +37,6 @@ with gr.Blocks() as demo:
|
|
63 |
user = gr.Radio(USERS, value="Dummy", label="์ฌ์ฉ์", info="๋น์ ์ ๋๊ตฌ์ ๊ฐ์?")
|
64 |
quiz_count = gr.Radio([10, 20, 30], value=10, label="์ด ํด์ฆ ๊ฐ์", info="ํด์ฆ๋ฅผ ๋ช ๊ฐ ํ ์์ ์ธ๊ฐ์?")
|
65 |
with gr.Column():
|
66 |
-
with gr.Row():
|
67 |
-
generation = gr.Dropdown(
|
68 |
-
[f"{k}์ธ๋" for k in range(1, 10)] + ["๋ชจ๋ ์ธ๋"],
|
69 |
-
value="๋ชจ๋ ์ธ๋",
|
70 |
-
label="ํฌ์ผ๋ชฌ ์ธ๋",
|
71 |
-
info="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ์ธ์."
|
72 |
-
)
|
73 |
-
poke_types = gr.Dropdown(
|
74 |
-
pokemons_types, value="๋ชจ๋ ํ์
",
|
75 |
-
label="ํฌ์ผ๋ชฌ ํ์
",
|
76 |
-
info="์ํ๋ ํฌ์ผ๋ชฌ ํ์
์ ์ ํํ์ธ์."
|
77 |
-
)
|
78 |
with gr.Row():
|
79 |
play = gr.Button(value="ํด์ฆ ์์", label="ํด์ฆ ์์")
|
80 |
skip = gr.Button(value="๋ฌธ์ ๋์ด๊ฐ๊ธฐ", label="๋ฌธ์ ์คํต")
|
@@ -84,17 +46,13 @@ with gr.Blocks() as demo:
|
|
84 |
"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0155_๋ธ์ผ์ธ.png"])
|
85 |
msg = gr.Textbox(placeholder="๋ฌธ์ ์ ๋ต์ ์
๋ ฅํ์ธ์.", label="๋ต")
|
86 |
|
87 |
-
def respond(message, chat_history, user, quiz_count,
|
88 |
message = message.strip()
|
89 |
done = info[user]['done']
|
90 |
-
start, end = GEN_RANGE[gen]
|
91 |
-
sdf = df[start:end]
|
92 |
-
pokemons_set = sdf[sdf['types'].apply(lambda x: (types in x)) | (types == "๋ชจ๋ ํ์
")]
|
93 |
-
pokemons_set = pokemons_set.to_dict("records")
|
94 |
if done:
|
95 |
if "ํด์ฆ์์" == message.replace(" ", ""):
|
96 |
-
q, a = get_question_answer(
|
97 |
-
bot_message = f"ํด์ฆ๋ฅผ
|
98 |
answer.value = a
|
99 |
info[user]['done'] = False
|
100 |
info[user]['score'] = 0
|
@@ -104,31 +62,22 @@ with gr.Blocks() as demo:
|
|
104 |
bot_message = "ํด์ฆ๋ฅผ ์์ํ๊ณ ์ถ์ผ์๋ฉด, **ํด์ฆ ์์** ๋ฒํผ์ ๋๋ฅด์ธ์."
|
105 |
else:
|
106 |
if answer.value == message:
|
107 |
-
q, a = get_question_answer(
|
108 |
answer.value = a
|
109 |
info[user]['score'] += 1
|
110 |
info[user]['count'] += 1
|
111 |
-
bot_message = f"๐์ ๋ต์
๋๋ค! ๋ค์ ๋ฌธ์ ์
๋๋ค.\n
|
112 |
|
113 |
elif "ํด์ฆ์ข
๋ฃ" == message.replace(" ", ""):
|
114 |
bot_message = f"ํด์ฆ๋ฅผ ๊ฐ์ ์ข
๋ฃํฉ๋๋ค."
|
115 |
info[user]['done'] = True
|
116 |
elif "๋ฌธ์ ๋์ด๊ฐ๊ธฐ" == message:
|
117 |
info[user]['count'] += 1
|
118 |
-
q, a = get_question_answer(
|
119 |
answer.value = a
|
120 |
-
bot_message = f"๋ฌธ์ ๋ฅผ ๋์ด๊ฐ๋๋ค.
|
121 |
else:
|
122 |
-
|
123 |
-
for i, y in enumerate(answer.value):
|
124 |
-
if i < len(message):
|
125 |
-
if message[i] == y:
|
126 |
-
hint1 += y
|
127 |
-
else:
|
128 |
-
hint1 += "X"
|
129 |
-
else:
|
130 |
-
hint1 += "X"
|
131 |
-
bot_message = f"***{message}***!? ๐ง ๋ค์ ํ๋ฒ ์๊ฐํด๋ณด์ธ์.\nํํธ: {hint1}"
|
132 |
info[user]['score'] -= 0.1
|
133 |
|
134 |
if quiz_count == info[user]['count']:
|
@@ -147,13 +96,13 @@ with gr.Blocks() as demo:
|
|
147 |
return "", chat_history, MD.format(content=md)
|
148 |
|
149 |
play.click(respond,
|
150 |
-
inputs=[play, chatbot, user, quiz_count
|
151 |
outputs=[msg, chatbot, markdown])
|
152 |
skip.click(respond,
|
153 |
-
inputs=[skip, chatbot, user, quiz_count
|
154 |
outputs=[msg, chatbot, markdown])
|
155 |
stop.click(respond,
|
156 |
-
inputs=[stop, chatbot, user, quiz_count
|
157 |
outputs=[msg, chatbot, markdown])
|
158 |
def update_table():
|
159 |
leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True)
|
@@ -164,7 +113,7 @@ with gr.Blocks() as demo:
|
|
164 |
demo.load(update_table,
|
165 |
inputs=None,
|
166 |
outputs=markdown)
|
167 |
-
msg.submit(respond, [msg, chatbot, user, quiz_count
|
168 |
|
169 |
demo.queue(concurrency_count=1)
|
170 |
demo.launch()
|
|
|
6 |
import json
|
7 |
import time
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
USERS = ["June", "Sean", "Woojoo", "Taejoo", "Dummy"]
|
10 |
QUESTION_TEMPLATE = {"question": "๋ค์ ํฌ์ผ๋ชฌ์ ์ด๋ฆ์ ๋ญ๊น์?![]({img_url})", "answer": "{name}"}
|
11 |
|
12 |
+
def get_question_answer():
|
13 |
+
a = random.randint(0, 10)
|
14 |
+
b = random.randint(0, 10)
|
15 |
+
c = a + b
|
16 |
+
q = f"$${a} + {b} = \square$$"
|
17 |
+
a = f"{c}"
|
|
|
|
|
18 |
return q, a
|
19 |
|
20 |
info = {u: {"done" : True, "score": 0, "count": 0, "best_score": 0, "best_time": float("inf"), "time": 0.0} for u in USERS}
|
21 |
|
22 |
+
MD = """# ์ํ ํด์ฆ
|
|
|
|
|
|
|
|
|
|
|
23 |
## ์ฌ์ฉ๋ฐฉ๋ฒ
|
24 |
|
25 |
1. ์ฌ์ฉ์๋ฅผ ์ ํํ์ธ์.
|
26 |
2. ์ด ํด์ฆ ๊ฐ์๋ฅผ ์ ํํ์ธ์.
|
|
|
|
|
27 |
|
28 |
## ์ ์ํ
|
29 |
{content}
|
|
|
37 |
user = gr.Radio(USERS, value="Dummy", label="์ฌ์ฉ์", info="๋น์ ์ ๋๊ตฌ์ ๊ฐ์?")
|
38 |
quiz_count = gr.Radio([10, 20, 30], value=10, label="์ด ํด์ฆ ๊ฐ์", info="ํด์ฆ๋ฅผ ๋ช ๊ฐ ํ ์์ ์ธ๊ฐ์?")
|
39 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Row():
|
41 |
play = gr.Button(value="ํด์ฆ ์์", label="ํด์ฆ ์์")
|
42 |
skip = gr.Button(value="๋ฌธ์ ๋์ด๊ฐ๊ธฐ", label="๋ฌธ์ ์คํต")
|
|
|
46 |
"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/images/No_0155_๋ธ์ผ์ธ.png"])
|
47 |
msg = gr.Textbox(placeholder="๋ฌธ์ ์ ๋ต์ ์
๋ ฅํ์ธ์.", label="๋ต")
|
48 |
|
49 |
+
def respond(message, chat_history, user, quiz_count, request: gr.Request):
|
50 |
message = message.strip()
|
51 |
done = info[user]['done']
|
|
|
|
|
|
|
|
|
52 |
if done:
|
53 |
if "ํด์ฆ์์" == message.replace(" ", ""):
|
54 |
+
q, a = get_question_answer()
|
55 |
+
bot_message = f"ํด์ฆ๋ฅผ ์์ํฉ๋๋ค. ๋น์นธ์ ๋ค์ด๊ฐ ์ซ์๋ฅผ ๋ง์ถฐ์ฃผ์ธ์.\n{q}"
|
56 |
answer.value = a
|
57 |
info[user]['done'] = False
|
58 |
info[user]['score'] = 0
|
|
|
62 |
bot_message = "ํด์ฆ๋ฅผ ์์ํ๊ณ ์ถ์ผ์๋ฉด, **ํด์ฆ ์์** ๋ฒํผ์ ๋๋ฅด์ธ์."
|
63 |
else:
|
64 |
if answer.value == message:
|
65 |
+
q, a = get_question_answer()
|
66 |
answer.value = a
|
67 |
info[user]['score'] += 1
|
68 |
info[user]['count'] += 1
|
69 |
+
bot_message = f"๐์ ๋ต์
๋๋ค! ๋ค์ ๋ฌธ์ ์
๋๋ค.\n- ํ์ฌ ์ ์: {info[user]['score']}์ \n- ์์ ์๊ฐ: {time.time() - info[user]['time']:4.3f}์ด\n. ๋น์นธ์ ๋ค์ด๊ฐ ์ซ์๋ฅผ ๋ง๏ฟฝ๏ฟฝ๏ฟฝ์ฃผ์ธ์.{q}"
|
70 |
|
71 |
elif "ํด์ฆ์ข
๋ฃ" == message.replace(" ", ""):
|
72 |
bot_message = f"ํด์ฆ๋ฅผ ๊ฐ์ ์ข
๋ฃํฉ๋๋ค."
|
73 |
info[user]['done'] = True
|
74 |
elif "๋ฌธ์ ๋์ด๊ฐ๊ธฐ" == message:
|
75 |
info[user]['count'] += 1
|
76 |
+
q, a = get_question_answer()
|
77 |
answer.value = a
|
78 |
+
bot_message = f"๋ฌธ์ ๋ฅผ ๋์ด๊ฐ๋๋ค. ๋ค์ ๋ฌธ์ ์
๋๋ค.\n{q}"
|
79 |
else:
|
80 |
+
bot_message = f"***{message}***!? ๐ง ๋ค์ ํ๋ฒ ์๊ฐํด๋ณด์ธ์."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
info[user]['score'] -= 0.1
|
82 |
|
83 |
if quiz_count == info[user]['count']:
|
|
|
96 |
return "", chat_history, MD.format(content=md)
|
97 |
|
98 |
play.click(respond,
|
99 |
+
inputs=[play, chatbot, user, quiz_count],
|
100 |
outputs=[msg, chatbot, markdown])
|
101 |
skip.click(respond,
|
102 |
+
inputs=[skip, chatbot, user, quiz_count],
|
103 |
outputs=[msg, chatbot, markdown])
|
104 |
stop.click(respond,
|
105 |
+
inputs=[stop, chatbot, user, quiz_count],
|
106 |
outputs=[msg, chatbot, markdown])
|
107 |
def update_table():
|
108 |
leader_board = sorted(info.items(), key=lambda x: (x[1]['best_score'], -x[1]['best_time']), reverse=True)
|
|
|
113 |
demo.load(update_table,
|
114 |
inputs=None,
|
115 |
outputs=markdown)
|
116 |
+
msg.submit(respond, [msg, chatbot, user, quiz_count], [msg, chatbot, markdown])
|
117 |
|
118 |
demo.queue(concurrency_count=1)
|
119 |
demo.launch()
|