Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -86,27 +86,53 @@ def sync_google_maps(url):
|
|
| 86 |
page = browser.new_page()
|
| 87 |
yield "🌐 [2/4] 連線中..."
|
| 88 |
page.goto(clean_url, wait_until="domcontentloaded", timeout=60000)
|
| 89 |
-
yield "⏳ [3/4] 等待加載 (10秒)..."
|
| 90 |
time.sleep(10)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
restaurant_list = []
|
| 96 |
-
for
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
restaurant_list.append({
|
| 101 |
"Name": name,
|
| 102 |
"Address": addr,
|
|
|
|
| 103 |
"RAG_Content": f"餐廳:{name},地址:{addr}",
|
| 104 |
"URL": f"https://www.google.com/search?q={urllib.parse.quote(name)}"
|
| 105 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
browser.close()
|
| 107 |
|
| 108 |
if not restaurant_list:
|
| 109 |
-
yield "⚠️ 找不到餐廳。請確認連結已設為「共用」。"
|
| 110 |
return
|
| 111 |
|
| 112 |
global_df = pd.DataFrame(restaurant_list)
|
|
@@ -114,7 +140,7 @@ def sync_google_maps(url):
|
|
| 114 |
vectorstore = FAISS.from_texts(global_df['RAG_Content'].tolist(), embeddings, metadatas=global_df.to_dict('records'))
|
| 115 |
global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 116 |
rag_initialized = True
|
| 117 |
-
yield f"✅ 同步成功!共有 {len(restaurant_list)} 間餐廳。"
|
| 118 |
except Exception as e:
|
| 119 |
yield f"❌ 系統錯誤:{str(e)}"
|
| 120 |
|
|
@@ -175,24 +201,28 @@ def mood_agent_logic(score_input, food_input, diary_input, debug_mode):
|
|
| 175 |
yield "⚠️ 請先同步地圖清單!", None, ""
|
| 176 |
return
|
| 177 |
|
|
|
|
| 178 |
restaurant, is_match, rec_categories, mood_reason, all_names = get_restaurant_data(score_input, food_input)
|
| 179 |
|
| 180 |
-
# 增加告知訊息
|
| 181 |
prefix = ""
|
| 182 |
status_hint = ""
|
| 183 |
if not is_match and food_input != "隨便":
|
| 184 |
prefix = f"> 💡 **溫馨提示**:您的清單目前只有:**{all_names}**,沒找到符合『{food_input}』的店。我先幫您從現有名單挑一間最讚的!\n\n"
|
| 185 |
status_hint = f"\n(注意:清單中無 '{food_input}',請幽默解釋為何先推薦這間。)"
|
| 186 |
|
| 187 |
-
name = restaurant['Name']
|
|
|
|
|
|
|
| 188 |
rag_info = str(restaurant.get('RAG_Content', '')) + status_hint
|
| 189 |
|
|
|
|
| 190 |
ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason)
|
| 191 |
|
| 192 |
final_response = f"{prefix}### 🍽️ 推薦:{name}\n\n{ai_text}"
|
| 193 |
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">🗺️ 導航</a></div>'
|
| 194 |
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
# ==========================================
|
| 198 |
# 3. 橋接邏輯 (保留原本情緒辨識部分)
|
|
|
|
| 86 |
page = browser.new_page()
|
| 87 |
yield "🌐 [2/4] 連線中..."
|
| 88 |
page.goto(clean_url, wait_until="domcontentloaded", timeout=60000)
|
| 89 |
+
yield "⏳ [3/4] 等待列表完整加載 (10秒)..."
|
| 90 |
time.sleep(10)
|
| 91 |
+
|
| 92 |
+
yield "📄 [4/4] 解析餐廳資訊與圖片..."
|
| 93 |
+
|
| 94 |
+
# 改用容器式爬取,確保資料對齊
|
| 95 |
+
# Google Maps 列表項目通常在 [role="article"] 或特定的 div 內
|
| 96 |
+
# 這裡使用更通用的定位方式找尋包含名稱的區塊
|
| 97 |
+
containers = page.locator('div.m67Hec').all()
|
| 98 |
|
| 99 |
restaurant_list = []
|
| 100 |
+
for item in containers:
|
| 101 |
+
try:
|
| 102 |
+
# 抓取名稱
|
| 103 |
+
name_el = item.locator('div.fontHeadlineSmall')
|
| 104 |
+
if name_el.count() == 0: continue
|
| 105 |
+
name = name_el.inner_text().strip()
|
| 106 |
+
|
| 107 |
+
# 抓取地址/描述
|
| 108 |
+
addr_el = item.locator('div.fontBodyMedium')
|
| 109 |
+
addr = addr_el.inner_text().strip() if addr_el.count() > 0 else "詳情見地圖"
|
| 110 |
+
|
| 111 |
+
# 抓取圖片連結 (抓取 img 標籤的 src)
|
| 112 |
+
img_el = item.locator('img')
|
| 113 |
+
img_url = None
|
| 114 |
+
if img_el.count() > 0:
|
| 115 |
+
img_url = img_el.get_attribute('src')
|
| 116 |
+
|
| 117 |
+
# 如果沒抓到圖片,給一個預設的餐廳占位圖
|
| 118 |
+
if not img_url or "http" not in img_url:
|
| 119 |
+
img_url = "https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=500"
|
| 120 |
+
|
| 121 |
restaurant_list.append({
|
| 122 |
"Name": name,
|
| 123 |
"Address": addr,
|
| 124 |
+
"Image_URL": img_url, # 儲存圖片網址
|
| 125 |
"RAG_Content": f"餐廳:{name},地址:{addr}",
|
| 126 |
"URL": f"https://www.google.com/search?q={urllib.parse.quote(name)}"
|
| 127 |
})
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"解析單筆資料出錯: {e}")
|
| 130 |
+
continue
|
| 131 |
+
|
| 132 |
browser.close()
|
| 133 |
|
| 134 |
if not restaurant_list:
|
| 135 |
+
yield "⚠️ 找不到餐廳。請確認連結已設為「共用」且頁面上有顯示列表。"
|
| 136 |
return
|
| 137 |
|
| 138 |
global_df = pd.DataFrame(restaurant_list)
|
|
|
|
| 140 |
vectorstore = FAISS.from_texts(global_df['RAG_Content'].tolist(), embeddings, metadatas=global_df.to_dict('records'))
|
| 141 |
global_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 142 |
rag_initialized = True
|
| 143 |
+
yield f"✅ 同步成功!共有 {len(restaurant_list)} 間餐廳(已含圖片)。"
|
| 144 |
except Exception as e:
|
| 145 |
yield f"❌ 系統錯誤:{str(e)}"
|
| 146 |
|
|
|
|
| 201 |
yield "⚠️ 請先同步地圖清單!", None, ""
|
| 202 |
return
|
| 203 |
|
| 204 |
+
# 從資料庫抽樣一間餐廳
|
| 205 |
restaurant, is_match, rec_categories, mood_reason, all_names = get_restaurant_data(score_input, food_input)
|
| 206 |
|
|
|
|
| 207 |
prefix = ""
|
| 208 |
status_hint = ""
|
| 209 |
if not is_match and food_input != "隨便":
|
| 210 |
prefix = f"> 💡 **溫馨提示**:您的清單目前只有:**{all_names}**,沒找到符合『{food_input}』的店。我先幫您從現有名單挑一間最讚的!\n\n"
|
| 211 |
status_hint = f"\n(注意:清單中無 '{food_input}',請幽默解釋為何先推薦這間。)"
|
| 212 |
|
| 213 |
+
name = restaurant['Name']
|
| 214 |
+
url = restaurant.get('URL', '#')
|
| 215 |
+
img_url = restaurant.get('Image_URL') # 取得我們剛抓到的圖片網址
|
| 216 |
rag_info = str(restaurant.get('RAG_Content', '')) + status_hint
|
| 217 |
|
| 218 |
+
# 呼叫 Groq 生成文字
|
| 219 |
ai_text = generate_content_with_groq(name, rag_info, diary_input, score_input, mood_reason)
|
| 220 |
|
| 221 |
final_response = f"{prefix}### 🍽️ 推薦:{name}\n\n{ai_text}"
|
| 222 |
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">🗺️ 導航</a></div>'
|
| 223 |
|
| 224 |
+
# 修改點:這裡的第二個回傳值改為 img_url,Gradio 會自動加載該網址圖片
|
| 225 |
+
yield final_response, img_url, map_html
|
| 226 |
|
| 227 |
# ==========================================
|
| 228 |
# 3. 橋接邏輯 (保留原本情緒辨識部分)
|