PhilipL commited on
Commit
0d1630d
·
verified ·
1 Parent(s): 2754901

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -159
app.py CHANGED
@@ -11,48 +11,80 @@ from langchain_community.vectorstores import FAISS
11
  from PIL import Image
12
  from dotenv import load_dotenv
13
 
14
- # --- 匯入組員的模組 ---
15
- import emotion
16
  # ---------------------------
17
 
18
- load_dotenv()
19
 
20
  # ==========================================
21
- # 0. 環境變數 & 1. 系統初始化 (保持不變)
22
  # ==========================================
23
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
24
  HF_TOKEN = os.getenv("HF_TOKEN")
25
 
 
26
  global_df = None
27
  global_mood_df = None
28
  global_retriever = None
29
  rag_initialized = False
30
 
 
 
 
31
  def init_rag_system():
32
  global global_df, global_mood_df, global_retriever, rag_initialized
 
33
  if rag_initialized: return
 
 
 
 
34
  try:
35
  global_df = pd.read_csv('restaurants.csv')
36
  global_df['RAG_Content'] = global_df['RAG_Content'].fillna("")
37
  global_df['Category'] = global_df['Category'].fillna("其他")
 
38
  global_mood_df = pd.read_csv('mood_food_guide.csv')
39
- except Exception: pass
 
 
 
 
 
40
  if os.path.exists("faiss_index"):
41
  try:
 
42
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
43
- vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
 
 
 
 
 
44
  global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
45
- except Exception: pass
 
 
 
 
 
 
46
  rag_initialized = True
47
 
48
  # ==========================================
49
- # 2. 核心功能 (保持不變)
50
  # ==========================================
 
51
  def get_restaurant_data(mood_score_str, food_choice):
52
  init_rag_system()
53
  if global_df is None or global_df.empty: return None, True, "資料庫未載入", "無建議"
54
- try: score = int(str(mood_score_str).split(' ')[0])
55
- except: score = 3
 
 
 
 
56
  mood_info = global_mood_df[global_mood_df['分數'] == score]
57
  if not mood_info.empty:
58
  rec_categories = mood_info.iloc[0]['推薦料理類別']
@@ -60,203 +92,159 @@ def get_restaurant_data(mood_score_str, food_choice):
60
  else:
61
  rec_categories = ""
62
  mood_reason = "隨意探索"
 
63
  candidates = global_df.copy()
 
64
  if rec_categories:
65
  candidates = candidates[candidates['Category'].apply(lambda x: str(x) in str(rec_categories) or str(rec_categories) in str(x))]
 
66
  food_keyword = "飯" if food_choice == "吃飯" else "麵" if food_choice == "吃麵" else ""
67
  if food_keyword:
68
- candidates = candidates[candidates['Name'].str.contains(food_keyword, case=False, na=False) | candidates['RAG_Content'].str.contains(food_keyword, case=False, na=False)]
 
 
 
 
69
  if candidates.empty:
70
- result = global_df.sample(1).iloc[0]; is_random = True
 
71
  else:
72
- result = candidates.sample(1).iloc[0]; is_random = False
 
 
73
  return result, is_random, rec_categories, mood_reason
74
 
75
  def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, mood_score, mood_guide_reason, debug_mode=False):
76
  if not GROQ_API_KEY: return "⚠️ 請設定 GROQ_API_KEY"
 
77
  client = OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1")
78
- system_prompt = "你是一個幽默、懂吃且善解人意的 AI 朋友..."
79
- user_msg = f"【狀態】心情分數:{mood_score}\n【日記】{user_diary}\n【建議原因】{mood_guide_reason}\n【餐廳】{restaurant_name}\n資料:{restaurant_detail}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  try:
81
- response = client.chat.completions.create(model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}])
82
- return response.choices[0].message.content
83
- except Exception as e: return f"Groq Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  def generate_image_huggingface(prompt):
86
  if not HF_TOKEN: return None
 
 
87
  try:
88
  hf_client = InferenceClient(token=HF_TOKEN)
89
  return hf_client.text_to_image(prompt=prompt, model="stabilityai/stable-diffusion-xl-base-1.0")
90
- except: return None
 
 
91
 
92
  def mood_agent_logic(score_input, food_input, diary_input, debug_mode):
93
  restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input)
