Update app.py
Browse files
app.py
CHANGED
@@ -22,6 +22,7 @@ GEN_RANGE = {
|
|
22 |
"9μΈλ": [906, 1017]
|
23 |
}
|
24 |
|
|
|
25 |
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"}
|
26 |
|
27 |
def get_question_answer(pokemons_set):
|
@@ -34,52 +35,70 @@ def get_question_answer(pokemons_set):
|
|
34 |
a = QUESTION_TEMPLATE['answer'].format(name=name)
|
35 |
return q, a
|
36 |
|
|
|
37 |
MD = """# ν¬μΌλͺ¬ ν΄μ¦
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
"""
|
39 |
with gr.Blocks() as demo:
|
40 |
-
quiz_start = gr.State(value=False)
|
41 |
-
score = gr.State(value=0)
|
42 |
answer = gr.State(value="")
|
43 |
with gr.Row():
|
44 |
with gr.Column():
|
45 |
-
gr.Markdown(MD)
|
|
|
|
|
46 |
with gr.Column():
|
47 |
with gr.Row():
|
48 |
generation = gr.Dropdown(
|
49 |
-
[f"{k}μΈλ" for k in range(1, 10)] + ["λͺ¨λ μΈλ"], value="
|
50 |
)
|
51 |
poke_types = gr.Dropdown(
|
52 |
pokemons_types, value="λͺ¨λ νμ
", label="ν¬μΌλͺ¬ νμ
", info="μνλ ν¬μΌλͺ¬ νμ
μ μ ννμΈμ."
|
53 |
)
|
54 |
chatbot = gr.Chatbot(bubble_full_width=False)
|
55 |
-
msg = gr.Textbox(value="ν΄μ¦ μμ")
|
56 |
-
clear = gr.ClearButton([msg, chatbot
|
57 |
|
58 |
-
def respond(gen, types, message, chat_history, request: gr.Request):
|
|
|
59 |
start, end = GEN_RANGE[gen]
|
60 |
sdf = df[start:end]
|
61 |
pokemons_set = sdf[sdf['types'].apply(lambda x: (types in x)) | (types == "λͺ¨λ νμ
")]
|
62 |
pokemons_set = pokemons_set.to_dict("records")
|
63 |
-
if
|
64 |
if "ν΄μ¦ μμ" == message:
|
65 |
q, a = get_question_answer(pokemons_set)
|
66 |
bot_message = f"ν΄μ¦λ₯Ό μμν©λλ€.\n{q}"
|
67 |
answer.value = a
|
68 |
-
|
|
|
69 |
else:
|
70 |
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ**μ΄λΌκ³ λ§μν΄μ£ΌμΈμ."
|
71 |
else:
|
72 |
if answer.value == message:
|
73 |
q, a = get_question_answer(pokemons_set)
|
74 |
answer.value = a
|
|
|
|
|
75 |
bot_message = f"πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€.\n{q}"
|
|
|
|
|
76 |
else:
|
77 |
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ."
|
|
|
78 |
|
79 |
chat_history.append((message, bot_message))
|
80 |
-
|
|
|
81 |
|
82 |
-
msg.submit(respond, [generation, poke_types, msg, chatbot], [msg, chatbot])
|
83 |
|
84 |
-
demo.queue(concurrency_count=
|
85 |
demo.launch()
|
|
|
22 |
"9μΈλ": [906, 1017]
|
23 |
}
|
24 |
|
25 |
+
USERS = ["June", "Sean", "Woojoo", "Taejoo", "Dummy"]
|
26 |
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"}
|
27 |
|
28 |
def get_question_answer(pokemons_set):
|
|
|
35 |
a = QUESTION_TEMPLATE['answer'].format(name=name)
|
36 |
return q, a
|
37 |
|
38 |
+
info = {u: {"done" : True, "score": 0, "count": 0} for u in USERS}
|
39 |
MD = """# ν¬μΌλͺ¬ ν΄μ¦
|
40 |
+
## μ¬μ©λ°©λ²
|
41 |
+
|
42 |
+
1. μ¬μ©μλ₯Ό μ ννμΈμ.
|
43 |
+
2. μ΄ ν΄μ¦ κ°μλ₯Ό μ ννμΈμ.
|
44 |
+
3. ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ.
|
45 |
+
4. ν¬μΌλͺ¬ νμ
μ μ ννμΈμ.
|
46 |
+
|
47 |
+
## μ μν
|
48 |
+
{content}
|
49 |
"""
|
50 |
with gr.Blocks() as demo:
|
|
|
|
|
51 |
answer = gr.State(value="")
|
52 |
with gr.Row():
|
53 |
with gr.Column():
|
54 |
+
markdown = gr.Markdown(MD.format(content='\n'.join([f"- {u}({info[u]['score']}μ )" for u in USERS])))
|
55 |
+
user = gr.Radio(USERS, value="Dummy", label="μ¬μ©μ", info="λΉμ μ λꡬμ κ°μ?")
|
56 |
+
quiz_count = gr.Radio([10, 20, 30], value=10, label="μ΄ ν΄μ¦ κ°μ", info="ν΄μ¦λ₯Ό λͺ κ° ν μμ μΈκ°μ?")
|
57 |
with gr.Column():
|
58 |
with gr.Row():
|
59 |
generation = gr.Dropdown(
|
60 |
+
[f"{k}μΈλ" for k in range(1, 10)] + ["λͺ¨λ μΈλ"], value="λͺ¨λ μΈλ", label="ν¬μΌλͺ¬ μΈλ", info="μνλ ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ."
|
61 |
)
|
62 |
poke_types = gr.Dropdown(
|
63 |
pokemons_types, value="λͺ¨λ νμ
", label="ν¬μΌλͺ¬ νμ
", info="μνλ ν¬μΌλͺ¬ νμ
μ μ ννμΈμ."
|
64 |
)
|
65 |
chatbot = gr.Chatbot(bubble_full_width=False)
|
66 |
+
msg = gr.Textbox(value="ν΄μ¦ μμ", label="λ΅")
|
67 |
+
clear = gr.ClearButton([msg, chatbot])
|
68 |
|
69 |
+
def respond(user, quiz_count, gen, types, message, chat_history, request: gr.Request):
|
70 |
+
done = info[user]['done']
|
71 |
start, end = GEN_RANGE[gen]
|
72 |
sdf = df[start:end]
|
73 |
pokemons_set = sdf[sdf['types'].apply(lambda x: (types in x)) | (types == "λͺ¨λ νμ
")]
|
74 |
pokemons_set = pokemons_set.to_dict("records")
|
75 |
+
if done:
|
76 |
if "ν΄μ¦ μμ" == message:
|
77 |
q, a = get_question_answer(pokemons_set)
|
78 |
bot_message = f"ν΄μ¦λ₯Ό μμν©λλ€.\n{q}"
|
79 |
answer.value = a
|
80 |
+
info[user]['done'] = False
|
81 |
+
info[user]['count'] = 0
|
82 |
else:
|
83 |
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ**μ΄λΌκ³ λ§μν΄μ£ΌμΈμ."
|
84 |
else:
|
85 |
if answer.value == message:
|
86 |
q, a = get_question_answer(pokemons_set)
|
87 |
answer.value = a
|
88 |
+
info[user]['score'] += 1
|
89 |
+
info[user]['count'] += 1
|
90 |
bot_message = f"πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€.\n{q}"
|
91 |
+
if quiz_count == info[user]['count']:
|
92 |
+
bot_message = f"λͺ¨λ ν΄μ¦λ₯Ό λ€ νμμ΅λλ€. μ μλ {info[user]['score']}μ μ
λλ€."
|
93 |
else:
|
94 |
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ."
|
95 |
+
info[user]['score'] = -0.1
|
96 |
|
97 |
chat_history.append((message, bot_message))
|
98 |
+
print(info)
|
99 |
+
return "", chat_history, MD.format(content='\n'.join([f"{u}({info[u]['score']}μ )" for u in USERS]))
|
100 |
|
101 |
+
msg.submit(respond, [user, quiz_count, generation, poke_types, msg, chatbot], [msg, chatbot, markdown])
|
102 |
|
103 |
+
demo.queue(concurrency_count=1)
|
104 |
demo.launch()
|