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) # Create an HTML table html_table = "" html_table += "" # Table headers for item in results: title = item.get("title", "No Title") snippet = item.get("snippet", "No Snippet") link = item.get("link", "No Link") html_table += f"" html_table += "
TitleSnippetLink
{title}{snippet}Link
" return "DuckDuckGoは、利用者のプライバシーの保護と利用履歴等を記録保存しないことを運営方針としているインターネット検索エンジンである。",html_table #return results myquery = '''トヨタ自動車株式会社 の2021年の売り上げが下がっている理由を教えてください。''' with gr.Blocks() as demo: gr.Markdown("# Web Searcher using DuckDuckGo") gr.Markdown("Search the web using DuckDuckGo.") with gr.Sidebar(open=False): gr.HTML(""" DuckDuckGoの特徴

DuckDuckGoの特徴

DuckDuckGo(ダックダックゴー)は、ユーザーのプライバシーを重視した検索エンジンです。以下のような特徴があります。

主な特徴

DuckDuckGoは「オンラインでのプライバシーを守りたい!」と考える方にぴったりの検索エンジンです!

""") with gr.Row(): search_query = gr.Textbox(value="トヨタ自動車株式会社 の2021年の売り上げが下がっている理由を教えてください。", label="Search query") max_results = gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Max results") description = gr.Textbox(label="Description") search_results = gr.HTML(label="Search results") with gr.Row(): search_button = gr.Button("Search") search_button.click( fn=search, inputs=[search_query, max_results], outputs=[description, search_results] ) demo.launch(mcp_server=True) '''demo = gr.Interface( fn = search, inputs=[ gr.Textbox(value=myquery, label="Search query"), gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Max results"), ], outputs=[gr.Textbox(label="Description"),gr.HTML(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)'''