Update app.py
Browse files
app.py
CHANGED
|
@@ -10,11 +10,6 @@ from langchain_huggingface import HuggingFaceEmbeddings
|
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
from PIL import Image
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
-
|
| 14 |
-
# --- [新增] 匯入組員的模組 ---
|
| 15 |
-
import teammate_logic
|
| 16 |
-
# ---------------------------
|
| 17 |
-
|
| 18 |
load_dotenv() # 自動尋找並載入 .env 檔案中的變數
|
| 19 |
|
| 20 |
# ==========================================
|
|
@@ -169,6 +164,7 @@ def get_restaurant_data(mood_score_str, food_choice):
|
|
| 169 |
|
| 170 |
return result, is_random, rec_categories, mood_reason
|
| 171 |
|
|
|
|
| 172 |
def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, mood_score, quiz_result, mood_guide_reason, debug_mode=False):
|
| 173 |
if not GROQ_API_KEY: return "⚠️ 請設定 GROQ_API_KEY"
|
| 174 |
|
|
@@ -207,7 +203,7 @@ def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, m
|
|
| 207 |
)
|
| 208 |
content = response.choices[0].message.content
|
| 209 |
|
| 210 |
-
# 如果開啟除錯模式,附加 Prompt 資訊
|
| 211 |
if debug_mode:
|
| 212 |
debug_info = f"""
|
| 213 |
\n\n--- 🛠️ [DEBUG] LLM Prompt 檢查 ---
|
|
@@ -232,6 +228,7 @@ def generate_image_huggingface(prompt):
|
|
| 232 |
print(f"❌ 生圖失敗: {e}")
|
| 233 |
return None
|
| 234 |
|
|
|
|
| 235 |
def mood_agent_logic(score_input, food_input, diary_input, quiz_state, debug_mode):
|
| 236 |
restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input)
|
| 237 |
|
|
@@ -252,10 +249,10 @@ def mood_agent_logic(score_input, food_input, diary_input, quiz_state, debug_mod
|
|
| 252 |
if docs:
|
| 253 |
rag_info = "\n".join([d.page_content for d in docs])
|
| 254 |
|
| 255 |
-
# 傳入 debug_mode
|
| 256 |
ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, quiz_state, mood_reason, debug_mode)
|
| 257 |
|
| 258 |
-
# 如果開啟除錯模式,附加圖片 Prompt 資訊
|
| 259 |
if debug_mode:
|
| 260 |
img_debug_info = f"""
|
| 261 |
\n\n--- 🛠️ [DEBUG] 圖片生成檢查 ---
|
|
@@ -303,12 +300,8 @@ def handle_q5_change(selected_label, current_state):
|
|
| 303 |
# ==========================================
|
| 304 |
# 5. Gradio 介面建構
|
| 305 |
# ==========================================
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
combined_css = custom_css + "\n" + teammate_logic.css
|
| 309 |
-
# ---------------------
|
| 310 |
-
|
| 311 |
-
with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
| 312 |
quiz_state = gr.State(value={})
|
| 313 |
|
| 314 |
with gr.Tabs(elem_id="hidden_tabs") as tabs:
|
|
@@ -357,11 +350,6 @@ with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
| 357 |
radio_q5 = gr.Radio(choices=list(q5_options_map.keys()), label="請選擇", elem_classes="vertical-radio")
|
| 358 |
btn_q5_finish = gr.Button("完成測驗 ➔", interactive=False)
|
| 359 |
|
| 360 |
-
# --- [新增] 組員功能 ---
|
| 361 |
-
with gr.TabItem("情緒辨識 (組員功能)", id=6):
|
| 362 |
-
teammate_logic.render_ui() # 呼叫組員的介面渲染函式
|
| 363 |
-
# ----------------------------
|
| 364 |
-
|
| 365 |
# Tab 5: Main
|
| 366 |
with gr.TabItem("Main", id=5):
|
| 367 |
with gr.Column():
|
|
@@ -372,6 +360,7 @@ with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
| 372 |
food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
|
| 373 |
diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...")
|
| 374 |
|
|
|
|
| 375 |
debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式 (顯示 Prompt 與圖片資訊)", value=False)
|
| 376 |
|
| 377 |
submit_btn = gr.Button("送出給 Agent", variant="primary")
|
|
@@ -396,6 +385,7 @@ with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
| 396 |
radio_q5.change(fn=handle_q5_change, inputs=[radio_q5, quiz_state], outputs=[quiz_state, btn_q5_finish])
|
| 397 |
btn_q5_finish.click(fn=lambda: gr.Tabs(selected=5), outputs=tabs)
|
| 398 |
|
|
|
|
| 399 |
submit_btn.click(
|
| 400 |
fn=mood_agent_logic,
|
| 401 |
inputs=[score_input, food_input, diary_input, quiz_state, debug_mode_btn],
|
|
|
|
| 10 |
from langchain_community.vectorstores import FAISS
|
| 11 |
from PIL import Image
|
| 12 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
load_dotenv() # 自動尋找並載入 .env 檔案中的變數
|
| 14 |
|
| 15 |
# ==========================================
|
|
|
|
| 164 |
|
| 165 |
return result, is_random, rec_categories, mood_reason
|
| 166 |
|
| 167 |
+
# [修改] 增加 debug_mode 參數
|
| 168 |
def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, mood_score, quiz_result, mood_guide_reason, debug_mode=False):
|
| 169 |
if not GROQ_API_KEY: return "⚠️ 請設定 GROQ_API_KEY"
|
| 170 |
|
|
|
|
| 203 |
)
|
| 204 |
content = response.choices[0].message.content
|
| 205 |
|
| 206 |
+
# [修改] 如果開啟除錯模式,附加 Prompt 資訊
|
| 207 |
if debug_mode:
|
| 208 |
debug_info = f"""
|
| 209 |
\n\n--- 🛠️ [DEBUG] LLM Prompt 檢查 ---
|
|
|
|
| 228 |
print(f"❌ 生圖失敗: {e}")
|
| 229 |
return None
|
| 230 |
|
| 231 |
+
# [修改] 增加 debug_mode 參數
|
| 232 |
def mood_agent_logic(score_input, food_input, diary_input, quiz_state, debug_mode):
|
| 233 |
restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input)
|
| 234 |
|
|
|
|
| 249 |
if docs:
|
| 250 |
rag_info = "\n".join([d.page_content for d in docs])
|
| 251 |
|
| 252 |
+
# [修改] 傳入 debug_mode
|
| 253 |
ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, quiz_state, mood_reason, debug_mode)
|
| 254 |
|
| 255 |
+
# [修改] 如果開啟除錯模式,附加圖片 Prompt 資訊
|
| 256 |
if debug_mode:
|
| 257 |
img_debug_info = f"""
|
| 258 |
\n\n--- 🛠️ [DEBUG] 圖片生成檢查 ---
|
|
|
|
| 300 |
# ==========================================
|
| 301 |
# 5. Gradio 介面建構
|
| 302 |
# ==========================================
|
| 303 |
+
with gr.Blocks(title="AI 心情食堂") as demo:
|
| 304 |
+
gr.HTML(f"<style>{custom_css}</style>")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
quiz_state = gr.State(value={})
|
| 306 |
|
| 307 |
with gr.Tabs(elem_id="hidden_tabs") as tabs:
|
|
|
|
| 350 |
radio_q5 = gr.Radio(choices=list(q5_options_map.keys()), label="請選擇", elem_classes="vertical-radio")
|
| 351 |
btn_q5_finish = gr.Button("完成測驗 ➔", interactive=False)
|
| 352 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
# Tab 5: Main
|
| 354 |
with gr.TabItem("Main", id=5):
|
| 355 |
with gr.Column():
|
|
|
|
| 360 |
food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
|
| 361 |
diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...")
|
| 362 |
|
| 363 |
+
# [新增] 除錯模式開關
|
| 364 |
debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式 (顯示 Prompt 與圖片資訊)", value=False)
|
| 365 |
|
| 366 |
submit_btn = gr.Button("送出給 Agent", variant="primary")
|
|
|
|
| 385 |
radio_q5.change(fn=handle_q5_change, inputs=[radio_q5, quiz_state], outputs=[quiz_state, btn_q5_finish])
|
| 386 |
btn_q5_finish.click(fn=lambda: gr.Tabs(selected=5), outputs=tabs)
|
| 387 |
|
| 388 |
+
# [修改] 加入 debug_mode_btn 到輸入
|
| 389 |
submit_btn.click(
|
| 390 |
fn=mood_agent_logic,
|
| 391 |
inputs=[score_input, food_input, diary_input, quiz_state, debug_mode_btn],
|