yoon-gu commited on
Commit
9227ae5
β€’
1 Parent(s): 85a30f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -29
app.py CHANGED
@@ -1,53 +1,83 @@
1
  import gradio as gr
2
- import pyjosa
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
- chatbot = gr.Chatbot(bubble_full_width=False)
15
- msg = gr.Textbox(value="ν€΄μ¦ˆ μ‹œμž‘")
16
- clear = gr.ClearButton([msg, chatbot])
17
-
18
-
19
- def respond(message, chat_history, request: gr.Request):
20
- print(request.client.host)
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- bot_message = "ν€΄μ¦ˆλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€.\n" + QUESTION_TEMPLATE["question"].format(img_url=img_url)
 
 
29
  quiz_start.value = True
30
  else:
31
  bot_message = "ν€΄μ¦ˆλ₯Ό μ‹œμž‘ν•˜κ³  μ‹ΆμœΌμ‹œλ©΄, **ν€΄μ¦ˆ μ‹œμž‘**이라고 λ§μ”€ν•΄μ£Όμ„Έμš”."
32
  else:
33
  if answer.value == message:
34
- chosen = random.choice(pokemons)
35
- name = chosen['name']
36
- image_path = chosen['image_path']
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
- try:
42
- trial = Josa.get_full_string(message, "λŠ”")
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()