PhilipL commited on
Commit
ec036ac
·
verified ·
1 Parent(s): 22085ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -67
app.py CHANGED
@@ -267,55 +267,36 @@ def detect_emotion_and_map(frame):
267
  return frame, mapped_score
268
 
269
  # ==========================================
270
- # 5. Gradio 介面建構 (新增:鏡頭開關邏輯)
271
  # ==========================================
272
 
273
  with gr.Blocks(title="AI 心情食堂") as demo:
274
 
275
- # 紀錄鏡頭開關狀態的變數 (False = 關閉, True = 開啟)
276
- camera_state = gr.State(value=False)
277
-
278
  with gr.Column(visible=True) as main_app_col:
279
  gr.Markdown(f"## 🍱 AI 心情食堂導航")
280
- gr.Markdown("請開啟鏡頭,讓 AI 幫你判斷今天的心情分數!")
281
 
282
  with gr.Row():
283
  # 左側:輸入區
284
  with gr.Column(scale=1):
285
 
 
286
  gr.Markdown("### 📸 步驟 1:心情偵測 (選用)")
287
 
288
- # --- 修改開始:鏡頭開關與顯示區域 ---
289
- with gr.Group():
290
- # 1. 開關按鈕 (預設是綠色的開啟)
291
- toggle_cam_btn = gr.Button("🔌 啟用鏡頭", variant="primary")
292
 
293
- with gr.Row():
294
- # 2. 鏡頭元件 (預設隱藏 visible=False)
295
- webcam_input = gr.Image(
296
- sources=["webcam"],
297
- label="即時鏡頭",
298
- streaming=True,
299
- visible=False # 預設不顯示
300
- )
301
-
302
- # 3. 替代文字 (當鏡頭關閉時顯示這個)
303
- placeholder_text = gr.Markdown(
304
- "### 💤 鏡頭目前已關閉\n請點擊上方按鈕啟用",
305
- visible=True
306
- )
307
-
308
- # 4. 截圖結果顯示
309
- captured_image = gr.Image(label="偵測結果截圖", interactive=False)
310
- # --- 修改結束 ---
311
-
312
- # 偵測按鈕 (預設不能按,因為鏡頭沒開)
313
- detect_btn = gr.Button("📸 截圖並偵測心情", variant="secondary", interactive=False)
314
 
315
  gr.Markdown("### 📝 步驟 2:確認與補充")
316
  score_input = gr.Radio(
317
  ["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"],
318
- label="1. 心情分數",
319
  value="3 (普通)"
320
  )
321
  food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
@@ -324,52 +305,22 @@ with gr.Blocks(title="AI 心情食堂") as demo:
324
  debug_mode_btn = gr.Checkbox(label="🔧 顯示 Prompt 除錯資訊", value=False)
325
  submit_btn = gr.Button("🚀 送出給 Agent", variant="primary")
326
 
327
- # 右側:Agent 輸出區
328
  with gr.Column(scale=1):
329
  agent_output = gr.Markdown(label="AI 回應")
330
  image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
331
  map_output = gr.HTML(label="地圖導航")
332
 
