PhilipL commited on
Commit
206dad0
·
verified ·
1 Parent(s): 8ea7fa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -77,7 +77,7 @@ class MockEmotion:
77
  def on_upload(img, st):
78
  return [None, gr.update(), gr.Tabs(), st]
79
 
80
- # 嘗試匯入 emotion 模組 (加入 Exception 捕捉,防止 FileNotFoundError 導致崩潰)
81
  try:
82
  import emotion
83
  print("✅ 成功載入 emotion 模組")
@@ -246,14 +246,15 @@ def generate_image_huggingface(prompt):
246
  )
247
  except: return None
248
 
249
- def mood_agent_logic(score_input, food_input, diary_input, debug_mode, show_prompt):
 
250
  if not rag_initialized:
251
- yield "⚠️ 請先同步地圖清單!", None, "", gr.update(), ""
252
  return
253
 
254
  restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input)
255
  if restaurant is None:
256
- yield "資料庫讀取錯誤", None, "", gr.update(), ""
257
  return
258
 
259
  name = restaurant['Name']; url = restaurant['URL']
@@ -273,16 +274,18 @@ def mood_agent_logic(score_input, food_input, diary_input, debug_mode, show_prom
273
  full_debug_log = groq_debug_log + f"\n\n### 🎨 Image Prompt Debug\n{img_prompt}"
274
  debug_output_update = gr.update(value=full_debug_log, visible=debug_mode)
275
 
276
- prompt_output_text = img_prompt if show_prompt else ""
277
  final_response = f"{prefix}### 🍽️ 推薦:{name}\n\n{ai_text}"
278
  map_html = f'<div style="text-align:center"><a href="{url}" target="_blank" style="background:#4CAF50;color:white;padding:8px 16px;border-radius:20px;text-decoration:none">🗺️ Google Map 導航</a></div>'
279
 
280
- yield final_response, None, map_html, debug_output_update, prompt_output_text
 
 
 
281
  image_output = generate_image_huggingface(img_prompt)
282
- yield final_response, image_output, map_html, debug_output_update, prompt_output_text
283
 
284
  # ==========================================
285
- # 4. 橋接邏輯 (Mock 支援)
286
  # ==========================================
287
  def _score_to_radio_value(score):
288
  mapping = {1: "1 (心情差)", 2: "2 (不太好)", 3: "3 (普通)", 4: "4 (不錯)", 5: "5 (超棒)"}
@@ -367,16 +370,17 @@ with gr.Blocks(title="AI 心情食堂", css=css_) as demo:
367
  score_input = gr.Radio(["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"], label="1. 心情分數 (由 Tab 1 自動填入)", value="3 (普通)")
368
  food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
369
  diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...")
370
- with gr.Row():
371
- show_prompt_chk = gr.Checkbox(label="顯示圖片 Prompt", value=False)
372
- debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式", value=False)
 
373
  submit_btn = gr.Button("🍱 送出給 Agent", variant="primary")
374
  debug_output = gr.Markdown(label="除錯資訊 (Debug Log)", visible=False)
375
 
376
  with gr.Column(scale=1):
377
  agent_output = gr.Markdown(label="AI 回應")
378
  image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
379
- prompt_output = gr.Textbox(label="圖片生成指令 (Prompt)", visible=True, interactive=False)
380
  map_output = gr.HTML(label="地圖導航")
381
 
382
  # 事件
@@ -386,7 +390,13 @@ with gr.Blocks(title="AI 心情食堂", css=css_) as demo:
386
  cam.stream(fn=bridge_predict_frame, inputs=[cam, st_state], outputs=[cam, result_markdown, st_state, btn_stop, btn_start, score_input, btn_go_dining])
387
  upload_img.change(fn=bridge_predict_upload, inputs=[upload_img, st_state], outputs=[result_markdown, score_input, tabs, st_state])
388
  btn_go_dining.click(fn=lambda: gr.Tabs(selected=1), inputs=None, outputs=tabs)
389
- submit_btn.click(fn=mood_agent_logic, inputs=[score_input, food_input, diary_input, debug_mode_btn, show_prompt_chk], outputs=[agent_output, image_output, map_output, debug_output, prompt_output])
 
 
 
 
 
 
390
 
391
  if __name__ == "__main__":
392
  demo.launch(ssr_mode=False)
 
77
  def on_upload(img, st):
78
  return [None, gr.update(), gr.Tabs(), st]
79
 
80
+ # 嘗試匯入 emotion 模組
81
  try:
82
  import emotion
83
  print("✅ 成功載入 emotion 模組")
 
246
  )
