SearXNG-Engine / app.py
Shreyas094's picture
Update app.py
c969677 verified
raw
history blame
2.87 kB
import gradio as gr
import requests
import json
import random
# 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
]
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
def search_news(query, num_results=10):
random.shuffle(SEARXNG_INSTANCES)
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
}
try:
response = requests.get(f"{searxng_url}/search", params=params, 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
return [], "Unable to fetch results from any SearXNG instance. Please try again later."
def format_news(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', 'No URL')
published_date = item.get('published', 'N/A') # Adjusted field name
content = item.get('content', 'N/A')
formatted_results += f"{i}. {title}\n"
formatted_results += f" URL: {url}\n"
formatted_results += f" Published: {published_date}\n"
formatted_results += f" Content: {content[:150]}...\n\n"
return formatted_results
def gradio_search_news(query, num_results):
news_items, error = search_news(query, int(num_results))
if news_items:
# Optionally include instance information in the response
return format_news(news_items, error)
else:
return format_news(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.Textbox(label="Search Results", lines=20),
title="SearXNG News Search",
description="Search for news articles using the SearXNG metasearch engine. If one instance fails, it will try others."
)
iface.launch()