0xmoose commited on
Commit
0668e4c
·
verified ·
1 Parent(s): 8ae3d7a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from duckduckgo_search import DDGS
4
+
5
+
6
+ def web_search(
7
+ query: str,
8
+ max_results: int = 5,
9
+ region: str = "wt-wt",
10
+ safesearch: str = "moderate",
11
+ timelimit: str | None = None,
12
+ ):
13
+ """
14
+ Search the web using DuckDuckGo and return the top results.
15
+
16
+ Args:
17
+ query: Search query string.
18
+ max_results: Maximum number of results to return (1-25 recommended).
19
+ region: DDG region (e.g. "wt-wt" global, "us-en", "ca-en", "uk-en").
20
+ safesearch: "off", "moderate", or "strict".
21
+ timelimit: Optional time filter: "d" (day), "w" (week), "m" (month), "y" (year), or None.
22
+
23
+ Returns:
24
+ A list of results, each with title, url, and snippet.
25
+ """
26
+ q = (query or "").strip()
27
+ if not q:
28
+ return []
29
+
30
+ max_results = int(max(1, min(max_results, 25)))
31
+
32
+ results = []
33
+ # DDGS returns dicts with keys like: title, href, body
34
+ with DDGS() as ddgs:
35
+ for r in ddgs.text(
36
+ q,
37
+ region=region,
38
+ safesearch=safesearch,
39
+ timelimit=timelimit,
40
+ max_results=max_results,
41
+ ):
42
+ results.append(
43
+ {
44
+ "title": r.get("title"),
45
+ "url": r.get("href"),
46
+ "snippet": r.get("body"),
47
+ }
48
+ )
49
+ return results
50
+
51
+
52
+ with gr.Blocks(title="Web Search (MCP-ready)") as demo:
53
+ gr.Markdown(
54
+ """
55
+ # Web Search (DuckDuckGo) — UI + MCP Tool
56
+ - Use the UI below, or connect as an MCP server from your agent/editor.
57
+ - The MCP endpoint appears in the Space footer → **View API** → **MCP**.
58
+ """
59
+ )
60
+
61
+ with gr.Row():
62
+ query = gr.Textbox(label="Query", placeholder="e.g. gradio mcp server hugging face spaces")
63
+ with gr.Row():
64
+ max_results = gr.Slider(1, 25, value=5, step=1, label="Max results")
65
+ region = gr.Dropdown(
66
+ choices=["wt-wt", "us-en", "ca-en", "uk-en", "au-en", "de-de", "fr-fr", "es-es", "it-it"],
67
+ value="wt-wt",
68
+ label="Region",
69
+ )
70
+ safesearch = gr.Dropdown(choices=["off", "moderate", "strict"], value="moderate", label="SafeSearch")
71
+ timelimit = gr.Dropdown(choices=[None, "d", "w", "m", "y"], value=None, label="Time limit")
72
+
73
+ run_btn = gr.Button("Search")
74
+ out = gr.JSON(label="Results (title/url/snippet)")
75
+
76
+ run_btn.click(
77
+ fn=web_search,
78
+ inputs=[query, max_results, region, safesearch, timelimit],
79
+ outputs=out,
80
+ api_name="web_search", # nicer MCP tool name
81
+ queue=False, # optional: reduces MCP progress overhead
82
+ )
83
+
84
+ if __name__ == "__main__":
85
+ # Gradio will expose MCP endpoints when mcp_server=True
86
+ demo.launch(mcp_server=True, server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))