yoon-gu commited on
Commit
a0025dc
โ€ข
1 Parent(s): 21ef542

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -70
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(pokemons_set):
30
- chosen = random.choice(pokemons_set)
31
- name = chosen['name'].replace("โ™€", "์•”์ปท").replace("โ™‚", "์ˆ˜์ปท")
32
- image_path = chosen['image_path']
33
- answer.value = QUESTION_TEMPLATE['answer'].format(name=name)
34
- img_url = f"https://huggingface.co/spaces/yoon-gu/pokemon/resolve/main/{image_path}"
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, gen, types, request: gr.Request):
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(pokemons_set)
97
- bot_message = f"ํ€ด์ฆˆ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.\n{q}"
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(pokemons_set)
108
  answer.value = a
109
  info[user]['score'] += 1
110
  info[user]['count'] += 1
111
- bot_message = f"๐ŸŽ‰์ •๋‹ต์ž…๋‹ˆ๋‹ค! ๋‹ค์Œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.\n{q}\n- ํ˜„์žฌ ์ ์ˆ˜: {info[user]['score']}์ \n- ์†Œ์š” ์‹œ๊ฐ„: {time.time() - info[user]['time']:4.3f}์ดˆ"
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(pokemons_set)
119
  answer.value = a
120
- bot_message = f"๋ฌธ์ œ๋ฅผ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. ๋‹ค์Œ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.\n{q}"
121
  else:
122
- hint1 = ""
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, generation, poke_types],
151
  outputs=[msg, chatbot, markdown])
152
  skip.click(respond,
153
- inputs=[skip, chatbot, user, quiz_count, generation, poke_types],
154
  outputs=[msg, chatbot, markdown])
155
  stop.click(respond,
156
- inputs=[stop, chatbot, user, quiz_count, generation, poke_types],
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, generation, poke_types], [msg, chatbot, markdown])
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()