333
- # ==========================================
334
- # 邏輯處理函式
335
- # ==========================================
336
-
337
- # 定義:切換鏡頭的函式
338
- def toggle_camera_logic(is_on):
339
- if not is_on:
340
- # 如果原本是關的 -> 要打開
341
- return (
342
- True, # 更新狀態為 True
343
- gr.update(visible=True), # 顯示鏡頭
344
- gr.update(visible=False), # 隱藏替代文字
345
- gr.update(value="🚫 關閉鏡頭", variant="stop"), # 按鈕變紅色
346
- gr.update(interactive=True) # 讓偵測按鈕可以按
347
- )
348
- else:
349
- # 如果原本是開的 -> 要關掉
350
- return (
351
- False, # 更新狀態為 False
352
- gr.update(value=None, visible=False), # 隱藏鏡頭並清空畫面
353
- gr.update(visible=True), # 顯示替代文字
354
- gr.update(value="🔌 啟用鏡頭", variant="primary"), # 按鈕變綠色
355
- gr.update(interactive=False) # 鎖住偵測按鈕
356
- )
357
-
358
- # 事件 1: 點擊開關按鈕 -> 觸發切換邏輯
359
- toggle_cam_btn.click(
360
- fn=toggle_camera_logic,
361
- inputs=[camera_state],
362
- outputs=[camera_state, webcam_input, placeholder_text, toggle_cam_btn, detect_btn]
363
- )
364
-
365
- # 事件 2: 點擊偵測按鈕 (維持不變)
366
  detect_btn.click(
367
  fn=detect_emotion_and_map,
368
  inputs=[webcam_input],
369
- outputs=[captured_image, score_input]
370
  )
371
 
372
- # 事件 3: 提交按鈕 (維持不變)
373
  submit_btn.click(
374
  fn=mood_agent_logic,
375
  inputs=[score_input, food_input, diary_input, debug_mode_btn],
 
267
  return frame, mapped_score
268
 
269
  # ==========================================
270
+ # 5. Gradio 介面建構 (修改版:鏡頭與結果分離)
271
  # ==========================================
272
 
273
  with gr.Blocks(title="AI 心情食堂") as demo:
274
 
 
 
 
275
  with gr.Column(visible=True) as main_app_col:
276
  gr.Markdown(f"## 🍱 AI 心情食堂導航")
277
+ gr.Markdown("請看著鏡頭,讓 AI 幫你判斷今天的心情分數!")
278
 
279
  with gr.Row():
280
  # 左側:輸入區
281
  with gr.Column(scale=1):
282
 
283
+ # ★ 修改重點:將鏡頭與結果分開顯示 ★
284
  gr.Markdown("### 📸 步驟 1:心情偵測 (選用)")
285
 
286
+ # 這裡用 Row 把兩個影像並排 (左邊鏡頭,右邊截圖)
287
+ with gr.Row():
288
+ # 左邊:永遠是即時鏡頭 (不設為 output)
289
+ webcam_input = gr.Image(sources=["webcam"], label="即時鏡頭 (請看這裡)", streaming=True)
290
 
291
+ # 右邊:顯示偵測後的「靜態截圖」
292
+ captured_image = gr.Image(label="偵測結果截圖", interactive=False)
293
+
294
+ detect_btn = gr.Button("📸 截圖並偵測心情 👇", variant="secondary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
 
296
  gr.Markdown("### 📝 步驟 2:確認與補充")
297
  score_input = gr.Radio(
298
  ["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"],
299
+ label="1. 心情分數 (AI 會自動填入,也可手動改)",
300
  value="3 (普通)"
301
  )
302
  food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
 
305
  debug_mode_btn = gr.Checkbox(label="🔧 顯示 Prompt 除錯資訊", value=False)
306
  submit_btn = gr.Button("🚀 送出給 Agent", variant="primary")
307
 
308
+ # 右側:Agent 輸出區 (維持不變)
309
  with gr.Column(scale=1):
310
  agent_output = gr.Markdown(label="AI 回應")
311
  image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
312
  map_output = gr.HTML(label="地圖導航")
313
 
314
+ # Events
315
+
316
+ # ★ 修改重點:按鈕點擊後,輸出目標改為 captured_image,不再覆蓋 webcam_input
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
  detect_btn.click(
318
  fn=detect_emotion_and_map,
319
  inputs=[webcam_input],
320
+ outputs=[captured_image, score_input] # 輸出到「右邊的截圖」和「分數選項」
321
  )
322
 
323
+ # 提交按鈕
324
  submit_btn.click(
325
  fn=mood_agent_logic,
326
  inputs=[score_input, food_input, diary_input, debug_mode_btn],