94
- if restaurant is None: yield "資料庫讀取錯誤", None, ""; return
95
- name = restaurant['Name']; address = restaurant['Address']; url = restaurant['URL']; img_prompt = restaurant.get('Visual_prompt')
 
 
 
 
 
 
 
 
 
 
96
  rag_info = str(restaurant.get('RAG_Content', ''))
97
  if global_retriever:
98
  docs = global_retriever.invoke(name)
99
- if docs: rag_info = "\n".join([d.page_content for d in docs])
 
 
 
100
  ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason, debug_mode)
101
- final_response = f"### 🍽️ 推薦:{name}\n\n{ai_text}"
 
 
 
 
 
 
 
 
 
 
 
102
  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>'
 
103
  yield final_response, None, map_html
104
  image_output = generate_image_huggingface(img_prompt)
105
  yield final_response, image_output, map_html
106
 
107
-
108
  # ==========================================
109
- # 3. [修正版] 橋接邏輯 (Bridge Functions)
110
  # ==========================================
111
 
112
- # 這是 app.py 自己的格式 (注意:這裡是半形括號)
113
- def _score_to_radio_value(score):
114
- mapping = {1: "1 (心情差)", 2: "2 (不太好)", 3: "3 (普通)", 4: "4 (不錯)", 5: "5 (超棒)"}
115
- # 預防萬一 score 是 None 或其他怪怪的值
116
- try:
117
- score = int(score)
118
- except:
119
- score = 3
120
- return mapping.get(score, "3 (普通)")
121
-
122
- # 橋接 1:開始
123
- def bridge_start_click(st):
124
- res = emotion.on_restart(st)
125
- return res[1], res[0], gr.update(visible=True), res[5], res[4]
126
-
127
- # 橋接 2:停止
128
- def bridge_stop_click(st):
129
- res = emotion.on_stop(st)
130
- return res[1], res[0], gr.update(visible=False), res[5], res[4]
131
-
132
- # 橋接 3:Webcam 串流 (修正分數填入問題)
133
- def bridge_predict_frame(frame, st):
134
- res = emotion.on_stream(frame, st)
135
-
136
- out_cam = res[0]
137
- out_result = res[1]
138
- # out_mood = res[2] # ❌ 我們不使用隊友回傳的 UI 更新,因為括號格式不同
139
- out_st = res[4]
140
- out_btn_start = res[5]
141
-
142
- score_update = gr.update()
143
- tabs_update = gr.update()
144
- out_btn_stop = gr.update()
145
-
146
- # 如果辨識完成,我們自己產生一個符合 app.py 格式的 UI 更新
147
- if out_st.finished and hasattr(out_st, 'final_score'):
148
- # ✅ 使用 _score_to_radio_value 將整數轉換為半形括號字串
149
- new_val = _score_to_radio_value(out_st.final_score)
150
- score_update = gr.update(value=new_val)
151
-
152
- tabs_update = gr.Tabs(selected=1)
153
- out_btn_stop = gr.update(visible=False)
154
-
155
- return out_cam, out_result, out_st, out_btn_stop, out_btn_start, score_update, tabs_update
156
-
157
- # 橋接 4:圖片上傳 (修正分數填入問題)
158
- def bridge_predict_upload(img, st):
159
- res = emotion.on_upload(img, st)
160
-
161
- out_result = res[0]
162
- # out_mood = res[1] # ❌ 同樣忽略隊友的格式
163
- out_st = res[2]
164
-
165
- score_update = gr.update()
166
- tabs_update = gr.update()
167
-
168
- if out_st.finished and hasattr(out_st, 'final_score'):
169
- # ✅ 使用 _score_to_radio_value 將整數轉換為半形括號字串
170
- new_val = _score_to_radio_value(out_st.final_score)
171
- score_update = gr.update(value=new_val)
172
-
173
- tabs_update = gr.Tabs(selected=1)
174
-
175
- return out_result, score_update, tabs_update, out_st
176
-
177
-
178
- # ==========================================
179
- # 4. Gradio 介面建構
180
- # ==========================================
181
-
182
- combined_css = emotion.css
183
 
184
  with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
185
 
