WebSearch / app.py
Nymbo's picture
Gradio 6.29; use ddgs directly (duckduckgo-search renamed; drop langchain)
922aab3 verified
Raw History Blame Contribute Delete
1.31 kB
import gradio as gr
from ddgs import DDGS
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.
"""
# duckduckgo-search was renamed to ddgs; langchain's wrapper no longer needed
hits = DDGS().text(input_query, max_results=int(max_results)) or []
return [
{"snippet": h.get("body", ""), "title": h.get("title", ""), "link": h.get("href", "")}
for h in hits
]
demo = gr.Interface(
fn = search,
api_name="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)