Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from pyjosa.josa import Josa
|
4 |
import random
|
5 |
import json
|
6 |
|
7 |
with open('pokemon.json', 'r') as f:
|
8 |
pokemons = json.load(f)
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
with gr.Blocks() as demo:
|
12 |
quiz_start = gr.State(value=False)
|
|
|
13 |
answer = gr.State(value="")
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
if not quiz_start.value:
|
22 |
-
chosen = random.choice(pokemons)
|
23 |
-
name = chosen['name']
|
24 |
-
image_path = chosen['image_path']
|
25 |
-
answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
|
26 |
-
img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
|
27 |
if "ν΄μ¦ μμ" == message:
|
28 |
-
|
|
|
|
|
29 |
quiz_start.value = True
|
30 |
else:
|
31 |
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ**μ΄λΌκ³ λ§μν΄μ£ΌμΈμ."
|
32 |
else:
|
33 |
if answer.value == message:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
|
38 |
-
img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
|
39 |
-
bot_message = "πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€." + QUESTION_TEMPLATE["question"].format(img_url=img_url)
|
40 |
else:
|
41 |
-
|
42 |
-
|
43 |
-
except pyjosa.exceptions.NotHangleException:
|
44 |
-
trial = f"{message}!?"
|
45 |
-
bot_message = f"***{trial[:-1]}***{trial[-1]} μ λ΅μΌκΉμ? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ."
|
46 |
-
|
47 |
chat_history.append((message, bot_message))
|
48 |
return "", chat_history
|
49 |
|
50 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
51 |
|
52 |
demo.queue(concurrency_count=3)
|
53 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
|
|
3 |
import random
|
4 |
import json
|
5 |
|
6 |
with open('pokemon.json', 'r') as f:
|
7 |
pokemons = json.load(f)
|
8 |
+
pokemons_types = ["λͺ¨λ νμ
"] + sorted(set([t for poke in pokemons for t in poke['types']]))
|
9 |
+
df = pd.DataFrame(pokemons)
|
10 |
+
GEN_RANGE = {
|
11 |
+
"λͺ¨λ μΈλ": [1, 1017],
|
12 |
+
"1μΈλ": [1, 151],
|
13 |
+
"2μΈλ": [152, 251],
|
14 |
+
"3μΈλ": [252, 386],
|
15 |
+
"4μΈλ": [387, 493],
|
16 |
+
"5μΈλ": [494, 649],
|
17 |
+
"6μΈλ": [650, 721],
|
18 |
+
"7μΈλ": [722, 809],
|
19 |
+
"8μΈλ": [810, 905],
|
20 |
+
"9μΈλ": [906, 1017]
|
21 |
+
}
|
22 |
+
|
23 |
QUESTION_TEMPLATE = {"question": "λ€μ ν¬μΌλͺ¬μ μ΄λ¦μ λκΉμ?![]({img_url})", "answer": "{name}"}
|
24 |
+
|
25 |
+
def get_question_answer(pokemons_set):
|
26 |
+
chosen = random.choice(pokemons_set)
|
27 |
+
name = chosen['name']
|
28 |
+
image_path = chosen['image_path']
|
29 |
+
answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
|
30 |
+
img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
|
31 |
+
q = QUESTION_TEMPLATE["question"].format(img_url=img_url)
|
32 |
+
a = QUESTION_TEMPLATE['answer'].format(name=name)
|
33 |
+
return q, a
|
34 |
+
|
35 |
+
MD = """# ν¬μΌλͺ¬ ν΄μ¦
|
36 |
+
"""
|
37 |
with gr.Blocks() as demo:
|
38 |
quiz_start = gr.State(value=False)
|
39 |
+
score = gr.State(value=0)
|
40 |
answer = gr.State(value="")
|
41 |
+
with gr.Row():
|
42 |
+
with gr.Column():
|
43 |
+
gr.Markdown(MD)
|
44 |
+
with gr.Column():
|
45 |
+
with gr.Row():
|
46 |
+
generation = gr.Dropdown(
|
47 |
+
[f"{k}μΈλ" for k in range(1, 10)] + ["λͺ¨λ μΈλ"], value="1μΈλ", label="ν¬μΌλͺ¬ μΈλ", info="μνλ ν¬μΌλͺ¬ μΈλλ₯Ό μ ννμΈμ."
|
48 |
+
)
|
49 |
+
poke_types = gr.Dropdown(
|
50 |
+
pokemons_types, value="λͺ¨λ νμ
", label="ν¬μΌλͺ¬ νμ
", info="μνλ ν¬μΌλͺ¬ νμ
μ μ ννμΈμ."
|
51 |
+
)
|
52 |
+
chatbot = gr.Chatbot(bubble_full_width=False)
|
53 |
+
msg = gr.Textbox(value="ν΄μ¦ μμ")
|
54 |
+
clear = gr.ClearButton([msg, chatbot, quiz_start])
|
55 |
+
|
56 |
+
def respond(gen, types, message, chat_history, request: gr.Request):
|
57 |
+
start, end = GEN_RANGE[gen]
|
58 |
+
sdf = df[start:end]
|
59 |
+
pokemons_set = sdf[sdf['types'].apply(lambda x: (types in x)) | (types == "λͺ¨λ νμ
")]
|
60 |
+
pokemons_set = pokemons_set.to_dict("records")
|
61 |
if not quiz_start.value:
|
|
|
|
|
|
|
|
|
|
|
62 |
if "ν΄μ¦ μμ" == message:
|
63 |
+
q, a = get_question_answer(pokemons_set)
|
64 |
+
bot_message = f"ν΄μ¦λ₯Ό μμν©λλ€.\n{q}"
|
65 |
+
answer.value = a
|
66 |
quiz_start.value = True
|
67 |
else:
|
68 |
bot_message = "ν΄μ¦λ₯Ό μμνκ³ μΆμΌμλ©΄, **ν΄μ¦ μμ**μ΄λΌκ³ λ§μν΄μ£ΌμΈμ."
|
69 |
else:
|
70 |
if answer.value == message:
|
71 |
+
q, a = get_question_answer(pokemons_set)
|
72 |
+
answer.value = a
|
73 |
+
bot_message = f"πμ λ΅μ
λλ€! λ€μ λ¬Έμ μ
λλ€.\n{q}"
|
|
|
|
|
|
|
74 |
else:
|
75 |
+
bot_message = f"***{message}***!? π§ λ€μ νλ² μκ°ν΄λ³΄μΈμ."
|
76 |
+
|
|
|
|
|
|
|
|
|
77 |
chat_history.append((message, bot_message))
|
78 |
return "", chat_history
|
79 |
|
80 |
+
msg.submit(respond, [generation, poke_types, msg, chatbot], [msg, chatbot])
|
81 |
|
82 |
demo.queue(concurrency_count=3)
|
83 |
demo.launch()
|