MakiAi commited on
Commit
7a40e75
·
verified ·
1 Parent(s): 154f42c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -12
app.py CHANGED
@@ -25,7 +25,7 @@ def create_zen_theme():
25
  ]
26
  ).set(
27
  body_background_fill="#f8f9fa",
28
- body_text_color="#2d3436", # 元に戻す
29
  button_primary_background_fill="#636e72",
30
  button_primary_background_fill_hover="#2d3436",
31
  button_primary_text_color="#ffffff",
@@ -54,12 +54,20 @@ def create_comprehensive_demo():
54
  })
55
 
56
  # 関数定義
57
- def greet(name, mood, age, subscribe):
58
  if not name:
59
  return "名前を入力してください。"
60
 
61
  age_msg = f"({age}歳)" if age else ""
62
  subscribe_msg = "ニュースレターの購読ありがとうございます!" if subscribe else ""
 
 
 
 
 
 
 
 
63
 
64
  greetings = {
65
  "😊 嬉しい": f"{name}さん{age_msg}、今日はとても良い日ですね!✨",
@@ -67,6 +75,9 @@ def create_comprehensive_demo():
67
  "😴 疲れた": f"{name}さん{age_msg}、お疲れ様です。少し休んでくださいね。🍵"
68
  }
69
  result = greetings.get(mood, f"こんにちは、{name}さん{age_msg}。")
 
 
 
70
  if subscribe_msg:
71
  result += f"\n{subscribe_msg}"
72
  return result
@@ -107,6 +118,43 @@ def create_comprehensive_demo():
107
  history.append([message, response])
108
  return history, ""
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  with gr.Blocks(theme=theme, title="ZEN") as demo:
111
 
112
  # ヘッダー
@@ -152,13 +200,15 @@ def create_comprehensive_demo():
152
  hobbies = gr.CheckboxGroup(
153
  choices=["🎵 音楽", "📚 読書", "🎨 アート", "🏃‍♂️ スポーツ", "🍳 料理", "🌱 ガーデニング"],
154
  label="趣味(複数選択可)",
155
- value=["📚 読書"]
 
156
  )
157
 
158
  prefecture = gr.Dropdown(
159
  choices=["北海道", "東京都", "大阪府", "愛知県", "福岡県", "沖縄県"],
160
  label="🗾 都道府県",
161
- value="東京都"
 
162
  )
163
 
164
  satisfaction = gr.Slider(
@@ -166,7 +216,30 @@ def create_comprehensive_demo():
166
  maximum=10,
167
  value=8,
168
  step=1,
169
- label="😊 満足度(1-10)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  )
171
 
172
  with gr.Row():
@@ -179,16 +252,46 @@ def create_comprehensive_demo():
179
  lines=4
180
  )
181
 
 
182
  submit_btn.click(
183
  fn=greet,
184
- inputs=[name_input, mood_selector, age_input, subscribe_check],
185
  outputs=result_output
186
  )
187
 
 
188
  clear_btn.click(
189
- lambda: ("", 25, "😐 普通", False, [], "東京都", 8, ""),
 
190
  outputs=[name_input, age_input, mood_selector, subscribe_check,
191
- hobbies, prefecture, satisfaction, result_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  )
193
 
194
  # 俳句生成
@@ -354,21 +457,33 @@ def create_comprehensive_demo():
354
  outputs=[chatbot, chat_input]
355
  )
356
 
357
- # クイック返信
 
 
 
 
 
 
 
 
 
 
 
 
358
  quick1.click(
359
- lambda h: h + [["おすすめメニューは?", "抹茶ラテと和スイーツセットが人気です!季節限定の桜もちもおすすめです。"]],
360
  inputs=[chatbot],
361
  outputs=[chatbot]
362
  )
363
 
364
  quick2.click(
365
- lambda h: h + [["営業時間は?", "平日・土日祝日ともに9:00-21:00で営業しています。定休日は火曜日です。"]],
366
  inputs=[chatbot],
367
  outputs=[chatbot]
368
  )
369
 
