File size: 1,144 Bytes
c80b126
 
fafd57a
 
c80b126
fafd57a
c80b126
fafd57a
 
 
c80b126
fafd57a
 
 
c80b126
fafd57a
 
 
c80b126
fafd57a
 
 
c80b126
fafd57a
c80b126
fafd57a
 
 
 
 
c80b126
fafd57a
 
 
c80b126
 
 
fafd57a
c80b126
fafd57a
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
import gradio as gr

from langchain_community.tools import DuckDuckGoSearchResults
from typing import Literal, List, Dict

def search(input_query : str, max_results : int = 5) -> List[Dict[Literal["snippet", "title", "link"], str]]:

  
    """
    Perform a web search using DuckDuckGo.

    Args:
        input_query: The query to search for.
        max_results: The maximum number of results to return. Defaults to 5.

    Returns:
        A list of dictionaries, each containing "snippet", "title", and "link" keys.
    """

    search = DuckDuckGoSearchResults(output_format="list", num_results = max_results)
    
    results = search.invoke(input_query)

    return results

demo = gr.Interface(
    fn = search,
    inputs=[
        gr.Textbox(value="Ahly SC of Egypt matches.", label="Search query"),
        gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Max results"),
    ],
    outputs=gr.JSON(label="Search results"),
    title = "Web Searcher using DuckDuckGo",
    description = "Search the web using DuckDuckGo.",
)


# Launch the interface and MCP server
if __name__ == "__main__":
    demo.launch(mcp_server=True)