Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -77,26 +77,50 @@ def text_to_speech(text: str) -> bytes:
|
|
| 77 |
return b""
|
| 78 |
|
| 79 |
# =========================
|
| 80 |
-
# ๐ SerpAPI with
|
| 81 |
# =========================
|
| 82 |
def serpapi_search(query: str, location=None, num_results=3):
|
| 83 |
print(f"\n[SEARCH] ๐ Starting search for: '{query}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
url = "https://serpapi.com/search.json"
|
| 85 |
params = {
|
| 86 |
"q": query,
|
| 87 |
-
"location":
|
| 88 |
"engine": "google",
|
| 89 |
"api_key": SERPAPI_KEY,
|
| 90 |
-
"num": num_results
|
|
|
|
|
|
|
| 91 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
try:
|
|
|
|
|
|
|
| 93 |
r = requests.get(url, params=params, timeout=10)
|
| 94 |
r.raise_for_status()
|
| 95 |
data = r.json()
|
| 96 |
|
| 97 |
-
results = []
|
| 98 |
-
images = []
|
| 99 |
-
|
| 100 |
if "organic_results" in data:
|
| 101 |
print(f"[SEARCH] ๐ Found {len(data['organic_results'])} organic results")
|
| 102 |
for item in data["organic_results"][:num_results]:
|
|
@@ -105,19 +129,34 @@ def serpapi_search(query: str, location=None, num_results=3):
|
|
| 105 |
"snippet": item.get("snippet", ""),
|
| 106 |
"link": item.get("link", "")
|
| 107 |
})
|
| 108 |
-
print(f"[SEARCH] - {item.get('title', 'No title')}")
|
| 109 |
else:
|
| 110 |
print("[SEARCH] โ ๏ธ No organic results found")
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if img_url:
|
| 117 |
images.append(img_url)
|
| 118 |
print(f"[SEARCH] - Image: {img_url[:60]}...")
|
| 119 |
else:
|
| 120 |
-
print("[SEARCH] โ ๏ธ No images found")
|
| 121 |
|
| 122 |
print(f"[SEARCH] โ
Total: {len(results)} results, {len(images)} images\n")
|
| 123 |
return {"results": results, "images": images}
|
|
@@ -191,7 +230,7 @@ def chat():
|
|
| 191 |
|
| 192 |
print(f"[USER] ๐ค Transcribed: '{user_text}'")
|
| 193 |
|
| 194 |
-
keywords = ["hotel", "mall", "resort", "villa", "tempat wisata"]
|
| 195 |
has_keyword = any(k in user_text.lower() for k in keywords)
|
| 196 |
|
| 197 |
if has_keyword:
|
|
@@ -234,7 +273,7 @@ def chat():
|
|
| 234 |
print(f"[USER] ๐ค Prompt: '{prompt}'")
|
| 235 |
print(f"[HISTORY] ๐ History length: {len(history)} messages")
|
| 236 |
|
| 237 |
-
keywords = ["hotel", "mall", "resort", "villa", "tempat wisata"]
|
| 238 |
has_keyword = any(k in prompt.lower() for k in keywords)
|
| 239 |
|
| 240 |
if has_keyword:
|
|
@@ -268,7 +307,8 @@ def chat():
|
|
| 268 |
if __name__ == "__main__":
|
| 269 |
print("\n" + "="*60)
|
| 270 |
print("๐ Vibow Talk GTE Server Running")
|
| 271 |
-
print("๐ Search keywords: hotel, mall, resort, villa, tempat wisata")
|
| 272 |
print("๐ผ๏ธ Image extraction: ENABLED")
|
|
|
|
| 273 |
print("="*60 + "\n")
|
| 274 |
app.run(host="0.0.0.0", port=7860, debug=True, threaded=True)
|
|
|
|
| 77 |
return b""
|
| 78 |
|
| 79 |
# =========================
|
| 80 |
+
# ๐ SerpAPI with Global Search + Auto-detect Region
|
| 81 |
# =========================
|
| 82 |
def serpapi_search(query: str, location=None, num_results=3):
|
| 83 |
print(f"\n[SEARCH] ๐ Starting search for: '{query}'")
|
| 84 |
+
|
| 85 |
+
# Detect Indonesian keywords to determine if search should be ID-focused
|
| 86 |
+
indonesian_keywords = ["di jakarta", "di bali", "di bekasi", "di surabaya", "di bandung",
|
| 87 |
+
"di indonesia", "di yogyakarta", "di medan", "di semarang",
|
| 88 |
+
"termurah", "terbaik di", "dekat", "murah"]
|
| 89 |
+
is_indonesian_query = any(kw in query.lower() for kw in indonesian_keywords)
|
| 90 |
+
|
| 91 |
+
# Set location and language based on query
|
| 92 |
+
if is_indonesian_query:
|
| 93 |
+
country = "id" # Indonesia
|
| 94 |
+
lang = "id"
|
| 95 |
+
search_location = location or "Indonesia"
|
| 96 |
+
print(f"[SEARCH] ๐ฎ๐ฉ Indonesian query detected - Using ID region")
|
| 97 |
+
else:
|
| 98 |
+
country = "us" # Global (US default)
|
| 99 |
+
lang = "en"
|
| 100 |
+
search_location = location or ""
|
| 101 |
+
print(f"[SEARCH] ๐ Global query - Using worldwide search")
|
| 102 |
+
|
| 103 |
url = "https://serpapi.com/search.json"
|
| 104 |
params = {
|
| 105 |
"q": query,
|
| 106 |
+
"location": search_location,
|
| 107 |
"engine": "google",
|
| 108 |
"api_key": SERPAPI_KEY,
|
| 109 |
+
"num": num_results,
|
| 110 |
+
"gl": country,
|
| 111 |
+
"hl": lang
|
| 112 |
}
|
| 113 |
+
|
| 114 |
+
results = []
|
| 115 |
+
images = []
|
| 116 |
+
|
| 117 |
try:
|
| 118 |
+
# Get text results
|
| 119 |
+
print("[SEARCH] ๐ Fetching text results...")
|
| 120 |
r = requests.get(url, params=params, timeout=10)
|
| 121 |
r.raise_for_status()
|
| 122 |
data = r.json()
|
| 123 |
|
|
|
|
|
|
|
|
|
|
| 124 |
if "organic_results" in data:
|
| 125 |
print(f"[SEARCH] ๐ Found {len(data['organic_results'])} organic results")
|
| 126 |
for item in data["organic_results"][:num_results]:
|
|
|
|
| 129 |
"snippet": item.get("snippet", ""),
|
| 130 |
"link": item.get("link", "")
|
| 131 |
})
|
| 132 |
+
print(f"[SEARCH] - {item.get('title', 'No title')[:60]}")
|
| 133 |
else:
|
| 134 |
print("[SEARCH] โ ๏ธ No organic results found")
|
| 135 |
|
| 136 |
+
# Get images - separate request for better results
|
| 137 |
+
img_params = {
|
| 138 |
+
"q": query,
|
| 139 |
+
"engine": "google_images",
|
| 140 |
+
"api_key": SERPAPI_KEY,
|
| 141 |
+
"num": 3,
|
| 142 |
+
"gl": country,
|
| 143 |
+
"hl": lang
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
print("[SEARCH] ๐ผ๏ธ Fetching images...")
|
| 147 |
+
img_r = requests.get(url, params=img_params, timeout=10)
|
| 148 |
+
img_r.raise_for_status()
|
| 149 |
+
img_data = img_r.json()
|
| 150 |
+
|
| 151 |
+
if "images_results" in img_data:
|
| 152 |
+
print(f"[SEARCH] ๐ผ๏ธ Found {len(img_data['images_results'])} images")
|
| 153 |
+
for img in img_data["images_results"][:3]:
|
| 154 |
+
img_url = img.get("original", img.get("thumbnail", ""))
|
| 155 |
if img_url:
|
| 156 |
images.append(img_url)
|
| 157 |
print(f"[SEARCH] - Image: {img_url[:60]}...")
|
| 158 |
else:
|
| 159 |
+
print("[SEARCH] โ ๏ธ No images found in image search")
|
| 160 |
|
| 161 |
print(f"[SEARCH] โ
Total: {len(results)} results, {len(images)} images\n")
|
| 162 |
return {"results": results, "images": images}
|
|
|
|
| 230 |
|
| 231 |
print(f"[USER] ๐ค Transcribed: '{user_text}'")
|
| 232 |
|
| 233 |
+
keywords = ["hotel", "mall", "resort", "villa", "tempat wisata", "restaurant", "cafe"]
|
| 234 |
has_keyword = any(k in user_text.lower() for k in keywords)
|
| 235 |
|
| 236 |
if has_keyword:
|
|
|
|
| 273 |
print(f"[USER] ๐ค Prompt: '{prompt}'")
|
| 274 |
print(f"[HISTORY] ๐ History length: {len(history)} messages")
|
| 275 |
|
| 276 |
+
keywords = ["hotel", "mall", "resort", "villa", "tempat wisata", "restaurant", "cafe"]
|
| 277 |
has_keyword = any(k in prompt.lower() for k in keywords)
|
| 278 |
|
| 279 |
if has_keyword:
|
|
|
|
| 307 |
if __name__ == "__main__":
|
| 308 |
print("\n" + "="*60)
|
| 309 |
print("๐ Vibow Talk GTE Server Running")
|
| 310 |
+
print("๐ Search keywords: hotel, mall, resort, villa, tempat wisata, restaurant, cafe")
|
| 311 |
print("๐ผ๏ธ Image extraction: ENABLED")
|
| 312 |
+
print("๐ Global search: ENABLED (auto-detect region)")
|
| 313 |
print("="*60 + "\n")
|
| 314 |
app.run(host="0.0.0.0", port=7860, debug=True, threaded=True)
|