186
- st_state = gr.State(emotion.AppState())
187
-
188
- with gr.Tabs() as tabs:
189
-
190
- # Tab 1: 情緒辨識
191
- with gr.TabItem("😊 情緒辨識 (Step 1)", id=0):
192
- with gr.Column(elem_id="app_container"):
193
- gr.Markdown("### 第一步:測測你的心情能量\n讓 AI 看看你的表情,自動幫你決定心情分數!(辨識完畢會自動跳轉)")
194
-
195
- with gr.Row():
196
- btn_start = gr.Button("📸 開啟攝影機辨識", variant="primary")
197
- btn_stop = gr.Button("⏹️ 停止", variant="secondary", visible=False)
198
-
199
- cam = gr.Image(sources=["webcam"], streaming=True, type="numpy", label="攝影機畫面", visible=False)
200
- result_markdown = gr.Markdown(emotion._hint_html("請按「開啟攝影機辨識」或下方上傳照片。"))
201
-
202
- gr.Markdown("---")
203
- gr.Markdown("### 或者:上傳照片")
204
- upload_img = gr.Image(sources=["upload"], type="numpy", label="上傳照片")
205
-
206
- # Tab 2: 主功能區
207
- with gr.TabItem("🍽️ AI 心情食堂 (Step 2)", id=1):
208
  with gr.Column():
209
- gr.Markdown(f"## 🍱 今天想吃點什麼?")
210
  with gr.Row():
211
  with gr.Column(scale=1):
212
- # 这里的 Radio 选项使用的是半形括号
213
- score_input = gr.Radio(
214
- ["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"],
215
- label="1. 心情分數 (由 Tab 1 自動填入)",
216
- value="3 (普通)"
217
- )
218
  food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
219
  diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...")
220
- debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式", value=False)
221
- submit_btn = gr.Button("🍱 送出給 Agent", variant="primary")
222
-
 
223
  with gr.Column(scale=1):
224
  agent_output = gr.Markdown(label="AI 回應")
225
  image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
226
  map_output = gr.HTML(label="地圖導航")
227
 
