Spaces:
Running
Running
from langchain.utilities import SearxSearchWrapper | |
import gradio as gr | |
import logging | |
# Set up logging | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
logger = logging.getLogger(__name__) | |
# Initialize the SearxNG search wrapper | |
# You can replace this URL with any SearXNG instance you prefer | |
searx = SearxSearchWrapper(searx_host="https://searx.thegpm.org") | |
def search_news(query, num_results=5): | |
try: | |
logger.info(f"Searching for query: {query} with {num_results} results") | |
# Perform the search | |
search_results = searx.results(query, num_results=num_results) | |
logger.info(f"Received {len(search_results)} results") | |
# Format the output | |
formatted_results = "Search Results:\n\n" | |
for i, result in enumerate(search_results, 1): | |
logger.debug(f"Processing result {i}: {result}") | |
title = result.get('title', 'No title') | |
link = result.get('link', 'No link') | |
snippet = result.get('snippet', 'No snippet available') | |
formatted_results += f"{i}. {title}\n" | |
formatted_results += f" URL: {link}\n" | |
formatted_results += f" Snippet: {snippet}\n\n" | |
return formatted_results | |
except Exception as e: | |
logger.error(f"An error occurred: {str(e)}", exc_info=True) | |
return f"An error occurred: {str(e)}\n\nPlease check the logs for more details." | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=search_news, | |
inputs=[ | |
gr.Textbox(label="Enter a news topic to search for"), | |
gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Number of results") | |
], | |
outputs=gr.Textbox(label="Search Results", lines=20), | |
title="News Search with LangChain and SearXNG", | |
description="Search for news articles using SearXNG through LangChain." | |
) | |
if __name__ == "__main__": | |
logger.info("Starting the application") | |
iface.launch() |