Update app.py
Browse files
app.py
CHANGED
|
@@ -267,7 +267,7 @@ 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:
|
|
@@ -280,23 +280,32 @@ with gr.Blocks(title="AI 心情食堂") as demo:
|
|
| 280 |
# 左側:輸入區
|
| 281 |
with gr.Column(scale=1):
|
| 282 |
|
| 283 |
-
#
|
| 284 |
-
gr.Markdown("### 📸 步驟 1:心情偵測 (選用)")
|
| 285 |
|
| 286 |
-
# 這裡用 Row 把兩個影像並排 (左邊鏡頭,右邊截圖)
|
| 287 |
with gr.Row():
|
| 288 |
-
#
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
-
#
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
-
|
|
|
|
| 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,7 +314,7 @@ with gr.Blocks(title="AI 心情食堂") as demo:
|
|
| 305 |
debug_mode_btn = gr.Checkbox(label="🔧 顯示 Prompt 除錯資訊", value=False)
|
| 306 |
submit_btn = gr.Button("🚀 送出給 Agent", variant="primary")
|
| 307 |
|
| 308 |
-
# 右側:
|
| 309 |
with gr.Column(scale=1):
|
| 310 |
agent_output = gr.Markdown(label="AI 回應")
|
| 311 |
image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
|
|
@@ -313,14 +322,13 @@ with gr.Blocks(title="AI 心情食堂") as demo:
|
|
| 313 |
|
| 314 |
# Events
|
| 315 |
|
| 316 |
-
#
|
| 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],
|
|
|
|
| 267 |
return frame, mapped_score
|
| 268 |
|
| 269 |
# ==========================================
|
| 270 |
+
# 5. Gradio 介面建構
|
| 271 |
# ==========================================
|
| 272 |
|
| 273 |
with gr.Blocks(title="AI 心情食堂") as demo:
|
|
|
|
| 280 |
# 左側:輸入區
|
| 281 |
with gr.Column(scale=1):
|
| 282 |
|
| 283 |
+
gr.Markdown("### 📸 步驟 1:心情偵測")
|
|
|
|
| 284 |
|
|
|
|
| 285 |
with gr.Row():
|
| 286 |
+
# 🔴 關鍵設定 1:webcam_input
|
| 287 |
+
# streaming=True 代表「鏡頭一直開著」,不會停下來
|
| 288 |
+
# 這就像是相機的「觀景窗」
|
| 289 |
+
webcam_input = gr.Image(
|
| 290 |
+
sources=["webcam"],
|
| 291 |
+
label="即時觀景窗 (請保持開啟)",
|
| 292 |
+
streaming=True # <--- 這個參數是實現「瞬間抓拍」的關鍵!
|
| 293 |
+
)
|
| 294 |
|
| 295 |
+
# 🔵 關鍵設定 2:captured_image
|
| 296 |
+
# 這是一個靜態圖片框,用來顯示「按下按鈕那一刻」的照片
|
| 297 |
+
captured_image = gr.Image(
|
| 298 |
+
label="剛剛拍下的瞬間",
|
| 299 |
+
interactive=False
|
| 300 |
+
)
|
| 301 |
|
| 302 |
+
# 這是你的快門按鈕
|
| 303 |
+
detect_btn = gr.Button("📸 3, 2, 1... 笑一個!(按下截圖)", variant="secondary")
|
| 304 |
|
| 305 |
gr.Markdown("### 📝 步驟 2:確認與補充")
|
| 306 |
score_input = gr.Radio(
|
| 307 |
["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"],
|
| 308 |
+
label="1. 心情分數 (AI 會自動填入)",
|
| 309 |
value="3 (普通)"
|
| 310 |
)
|
| 311 |
food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
|
|
|
|
| 314 |
debug_mode_btn = gr.Checkbox(label="🔧 顯示 Prompt 除錯資訊", value=False)
|
| 315 |
submit_btn = gr.Button("🚀 送出給 Agent", variant="primary")
|
| 316 |
|
| 317 |
+
# 右側:輸出區
|
| 318 |
with gr.Column(scale=1):
|
| 319 |
agent_output = gr.Markdown(label="AI 回應")
|
| 320 |
image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
|
|
|
|
| 322 |
|
| 323 |
# Events
|
| 324 |
|
| 325 |
+
# 🟠 關鍵邏輯:按下按鈕時...
|
| 326 |
detect_btn.click(
|
| 327 |
fn=detect_emotion_and_map,
|
| 328 |
+
inputs=[webcam_input], # 1. 程式會去抓「觀景窗」現在這一瞬間的畫面
|
| 329 |
+
outputs=[captured_image, score_input] # 2. 把結果丟到「右邊的相框」和「分數選項」
|
| 330 |
)
|
| 331 |
|
|
|
|
| 332 |
submit_btn.click(
|
| 333 |
fn=mood_agent_logic,
|
| 334 |
inputs=[score_input, food_input, diary_input, debug_mode_btn],
|