Spaces:
Sleeping
Sleeping
Update search_utils.py
Browse files- search_utils.py +16 -8
search_utils.py
CHANGED
@@ -1,9 +1,13 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
|
|
|
4 |
SERPAPI_KEY = "e41a265c89513f03e569eda056f6f50374332cd6c76feb1662baf401c7adb564"
|
5 |
|
6 |
def web_search(query, num_results=3):
|
|
|
|
|
|
|
7 |
url = "https://serpapi.com/search"
|
8 |
params = {
|
9 |
"engine": "google",
|
@@ -12,12 +16,16 @@ def web_search(query, num_results=3):
|
|
12 |
"num": num_results
|
13 |
}
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
for res in results if res.get("title") and res.get("snippet")
|
23 |
-
]
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
|
4 |
+
# 🔑 Replace with your own SERPAPI key if needed
|
5 |
SERPAPI_KEY = "e41a265c89513f03e569eda056f6f50374332cd6c76feb1662baf401c7adb564"
|
6 |
|
7 |
def web_search(query, num_results=3):
|
8 |
+
"""
|
9 |
+
Perform a real-time web search using SerpAPI and return top result snippets.
|
10 |
+
"""
|
11 |
url = "https://serpapi.com/search"
|
12 |
params = {
|
13 |
"engine": "google",
|
|
|
16 |
"num": num_results
|
17 |
}
|
18 |
|
19 |
+
try:
|
20 |
+
response = requests.get(url, params=params)
|
21 |
+
response.raise_for_status()
|
22 |
+
results = response.json().get("organic_results", [])
|
23 |
+
formatted_results = [
|
24 |
+
f"{res.get('title')}: {res.get('snippet')}"
|
25 |
+
for res in results if res.get("title") and res.get("snippet")
|
26 |
+
]
|
27 |
+
return formatted_results
|
28 |
|
29 |
+
except Exception as e:
|
30 |
+
print(f"[Search Error] {e}")
|
31 |
+
return []
|
|
|
|