Shrijeeth-Suresh commited on
Commit
5069259
·
1 Parent(s): 34bff5b

feat: add SerpAPI search engine integration with UI tab

Browse files
Files changed (2) hide show
  1. app.py +13 -2
  2. search_engines/serpapi.py +54 -0
app.py CHANGED
@@ -2,11 +2,22 @@ import gradio as gr
2
 
3
  from search_engines.duckduckgo import duckduckgo_interface
4
  from search_engines.searxng import searxng_interface
 
5
  from search_engines.tavily import tavily_interface
6
 
7
  app = gr.TabbedInterface(
8
- interface_list=[duckduckgo_interface, tavily_interface, searxng_interface],
9
- tab_names=["DuckDuckGo Search", "Tavily Search", "SearxNG Search"],
 
 
 
 
 
 
 
 
 
 
10
  )
11
 
12
 
 
2
 
3
  from search_engines.duckduckgo import duckduckgo_interface
4
  from search_engines.searxng import searxng_interface
5
+ from search_engines.serpapi import serpapi_interface
6
  from search_engines.tavily import tavily_interface
7
 
8
  app = gr.TabbedInterface(
9
+ interface_list=[
10
+ duckduckgo_interface,
11
+ tavily_interface,
12
+ searxng_interface,
13
+ serpapi_interface,
14
+ ],
15
+ tab_names=[
16
+ "DuckDuckGo Search",
17
+ "Tavily Search",
18
+ "SearxNG Search",
19
+ "SerpAPI Search",
20
+ ],
21
  )
22
 
23
 
search_engines/serpapi.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from langchain_community.utilities import SerpAPIWrapper
4
+
5
+ from utils.helpers import map_results
6
+
7
+
8
+ async def serpapi_search(
9
+ query: str, api_key: str, max_results: int = 5
10
+ ) -> pd.DataFrame:
11
+ """
12
+ Given a search query, returns the search results from SerpAPI.
13
+
14
+ Args:
15
+ query (str): The search query.
16
+ api_key (str): The API key for SerpAPI.
17
+ max_results (int, optional): The number of maximum results to return. Defaults to 5.
18
+
19
+ Returns:
20
+ str: The search results from SerpAPI Search Engine.
21
+ """
22
+ serpapi = SerpAPIWrapper(
23
+ serpapi_api_key=api_key,
24
+ )
25
+ results = await serpapi.aresults(query)
26
+ results = results["organic_results"]
27
+ results = results[:max_results]
28
+ mapping = {
29
+ "title": "title",
30
+ "link": "link",
31
+ "snippet": "body",
32
+ }
33
+ results = await map_results(results, mapping)
34
+ return results
35
+
36
+
37
+ serpapi_interface = gr.Interface(
38
+ fn=serpapi_search,
39
+ inputs=[
40
+ gr.Textbox(label="Search Query"),
41
+ gr.Textbox(label="API Key", type="password"),
42
+ gr.Slider(minimum=1, maximum=10, step=1, value=5, label="Number of Results"),
43
+ ],
44
+ outputs=gr.Dataframe(
45
+ label="Search Results",
46
+ headers=["title", "body", "link"],
47
+ show_fullscreen_button=True,
48
+ show_row_numbers=True,
49
+ show_copy_button=True,
50
+ wrap=True,
51
+ ),
52
+ title="SerpAPI Search",
53
+ description="Search the web using SerpAPI Search Engine.",
54
+ )