Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,21 @@
|
|
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 |
|
8 |
:param query: The search query string.
|
9 |
:param instance_url: The URL of the Searx instance.
|
@@ -20,9 +32,17 @@ def search_searx(query, instance_url='https://searx.org', categories='general'):
|
|
20 |
'engines': '', # Specify search engines, comma-separated
|
21 |
'safesearch': '0' # Safe search (0: off, 1: moderate, 2: strict)
|
22 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
try:
|
25 |
-
response = requests.get(search_endpoint, params=params, timeout=10)
|
|
|
|
|
26 |
response.raise_for_status() # Raise an error for bad status codes
|
27 |
data = response.json()
|
28 |
|
@@ -34,12 +54,18 @@ def search_searx(query, instance_url='https://searx.org', categories='general'):
|
|
34 |
title = result.get('title', 'No Title')
|
35 |
url = result.get('url', 'No URL')
|
36 |
snippet = result.get('content', 'No Description')
|
37 |
-
|
|
|
38 |
|
39 |
return formatted_results
|
40 |
|
|
|
|
|
|
|
41 |
except requests.exceptions.RequestException as e:
|
42 |
return f"An error occurred while searching: {e}"
|
|
|
|
|
43 |
|
44 |
def create_gradio_interface():
|
45 |
"""
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type
|
4 |
+
import json
|
5 |
|
6 |
+
# Define custom exceptions for clarity
|
7 |
+
class RateLimitException(Exception):
|
8 |
+
pass
|
9 |
+
|
10 |
+
# Retry configuration: Retry up to 5 times with exponential backoff
|
11 |
+
@retry(
|
12 |
+
wait=wait_exponential(multiplier=1, min=4, max=10),
|
13 |
+
stop=stop_after_attempt(5),
|
14 |
+
retry=retry_if_exception_type(RateLimitException)
|
15 |
+
)
|
16 |
def search_searx(query, instance_url='https://searx.org', categories='general'):
|
17 |
"""
|
18 |
+
Perform a search using the Searx API with retry logic.
|
19 |
|
20 |
:param query: The search query string.
|
21 |
:param instance_url: The URL of the Searx instance.
|
|
|
32 |
'engines': '', # Specify search engines, comma-separated
|
33 |
'safesearch': '0' # Safe search (0: off, 1: moderate, 2: strict)
|
34 |
}
|
35 |
+
|
36 |
+
headers = {
|
37 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' +
|
38 |
+
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
|
39 |
+
'Chrome/58.0.3029.110 Safari/537.3'
|
40 |
+
}
|
41 |
|
42 |
try:
|
43 |
+
response = requests.get(search_endpoint, params=params, headers=headers, timeout=10)
|
44 |
+
if response.status_code == 429:
|
45 |
+
raise RateLimitException("Rate limit exceeded. Retrying...")
|
46 |
response.raise_for_status() # Raise an error for bad status codes
|
47 |
data = response.json()
|
48 |
|
|
|
54 |
title = result.get('title', 'No Title')
|
55 |
url = result.get('url', 'No URL')
|
56 |
snippet = result.get('content', 'No Description')
|
57 |
+
# Using Markdown link formatting
|
58 |
+
formatted_results += f"**{idx}. [{title}]({url})**\n{snippet}\n\n"
|
59 |
|
60 |
return formatted_results
|
61 |
|
62 |
+
except RateLimitException as e:
|
63 |
+
# After retries, if still failing due to rate limits
|
64 |
+
return f"Rate limit exceeded. Please try again later.\nError: {e}"
|
65 |
except requests.exceptions.RequestException as e:
|
66 |
return f"An error occurred while searching: {e}"
|
67 |
+
except json.JSONDecodeError:
|
68 |
+
return "Failed to parse the response from the Searx instance."
|
69 |
|
70 |
def create_gradio_interface():
|
71 |
"""
|