247
  except: return None
248
 
249
+ # 移除 show_prompt 參數
250
+ def mood_agent_logic(score_input, food_input, diary_input, debug_mode):
251
  if not rag_initialized:
252
+ yield "⚠️ 請先同步地圖清單!", None, "", gr.update()
253
  return
254
 
255
  restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input)
256
  if restaurant is None:
257
+ yield "資料庫讀取錯誤", None, "", gr.update()
258
  return
259
 
260
  name = restaurant['Name']; url = restaurant['URL']
 
274
  full_debug_log = groq_debug_log + f"\n\n### 🎨 Image Prompt Debug\n{img_prompt}"
275
  debug_output_update = gr.update(value=full_debug_log, visible=debug_mode)
276
 
 
277
  final_response = f"{prefix}### 🍽️ 推薦:{name}\n\n{ai_text}"
278
  map_html = f'<div style="text-align:center"><a href="{url}" target="_blank" style="background:#4CAF50;color:white;padding:8px 16px;border-radius:20px;text-decoration:none">🗺️ Google Map 導航</a></div>'
279
 
280
+ # 先回傳文字
281
+ yield final_response, None, map_html, debug_output_update
282
+
283
+ # 再回傳圖片
284
  image_output = generate_image_huggingface(img_prompt)
285
+ yield final_response, image_output, map_html, debug_output_update
286
 
287
  # ==========================================
288
+ # 4. 橋接邏輯
289
  # ==========================================
290
  def _score_to_radio_value(score):
291
  mapping = {1: "1 (心情差)", 2: "2 (不太好)", 3: "3 (普通)", 4: "4 (不錯)", 5: "5 (超棒)"}
 
370
  score_input = gr.Radio(["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"], label="1. 心情分數 (由 Tab 1 自動填入)", value="3 (普通)")
371
  food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
372
  diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...")
373
+
374
+ # [移除 show_prompt_chk]
375
+ debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式", value=False)
376
+
377
  submit_btn = gr.Button("🍱 送出給 Agent", variant="primary")
378
  debug_output = gr.Markdown(label="除錯資訊 (Debug Log)", visible=False)
379
 
380
  with gr.Column(scale=1):
381
  agent_output = gr.Markdown(label="AI 回應")
382
  image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
383
+ # [移除 prompt_output]
384
  map_output = gr.HTML(label="地圖導航")
385
 
386
  # 事件
 
390
  cam.stream(fn=bridge_predict_frame, inputs=[cam, st_state], outputs=[cam, result_markdown, st_state, btn_stop, btn_start, score_input, btn_go_dining])
391
  upload_img.change(fn=bridge_predict_upload, inputs=[upload_img, st_state], outputs=[result_markdown, score_input, tabs, st_state])
392
  btn_go_dining.click(fn=lambda: gr.Tabs(selected=1), inputs=None, outputs=tabs)
393
+
394
+ # 移除 show_prompt_chk 輸入 與 prompt_output 輸出
395
+ submit_btn.click(
396
+ fn=mood_agent_logic,
397
+ inputs=[score_input, food_input, diary_input, debug_mode_btn],
398
+ outputs=[agent_output, image_output, map_output, debug_output]
399
+ )
400
 
401
  if __name__ == "__main__":
402
  demo.launch(ssr_mode=False)