Shreyas094 commited on
Commit
23d2217
·
verified ·
1 Parent(s): d8d3199

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -1,5 +1,10 @@
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
@@ -7,20 +12,30 @@ 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(
@@ -34,4 +49,6 @@ iface = gr.Interface(
34
  description="Search for news articles using SearXNG through LangChain."
35
  )
36
 
37
- iface.launch()
 
 
 
1
  from langchain.utilities import SearxSearchWrapper
2
  import gradio as gr
3
+ import logging
4
+
5
+ # Set up logging
6
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
7
+ logger = logging.getLogger(__name__)
8
 
9
  # Initialize the SearxNG search wrapper
10
  # You can replace this URL with any SearXNG instance you prefer
 
12
 
13
  def search_news(query, num_results=5):
14
  try:
15
+ logger.info(f"Searching for query: {query} with {num_results} results")
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
  except Exception as e:
37
+ logger.error(f"An error occurred: {str(e)}", exc_info=True)
38
+ return f"An error occurred: {str(e)}\n\nPlease check the logs for more details."
39
 
40
  # Create Gradio interface
41
  iface = gr.Interface(
 
49
  description="Search for news articles using SearXNG through LangChain."
50
  )
51
 
52
+ if __name__ == "__main__":
53
+ logger.info("Starting the application")
54
+ iface.launch()