Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,37 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
-
import random
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
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=
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
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=
|
64 |
inputs=[
|
65 |
gr.Textbox(label="Enter a news topic to search for"),
|
66 |
-
gr.Slider(minimum=1, maximum=
|
67 |
],
|
68 |
outputs=gr.Textbox(label="Search Results", lines=20),
|
69 |
-
title="
|
70 |
-
description="Search for news articles using SearXNG
|
71 |
)
|
72 |
|
73 |
iface.launch()
|
|
|
1 |
+
from langchain.utilities import SearxSearchWrapper
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
+
# Initialize the SearxNG search wrapper
|
5 |
+
# You can replace this URL with any SearXNG instance you prefer
|
6 |
+
searx = SearxSearchWrapper(searx_host="https://searx.thegpm.org")
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def search_news(query, num_results=5):
|
9 |
+
try:
|
10 |
+
# Perform the search
|
11 |
+
search_results = searx.results(query, num_results=num_results)
|
12 |
+
|
13 |
+
# Format the output
|
14 |
+
formatted_results = "Search Results:\n\n"
|
15 |
+
for i, result in enumerate(search_results, 1):
|
16 |
+
formatted_results += f"{i}. {result['title']}\n"
|
17 |
+
formatted_results += f" URL: {result['link']}\n"
|
18 |
+
formatted_results += f" Snippet: {result['snippet']}\n\n"
|
19 |
+
|
20 |
+
return formatted_results
|
21 |
|
22 |
+
except Exception as e:
|
23 |
+
return f"An error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# Create Gradio interface
|
26 |
iface = gr.Interface(
|
27 |
+
fn=search_news,
|
28 |
inputs=[
|
29 |
gr.Textbox(label="Enter a news topic to search for"),
|
30 |
+
gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Number of results")
|
31 |
],
|
32 |
outputs=gr.Textbox(label="Search Results", lines=20),
|
33 |
+
title="News Search with LangChain and SearXNG",
|
34 |
+
description="Search for news articles using SearXNG through LangChain."
|
35 |
)
|
36 |
|
37 |
iface.launch()
|