Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,73 @@
|
|
1 |
-
from langchain_community.utilities import SearxSearchWrapper
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def
|
14 |
-
|
15 |
-
|
16 |
-
# Perform the search
|
17 |
-
search_results = searx.results(query, num_results=num_results)
|
18 |
-
|
19 |
-
logger.info(f"Received {len(search_results)} results")
|
20 |
-
|
21 |
-
# Format the output
|
22 |
-
formatted_results = "Search Results:\n\n"
|
23 |
-
for i, result in enumerate(search_results, 1):
|
24 |
-
logger.debug(f"Processing result {i}: {result}")
|
25 |
-
|
26 |
-
title = result.get('title', 'No title')
|
27 |
-
link = result.get('link', 'No link')
|
28 |
-
snippet = result.get('snippet', 'No snippet available')
|
29 |
-
|
30 |
-
formatted_results += f"{i}. {title}\n"
|
31 |
-
formatted_results += f" URL: {link}\n"
|
32 |
-
formatted_results += f" Snippet: {snippet}\n\n"
|
33 |
-
|
34 |
-
return formatted_results
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
# Create Gradio interface
|
41 |
iface = gr.Interface(
|
42 |
-
fn=
|
43 |
inputs=[
|
44 |
gr.Textbox(label="Enter a news topic to search for"),
|
45 |
-
gr.Slider(minimum=1, maximum=
|
46 |
],
|
47 |
outputs=gr.Textbox(label="Search Results", lines=20),
|
48 |
-
title="News Search
|
49 |
-
description="Search for news articles using SearXNG
|
50 |
)
|
51 |
|
52 |
-
|
53 |
-
logger.info("Starting the application")
|
54 |
-
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://search.inetol.net",
|
9 |
+
"https://northboot.xyz",
|
10 |
+
"https://search.hbubli.cc",
|
11 |
+
"https://searx.tiekoetter.com",
|
12 |
+
"https://search.bus-hit.me",
|
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"
|
53 |
+
formatted_results += f" URL: {item['url']}\n"
|
54 |
+
formatted_results += f" Published: {item.get('publishedDate', 'N/A')}\n"
|
55 |
+
formatted_results += f" Content: {item.get('content', 'N/A')[:150]}...\n\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,
|
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()
|
|
|
|