private_search / app.py
fudii0921's picture
Update app.py
05ddd2b verified
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 = "<table border='1'>"
html_table += "<tr><th>Title</th><th>Snippet</th><th>Link</th></tr>" # 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"<tr><td>{title}</td><td>{snippet}</td><td><a href='{link}'>Link</a></td></tr>"
html_table += "</table>"
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("""<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DuckDuckGoの特徴</title>
</head>
<body>
<h1>DuckDuckGoの特徴</h1>
<p>DuckDuckGo(ダックダックゴー)は、ユーザーのプライバシーを重視した検索エンジンです。以下のような特徴があります。</p>
<h2>主な特徴</h2>
<ul>
<li><strong>プライバシー重視</strong>: 検索履歴や個人データを保存せず、IPアドレスやCookie情報を追跡しません。</li>
<li><strong>広告の最小化</strong>: 広告は検索内容に基づいて表示されますが、ユーザーの行動を追跡することはありません。</li>
<li><strong>トラッカーのブロック</strong>: FacebookやGoogleなどのトラッカーをブロックし、オンライン活動の監視を防ぎます。</li>
<li><strong>カスタマイズ性</strong>: 検索エンジンをデフォルトに設定可能で、Windows、Mac、Android、iOS向けに専用ブラウザが提供されています。</li>
<li><strong>位置情報の管理</strong>: 匿名性を保ちながら位置情報を利用し、適切な検索結果を提供します。</li>
</ul>
<p>DuckDuckGoは「オンラインでのプライバシーを守りたい!」と考える方にぴったりの検索エンジンです!</p>
</body>
</html>
""")
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)'''