228
- # ==========================================
229
- # 事件綁定
230
- # ==========================================
231
-
232
- btn_start.click(
233
- fn=bridge_start_click,
234
- inputs=[st_state],
235
- outputs=[result_markdown, cam, btn_stop, btn_start, st_state],
236
- show_progress="minimal"
237
- )
238
-
239
- btn_stop.click(
240
- fn=bridge_stop_click,
241
- inputs=[st_state],
242
- outputs=[result_markdown, cam, btn_stop, btn_start, st_state],
243
- show_progress="minimal"
244
- )
245
-
246
- cam.stream(
247
- fn=bridge_predict_frame,
248
- inputs=[cam, st_state],
249
- outputs=[cam, result_markdown, st_state, btn_stop, btn_start, score_input, tabs],
250
- show_progress="minimal"
251
- )
252
-
253
- upload_img.change(
254
- fn=bridge_predict_upload,
255
- inputs=[upload_img, st_state],
256
- outputs=[result_markdown, score_input, tabs, st_state],
257
- show_progress="minimal"
258
- )
259
 
 
260
  submit_btn.click(
261
  fn=mood_agent_logic,
262
  inputs=[score_input, food_input, diary_input, debug_mode_btn],
 
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
  # ==========================================
21
+ # 0. 環境變數
22
  # ==========================================
23
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
24
  HF_TOKEN = os.getenv("HF_TOKEN")
25
 
26
+ # 全域變數
27
  global_df = None
28
  global_mood_df = None
29
  global_retriever = None
30
  rag_initialized = False
31
 
32
+ # ==========================================
33
+ # 1. 系統初始化 (讀取 CSV + 讀取 FAISS)
34
+ # ==========================================
35
  def init_rag_system():
36
  global global_df, global_mood_df, global_retriever, rag_initialized
37
+
38
  if rag_initialized: return
39
+
40
+ print("⏳ 正在初始化系統...")
41
+
42
+ # --- A. 讀取 CSV (用於篩選與對照) ---
43
  try:
44
  global_df = pd.read_csv('restaurants.csv')
45
  global_df['RAG_Content'] = global_df['RAG_Content'].fillna("")
46
  global_df['Category'] = global_df['Category'].fillna("其他")
47
+
48
  global_mood_df = pd.read_csv('mood_food_guide.csv')
49
+ print(f"✅ CSV 資料讀取成功 (餐廳: {len(global_df)} 筆)")
50
+ except Exception as e:
51
+ print(f"❌ CSV 讀取失敗: {e}")
52
+ return
53
+
54
+ # --- B. 讀取預先建立好的 FAISS 索引 ---
55
  if os.path.exists("faiss_index"):
56
  try:
57
+ print("⏳ 正在載入 FAISS 向量資料庫...")
58
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
59
+
60
+ vectorstore = FAISS.load_local(
61
+ "faiss_index",
62
+ embeddings,
63
+ allow_dangerous_deserialization=True
64
+ )
65
  global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
66
+ print("✅ FAISS 資料庫載入完成!")
67
+ except Exception as e:
68
+ print(f"❌ FAISS 載入失敗: {e}")
69
+ else:
70
+ print("⚠️ 警告:找不到 'faiss_index' 資料夾,RAG 功能將無法使用。")
71
+ print("請先執行 build_index.py 並上傳資料夾。")
72
+
73
  rag_initialized = True
74
 
75
  # ==========================================
76
+ # 2. 核心功能
77
  # ==========================================
78
+
79
  def get_restaurant_data(mood_score_str, food_choice):
80
  init_rag_system()
81
  if global_df is None or global_df.empty: return None, True, "資料庫未載入", "無建議"
82
+
83
+ try:
84
+ score = int(str(mood_score_str).split(' ')[0])
85
+ except:
86
+ score = 3
87
+
88
  mood_info = global_mood_df[global_mood_df['分數'] == score]
89
  if not mood_info.empty:
90
  rec_categories = mood_info.iloc[0]['推薦料理類別']
 
92
  else:
93
  rec_categories = ""
94
  mood_reason = "隨意探索"
95
+
96
  candidates = global_df.copy()
97
+
98
  if rec_categories:
99
  candidates = candidates[candidates['Category'].apply(lambda x: str(x) in str(rec_categories) or str(rec_categories) in str(x))]
100
+
101
  food_keyword = "飯" if food_choice == "吃飯" else "麵" if food_choice == "吃麵" else ""
102
  if food_keyword:
103
+ candidates = candidates[
104
+ candidates['Name'].str.contains(food_keyword, case=False, na=False) |
105
+ candidates['RAG_Content'].str.contains(food_keyword, case=False, na=False)
106
+ ]
107
+
108
  if candidates.empty:
109
+ result = global_df.sample(1).iloc[0]
110
+ is_random = True
111
  else:
112
+ result = candidates.sample(1).iloc[0]
113
+ is_random = False
114
+
115
  return result, is_random, rec_categories, mood_reason
116
 
117
  def generate_content_with_groq(restaurant_name, restaurant_detail, user_diary, mood_score, mood_guide_reason, debug_mode=False):
118
  if not GROQ_API_KEY: return "⚠️ 請設定 GROQ_API_KEY"
119
+
120
  client = OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1")
121
+
122
+ system_prompt = "你是一個幽默、懂吃且善解人意的 AI 朋友。請根據使用者的日記、心情以及「心情美食指南」來推薦餐廳。"
123
+
124
+ # 移除了測驗相關的 user_msg 內容
125
+ user_msg = f"""
126
+ 【狀態】心情分數:{mood_score}
127
+ 【日記】{user_diary}
128
+
129
+ 【心情美食指南建議】
130
+ 因為分數是 {mood_score},建議吃這類食物的原因是:「{mood_guide_reason}」。
131
+
132
+ 【推薦餐廳】
133
+ 名稱:{restaurant_name}
134
+ 資料:{restaurant_detail}
135
+
136
+ 任務:
137
+ 請用繁體中文寫一段溫暖有趣的回覆:
138
+ 1. 綜合回應他的日記與心情。
139
+ 2. 引用「心情美食指南」的原因。
140
+ 3. 介紹這家餐廳的特色。
141
+ (只需要回覆文字內容)
142
+ """
143
+
144
  try:
145
+ response = client.chat.completions.create(
146
+ model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}]
147
+ )
148
+ content = response.choices[0].message.content
149
+
150
+ # 如果開啟除錯模式,附加 Prompt 資訊
151
+ if debug_mode:
152
+ debug_info = f"""
153
+ \n\n--- 🛠️ [DEBUG] LLM Prompt 檢查 ---
154
+ \n**System Prompt:**\n{system_prompt}
155
+ \n**User Message:**\n{user_msg}
156
+ \n-----------------------------------
157
+ """
158
+ return content + debug_info
159
+
160
+ return content
161
+ except Exception as e:
162
+ return f"Groq Error: {str(e)}"
163
 
164
  def generate_image_huggingface(prompt):