370
  quick3.click(
371
- lambda h: h + [["アクセス方法は?", "JR山手線「表参道駅」から徒歩5分です。青山通り沿いの和風の建物が目印です。"]],
372
  inputs=[chatbot],
373
  outputs=[chatbot]
374
  )
 
25
  ]
26
  ).set(
27
  body_background_fill="#f8f9fa",
28
+ body_text_color="#2d3436",
29
  button_primary_background_fill="#636e72",
30
  button_primary_background_fill_hover="#2d3436",
31
  button_primary_text_color="#ffffff",
 
54
  })
55
 
56
  # 関数定義
57
+ def greet(name, mood, age, subscribe, hobbies_selected, prefecture_selected, satisfaction_level):
58
  if not name:
59
  return "名前を入力してください。"
60
 
61
  age_msg = f"({age}歳)" if age else ""
62
  subscribe_msg = "ニュースレターの購読ありがとうございます!" if subscribe else ""
63
+ hobbies_msg = f"趣味: {', '.join(hobbies_selected)}" if hobbies_selected else "趣味: 未選択"
64
+ prefecture_msg = f"出身: {prefecture_selected}"
65
+
66
+ # 満足度のNone値を処理
67
+ if satisfaction_level is None:
68
+ satisfaction_msg = "満足度: 未設定"
69
+ else:
70
+ satisfaction_msg = f"満足度: {int(satisfaction_level)}/10"
71
 
72
  greetings = {
73
  "😊 嬉しい": f"{name}さん{age_msg}、今日はとても良い日ですね!✨",
 
75
  "😴 疲れた": f"{name}さん{age_msg}、お疲れ様です。少し休んでくださいね。🍵"
76
  }
77
  result = greetings.get(mood, f"こんにちは、{name}さん{age_msg}。")
78
+ result += f"\n{hobbies_msg}"
79
+ result += f"\n{prefecture_msg}"
80
+ result += f"\n{satisfaction_msg}"
81
  if subscribe_msg:
82
  result += f"\n{subscribe_msg}"
83
  return result
 
118
  history.append([message, response])
119
  return history, ""
120
 
121
+ # クリア機能の修正
122
+ def clear_all_inputs():
123
+ return [
124
+ "", # name_input
125
+ 25, # age_input
126
+ "😐 普通", # mood_selector
127
+ False, # subscribe_check
128
+ [], # hobbies
129
+ "東京都", # prefecture
130
+ 8, # satisfaction
131
+ "", # hobbies_debug
132
+ "東京都, 満足度: 8", # status_debug
133
+ "8 (type: int)", # satisfaction_debug
134
+ "" # result_output
135
+ ]
136
+
137
+ # 趣味選択の変更を監視する関数
138
+ def update_hobbies_debug(selected_hobbies):
139
+ if selected_hobbies:
140
+ return ", ".join(selected_hobbies)
141
+ else:
142
+ return "未選択"
143
+
144
+ # 都道府県と満足度の変更を監視する関数
145
+ def update_status_debug(prefecture_val, satisfaction_val):
146
+ if satisfaction_val is None:
147
+ return f"{prefecture_val}, 満足度: 未設定"
148
+ else:
149
+ return f"{prefecture_val}, 満足度: {int(satisfaction_val)}"
150
+
151
+ # 満足度の変更を監視する関数(専用)
152
+ def update_satisfaction_debug(satisfaction_val):
153
+ if satisfaction_val is None:
154
+ return "None (未設定)"
155
+ else:
156
+ return f"{satisfaction_val} (type: {type(satisfaction_val).__name__})"
157
+
158
  with gr.Blocks(theme=theme, title="ZEN") as demo:
159
 
160
  # ヘッダー
 
200
  hobbies = gr.CheckboxGroup(
201
  choices=["🎵 音楽", "📚 読書", "🎨 アート", "🏃‍♂️ スポーツ", "🍳 料理", "🌱 ガーデニング"],
202
  label="趣味(複数選択可)",
203
+ value=["📚 読書"],
204
+ interactive=True
205
  )
206
 
