yoon-gu commited on
Commit
59c23dd
β€’
1 Parent(s): 4f69797

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -12
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="1μ„ΈλŒ€", label="포켓λͺ¬ μ„ΈλŒ€", info="μ›ν•˜λŠ” 포켓λͺ¬ μ„ΈλŒ€λ₯Ό μ„ νƒν•˜μ„Έμš”."
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, quiz_start])
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 not quiz_start.value:
64
  if "ν€΄μ¦ˆ μ‹œμž‘" == message:
65
  q, a = get_question_answer(pokemons_set)
66
  bot_message = f"ν€΄μ¦ˆλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€.\n{q}"
67
  answer.value = a
68
- quiz_start.value = True
 
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
- return "", chat_history
 
81
 
82
- msg.submit(respond, [generation, poke_types, msg, chatbot], [msg, chatbot])
83
 
84
- demo.queue(concurrency_count=3)
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()