Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
MCP_URL = "https://huggingface.co/mcp"
|
6 |
+
HEADERS = {
|
7 |
+
"Content-Type": "application/json",
|
8 |
+
"Accept": "application/json, text/event-stream"
|
9 |
+
}
|
10 |
+
|
11 |
+
def call_mcp_tool(method, params):
|
12 |
+
payload = {
|
13 |
+
"jsonrpc": "2.0",
|
14 |
+
"method": method,
|
15 |
+
"params": params,
|
16 |
+
"id": 1
|
17 |
+
}
|
18 |
+
response = requests.post(MCP_URL, headers=HEADERS, data=json.dumps(payload))
|
19 |
+
try:
|
20 |
+
return response.json()
|
21 |
+
except Exception as e:
|
22 |
+
return {"error": str(e), "raw_response": response.text}
|
23 |
+
|
24 |
+
def space_search(query, limit=10, mcp=False):
|
25 |
+
params = {
|
26 |
+
"query": query,
|
27 |
+
"limit": limit,
|
28 |
+
"mcp": mcp
|
29 |
+
}
|
30 |
+
result = call_mcp_tool("tools/call", {"tool": "space_search", "input": params})
|
31 |
+
return result
|
32 |
+
|
33 |
+
def model_detail(model_id):
|
34 |
+
params = {
|
35 |
+
"model_id": model_id
|
36 |
+
}
|
37 |
+
result = call_mcp_tool("tools/call", {"tool": "model_detail", "input": params})
|
38 |
+
return result
|
39 |
+
|
40 |
+
def paper_search(query, results_limit=12, concise_only=False):
|
41 |
+
params = {
|
42 |
+
"query": query,
|
43 |
+
"results_limit": results_limit,
|
44 |
+
"concise_only": concise_only
|
45 |
+
}
|
46 |
+
result = call_mcp_tool("tools/call", {"tool": "paper_search", "input": params})
|
47 |
+
return result
|
48 |
+
|
49 |
+
def dataset_search(query="", author="", tags=None, limit=20, sort="downloads"):
|
50 |
+
if tags is None:
|
51 |
+
tags = []
|
52 |
+
params = {
|
53 |
+
"query": query,
|
54 |
+
"author": author,
|
55 |
+
"tags": tags,
|
56 |
+
"limit": limit,
|
57 |
+
"sort": sort
|
58 |
+
}
|
59 |
+
result = call_mcp_tool("tools/call", {"tool": "dataset_search", "input": params})
|
60 |
+
return result
|
61 |
+
|
62 |
+
def dataset_detail(dataset_id):
|
63 |
+
params = {
|
64 |
+
"dataset_id": dataset_id
|
65 |
+
}
|
66 |
+
result = call_mcp_tool("tools/call", {"tool": "dataset_detail", "input": params})
|
67 |
+
return result
|
68 |
+
|
69 |
+
def duplicate_space(sourceSpaceId, newSpaceId="", hardware="freecpu", private=True):
|
70 |
+
params = {
|
71 |
+
"sourceSpaceId": sourceSpaceId,
|
72 |
+
"newSpaceId": newSpaceId,
|
73 |
+
"hardware": hardware,
|
74 |
+
"private": private
|
75 |
+
}
|
76 |
+
result = call_mcp_tool("tools/call", {"tool": "duplicate_space", "input": params})
|
77 |
+
return result
|
78 |
+
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown("# MCP Gradio Client")
|
81 |
+
|
82 |
+
with gr.Tab("Space Search"):
|
83 |
+
query = gr.Textbox(label="Query")
|
84 |
+
limit = gr.Slider(1, 50, value=10, label="Limit")
|
85 |
+
mcp = gr.Checkbox(label="MCP Only")
|
86 |
+
output = gr.JSON()
|
87 |
+
btn = gr.Button("Search")
|
88 |
+
btn.click(space_search, inputs=[query, limit, mcp], outputs=output)
|
89 |
+
|
90 |
+
with gr.Tab("Model Detail"):
|
91 |
+
model_id = gr.Textbox(label="Model ID")
|
92 |
+
output = gr.JSON()
|
93 |
+
btn = gr.Button("Get Details")
|
94 |
+
btn.click(model_detail, inputs=model_id, outputs=output)
|
95 |
+
|
96 |
+
with gr.Tab("Paper Search"):
|
97 |
+
query = gr.Textbox(label="Query")
|
98 |
+
results_limit = gr.Slider(1, 50, value=12, label="Results Limit")
|
99 |
+
concise_only = gr.Checkbox(label="Concise Only")
|
100 |
+
output = gr.JSON()
|
101 |
+
btn = gr.Button("Search")
|
102 |
+
btn.click(paper_search, inputs=[query, results_limit, concise_only], outputs=output)
|
103 |
+
|
104 |
+
with gr.Tab("Dataset Search"):
|
105 |
+
query = gr.Textbox(label="Query")
|
106 |
+
author = gr.Textbox(label="Author")
|
107 |
+
tags = gr.Textbox(label="Tags (comma-separated)")
|
108 |
+
limit = gr.Slider(1, 100, value=20, label="Limit")
|
109 |
+
sort = gr.Dropdown(choices=["downloads", "likes", "lastModified"], value="downloads", label="Sort By")
|
110 |
+
output = gr.JSON()
|
111 |
+
btn = gr.Button("Search")
|
112 |
+
def parse_tags(tags_str):
|
113 |
+
return [tag.strip() for tag in tags_str.split(",") if tag.strip()]
|
114 |
+
btn.click(lambda q, a, t, l, s: dataset_search(q, a, parse_tags(t), l, s),
|
115 |
+
inputs=[query, author, tags, limit, sort], outputs=output)
|
116 |
+
|
117 |
+
with gr.Tab("Dataset Detail"):
|
118 |
+
dataset_id = gr.Textbox(label="Dataset ID")
|
119 |
+
output = gr.JSON()
|
120 |
+
btn = gr.Button("Get Details")
|
121 |
+
btn.click(dataset_detail, inputs=dataset_id, outputs=output)
|
122 |
+
|
123 |
+
with gr.Tab("Duplicate Space"):
|
124 |
+
sourceSpaceId = gr.Textbox(label="Source Space ID")
|
125 |
+
newSpaceId = gr.Textbox(label="New Space ID")
|
126 |
+
hardware = gr.Dropdown(choices=["freecpu", "zerogpu"], value="freecpu", label="Hardware")
|
127 |
+
private = gr.Checkbox(label="Private", value=True)
|
128 |
+
output = gr.JSON()
|
129 |
+
btn = gr.Button("Duplicate")
|
130 |
+
btn.click(duplicate_space, inputs=[sourceSpaceId, newSpaceId, hardware, private], outputs=output)
|
131 |
+
|
132 |
+
demo.launch(mcp_server=True,share=True,debug=True)
|