Spaces:
Running
Running
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) | |