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