Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,8 +13,12 @@ SEARXNG_INSTANCES = [
|
|
13 |
# Add more instances here
|
14 |
]
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
def search_news(query, num_results=10):
|
17 |
-
# Shuffle the list of instances to distribute load
|
18 |
random.shuffle(SEARXNG_INSTANCES)
|
19 |
|
20 |
for searxng_url in SEARXNG_INSTANCES:
|
@@ -22,24 +26,26 @@ def search_news(query, num_results=10):
|
|
22 |
"q": query,
|
23 |
"categories": "news",
|
24 |
"format": "json",
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"time_range": "None",
|
28 |
"engines": "google_news,bing_news,yahoo_news",
|
29 |
-
"
|
30 |
}
|
31 |
try:
|
32 |
response = requests.get(f"{searxng_url}/search", params=params, timeout=10)
|
33 |
response.raise_for_status()
|
34 |
results = response.json()
|
35 |
-
news_items = results.get("
|
36 |
if news_items:
|
|
|
37 |
return news_items, None
|
38 |
except requests.RequestException as e:
|
39 |
-
|
|
|
40 |
|
41 |
return [], "Unable to fetch results from any SearXNG instance. Please try again later."
|
42 |
|
|
|
43 |
def format_news(news_items, error=None):
|
44 |
if error:
|
45 |
return f"Error: {error}"
|
@@ -49,25 +55,35 @@ def format_news(news_items, error=None):
|
|
49 |
|
50 |
formatted_results = ""
|
51 |
for i, item in enumerate(news_items, 1):
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
return formatted_results
|
57 |
|
|
|
58 |
def gradio_search_news(query, num_results):
|
59 |
news_items, error = search_news(query, int(num_results))
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
|
62 |
iface = gr.Interface(
|
63 |
fn=gradio_search_news,
|
64 |
inputs=[
|
65 |
-
gr.Textbox(label="Enter a news topic to search for"),
|
66 |
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Number of results")
|
67 |
],
|
68 |
outputs=gr.Textbox(label="Search Results", lines=20),
|
69 |
title="SearXNG News Search",
|
70 |
-
description="Search for news articles using SearXNG metasearch engine. If one instance fails, it will try others."
|
71 |
)
|
72 |
|
73 |
iface.launch()
|
|
|
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:
|
|
|
26 |
"q": query,
|
27 |
"categories": "news",
|
28 |
"format": "json",
|
29 |
+
"language": "en",
|
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", [])
|
39 |
if news_items:
|
40 |
+
logging.info(f"Success from instance: {searxng_url}")
|
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: {error}"
|
|
|
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', 'No URL')
|
60 |
+
published_date = item.get('published', 'N/A') # Adjusted field name
|
61 |
+
content = item.get('content', 'N/A')
|
62 |
+
|
63 |
+
formatted_results += f"{i}. {title}\n"
|
64 |
+
formatted_results += f" URL: {url}\n"
|
65 |
+
formatted_results += f" Published: {published_date}\n"
|
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 |
+
if news_items:
|
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,
|
80 |
inputs=[
|
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.Textbox(label="Search Results", lines=20),
|
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 |
)
|
88 |
|
89 |
iface.launch()
|