Spaces:
Running
Running
import gradio as gr | |
import requests | |
import time | |
import random | |
def search_searx(query, instance_url='https://searx.org', categories='general', max_retries=3): | |
""" | |
Perform a search using the Searx API with error handling and retry logic. | |
""" | |
search_endpoint = f"{instance_url}/search" | |
params = { | |
'q': query, | |
'format': 'json', | |
'categories': categories, | |
'pageno': 1, | |
'time_range': '', | |
'engines': '', | |
'safesearch': '0' | |
} | |
headers = { | |
'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', | |
'Accept': 'application/json, text/javascript, */*; q=0.01', | |
'Accept-Language': 'en-US,en;q=0.5', | |
'Referer': instance_url, | |
'DNT': '1', | |
'Connection': 'keep-alive', | |
'Upgrade-Insecure-Requests': '1' | |
} | |
for attempt in range(max_retries): | |
try: | |
response = requests.get(search_endpoint, params=params, headers=headers, timeout=10) | |
response.raise_for_status() | |
data = response.json() | |
if 'results' not in data or not data['results']: | |
return "No results found." | |
formatted_results = "" | |
for idx, result in enumerate(data['results'], start=1): | |
title = result.get('title', 'No Title') | |
url = result.get('url', 'No URL') | |
snippet = result.get('content', 'No Description') | |
formatted_results += f"**{idx}. {title}**\n[{url}]({url})\n{snippet}\n\n" | |
return formatted_results | |
except requests.exceptions.RequestException as e: | |
if response.status_code == 429: | |
wait_time = 2 ** attempt + random.uniform(0, 1) | |
time.sleep(wait_time) | |
else: | |
return f"An error occurred while searching: {e}" | |
return "Max retries reached. Please try again later." | |
def create_gradio_interface(): | |
""" | |
Creates and returns the Gradio interface. | |
""" | |
with gr.Blocks() as demo: | |
gr.Markdown("# π΅οΈββοΈ Private Search with Searx and Gradio") | |
gr.Markdown( | |
"This application allows you to perform private searches using the [Searx](https://searx.org/) metasearch engine." | |
) | |
with gr.Row(): | |
with gr.Column(): | |
query = gr.Textbox( | |
label="Search Query", | |
placeholder="Enter your search query here...", | |
lines=1 | |
) | |
instance_url = gr.Textbox( | |
label="Searx Instance URL", | |
value="https://searx.org", | |
placeholder="https://searx.instance.url", | |
lines=1 | |
) | |
categories = gr.Textbox( | |
label="Categories", | |
value="general", | |
placeholder="e.g., general, images, videos", | |
lines=1 | |
) | |
search_button = gr.Button("Search") | |
with gr.Column(): | |
results = gr.Markdown("### Search Results will appear here...") | |
def perform_search(q, url, cats): | |
return search_searx(q, instance_url=url, categories=cats) | |
search_button.click( | |
perform_search, | |
inputs=[query, instance_url, categories], | |
outputs=results | |
) | |
gr.Markdown( | |
""" | |
--- | |
**Note:** This application uses the Searx metasearch engine to fetch results from multiple sources while preserving your privacy. | |
""" | |
) | |
return demo | |
iface = create_gradio_interface() | |
if __name__ == "__main__": | |
iface.launch() |