Spaces:
Sleeping
Sleeping
File size: 2,737 Bytes
f02770e 409c813 ceb6868 23d2217 409c813 e4ad246 409c813 617c3a2 409c813 ceb6868 409c813 ceb6868 409c813 c969677 409c813 c969677 ceb6868 409c813 ceb6868 409c813 c969677 409c813 c969677 409c813 c969677 ceb6868 409c813 00536e4 ceb6868 409c813 ceb6868 617c3a2 409c813 ceb6868 409c813 c969677 ceb6868 c969677 ceb6868 409c813 ceb6868 f02770e 409c813 f02770e c969677 409c813 f02770e ceb6868 409c813 c969677 f02770e 409c813 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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://searx.work",
# 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() |