Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,10 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# List of SearXNG instances to try
|
| 7 |
SEARXNG_INSTANCES = [
|
|
@@ -13,14 +17,14 @@ SEARXNG_INSTANCES = [
|
|
| 13 |
# Add more instances here
|
| 14 |
]
|
| 15 |
|
| 16 |
-
import logging
|
| 17 |
-
|
| 18 |
-
# Configure logging
|
| 19 |
-
logging.basicConfig(level=logging.INFO)
|
| 20 |
-
|
| 21 |
def search_news(query, num_results=10):
|
|
|
|
| 22 |
random.shuffle(SEARXNG_INSTANCES)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
for searxng_url in SEARXNG_INSTANCES:
|
| 25 |
params = {
|
| 26 |
"q": query,
|
|
@@ -30,9 +34,10 @@ def search_news(query, num_results=10):
|
|
| 30 |
"page": 1,
|
| 31 |
"engines": "google_news,bing_news,yahoo_news",
|
| 32 |
"count": num_results
|
|
|
|
| 33 |
}
|
| 34 |
try:
|
| 35 |
-
response = requests.get(f"{searxng_url}/search", params=params, timeout=10)
|
| 36 |
response.raise_for_status()
|
| 37 |
results = response.json()
|
| 38 |
news_items = results.get("news", [])
|
|
@@ -41,39 +46,32 @@ def search_news(query, num_results=10):
|
|
| 41 |
return news_items, None
|
| 42 |
except requests.RequestException as e:
|
| 43 |
logging.warning(f"Instance {searxng_url} failed: {e}")
|
| 44 |
-
continue
|
| 45 |
|
| 46 |
return [], "Unable to fetch results from any SearXNG instance. Please try again later."
|
| 47 |
|
| 48 |
-
|
| 49 |
-
def format_news(news_items, error=None):
|
| 50 |
if error:
|
| 51 |
-
return f"Error
|
| 52 |
|
| 53 |
if not news_items:
|
| 54 |
-
return "No news items found
|
| 55 |
|
| 56 |
formatted_results = ""
|
| 57 |
for i, item in enumerate(news_items, 1):
|
| 58 |
title = item.get('title', 'No Title')
|
| 59 |
-
url = item.get('url', '
|
| 60 |
-
published_date = item.get('published', 'N/A')
|
| 61 |
content = item.get('content', 'N/A')
|
| 62 |
|
| 63 |
-
formatted_results += f"{i}. {title}\n"
|
| 64 |
-
formatted_results += f"
|
| 65 |
-
formatted_results += f"
|
| 66 |
-
formatted_results += f" Content: {content[:150]}...\n\n"
|
| 67 |
return formatted_results
|
| 68 |
|
| 69 |
-
|
| 70 |
def gradio_search_news(query, num_results):
|
| 71 |
news_items, error = search_news(query, int(num_results))
|
| 72 |
-
|
| 73 |
-
# Optionally include instance information in the response
|
| 74 |
-
return format_news(news_items, error)
|
| 75 |
-
else:
|
| 76 |
-
return format_news(news_items, error)
|
| 77 |
|
| 78 |
iface = gr.Interface(
|
| 79 |
fn=gradio_search_news,
|
|
@@ -81,7 +79,7 @@ iface = gr.Interface(
|
|
| 81 |
gr.Textbox(label="Enter a news topic to search for", placeholder="e.g., Artificial Intelligence"),
|
| 82 |
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Number of results")
|
| 83 |
],
|
| 84 |
-
outputs=gr.
|
| 85 |
title="SearXNG News Search",
|
| 86 |
description="Search for news articles using the SearXNG metasearch engine. If one instance fails, it will try others."
|
| 87 |
)
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import random
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Configure logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
|
| 10 |
# List of SearXNG instances to try
|
| 11 |
SEARXNG_INSTANCES = [
|
|
|
|
| 17 |
# Add more instances here
|
| 18 |
]
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def search_news(query, num_results=10):
|
| 21 |
+
# Shuffle the list of instances to distribute load
|
| 22 |
random.shuffle(SEARXNG_INSTANCES)
|
| 23 |
|
| 24 |
+
headers = {
|
| 25 |
+
"User-Agent": "SearXNG-NewsSearch/1.0"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
for searxng_url in SEARXNG_INSTANCES:
|
| 29 |
params = {
|
| 30 |
"q": query,
|
|
|
|
| 34 |
"page": 1,
|
| 35 |
"engines": "google_news,bing_news,yahoo_news",
|
| 36 |
"count": num_results
|
| 37 |
+
# Omitted 'time_range'
|
| 38 |
}
|
| 39 |
try:
|
| 40 |
+
response = requests.get(f"{searxng_url}/search", params=params, headers=headers, timeout=10)
|
| 41 |
response.raise_for_status()
|
| 42 |
results = response.json()
|
| 43 |
news_items = results.get("news", [])
|
|
|
|
| 46 |
return news_items, None
|
| 47 |
except requests.RequestException as e:
|
| 48 |
logging.warning(f"Instance {searxng_url} failed: {e}")
|
| 49 |
+
continue # Try the next instance
|
| 50 |
|
| 51 |
return [], "Unable to fetch results from any SearXNG instance. Please try again later."
|
| 52 |
|
| 53 |
+
def format_news_markdown(news_items, error=None):
|
|
|
|
| 54 |
if error:
|
| 55 |
+
return f"**Error:** {error}"
|
| 56 |
|
| 57 |
if not news_items:
|
| 58 |
+
return "**No news items found.**"
|
| 59 |
|
| 60 |
formatted_results = ""
|
| 61 |
for i, item in enumerate(news_items, 1):
|
| 62 |
title = item.get('title', 'No Title')
|
| 63 |
+
url = item.get('url', '#')
|
| 64 |
+
published_date = item.get('published', 'N/A')
|
| 65 |
content = item.get('content', 'N/A')
|
| 66 |
|
| 67 |
+
formatted_results += f"### {i}. [{title}]({url})\n"
|
| 68 |
+
formatted_results += f"**Published:** {published_date}\n\n"
|
| 69 |
+
formatted_results += f"{content[:150]}...\n\n"
|
|
|
|
| 70 |
return formatted_results
|
| 71 |
|
|
|
|
| 72 |
def gradio_search_news(query, num_results):
|
| 73 |
news_items, error = search_news(query, int(num_results))
|
| 74 |
+
return format_news_markdown(news_items, error)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
iface = gr.Interface(
|
| 77 |
fn=gradio_search_news,
|
|
|
|
| 79 |
gr.Textbox(label="Enter a news topic to search for", placeholder="e.g., Artificial Intelligence"),
|
| 80 |
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Number of results")
|
| 81 |
],
|
| 82 |
+
outputs=gr.Markdown(label="Search Results"),
|
| 83 |
title="SearXNG News Search",
|
| 84 |
description="Search for news articles using the SearXNG metasearch engine. If one instance fails, it will try others."
|
| 85 |
)
|