File size: 2,014 Bytes
200eb3a
f02770e
23d2217
 
 
 
 
617c3a2
200eb3a
 
 
00536e4
200eb3a
 
23d2217
200eb3a
 
 
23d2217
 
200eb3a
 
 
23d2217
 
 
 
 
 
 
 
 
200eb3a
 
617c3a2
200eb3a
23d2217
 
f02770e
200eb3a
f02770e
200eb3a
f02770e
 
200eb3a
f02770e
 
200eb3a
 
f02770e
 
23d2217
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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()