165
  if not HF_TOKEN: return None
166
+ if not prompt or pd.isna(prompt): prompt = "Delicious gourmet food, photorealistic, 8k"
167
+
168
  try:
169
  hf_client = InferenceClient(token=HF_TOKEN)
170
  return hf_client.text_to_image(prompt=prompt, model="stabilityai/stable-diffusion-xl-base-1.0")
171
+ except Exception as e:
172
+ print(f"❌ 生圖失敗: {e}")
173
+ return None
174
 
175
  def mood_agent_logic(score_input, food_input, diary_input, debug_mode):
176
  restaurant, is_random, rec_categories, mood_reason = get_restaurant_data(score_input, food_input)
177
+
178
+ if restaurant is None:
179
+ yield "資料庫讀取錯誤", None, ""
180
+ return
181
+
182
+ name = restaurant['Name']
183
+ address = restaurant['Address']
184
+ url = restaurant['URL']
185
+ img_prompt = restaurant.get('Visual_prompt')
186
+ note = "(隨機推薦)" if is_random else ""
187
+
188
+ # RAG 檢索
189
  rag_info = str(restaurant.get('RAG_Content', ''))
190
  if global_retriever:
191
  docs = global_retriever.invoke(name)
192
+ if docs:
193
+ rag_info = "\n".join([d.page_content for d in docs])
194
+
195
+ # 呼叫生成函式 (已移除 quiz_state 參數)
196
  ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason, debug_mode)
197
+
198
+ # 如果開啟除錯模式,附加圖片 Prompt 資訊
199
+ if debug_mode:
200
+ img_debug_info = f"""
201
+ \n\n--- 🛠️ [DEBUG] 圖片生成檢查 ---
202
+ \n**使用的 Visual Prompt:**\n{img_prompt}
203
+ \n(如果上方圖片為空白,可能是 HF API 忙碌或 Prompt 無效)
204
+ \n-----------------------------------
205
+ """
206
+ ai_text += img_debug_info
207
+
208
+ final_response = f"### 🍽️ 推薦:{name} {note}\n\n{ai_text}"
209
  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>'
210
+
211
  yield final_response, None, map_html
212
  image_output = generate_image_huggingface(img_prompt)
213
  yield final_response, image_output, map_html
214
 
 
215
  # ==========================================
216
+ # 3. Gradio 介面建構
217
  # ==========================================
218
 
219
+ # 結合組員的 CSS
220
+ combined_css = teammate_logic.css
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
 
222
  with gr.Blocks(title="AI 心情食堂", css=combined_css) as demo:
223
 
224
+ with gr.Tabs():
225
+ # Tab 1: 主功能區 (已移除 Q1-Q5,直接顯示點餐介面)
226
+ with gr.TabItem("🍽️ AI 心情食堂 (點餐推薦)", id=0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  with gr.Column():
228
+ gr.Markdown(f"## 🍱 呷飽沒??!")
229
  with gr.Row():
230
  with gr.Column(scale=1):
231
+ score_input = gr.Radio(["1 (心情差)", "2 (不太好)", "3 (普通)", "4 (不錯)", "5 (超棒)"], label="1. 心情分數", value="3 (普通)")
 
 
 
 
 
232
  food_input = gr.Radio(["吃飯", "吃麵", "隨便"], label="2. 想吃什麼", value="隨便")
233
  diary_input = gr.Textbox(lines=4, label="3. 心情日記", placeholder="寫下今天發生的事...")
234
+
235
+ debug_mode_btn = gr.Checkbox(label="🔧 開啟除錯模式 (顯示 Prompt 與圖片資訊)", value=False)
236
+
237
+ submit_btn = gr.Button("送出給 Agent", variant="primary")
238
  with gr.Column(scale=1):
239
  agent_output = gr.Markdown(label="AI 回應")
240
  image_output = gr.Image(label="AI 推薦美食圖", type="pil", width=400)
241
  map_output = gr.HTML(label="地圖導航")
242
 
243
+ # Tab 2: 組員功能
244
+ with gr.TabItem("情緒辨識 (組員功能)", id=1):
245
+ teammate_logic.render_ui() # 呼叫組員的介面渲染函式
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
 
247
+ # 事件綁定 (只保留主功能)
248
  submit_btn.click(
249
  fn=mood_agent_logic,
250
  inputs=[score_input, food_input, diary_input, debug_mode_btn],