Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ def init_rag_system():
|
|
| 46 |
rag_initialized = True
|
| 47 |
|
| 48 |
# ==========================================
|
| 49 |
-
# 2. 核心功能
|
| 50 |
# ==========================================
|
| 51 |
def get_restaurant_data(mood_score_str, food_choice):
|
| 52 |
init_rag_system()
|
|
@@ -80,7 +80,6 @@ def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, m
|
|
| 80 |
system_prompt = "你是一個幽默、懂吃且善解人意的 AI 朋友..."
|
| 81 |
user_msg = f"【狀態】心情分數:{mood_score}\n【日記】{user_diary}\n【建議原因】{mood_guide_reason}\n【餐廳】{restaurant_name}\n資料:{restaurant_detail}"
|
| 82 |
|
| 83 |
-
# 準備除錯資訊
|
| 84 |
debug_log = ""
|
| 85 |
if debug_mode:
|
| 86 |
debug_log = f"""
|
|
@@ -95,7 +94,7 @@ def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, m
|
|
| 95 |
try:
|
| 96 |
response = client.chat.completions.create(model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}])
|
| 97 |
content = response.choices[0].message.content
|
| 98 |
-
return content, debug_log
|
| 99 |
except Exception as e:
|
| 100 |
return f"Groq Error: {str(e)}", debug_log
|
| 101 |
|
|
@@ -117,10 +116,8 @@ def mood_agent_logic(score_input, food_input, diary_input, debug_mode):
|
|
| 117 |
docs = global_retriever.invoke(name)
|
| 118 |
if docs: rag_info = "\n".join([d.page_content for d in docs])
|
| 119 |
|
| 120 |
-
# 呼叫 Groq 生成內容與取得除錯資訊
|
| 121 |
ai_text, groq_debug_log = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason, debug_mode)
|
| 122 |
|
| 123 |
-
# 準備完整的除錯資訊 (包含圖片 Prompt)
|
| 124 |
full_debug_log = ""
|
| 125 |
if debug_mode:
|
| 126 |
img_debug_log = f"""
|
|
@@ -130,16 +127,13 @@ def mood_agent_logic(score_input, food_input, diary_input, debug_mode):
|
|
| 130 |
"""
|
| 131 |
full_debug_log = groq_debug_log + "\n\n" + img_debug_log
|
| 132 |
|
| 133 |
-
# 設定除錯視窗的狀態 (有勾選才顯示)
|
| 134 |
debug_output_update = gr.update(value=full_debug_log, visible=debug_mode)
|
| 135 |
|
| 136 |
final_response = f"### 🍽️ 推薦:{name}\n\n{ai_text}"
|
| 137 |
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>'
|
| 138 |
|
| 139 |
-
# 先回傳文字與除錯資訊
|
| 140 |
yield final_response, None, map_html, debug_output_update
|
| 141 |
|
| 142 |
-
# 再回傳圖片
|
| 143 |
image_output = generate_image_huggingface(img_prompt)
|
| 144 |
yield final_response, image_output, map_html, debug_output_update
|
| 145 |
|
|
@@ -180,6 +174,7 @@ def bridge_predict_frame(frame, st):
|
|
| 180 |
# 4. Gradio 介面建構
|
| 181 |
# ==========================================
|
| 182 |
|
|
|
|
| 183 |
combined_css = emotion.css
|
| 184 |
|
| 185 |
with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
@@ -217,9 +212,7 @@ with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
| 217 |
|
| 218 |
submit_btn = gr.Button("🍱 送出給 Agent", variant="primary")
|
| 219 |
|
| 220 |
-
# --- [新增] 除錯資訊顯示區 (位於按鈕下方) ---
|
| 221 |
debug_output = gr.Markdown(label="除錯資訊 (Debug Log)", visible=False)
|
| 222 |
-
# ----------------------------------------
|
| 223 |
|
| 224 |
with gr.Column(scale=1):
|
| 225 |
agent_output = gr.Markdown(label="AI 回應")
|
|
@@ -254,7 +247,7 @@ with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
| 254 |
submit_btn.click(
|
| 255 |
fn=mood_agent_logic,
|
| 256 |
inputs=[score_input, food_input, diary_input, debug_mode_btn],
|
| 257 |
-
outputs=[agent_output, image_output, map_output, debug_output]
|
| 258 |
)
|
| 259 |
|
| 260 |
if __name__ == "__main__":
|
|
|
|
| 46 |
rag_initialized = True
|
| 47 |
|
| 48 |
# ==========================================
|
| 49 |
+
# 2. 核心功能
|
| 50 |
# ==========================================
|
| 51 |
def get_restaurant_data(mood_score_str, food_choice):
|
| 52 |
init_rag_system()
|
|
|
|
| 80 |
system_prompt = "你是一個幽默、懂吃且善解人意的 AI 朋友..."
|
| 81 |
user_msg = f"【狀態】心情分數:{mood_score}\n【日記】{user_diary}\n【建議原因】{mood_guide_reason}\n【餐廳】{restaurant_name}\n資料:{restaurant_detail}"
|
| 82 |
|
|
|
|
| 83 |
debug_log = ""
|
| 84 |
if debug_mode:
|
| 85 |
debug_log = f"""
|
|
|
|
| 94 |
try:
|
| 95 |
response = client.chat.completions.create(model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}])
|
| 96 |
content = response.choices[0].message.content
|
| 97 |
+
return content, debug_log
|
| 98 |
except Exception as e:
|
| 99 |
return f"Groq Error: {str(e)}", debug_log
|
| 100 |
|
|
|
|
| 116 |
docs = global_retriever.invoke(name)
|
| 117 |
if docs: rag_info = "\n".join([d.page_content for d in docs])
|
| 118 |
|
|
|
|
| 119 |
ai_text, groq_debug_log = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason, debug_mode)
|
| 120 |
|
|
|
|
| 121 |
full_debug_log = ""
|
| 122 |
if debug_mode:
|
| 123 |
img_debug_log = f"""
|
|
|
|
| 127 |
"""
|
| 128 |
full_debug_log = groq_debug_log + "\n\n" + img_debug_log
|
| 129 |
|
|
|
|
| 130 |
debug_output_update = gr.update(value=full_debug_log, visible=debug_mode)
|
| 131 |
|
| 132 |
final_response = f"### 🍽️ 推薦:{name}\n\n{ai_text}"
|
| 133 |
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>'
|
| 134 |
|
|
|
|
| 135 |
yield final_response, None, map_html, debug_output_update
|
| 136 |
|
|
|
|
| 137 |
image_output = generate_image_huggingface(img_prompt)
|
| 138 |
yield final_response, image_output, map_html, debug_output_update
|
| 139 |
|
|
|
|
| 174 |
# 4. Gradio 介面建構
|
| 175 |
# ==========================================
|
| 176 |
|
| 177 |
+
# 這裡會讀取 emotion.py 中我們剛剛修改過的 CSS
|
| 178 |
combined_css = emotion.css
|
| 179 |
|
| 180 |
with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
|
|
|
|
| 212 |
|
| 213 |
submit_btn = gr.Button("🍱 送出給 Agent", variant="primary")
|
| 214 |
|
|
|
|
| 215 |
debug_output = gr.Markdown(label="除錯資訊 (Debug Log)", visible=False)
|
|
|
|
| 216 |
|
| 217 |
with gr.Column(scale=1):
|
| 218 |
agent_output = gr.Markdown(label="AI 回應")
|
|
|
|
| 247 |
submit_btn.click(
|
| 248 |
fn=mood_agent_logic,
|
| 249 |
inputs=[score_input, food_input, diary_input, debug_mode_btn],
|
| 250 |
+
outputs=[agent_output, image_output, map_output, debug_output]
|
| 251 |
)
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|