SearXNG-Engine / app.py
Shreyas094's picture
Update app.py
ceb6868 verified
raw
history blame
2.87 kB
import gradio as gr
import requests
import json
import random
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
# List of SearXNG instances to try
SEARXNG_INSTANCES = [
"https://search.inetol.net",
"https://northboot.xyz",
"https://search.hbubli.cc",
"https://searx.tiekoetter.com",
"https://search.bus-hit.me",
# Add more instances here
]
def search_news(query, num_results=10):
# Shuffle the list of instances to distribute load
random.shuffle(SEARXNG_INSTANCES)
headers = {
"User-Agent": "SearXNG-NewsSearch/1.0"
}
for searxng_url in SEARXNG_INSTANCES:
params = {
"q": query,
"categories": "news",
"format": "json",
"language": "en",
"page": 1,
"engines": "google_news,bing_news,yahoo_news",
"count": num_results
# Omitted 'time_range'
}
try:
response = requests.get(f"{searxng_url}/search", params=params, headers=headers, timeout=10)
response.raise_for_status()
results = response.json()
news_items = results.get("news", [])
if news_items:
logging.info(f"Success from instance: {searxng_url}")
return news_items, None
except requests.RequestException as e:
logging.warning(f"Instance {searxng_url} failed: {e}")
continue # Try the next instance
return [], "Unable to fetch results from any SearXNG instance. Please try again later."
def format_news_markdown(news_items, error=None):
if error:
return f"**Error:** {error}"
if not news_items:
return "**No news items found.**"
formatted_results = ""
for i, item in enumerate(news_items, 1):
title = item.get('title', 'No Title')
url = item.get('url', '#')
published_date = item.get('published', 'N/A')
content = item.get('content', 'N/A')
formatted_results += f"### {i}. [{title}]({url})\n"
formatted_results += f"**Published:** {published_date}\n\n"
formatted_results += f"{content[:150]}...\n\n"
return formatted_results
def gradio_search_news(query, num_results):
news_items, error = search_news(query, int(num_results))
return format_news_markdown(news_items, error)
iface = gr.Interface(
fn=gradio_search_news,
inputs=[
gr.Textbox(label="Enter a news topic to search for", placeholder="e.g., Artificial Intelligence"),
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Number of results")
],
outputs=gr.Markdown(label="Search Results"),
title="SearXNG News Search",
description="Search for news articles using the SearXNG metasearch engine. If one instance fails, it will try others."
)
iface.launch()