Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
|
|
3 |
|
4 |
-
def search_searx(query, instance_url='https://searx.org', categories='general'):
|
5 |
"""
|
6 |
-
Perform a search using the Searx API.
|
7 |
-
:param query: The search query string.
|
8 |
-
:param instance_url: The URL of the Searx instance.
|
9 |
-
:param categories: Categories to search in (e.g., 'general', 'images', 'videos').
|
10 |
-
:return: A list of formatted search results or an error message.
|
11 |
"""
|
12 |
search_endpoint = f"{instance_url}/search"
|
13 |
params = {
|
14 |
'q': query,
|
15 |
'format': 'json',
|
16 |
'categories': categories,
|
17 |
-
'pageno': 1,
|
18 |
-
'time_range': '',
|
19 |
-
'engines': '',
|
20 |
-
'safesearch': '0'
|
21 |
}
|
22 |
|
23 |
-
# Define a user agent
|
24 |
headers = {
|
25 |
-
'User-Agent': '
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
def create_gradio_interface():
|
48 |
"""
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import time
|
4 |
+
import random
|
5 |
|
6 |
+
def search_searx(query, instance_url='https://searx.org', categories='general', max_retries=3):
|
7 |
"""
|
8 |
+
Perform a search using the Searx API with error handling and retry logic.
|
|
|
|
|
|
|
|
|
9 |
"""
|
10 |
search_endpoint = f"{instance_url}/search"
|
11 |
params = {
|
12 |
'q': query,
|
13 |
'format': 'json',
|
14 |
'categories': categories,
|
15 |
+
'pageno': 1,
|
16 |
+
'time_range': '',
|
17 |
+
'engines': '',
|
18 |
+
'safesearch': '0'
|
19 |
}
|
20 |
|
|
|
21 |
headers = {
|
22 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
23 |
+
'Accept': 'application/json, text/javascript, */*; q=0.01',
|
24 |
+
'Accept-Language': 'en-US,en;q=0.5',
|
25 |
+
'Referer': instance_url,
|
26 |
+
'DNT': '1',
|
27 |
+
'Connection': 'keep-alive',
|
28 |
+
'Upgrade-Insecure-Requests': '1'
|
29 |
}
|
30 |
|
31 |
+
for attempt in range(max_retries):
|
32 |
+
try:
|
33 |
+
response = requests.get(search_endpoint, params=params, headers=headers, timeout=10)
|
34 |
+
response.raise_for_status()
|
35 |
+
data = response.json()
|
36 |
+
|
37 |
+
if 'results' not in data or not data['results']:
|
38 |
+
return "No results found."
|
39 |
+
|
40 |
+
formatted_results = ""
|
41 |
+
for idx, result in enumerate(data['results'], start=1):
|
42 |
+
title = result.get('title', 'No Title')
|
43 |
+
url = result.get('url', 'No URL')
|
44 |
+
snippet = result.get('content', 'No Description')
|
45 |
+
formatted_results += f"**{idx}. {title}**\n[{url}]({url})\n{snippet}\n\n"
|
46 |
+
|
47 |
+
return formatted_results
|
48 |
+
except requests.exceptions.RequestException as e:
|
49 |
+
if response.status_code == 429:
|
50 |
+
wait_time = 2 ** attempt + random.uniform(0, 1)
|
51 |
+
time.sleep(wait_time)
|
52 |
+
else:
|
53 |
+
return f"An error occurred while searching: {e}"
|
54 |
+
|
55 |
+
return "Max retries reached. Please try again later."
|
56 |
|
57 |
def create_gradio_interface():
|
58 |
"""
|