Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def search_news(query, num_results=10):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
def format_news(news_items):
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
formatted_results = ""
|
28 |
for i, item in enumerate(news_items, 1):
|
29 |
formatted_results += f"{i}. {item['title']}\n"
|
@@ -33,8 +56,8 @@ def format_news(news_items):
|
|
33 |
return formatted_results
|
34 |
|
35 |
def gradio_search_news(query, num_results):
|
36 |
-
news_items = search_news(query, int(num_results))
|
37 |
-
return format_news(news_items)
|
38 |
|
39 |
iface = gr.Interface(
|
40 |
fn=gradio_search_news,
|
@@ -44,7 +67,7 @@ iface = gr.Interface(
|
|
44 |
],
|
45 |
outputs=gr.Textbox(label="Search Results", lines=20),
|
46 |
title="SearXNG News Search",
|
47 |
-
description="Search for news articles using SearXNG metasearch engine."
|
48 |
)
|
49 |
|
50 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
4 |
+
import random
|
5 |
+
|
6 |
+
# List of SearXNG instances to try
|
7 |
+
SEARXNG_INSTANCES = [
|
8 |
+
"https://searx.be",
|
9 |
+
"https://searx.tiekoetter.com",
|
10 |
+
"https://searx.thegpm.org",
|
11 |
+
"https://searx.ebnar.xyz",
|
12 |
+
"https://searx.feneas.org",
|
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:
|
21 |
+
params = {
|
22 |
+
"q": query,
|
23 |
+
"categories": "news",
|
24 |
+
"format": "json",
|
25 |
+
"lang": "en",
|
26 |
+
"pageno": 1,
|
27 |
+
"time_range": "None",
|
28 |
+
"engines": "google_news,bing_news,yahoo_news",
|
29 |
+
"results": num_results
|
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("results", [])
|
36 |
+
if news_items:
|
37 |
+
return news_items, None
|
38 |
+
except requests.RequestException as e:
|
39 |
+
continue # Try the next instance
|
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}"
|
46 |
+
|
47 |
+
if not news_items:
|
48 |
+
return "No news items found."
|
49 |
+
|
50 |
formatted_results = ""
|
51 |
for i, item in enumerate(news_items, 1):
|
52 |
formatted_results += f"{i}. {item['title']}\n"
|
|
|
56 |
return formatted_results
|
57 |
|
58 |
def gradio_search_news(query, num_results):
|
59 |
+
news_items, error = search_news(query, int(num_results))
|
60 |
+
return format_news(news_items, error)
|
61 |
|
62 |
iface = gr.Interface(
|
63 |
fn=gradio_search_news,
|
|
|
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()
|