Update app.py
Browse files
app.py
CHANGED
|
@@ -267,12 +267,11 @@ def detect_emotion_and_map(frame):
|
|
| 267 |
return frame, mapped_score
|
| 268 |
|
| 269 |
# ==========================================
|
| 270 |
-
#
|
| 271 |
# ==========================================
|
| 272 |
|
| 273 |
with gr.Blocks(title="AI 心情食堂") as demo:
|
| 274 |
|
| 275 |
-
# 只有一個主應用程式 Column,並設定為預設顯示
|
| 276 |
with gr.Column(visible=True) as main_app_col:
|
| 277 |
gr.Markdown(f"## 🍱 AI 心情食堂導航")
|
| 278 |
gr.Markdown("請看著鏡頭,讓 AI 幫你判斷今天的心情分數!")
|
|
@@ -281,42 +280,47 @@ with gr.Blocks(title="AI 心情食堂") as demo:
|
|
| 281 |
# 左側:輸入區
|
| 282 |
with gr.Column(scale=1):
|
| 283 |
|
| 284 |
-
#
|
| 285 |
gr.Markdown("### 📸 步驟 1:心情偵測 (選用)")
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
gr.Markdown("### 📝 步驟 2:確認與補充")
|
| 291 |
score_input = gr.Radio(
|
| 292 |
["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"],
|
| 293 |
-
label="1. 心情分數 (可手動
|
| 294 |
value="3 (普通)"
|
| 295 |
)
|
| 296 |
food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
|
| 297 |
diary_input = gr.Textbox(lines=3, label="3. 心情日記 (選填)", placeholder="例如:今天被老闆罵了,想吃點好料的...")
|
| 298 |
|
| 299 |
debug_mode_btn = gr.Checkbox(label="🔧 顯示 Prompt 除錯資訊", value=False)
|
| 300 |
-
|
| 301 |
-
submit_btn = gr.Button("🚀 送出給 Agent", variant="primary")
|
| 302 |
|
| 303 |
-
# 右側:輸出區 (維持不變)
|
| 304 |
with gr.Column(scale=1):
|
| 305 |
agent_output = gr.Markdown(label="AI 回應")
|
| 306 |
image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
|
| 307 |
map_output = gr.HTML(label="地圖導航")
|
| 308 |
|
| 309 |
-
# Events
|
| 310 |
|
| 311 |
-
# 點
|
| 312 |
detect_btn.click(
|
| 313 |
fn=detect_emotion_and_map,
|
| 314 |
inputs=[webcam_input],
|
| 315 |
-
outputs=[
|
| 316 |
)
|
| 317 |
|
| 318 |
# 提交按鈕
|
| 319 |
-
# 移除了 quiz_state 的輸入
|
| 320 |
submit_btn.click(
|
| 321 |
fn=mood_agent_logic,
|
| 322 |
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 幫你判斷今天的心情分數!")
|
|
|
|
| 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="隨便")
|
| 303 |
diary_input = gr.Textbox(lines=3, label="3. 心情日記 (選填)", placeholder="例如:今天被老闆罵了,想吃點好料的...")
|
| 304 |
|
| 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],
|