Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from openai import OpenAI | |
| from huggingface_hub import InferenceClient | |
| import os | |
| import time | |
| import requests | |
| import urllib.parse | |
| import pandas as pd | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import FAISS | |
| from PIL import Image | |
| from dotenv import load_dotenv | |
| # --- 匯入組員的模組 --- | |
| import teammate_logic | |
| # --------------------------- | |
| load_dotenv() | |
| # ========================================== | |
| # 0. 環境變數 & 1. 系統初始化 (保持不變) | |
| # ========================================== | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| global_df = None | |
| global_mood_df = None | |
| global_retriever = None | |
| rag_initialized = False | |
| def init_rag_system(): | |
| # ... (保持原樣,省略以節省篇幅) ... | |
| global global_df, global_mood_df, global_retriever, rag_initialized | |
| if rag_initialized: return | |
| try: | |
| global_df = pd.read_csv('restaurants.csv') | |
| global_df['RAG_Content'] = global_df['RAG_Content'].fillna("") | |
| global_df['Category'] = global_df['Category'].fillna("其他") | |
| global_mood_df = pd.read_csv('mood_food_guide.csv') | |
| except Exception: pass | |
| if os.path.exists("faiss_index"): | |
| try: | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
| vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True) | |
| global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3}) | |
| except Exception: pass | |
| rag_initialized = True | |
| # ========================================== | |
| # 2. 核心功能 (保持不變) | |
| # ========================================== | |
| def get_restaurant_data(mood_score_str, food_choice): | |
| # ... (保持原樣) ... | |
| init_rag_system() | |
| if global_df is None or global_df.empty: return None, True, "資料庫未載入", "無建議" | |
| try: score = int(str(mood_score_str).split(' ')[0]) | |
| except: score = 3 | |
| mood_info = global_mood_df[global_mood_df['分數'] == score] | |
| if not mood_info.empty: | |
| rec_categories = mood_info.iloc[0]['推薦料理類別'] | |
| mood_reason = mood_info.iloc[0]['原因'] | |
| else: | |
| rec_categories = "" | |
| mood_reason = "隨意探索" | |
| candidates = global_df.copy() | |
| if rec_categories: | |
| candidates = candidates[candidates['Category'].apply(lambda x: str(x) in str(rec_categories) or str(rec_categories) in str(x))] | |
| food_keyword = "飯" if food_choice == "吃飯" else "麵" if food_choice == "吃麵" else "" | |
| if food_keyword: | |
| candidates = candidates[candidates['Name'].str.contains(food_keyword, case=False, na=False) | candidates['RAG_Content'].str.contains(food_keyword, case=False, na=False)] | |
| if candidates.empty: | |
| result = global_df.sample(1).iloc[0]; is_random = True | |
| else: | |
| result = candidates.sample(1).iloc[0]; is_random = False | |
| return result, is_random, rec_categories, mood_reason | |
| def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, mood_score, mood_guide_reason, debug_mode=False): | |
| # ... (保持原樣) ... | |
| if not GROQ_API_KEY: return "⚠️ 請設定 GROQ_API_KEY" | |
| client = OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1") | |
| system_prompt = "你是一個幽默、懂吃且善解人意的 AI 朋友..." | |
| user_msg = f"【狀態】心情分數:{mood_score}\n【日記】{user_diary}\n【建議原因】{mood_guide_reason}\n【餐廳】{restaurant_name}\n資料:{restaurant_detail}" | |
| try: | |
| response = client.chat.completions.create(model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}]) | |
| return response.choices[0].message.content | |
| except Exception as e: return f"Groq Error: {str(e)}" | |
| def generate_image_huggingface(prompt): | |
| # ... (保持原樣) ... | |
| if not HF_TOKEN: return None | |
| try: | |
| hf_client = InferenceClient(token=HF_TOKEN) | |
| return hf_client.text_to_image(prompt=prompt, model="stabilityai/stable-diffusion-xl-base-1.0") | |
| except: return None | |
| def mood_agent_logic(score_input, food_input, diary_input, debug_mode): | |
| # ... (保持原樣) ... | |
| restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input) | |
| if restaurant is None: yield "資料庫讀取錯誤", None, ""; return | |
| name = restaurant['Name']; address = restaurant['Address']; url = restaurant['URL']; img_prompt = restaurant.get('Visual_prompt') | |
| rag_info = str(restaurant.get('RAG_Content', '')) | |
| if global_retriever: | |
| docs = global_retriever.invoke(name) | |
| if docs: rag_info = "\n".join([d.page_content for d in docs]) | |
| ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason, debug_mode) | |
| final_response = f"### 🍽️ 推薦:{name}\n\n{ai_text}" | |
| 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>' | |
| yield final_response, None, map_html | |
| image_output = generate_image_huggingface(img_prompt) | |
| yield final_response, image_output, map_html | |
| # ========================================== | |
| # 3. [修改] 橋接邏輯 (Bridge Functions) | |
| # ========================================== | |
| def _score_to_radio_value(score): | |
| mapping = {1: "1 (心情差)", 2: "2 (不太好)", 3: "3 (普通)", 4: "4 (不錯)", 5: "5 (超棒)"} | |
| return mapping.get(score, "3 (普通)") | |
| # 橋接函式 1:Webcam 串流 | |
| def bridge_predict_frame(frame, st): | |
| out_cam, out_result, out_st, out_btn = teammate_logic.predict_from_frame(frame, st) | |
| score_update = gr.update() | |
| tabs_update = gr.update() # ### [修改] 初始化 tab 更新狀態 | |
| if out_st.done and hasattr(out_st, 'final_score'): | |
| new_val = _score_to_radio_value(out_st.final_score) | |
| score_update = gr.update(value=new_val) | |
| # ### [修改] 當辨識完成時,將 Tabs 切換到 id=1 (主功能區) | |
| tabs_update = gr.Tabs(selected=1) | |
| # ### [修改] 回傳多了 tabs_update | |
| return out_cam, out_result, out_st, out_btn, score_update, tabs_update | |
| # 橋接函式 2:圖片上傳 | |
| def bridge_predict_upload(img): | |
| result_html = teammate_logic.predict_from_upload(img) | |
| score_update = gr.update() | |
| tabs_update = gr.update() # ### [修改] 初始化 tab 更新狀態 | |
| if img is not None: | |
| small = teammate_logic._downsample_rgb(img.astype('uint8'), teammate_logic.DOWNSAMPLE_W) | |
| face_roi, found = teammate_logic._extract_largest_face(small) | |
| if found: | |
| emo_dict = teammate_logic._analyze_emotion(face_roi) | |
| if emo_dict: | |
| top_emo = max(emo_dict, key=emo_dict.get) | |
| score = teammate_logic.get_emotion_score(top_emo) | |
| score_update = gr.update(value=_score_to_radio_value(score)) | |
| # ### [修改] 圖片上傳辨識成功後,切換到 id=1 | |
| tabs_update = gr.Tabs(selected=1) | |
| # ### [修改] 回傳多了 tabs_update | |
| return result_html, score_update, tabs_update | |
| # ========================================== | |
| # 4. Gradio 介面建構 | |
| # ========================================== | |
| combined_css = teammate_logic.css | |
| with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo: | |
| st_state = gr.State(teammate_logic._init_state()) | |
| # ### [修改] 將 gr.Tabs() 賦值給變數 'tabs',以便後續控制 | |
| with gr.Tabs() as tabs: | |
| # Tab 1: 情緒辨識 | |
| with gr.TabItem("😊 情緒辨識 (Step 1)", id=0): # id=0 | |
| with gr.Column(elem_id="app_container"): | |
| gr.Markdown("### 第一步:測測你的心情能量\n讓 AI 看看你的表情,自動幫你決定心情分數!(辨識完畢會自動跳轉)") | |
| with gr.Row(): | |
| btn_start = gr.Button("📸 開啟攝影機辨識", variant="primary") | |
| btn_stop = gr.Button("⏹️ 停止", variant="secondary", visible=False) | |
| cam = gr.Image(sources=["webcam"], streaming=True, type="numpy", label="攝影機畫面", visible=False) | |
| result_markdown = gr.Markdown(teammate_logic._hint_html("請按「開啟攝影機辨識」或下方上傳照片。")) | |
| gr.Markdown("---") | |
| gr.Markdown("### 或者:上傳照片") | |
| upload_img = gr.Image(sources=["upload"], type="numpy", label="上傳照片") | |
| # Tab 2: 主功能區 | |
| with gr.TabItem("🍽️ AI 心情食堂 (Step 2)", id=1): # id=1 | |
| with gr.Column(): | |
| gr.Markdown(f"## 🍱 今天想吃點什麼?") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| score_input = gr.Radio( | |
| ["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"], | |
| label="1. 心情分數 (由 Tab 1 自動填入)", | |
| value="3 (普通)" | |
| ) | |
| food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便") | |
| diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...") | |
| debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式", value=False) | |
| submit_btn = gr.Button("🍱 送出給 Agent", variant="primary") | |
| with gr.Column(scale=1): | |
| agent_output = gr.Markdown(label="AI 回應") | |
| image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400) | |
| map_output = gr.HTML(label="地圖導航") | |
| # ========================================== | |
| # 事件綁定 (Event Listeners) | |
| # ========================================== | |
| # 1. 開始按鈕 (不變) | |
| btn_start.click( | |
| fn=teammate_logic.start_webcam, | |
| inputs=[st_state], | |
| outputs=[result_markdown, cam, btn_stop, btn_start, st_state], | |
| show_progress="minimal" | |
| ) | |
| # 2. 停止按鈕 (不變) | |
| btn_stop.click( | |
| fn=teammate_logic.stop_webcam, | |
| inputs=[st_state], | |
| outputs=[result_markdown, cam, btn_stop, btn_start, st_state], | |
| show_progress="minimal" | |
| ) | |
| # 3. Webcam 串流 ### [修改] outputs 加入了 'tabs' | |
| cam.stream( | |
| fn=bridge_predict_frame, | |
| inputs=[cam, st_state], | |
| outputs=[cam, result_markdown, st_state, btn_stop, score_input, tabs], # <--- 這裡加了 tabs | |
| show_progress="minimal" | |
| ) | |
| # 4. 圖片上傳 ### [修改] outputs 加入了 'tabs' | |
| upload_img.change( | |
| fn=bridge_predict_upload, | |
| inputs=[upload_img], | |
| outputs=[result_markdown, score_input, tabs], # <--- 這裡加了 tabs | |
| show_progress="minimal" | |
| ) | |
| # 主功能 (不變) | |
| submit_btn.click( | |
| fn=mood_agent_logic, | |
| inputs=[score_input, food_input, diary_input, debug_mode_btn], | |
| outputs=[agent_output, image_output, map_output] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(ssr_mode=False) |