Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,9 @@ 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
|
9 |
"""
|
10 |
search_endpoint = f"{instance_url}/search"
|
11 |
params = {
|
@@ -15,7 +15,8 @@ def search_searx(query, instance_url='https://searx.org', categories='general',
|
|
15 |
'pageno': 1,
|
16 |
'time_range': '',
|
17 |
'engines': '',
|
18 |
-
'safesearch': '0'
|
|
|
19 |
}
|
20 |
|
21 |
headers = {
|
@@ -38,11 +39,15 @@ def search_searx(query, instance_url='https://searx.org', categories='general',
|
|
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 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
return formatted_results
|
48 |
except requests.exceptions.RequestException as e:
|
@@ -82,16 +87,23 @@ def create_gradio_interface():
|
|
82 |
placeholder="e.g., general, images, videos",
|
83 |
lines=1
|
84 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
search_button = gr.Button("Search")
|
86 |
with gr.Column():
|
87 |
results = gr.Markdown("### Search Results will appear here...")
|
88 |
|
89 |
-
def perform_search(q, url, cats):
|
90 |
-
return search_searx(q, instance_url=url, categories=cats)
|
91 |
|
92 |
search_button.click(
|
93 |
perform_search,
|
94 |
-
inputs=[query, instance_url, categories],
|
95 |
outputs=results
|
96 |
)
|
97 |
|
|
|
3 |
import time
|
4 |
import random
|
5 |
|
6 |
+
def search_searx(query, instance_url='https://searx.org', categories='general', max_retries=3, num_results=10):
|
7 |
"""
|
8 |
+
Perform a search using the Searx API with error handling, retry logic, and limited results.
|
9 |
"""
|
10 |
search_endpoint = f"{instance_url}/search"
|
11 |
params = {
|
|
|
15 |
'pageno': 1,
|
16 |
'time_range': '',
|
17 |
'engines': '',
|
18 |
+
'safesearch': '0',
|
19 |
+
'results': str(num_results) # Limit the number of results
|
20 |
}
|
21 |
|
22 |
headers = {
|
|
|
39 |
return "No results found."
|
40 |
|
41 |
formatted_results = ""
|
42 |
+
for idx, result in enumerate(data['results'][:num_results], start=1):
|
43 |
title = result.get('title', 'No Title')
|
44 |
url = result.get('url', 'No URL')
|
45 |
snippet = result.get('content', 'No Description')
|
46 |
+
|
47 |
+
# Try to get a longer snippet if available
|
48 |
+
long_content = result.get('long_content', snippet)
|
49 |
+
|
50 |
+
formatted_results += f"**{idx}. {title}**\n[{url}]({url})\n{long_content}\n\n"
|
51 |
|
52 |
return formatted_results
|
53 |
except requests.exceptions.RequestException as e:
|
|
|
87 |
placeholder="e.g., general, images, videos",
|
88 |
lines=1
|
89 |
)
|
90 |
+
num_results = gr.Slider(
|
91 |
+
minimum=1,
|
92 |
+
maximum=20,
|
93 |
+
value=10,
|
94 |
+
step=1,
|
95 |
+
label="Number of Results"
|
96 |
+
)
|
97 |
search_button = gr.Button("Search")
|
98 |
with gr.Column():
|
99 |
results = gr.Markdown("### Search Results will appear here...")
|
100 |
|
101 |
+
def perform_search(q, url, cats, num):
|
102 |
+
return search_searx(q, instance_url=url, categories=cats, num_results=int(num))
|
103 |
|
104 |
search_button.click(
|
105 |
perform_search,
|
106 |
+
inputs=[query, instance_url, categories, num_results],
|
107 |
outputs=results
|
108 |
)
|
109 |
|