207
  prefecture = gr.Dropdown(
208
  choices=["北海道", "東京都", "大阪府", "愛知県", "福岡県", "沖縄県"],
209
  label="🗾 都道府県",
210
+ value="東京都",
211
+ interactive=True
212
  )
213
 
214
  satisfaction = gr.Slider(
 
216
  maximum=10,
217
  value=8,
218
  step=1,
219
+ label="😊 満足度(1-10)",
220
+ interactive=True,
221
+ show_label=True
222
+ )
223
+
224
+ # デバッグ用:選択された趣味を表示
225
+ hobbies_debug = gr.Textbox(
226
+ label="���� 選択中の趣味(デバッグ用)",
227
+ interactive=False,
228
+ value="📚 読書"
229
+ )
230
+
231
+ # デバッグ用:都道府県と満足度を表示
232
+ status_debug = gr.Textbox(
233
+ label="🔍 都道府県・満足度(デバッグ用)",
234
+ interactive=False,
235
+ value="東京都, 満足度: 8"
236
+ )
237
+
238
+ # デバッグ用:満足度の生の値を表示
239
+ satisfaction_debug = gr.Textbox(
240
+ label="🔍 満足度の生の値(デバッグ用)",
241
+ interactive=False,
242
+ value="8"
243
  )
244
 
245
  with gr.Row():
 
252
  lines=4
253
  )
254
 
255
+ # イベントハンドラーの修正
256
  submit_btn.click(
257
  fn=greet,
258
+ inputs=[name_input, mood_selector, age_input, subscribe_check, hobbies, prefecture, satisfaction],
259
  outputs=result_output
260
  )
261
 
262
+ # クリアボタンの修正
263
  clear_btn.click(
264
+ fn=clear_all_inputs,
265
+ inputs=None,
266
  outputs=[name_input, age_input, mood_selector, subscribe_check,
267
+ hobbies, prefecture, satisfaction, hobbies_debug, status_debug, satisfaction_debug, result_output]
268
+ )
269
+
270
+ # 趣味選択の変更を監視
271
+ hobbies.change(
272
+ fn=update_hobbies_debug,
273
+ inputs=[hobbies],
274
+ outputs=[hobbies_debug]
275
+ )
276
+
277
+ # 都道府県と満足度の変更を監視
278
+ prefecture.change(
279
+ fn=update_status_debug,
280
+ inputs=[prefecture, satisfaction],
281
+ outputs=[status_debug]
282
+ )
283
+
284
+ satisfaction.change(
285
+ fn=update_status_debug,
286
+ inputs=[prefecture, satisfaction],
287
+ outputs=[status_debug]
288
+ )
289
+
290
+ # 満足度専用の監視
291
+ satisfaction.change(
292
+ fn=update_satisfaction_debug,
293
+ inputs=[satisfaction],
294
+ outputs=[satisfaction_debug]
295
  )
296
 
297
  # 俳句生成
 
457
  outputs=[chatbot, chat_input]
458
  )
459
 
460
+ # クイック返信の修正
461
+ def quick_reply_1(history):
462
+ new_history = history + [["おすすめメニューは?", "抹茶ラテと和スイーツセットが人気です!季節限定の桜もちもおすすめです。"]]
463
+ return new_history
464
+
465
+ def quick_reply_2(history):
466
+ new_history = history + [["営業時間は?", "平日・土日祝日ともに9:00-21:00で営業しています。定休日は火曜日です。"]]
467
+ return new_history
468
+
469
+ def quick_reply_3(history):
470
+ new_history = history + [["アクセス方法は?", "JR山手線「表参道駅」から徒歩5分です。青山通り沿いの和風の建物が目印です。"]]
471
+ return new_history
472
+
473
  quick1.click(
474
+ fn=quick_reply_1,
475
  inputs=[chatbot],
476
  outputs=[chatbot]
477
  )
478
 
479
  quick2.click(
480
+ fn=quick_reply_2,
481
  inputs=[chatbot],
482
  outputs=[chatbot]
483
  )
484
 
485
  quick3.click(
486
+ fn=quick_reply_3,
487
  inputs=[chatbot],
488
  outputs=[chatbot]
489
  )