Spaces:
Runtime error
Runtime error
Commit
·
800b8b5
1
Parent(s):
170fd5f
Refactor to use Gradio's built-in MCP server support
Browse files- Replace manual MCP protocol implementation with Gradio's mcp_server=True
- Simplify from ~650 to ~494 lines of code
- Add Gradio UI with tabs for all 8 search/download functions
- Automatically generate MCP tools from function docstrings and type hints
app.py
CHANGED
|
@@ -1,654 +1,494 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
MCP Server for Hugging Face Dataset and Model Search API
|
| 4 |
"""
|
| 5 |
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
from typing import Any, Dict, Optional
|
| 9 |
|
|
|
|
| 10 |
import httpx
|
| 11 |
-
from mcp.server import Server
|
| 12 |
-
from mcp.server.stdio import stdio_server
|
| 13 |
-
from mcp.types import (
|
| 14 |
-
Tool,
|
| 15 |
-
TextContent,
|
| 16 |
-
CallToolResult,
|
| 17 |
-
CallToolRequest,
|
| 18 |
-
ListToolsResult,
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 65 |
-
|
| 66 |
-
|
| 67 |
-
"
|
| 68 |
-
"k": k,
|
| 69 |
-
"sort_by": sort_by,
|
| 70 |
-
"min_likes": min_likes,
|
| 71 |
-
"min_downloads": min_downloads
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
response = await self.client.get(
|
| 75 |
-
f"{self.base_url}/similarity/datasets",
|
| 76 |
-
params=params
|
| 77 |
)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
"query"
|
| 94 |
-
"k": k,
|
| 95 |
-
"sort_by": sort_by,
|
| 96 |
-
"min_likes": min_likes,
|
| 97 |
-
"min_downloads": min_downloads,
|
| 98 |
-
"min_param_count": min_param_count
|
| 99 |
-
}
|
| 100 |
-
if max_param_count is not None:
|
| 101 |
-
params["max_param_count"] = max_param_count
|
| 102 |
-
|
| 103 |
-
response = await self.client.get(
|
| 104 |
-
f"{self.base_url}/search/models",
|
| 105 |
-
params=params
|
| 106 |
)
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
"
|
| 123 |
-
"k": k,
|
| 124 |
-
"sort_by": sort_by,
|
| 125 |
-
"min_likes": min_likes,
|
| 126 |
-
"min_downloads": min_downloads,
|
| 127 |
-
"min_param_count": min_param_count
|
| 128 |
-
}
|
| 129 |
-
if max_param_count is not None:
|
| 130 |
-
params["max_param_count"] = max_param_count
|
| 131 |
-
|
| 132 |
-
response = await self.client.get(
|
| 133 |
-
f"{self.base_url}/similarity/models",
|
| 134 |
-
params=params
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
"
|
| 150 |
-
"min_likes": min_likes,
|
| 151 |
-
"min_downloads": min_downloads,
|
| 152 |
-
"min_param_count": min_param_count
|
| 153 |
-
}
|
| 154 |
-
if max_param_count is not None:
|
| 155 |
-
params["max_param_count"] = max_param_count
|
| 156 |
-
|
| 157 |
-
response = await self.client.get(
|
| 158 |
-
f"{self.base_url}/trending/models",
|
| 159 |
-
params=params
|
| 160 |
)
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
"
|
| 173 |
-
"min_likes": min_likes,
|
| 174 |
-
"min_downloads": min_downloads
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
response = await self.client.get(
|
| 178 |
-
f"{self.base_url}/trending/datasets",
|
| 179 |
-
params=params
|
| 180 |
)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
model_id (str): The model ID (e.g., 'username/model-name')
|
| 190 |
-
|
| 191 |
-
Returns:
|
| 192 |
-
str: The content of the model card (README.md)
|
| 193 |
-
"""
|
| 194 |
-
url = f"https://huggingface.co/{model_id}/raw/main/README.md"
|
| 195 |
-
response = await self.client.get(url)
|
| 196 |
-
response.raise_for_status()
|
| 197 |
-
return response.text
|
| 198 |
-
|
| 199 |
-
async def download_dataset_card(self, dataset_id: str) -> str:
|
| 200 |
-
"""
|
| 201 |
-
Download the README card for a HuggingFace dataset.
|
| 202 |
-
|
| 203 |
-
Args:
|
| 204 |
-
dataset_id (str): The dataset ID (e.g., 'username/dataset-name')
|
| 205 |
-
|
| 206 |
-
Returns:
|
| 207 |
-
str: The content of the dataset card (README.md)
|
| 208 |
-
"""
|
| 209 |
-
url = f"https://huggingface.co/datasets/{dataset_id}/raw/main/README.md"
|
| 210 |
-
response = await self.client.get(url)
|
| 211 |
-
response.raise_for_status()
|
| 212 |
-
return response.text
|
| 213 |
-
|
| 214 |
-
# Initialize server and API client
|
| 215 |
-
server = Server("hf-search")
|
| 216 |
-
api_client: Optional[HFSearchServer] = None
|
| 217 |
-
|
| 218 |
-
@server.list_tools()
|
| 219 |
-
async def list_tools() -> ListToolsResult:
|
| 220 |
-
"""List available tools"""
|
| 221 |
-
return ListToolsResult(
|
| 222 |
-
tools=[
|
| 223 |
-
Tool(
|
| 224 |
-
name="search_datasets",
|
| 225 |
-
description="Search for datasets based on a text query",
|
| 226 |
-
inputSchema={
|
| 227 |
-
"type": "object",
|
| 228 |
-
"properties": {
|
| 229 |
-
"query": {
|
| 230 |
-
"type": "string",
|
| 231 |
-
"description": "Search query text"
|
| 232 |
-
},
|
| 233 |
-
"k": {
|
| 234 |
-
"type": "integer",
|
| 235 |
-
"description": "Number of results to return (1-100)",
|
| 236 |
-
"minimum": 1,
|
| 237 |
-
"maximum": 100,
|
| 238 |
-
"default": 5
|
| 239 |
-
},
|
| 240 |
-
"sort_by": {
|
| 241 |
-
"type": "string",
|
| 242 |
-
"description": "Sort method for results",
|
| 243 |
-
"enum": ["similarity", "likes", "downloads", "trending"],
|
| 244 |
-
"default": "similarity"
|
| 245 |
-
},
|
| 246 |
-
"min_likes": {
|
| 247 |
-
"type": "integer",
|
| 248 |
-
"description": "Minimum likes filter",
|
| 249 |
-
"minimum": 0,
|
| 250 |
-
"default": 0
|
| 251 |
-
},
|
| 252 |
-
"min_downloads": {
|
| 253 |
-
"type": "integer",
|
| 254 |
-
"description": "Minimum downloads filter",
|
| 255 |
-
"minimum": 0,
|
| 256 |
-
"default": 0
|
| 257 |
-
}
|
| 258 |
-
},
|
| 259 |
-
"required": ["query"]
|
| 260 |
-
}
|
| 261 |
-
),
|
| 262 |
-
Tool(
|
| 263 |
-
name="find_similar_datasets",
|
| 264 |
-
description="Find datasets similar to a specified dataset",
|
| 265 |
-
inputSchema={
|
| 266 |
-
"type": "object",
|
| 267 |
-
"properties": {
|
| 268 |
-
"dataset_id": {
|
| 269 |
-
"type": "string",
|
| 270 |
-
"description": "Dataset ID to find similar datasets for"
|
| 271 |
-
},
|
| 272 |
-
"k": {
|
| 273 |
-
"type": "integer",
|
| 274 |
-
"description": "Number of results to return (1-100)",
|
| 275 |
-
"minimum": 1,
|
| 276 |
-
"maximum": 100,
|
| 277 |
-
"default": 5
|
| 278 |
-
},
|
| 279 |
-
"sort_by": {
|
| 280 |
-
"type": "string",
|
| 281 |
-
"description": "Sort method for results",
|
| 282 |
-
"enum": ["similarity", "likes", "downloads", "trending"],
|
| 283 |
-
"default": "similarity"
|
| 284 |
-
},
|
| 285 |
-
"min_likes": {
|
| 286 |
-
"type": "integer",
|
| 287 |
-
"description": "Minimum likes filter",
|
| 288 |
-
"minimum": 0,
|
| 289 |
-
"default": 0
|
| 290 |
-
},
|
| 291 |
-
"min_downloads": {
|
| 292 |
-
"type": "integer",
|
| 293 |
-
"description": "Minimum downloads filter",
|
| 294 |
-
"minimum": 0,
|
| 295 |
-
"default": 0
|
| 296 |
-
}
|
| 297 |
-
},
|
| 298 |
-
"required": ["dataset_id"]
|
| 299 |
-
}
|
| 300 |
-
),
|
| 301 |
-
Tool(
|
| 302 |
-
name="search_models",
|
| 303 |
-
description="Search for models based on a text query with optional parameter count filtering",
|
| 304 |
-
inputSchema={
|
| 305 |
-
"type": "object",
|
| 306 |
-
"properties": {
|
| 307 |
-
"query": {
|
| 308 |
-
"type": "string",
|
| 309 |
-
"description": "Search query text"
|
| 310 |
-
},
|
| 311 |
-
"k": {
|
| 312 |
-
"type": "integer",
|
| 313 |
-
"description": "Number of results to return (1-100)",
|
| 314 |
-
"minimum": 1,
|
| 315 |
-
"maximum": 100,
|
| 316 |
-
"default": 5
|
| 317 |
-
},
|
| 318 |
-
"sort_by": {
|
| 319 |
-
"type": "string",
|
| 320 |
-
"description": "Sort method for results",
|
| 321 |
-
"enum": ["similarity", "likes", "downloads", "trending"],
|
| 322 |
-
"default": "similarity"
|
| 323 |
-
},
|
| 324 |
-
"min_likes": {
|
| 325 |
-
"type": "integer",
|
| 326 |
-
"description": "Minimum likes filter",
|
| 327 |
-
"minimum": 0,
|
| 328 |
-
"default": 0
|
| 329 |
-
},
|
| 330 |
-
"min_downloads": {
|
| 331 |
-
"type": "integer",
|
| 332 |
-
"description": "Minimum downloads filter",
|
| 333 |
-
"minimum": 0,
|
| 334 |
-
"default": 0
|
| 335 |
-
},
|
| 336 |
-
"min_param_count": {
|
| 337 |
-
"type": "integer",
|
| 338 |
-
"description": "Minimum parameter count (excludes models with unknown params)",
|
| 339 |
-
"minimum": 0,
|
| 340 |
-
"default": 0
|
| 341 |
-
},
|
| 342 |
-
"max_param_count": {
|
| 343 |
-
"type": ["integer", "null"],
|
| 344 |
-
"description": "Maximum parameter count (null for no limit)",
|
| 345 |
-
"minimum": 0,
|
| 346 |
-
"default": None
|
| 347 |
-
}
|
| 348 |
-
},
|
| 349 |
-
"required": ["query"]
|
| 350 |
-
}
|
| 351 |
-
),
|
| 352 |
-
Tool(
|
| 353 |
-
name="find_similar_models",
|
| 354 |
-
description="Find models similar to a specified model",
|
| 355 |
-
inputSchema={
|
| 356 |
-
"type": "object",
|
| 357 |
-
"properties": {
|
| 358 |
-
"model_id": {
|
| 359 |
-
"type": "string",
|
| 360 |
-
"description": "Model ID to find similar models for"
|
| 361 |
-
},
|
| 362 |
-
"k": {
|
| 363 |
-
"type": "integer",
|
| 364 |
-
"description": "Number of results to return (1-100)",
|
| 365 |
-
"minimum": 1,
|
| 366 |
-
"maximum": 100,
|
| 367 |
-
"default": 5
|
| 368 |
-
},
|
| 369 |
-
"sort_by": {
|
| 370 |
-
"type": "string",
|
| 371 |
-
"description": "Sort method for results",
|
| 372 |
-
"enum": ["similarity", "likes", "downloads", "trending"],
|
| 373 |
-
"default": "similarity"
|
| 374 |
-
},
|
| 375 |
-
"min_likes": {
|
| 376 |
-
"type": "integer",
|
| 377 |
-
"description": "Minimum likes filter",
|
| 378 |
-
"minimum": 0,
|
| 379 |
-
"default": 0
|
| 380 |
-
},
|
| 381 |
-
"min_downloads": {
|
| 382 |
-
"type": "integer",
|
| 383 |
-
"description": "Minimum downloads filter",
|
| 384 |
-
"minimum": 0,
|
| 385 |
-
"default": 0
|
| 386 |
-
},
|
| 387 |
-
"min_param_count": {
|
| 388 |
-
"type": "integer",
|
| 389 |
-
"description": "Minimum parameter count (excludes models with unknown params)",
|
| 390 |
-
"minimum": 0,
|
| 391 |
-
"default": 0
|
| 392 |
-
},
|
| 393 |
-
"max_param_count": {
|
| 394 |
-
"type": ["integer", "null"],
|
| 395 |
-
"description": "Maximum parameter count (null for no limit)",
|
| 396 |
-
"minimum": 0,
|
| 397 |
-
"default": None
|
| 398 |
-
}
|
| 399 |
-
},
|
| 400 |
-
"required": ["model_id"]
|
| 401 |
-
}
|
| 402 |
-
),
|
| 403 |
-
Tool(
|
| 404 |
-
name="get_trending_models",
|
| 405 |
-
description="Get trending models with their summaries and optional filtering",
|
| 406 |
-
inputSchema={
|
| 407 |
-
"type": "object",
|
| 408 |
-
"properties": {
|
| 409 |
-
"limit": {
|
| 410 |
-
"type": "integer",
|
| 411 |
-
"description": "Number of results to return (1-100)",
|
| 412 |
-
"minimum": 1,
|
| 413 |
-
"maximum": 100,
|
| 414 |
-
"default": 10
|
| 415 |
-
},
|
| 416 |
-
"min_likes": {
|
| 417 |
-
"type": "integer",
|
| 418 |
-
"description": "Minimum likes filter",
|
| 419 |
-
"minimum": 0,
|
| 420 |
-
"default": 0
|
| 421 |
-
},
|
| 422 |
-
"min_downloads": {
|
| 423 |
-
"type": "integer",
|
| 424 |
-
"description": "Minimum downloads filter",
|
| 425 |
-
"minimum": 0,
|
| 426 |
-
"default": 0
|
| 427 |
-
},
|
| 428 |
-
"min_param_count": {
|
| 429 |
-
"type": "integer",
|
| 430 |
-
"description": "Minimum parameter count (excludes models with unknown params)",
|
| 431 |
-
"minimum": 0,
|
| 432 |
-
"default": 0
|
| 433 |
-
},
|
| 434 |
-
"max_param_count": {
|
| 435 |
-
"type": ["integer", "null"],
|
| 436 |
-
"description": "Maximum parameter count (null for no limit)",
|
| 437 |
-
"minimum": 0,
|
| 438 |
-
"default": None
|
| 439 |
-
}
|
| 440 |
-
}
|
| 441 |
-
}
|
| 442 |
-
),
|
| 443 |
-
Tool(
|
| 444 |
-
name="get_trending_datasets",
|
| 445 |
-
description="Get trending datasets with their summaries",
|
| 446 |
-
inputSchema={
|
| 447 |
-
"type": "object",
|
| 448 |
-
"properties": {
|
| 449 |
-
"limit": {
|
| 450 |
-
"type": "integer",
|
| 451 |
-
"description": "Number of results to return (1-100)",
|
| 452 |
-
"minimum": 1,
|
| 453 |
-
"maximum": 100,
|
| 454 |
-
"default": 10
|
| 455 |
-
},
|
| 456 |
-
"min_likes": {
|
| 457 |
-
"type": "integer",
|
| 458 |
-
"description": "Minimum likes filter",
|
| 459 |
-
"minimum": 0,
|
| 460 |
-
"default": 0
|
| 461 |
-
},
|
| 462 |
-
"min_downloads": {
|
| 463 |
-
"type": "integer",
|
| 464 |
-
"description": "Minimum downloads filter",
|
| 465 |
-
"minimum": 0,
|
| 466 |
-
"default": 0
|
| 467 |
-
}
|
| 468 |
-
}
|
| 469 |
-
}
|
| 470 |
-
),
|
| 471 |
-
Tool(
|
| 472 |
-
name="download_model_card",
|
| 473 |
-
description="Download the README card for a HuggingFace model",
|
| 474 |
-
inputSchema={
|
| 475 |
-
"type": "object",
|
| 476 |
-
"properties": {
|
| 477 |
-
"model_id": {
|
| 478 |
-
"type": "string",
|
| 479 |
-
"description": "The model ID (e.g., 'username/model-name')"
|
| 480 |
-
}
|
| 481 |
-
},
|
| 482 |
-
"required": ["model_id"]
|
| 483 |
-
}
|
| 484 |
-
),
|
| 485 |
-
Tool(
|
| 486 |
-
name="download_dataset_card",
|
| 487 |
-
description="Download the README card for a HuggingFace dataset",
|
| 488 |
-
inputSchema={
|
| 489 |
-
"type": "object",
|
| 490 |
-
"properties": {
|
| 491 |
-
"dataset_id": {
|
| 492 |
-
"type": "string",
|
| 493 |
-
"description": "The dataset ID (e.g., 'username/dataset-name')"
|
| 494 |
-
}
|
| 495 |
-
},
|
| 496 |
-
"required": ["dataset_id"]
|
| 497 |
-
}
|
| 498 |
-
)
|
| 499 |
-
]
|
| 500 |
-
)
|
| 501 |
-
|
| 502 |
-
@server.call_tool()
|
| 503 |
-
async def call_tool(request: CallToolRequest) -> CallToolResult:
|
| 504 |
-
"""Handle tool calls"""
|
| 505 |
-
global api_client
|
| 506 |
-
|
| 507 |
-
if api_client is None:
|
| 508 |
-
# Initialize API client with base URL from environment or default
|
| 509 |
-
import os
|
| 510 |
-
base_url = os.getenv("HF_SEARCH_API_URL", "http://localhost:8000")
|
| 511 |
-
api_client = HFSearchServer(base_url)
|
| 512 |
-
|
| 513 |
-
try:
|
| 514 |
-
# Parse arguments
|
| 515 |
-
args = request.params.arguments if hasattr(request.params, 'arguments') else {}
|
| 516 |
-
|
| 517 |
-
# Format results helper
|
| 518 |
-
def format_dataset_results(data: Dict[str, Any]) -> str:
|
| 519 |
-
results = data.get("results", [])
|
| 520 |
-
if not results:
|
| 521 |
-
return "No datasets found."
|
| 522 |
-
|
| 523 |
-
output = []
|
| 524 |
-
for i, result in enumerate(results, 1):
|
| 525 |
-
output.append(f"{i}. **{result['dataset_id']}**")
|
| 526 |
-
output.append(f" - Summary: {result['summary']}")
|
| 527 |
-
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 528 |
-
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 529 |
-
output.append("")
|
| 530 |
-
|
| 531 |
-
return "\n".join(output)
|
| 532 |
-
|
| 533 |
-
def format_model_results(data: Dict[str, Any]) -> str:
|
| 534 |
-
results = data.get("results", [])
|
| 535 |
-
if not results:
|
| 536 |
-
return "No models found."
|
| 537 |
-
|
| 538 |
-
output = []
|
| 539 |
-
for i, result in enumerate(results, 1):
|
| 540 |
-
output.append(f"{i}. **{result['model_id']}**")
|
| 541 |
-
output.append(f" - Summary: {result['summary']}")
|
| 542 |
-
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 543 |
-
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 544 |
-
if result.get('param_count') is not None and result['param_count'] > 0:
|
| 545 |
-
# Format parameter count nicely
|
| 546 |
-
param_count = result['param_count']
|
| 547 |
-
if param_count >= 1_000_000_000:
|
| 548 |
-
param_str = f"{param_count / 1_000_000_000:.1f}B"
|
| 549 |
-
elif param_count >= 1_000_000:
|
| 550 |
-
param_str = f"{param_count / 1_000_000:.1f}M"
|
| 551 |
-
elif param_count >= 1_000:
|
| 552 |
-
param_str = f"{param_count / 1_000:.1f}K"
|
| 553 |
-
else:
|
| 554 |
-
param_str = str(param_count)
|
| 555 |
-
output.append(f" - Parameters: {param_str}")
|
| 556 |
-
output.append("")
|
| 557 |
-
|
| 558 |
-
return "\n".join(output)
|
| 559 |
-
|
| 560 |
-
# Route to appropriate method
|
| 561 |
-
if request.params.name == "search_datasets":
|
| 562 |
-
result = await api_client.search_datasets(**args)
|
| 563 |
-
formatted = format_dataset_results(result)
|
| 564 |
-
return CallToolResult(
|
| 565 |
-
content=[TextContent(text=formatted)],
|
| 566 |
-
isError=False
|
| 567 |
-
)
|
| 568 |
-
|
| 569 |
-
elif request.params.name == "find_similar_datasets":
|
| 570 |
-
result = await api_client.find_similar_datasets(**args)
|
| 571 |
-
formatted = format_dataset_results(result)
|
| 572 |
-
return CallToolResult(
|
| 573 |
-
content=[TextContent(text=formatted)],
|
| 574 |
-
isError=False
|
| 575 |
-
)
|
| 576 |
-
|
| 577 |
-
elif request.params.name == "search_models":
|
| 578 |
-
result = await api_client.search_models(**args)
|
| 579 |
-
formatted = format_model_results(result)
|
| 580 |
-
return CallToolResult(
|
| 581 |
-
content=[TextContent(text=formatted)],
|
| 582 |
-
isError=False
|
| 583 |
-
)
|
| 584 |
-
|
| 585 |
-
elif request.params.name == "find_similar_models":
|
| 586 |
-
result = await api_client.find_similar_models(**args)
|
| 587 |
-
formatted = format_model_results(result)
|
| 588 |
-
return CallToolResult(
|
| 589 |
-
content=[TextContent(text=formatted)],
|
| 590 |
-
isError=False
|
| 591 |
-
)
|
| 592 |
-
|
| 593 |
-
elif request.params.name == "get_trending_models":
|
| 594 |
-
result = await api_client.get_trending_models(**args)
|
| 595 |
-
formatted = format_model_results(result)
|
| 596 |
-
return CallToolResult(
|
| 597 |
-
content=[TextContent(text=formatted)],
|
| 598 |
-
isError=False
|
| 599 |
-
)
|
| 600 |
-
|
| 601 |
-
elif request.params.name == "get_trending_datasets":
|
| 602 |
-
result = await api_client.get_trending_datasets(**args)
|
| 603 |
-
formatted = format_dataset_results(result)
|
| 604 |
-
return CallToolResult(
|
| 605 |
-
content=[TextContent(text=formatted)],
|
| 606 |
-
isError=False
|
| 607 |
-
)
|
| 608 |
-
|
| 609 |
-
elif request.params.name == "download_model_card":
|
| 610 |
-
result = await api_client.download_model_card(**args)
|
| 611 |
-
return CallToolResult(
|
| 612 |
-
content=[TextContent(text=result)],
|
| 613 |
-
isError=False
|
| 614 |
-
)
|
| 615 |
-
|
| 616 |
-
elif request.params.name == "download_dataset_card":
|
| 617 |
-
result = await api_client.download_dataset_card(**args)
|
| 618 |
-
return CallToolResult(
|
| 619 |
-
content=[TextContent(text=result)],
|
| 620 |
-
isError=False
|
| 621 |
-
)
|
| 622 |
-
|
| 623 |
-
else:
|
| 624 |
-
return CallToolResult(
|
| 625 |
-
content=[TextContent(text=f"Unknown tool: {request.params.name}")],
|
| 626 |
-
isError=True
|
| 627 |
-
)
|
| 628 |
-
|
| 629 |
-
except httpx.HTTPStatusError as e:
|
| 630 |
-
error_msg = f"API request failed with status {e.response.status_code}: {e.response.text}"
|
| 631 |
-
logger.error(error_msg)
|
| 632 |
-
return CallToolResult(
|
| 633 |
-
content=[TextContent(text=error_msg)],
|
| 634 |
-
isError=True
|
| 635 |
)
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
|
|
|
|
|
|
| 642 |
)
|
| 643 |
|
| 644 |
-
async def main():
|
| 645 |
-
"""Main entry point"""
|
| 646 |
-
async with stdio_server() as (read_stream, write_stream):
|
| 647 |
-
await server.run(read_stream, write_stream)
|
| 648 |
-
|
| 649 |
-
# Cleanup
|
| 650 |
-
if api_client:
|
| 651 |
-
await api_client.close()
|
| 652 |
|
| 653 |
if __name__ == "__main__":
|
| 654 |
-
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
MCP Server for Hugging Face Dataset and Model Search API using Gradio
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import os
|
| 7 |
+
from typing import Optional
|
|
|
|
| 8 |
|
| 9 |
+
import gradio as gr
|
| 10 |
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Initialize HTTP client
|
| 13 |
+
client = httpx.Client(timeout=30.0)
|
| 14 |
+
base_url = os.getenv("HF_SEARCH_API_URL", "http://localhost:8000")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def search_datasets(
|
| 18 |
+
query: str,
|
| 19 |
+
k: int = 5,
|
| 20 |
+
sort_by: str = "similarity",
|
| 21 |
+
min_likes: int = 0,
|
| 22 |
+
min_downloads: int = 0
|
| 23 |
+
) -> str:
|
| 24 |
+
"""
|
| 25 |
+
Search for datasets based on a text query.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
query: Search query text
|
| 29 |
+
k: Number of results to return (1-100)
|
| 30 |
+
sort_by: Sort method for results (similarity, likes, downloads, trending)
|
| 31 |
+
min_likes: Minimum likes filter
|
| 32 |
+
min_downloads: Minimum downloads filter
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
Formatted search results with dataset IDs, summaries, and metadata
|
| 36 |
+
"""
|
| 37 |
+
params = {
|
| 38 |
+
"query": query,
|
| 39 |
+
"k": k,
|
| 40 |
+
"sort_by": sort_by,
|
| 41 |
+
"min_likes": min_likes,
|
| 42 |
+
"min_downloads": min_downloads
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
response = client.get(f"{base_url}/search/datasets", params=params)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
+
data = response.json()
|
| 48 |
+
|
| 49 |
+
results = data.get("results", [])
|
| 50 |
+
if not results:
|
| 51 |
+
return "No datasets found."
|
| 52 |
+
|
| 53 |
+
output = []
|
| 54 |
+
for i, result in enumerate(results, 1):
|
| 55 |
+
output.append(f"{i}. **{result['dataset_id']}**")
|
| 56 |
+
output.append(f" - Summary: {result['summary']}")
|
| 57 |
+
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 58 |
+
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 59 |
+
output.append("")
|
| 60 |
+
|
| 61 |
+
return "\n".join(output)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def find_similar_datasets(
|
| 65 |
+
dataset_id: str,
|
| 66 |
+
k: int = 5,
|
| 67 |
+
sort_by: str = "similarity",
|
| 68 |
+
min_likes: int = 0,
|
| 69 |
+
min_downloads: int = 0
|
| 70 |
+
) -> str:
|
| 71 |
+
"""
|
| 72 |
+
Find datasets similar to a specified dataset.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
dataset_id: Dataset ID to find similar datasets for
|
| 76 |
+
k: Number of results to return (1-100)
|
| 77 |
+
sort_by: Sort method for results (similarity, likes, downloads, trending)
|
| 78 |
+
min_likes: Minimum likes filter
|
| 79 |
+
min_downloads: Minimum downloads filter
|
| 80 |
+
|
| 81 |
+
Returns:
|
| 82 |
+
Formatted list of similar datasets with metadata
|
| 83 |
+
"""
|
| 84 |
+
params = {
|
| 85 |
+
"dataset_id": dataset_id,
|
| 86 |
+
"k": k,
|
| 87 |
+
"sort_by": sort_by,
|
| 88 |
+
"min_likes": min_likes,
|
| 89 |
+
"min_downloads": min_downloads
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
response = client.get(f"{base_url}/similarity/datasets", params=params)
|
| 93 |
+
response.raise_for_status()
|
| 94 |
+
data = response.json()
|
| 95 |
+
|
| 96 |
+
results = data.get("results", [])
|
| 97 |
+
if not results:
|
| 98 |
+
return "No similar datasets found."
|
| 99 |
+
|
| 100 |
+
output = []
|
| 101 |
+
for i, result in enumerate(results, 1):
|
| 102 |
+
output.append(f"{i}. **{result['dataset_id']}**")
|
| 103 |
+
output.append(f" - Summary: {result['summary']}")
|
| 104 |
+
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 105 |
+
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 106 |
+
output.append("")
|
| 107 |
+
|
| 108 |
+
return "\n".join(output)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def search_models(
|
| 112 |
+
query: str,
|
| 113 |
+
k: int = 5,
|
| 114 |
+
sort_by: str = "similarity",
|
| 115 |
+
min_likes: int = 0,
|
| 116 |
+
min_downloads: int = 0,
|
| 117 |
+
min_param_count: int = 0,
|
| 118 |
+
max_param_count: Optional[int] = None
|
| 119 |
+
) -> str:
|
| 120 |
+
"""
|
| 121 |
+
Search for models based on a text query with optional parameter count filtering.
|
| 122 |
+
|
| 123 |
+
Args:
|
| 124 |
+
query: Search query text
|
| 125 |
+
k: Number of results to return (1-100)
|
| 126 |
+
sort_by: Sort method for results (similarity, likes, downloads, trending)
|
| 127 |
+
min_likes: Minimum likes filter
|
| 128 |
+
min_downloads: Minimum downloads filter
|
| 129 |
+
min_param_count: Minimum parameter count (excludes models with unknown params)
|
| 130 |
+
max_param_count: Maximum parameter count (None for no limit)
|
| 131 |
+
|
| 132 |
+
Returns:
|
| 133 |
+
Formatted search results with model IDs, summaries, and metadata
|
| 134 |
+
"""
|
| 135 |
+
params = {
|
| 136 |
+
"query": query,
|
| 137 |
+
"k": k,
|
| 138 |
+
"sort_by": sort_by,
|
| 139 |
+
"min_likes": min_likes,
|
| 140 |
+
"min_downloads": min_downloads,
|
| 141 |
+
"min_param_count": min_param_count
|
| 142 |
+
}
|
| 143 |
+
if max_param_count is not None:
|
| 144 |
+
params["max_param_count"] = max_param_count
|
| 145 |
+
|
| 146 |
+
response = client.get(f"{base_url}/search/models", params=params)
|
| 147 |
+
response.raise_for_status()
|
| 148 |
+
data = response.json()
|
| 149 |
+
|
| 150 |
+
results = data.get("results", [])
|
| 151 |
+
if not results:
|
| 152 |
+
return "No models found."
|
| 153 |
+
|
| 154 |
+
output = []
|
| 155 |
+
for i, result in enumerate(results, 1):
|
| 156 |
+
output.append(f"{i}. **{result['model_id']}**")
|
| 157 |
+
output.append(f" - Summary: {result['summary']}")
|
| 158 |
+
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 159 |
+
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 160 |
+
if result.get('param_count') is not None and result['param_count'] > 0:
|
| 161 |
+
# Format parameter count nicely
|
| 162 |
+
param_count = result['param_count']
|
| 163 |
+
if param_count >= 1_000_000_000:
|
| 164 |
+
param_str = f"{param_count / 1_000_000_000:.1f}B"
|
| 165 |
+
elif param_count >= 1_000_000:
|
| 166 |
+
param_str = f"{param_count / 1_000_000:.1f}M"
|
| 167 |
+
elif param_count >= 1_000:
|
| 168 |
+
param_str = f"{param_count / 1_000:.1f}K"
|
| 169 |
+
else:
|
| 170 |
+
param_str = str(param_count)
|
| 171 |
+
output.append(f" - Parameters: {param_str}")
|
| 172 |
+
output.append("")
|
| 173 |
+
|
| 174 |
+
return "\n".join(output)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def find_similar_models(
|
| 178 |
+
model_id: str,
|
| 179 |
+
k: int = 5,
|
| 180 |
+
sort_by: str = "similarity",
|
| 181 |
+
min_likes: int = 0,
|
| 182 |
+
min_downloads: int = 0,
|
| 183 |
+
min_param_count: int = 0,
|
| 184 |
+
max_param_count: Optional[int] = None
|
| 185 |
+
) -> str:
|
| 186 |
+
"""
|
| 187 |
+
Find models similar to a specified model.
|
| 188 |
+
|
| 189 |
+
Args:
|
| 190 |
+
model_id: Model ID to find similar models for
|
| 191 |
+
k: Number of results to return (1-100)
|
| 192 |
+
sort_by: Sort method for results (similarity, likes, downloads, trending)
|
| 193 |
+
min_likes: Minimum likes filter
|
| 194 |
+
min_downloads: Minimum downloads filter
|
| 195 |
+
min_param_count: Minimum parameter count (excludes models with unknown params)
|
| 196 |
+
max_param_count: Maximum parameter count (None for no limit)
|
| 197 |
+
|
| 198 |
+
Returns:
|
| 199 |
+
Formatted list of similar models with metadata
|
| 200 |
+
"""
|
| 201 |
+
params = {
|
| 202 |
+
"model_id": model_id,
|
| 203 |
+
"k": k,
|
| 204 |
+
"sort_by": sort_by,
|
| 205 |
+
"min_likes": min_likes,
|
| 206 |
+
"min_downloads": min_downloads,
|
| 207 |
+
"min_param_count": min_param_count
|
| 208 |
+
}
|
| 209 |
+
if max_param_count is not None:
|
| 210 |
+
params["max_param_count"] = max_param_count
|
| 211 |
+
|
| 212 |
+
response = client.get(f"{base_url}/similarity/models", params=params)
|
| 213 |
+
response.raise_for_status()
|
| 214 |
+
data = response.json()
|
| 215 |
+
|
| 216 |
+
results = data.get("results", [])
|
| 217 |
+
if not results:
|
| 218 |
+
return "No similar models found."
|
| 219 |
+
|
| 220 |
+
output = []
|
| 221 |
+
for i, result in enumerate(results, 1):
|
| 222 |
+
output.append(f"{i}. **{result['model_id']}**")
|
| 223 |
+
output.append(f" - Summary: {result['summary']}")
|
| 224 |
+
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 225 |
+
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 226 |
+
if result.get('param_count') is not None and result['param_count'] > 0:
|
| 227 |
+
# Format parameter count nicely
|
| 228 |
+
param_count = result['param_count']
|
| 229 |
+
if param_count >= 1_000_000_000:
|
| 230 |
+
param_str = f"{param_count / 1_000_000_000:.1f}B"
|
| 231 |
+
elif param_count >= 1_000_000:
|
| 232 |
+
param_str = f"{param_count / 1_000_000:.1f}M"
|
| 233 |
+
elif param_count >= 1_000:
|
| 234 |
+
param_str = f"{param_count / 1_000:.1f}K"
|
| 235 |
+
else:
|
| 236 |
+
param_str = str(param_count)
|
| 237 |
+
output.append(f" - Parameters: {param_str}")
|
| 238 |
+
output.append("")
|
| 239 |
+
|
| 240 |
+
return "\n".join(output)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def get_trending_models(
|
| 244 |
+
limit: int = 10,
|
| 245 |
+
min_likes: int = 0,
|
| 246 |
+
min_downloads: int = 0,
|
| 247 |
+
min_param_count: int = 0,
|
| 248 |
+
max_param_count: Optional[int] = None
|
| 249 |
+
) -> str:
|
| 250 |
+
"""
|
| 251 |
+
Get trending models with their summaries and optional filtering.
|
| 252 |
+
|
| 253 |
+
Args:
|
| 254 |
+
limit: Number of results to return (1-100)
|
| 255 |
+
min_likes: Minimum likes filter
|
| 256 |
+
min_downloads: Minimum downloads filter
|
| 257 |
+
min_param_count: Minimum parameter count (excludes models with unknown params)
|
| 258 |
+
max_param_count: Maximum parameter count (None for no limit)
|
| 259 |
+
|
| 260 |
+
Returns:
|
| 261 |
+
Formatted list of trending models with metadata
|
| 262 |
+
"""
|
| 263 |
+
params = {
|
| 264 |
+
"limit": limit,
|
| 265 |
+
"min_likes": min_likes,
|
| 266 |
+
"min_downloads": min_downloads,
|
| 267 |
+
"min_param_count": min_param_count
|
| 268 |
+
}
|
| 269 |
+
if max_param_count is not None:
|
| 270 |
+
params["max_param_count"] = max_param_count
|
| 271 |
+
|
| 272 |
+
response = client.get(f"{base_url}/trending/models", params=params)
|
| 273 |
+
response.raise_for_status()
|
| 274 |
+
data = response.json()
|
| 275 |
+
|
| 276 |
+
results = data.get("results", [])
|
| 277 |
+
if not results:
|
| 278 |
+
return "No trending models found."
|
| 279 |
+
|
| 280 |
+
output = []
|
| 281 |
+
for i, result in enumerate(results, 1):
|
| 282 |
+
output.append(f"{i}. **{result['model_id']}**")
|
| 283 |
+
output.append(f" - Summary: {result['summary']}")
|
| 284 |
+
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 285 |
+
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 286 |
+
if result.get('param_count') is not None and result['param_count'] > 0:
|
| 287 |
+
# Format parameter count nicely
|
| 288 |
+
param_count = result['param_count']
|
| 289 |
+
if param_count >= 1_000_000_000:
|
| 290 |
+
param_str = f"{param_count / 1_000_000_000:.1f}B"
|
| 291 |
+
elif param_count >= 1_000_000:
|
| 292 |
+
param_str = f"{param_count / 1_000_000:.1f}M"
|
| 293 |
+
elif param_count >= 1_000:
|
| 294 |
+
param_str = f"{param_count / 1_000:.1f}K"
|
| 295 |
+
else:
|
| 296 |
+
param_str = str(param_count)
|
| 297 |
+
output.append(f" - Parameters: {param_str}")
|
| 298 |
+
output.append("")
|
| 299 |
+
|
| 300 |
+
return "\n".join(output)
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
def get_trending_datasets(
|
| 304 |
+
limit: int = 10,
|
| 305 |
+
min_likes: int = 0,
|
| 306 |
+
min_downloads: int = 0
|
| 307 |
+
) -> str:
|
| 308 |
+
"""
|
| 309 |
+
Get trending datasets with their summaries.
|
| 310 |
+
|
| 311 |
+
Args:
|
| 312 |
+
limit: Number of results to return (1-100)
|
| 313 |
+
min_likes: Minimum likes filter
|
| 314 |
+
min_downloads: Minimum downloads filter
|
| 315 |
+
|
| 316 |
+
Returns:
|
| 317 |
+
Formatted list of trending datasets with metadata
|
| 318 |
+
"""
|
| 319 |
+
params = {
|
| 320 |
+
"limit": limit,
|
| 321 |
+
"min_likes": min_likes,
|
| 322 |
+
"min_downloads": min_downloads
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
response = client.get(f"{base_url}/trending/datasets", params=params)
|
| 326 |
+
response.raise_for_status()
|
| 327 |
+
data = response.json()
|
| 328 |
+
|
| 329 |
+
results = data.get("results", [])
|
| 330 |
+
if not results:
|
| 331 |
+
return "No trending datasets found."
|
| 332 |
+
|
| 333 |
+
output = []
|
| 334 |
+
for i, result in enumerate(results, 1):
|
| 335 |
+
output.append(f"{i}. **{result['dataset_id']}**")
|
| 336 |
+
output.append(f" - Summary: {result['summary']}")
|
| 337 |
+
output.append(f" - Similarity: {result['similarity']:.3f}")
|
| 338 |
+
output.append(f" - Likes: {result['likes']:,} | Downloads: {result['downloads']:,}")
|
| 339 |
+
output.append("")
|
| 340 |
+
|
| 341 |
+
return "\n".join(output)
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
def download_model_card(model_id: str) -> str:
|
| 345 |
+
"""
|
| 346 |
+
Download the README card for a HuggingFace model.
|
| 347 |
+
|
| 348 |
+
Args:
|
| 349 |
+
model_id: The model ID (e.g., 'username/model-name')
|
| 350 |
+
|
| 351 |
+
Returns:
|
| 352 |
+
The content of the model card (README.md)
|
| 353 |
+
"""
|
| 354 |
+
url = f"https://huggingface.co/{model_id}/raw/main/README.md"
|
| 355 |
+
response = client.get(url)
|
| 356 |
+
response.raise_for_status()
|
| 357 |
+
return response.text
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def download_dataset_card(dataset_id: str) -> str:
|
| 361 |
+
"""
|
| 362 |
+
Download the README card for a HuggingFace dataset.
|
| 363 |
+
|
| 364 |
+
Args:
|
| 365 |
+
dataset_id: The dataset ID (e.g., 'username/dataset-name')
|
| 366 |
+
|
| 367 |
+
Returns:
|
| 368 |
+
The content of the dataset card (README.md)
|
| 369 |
+
"""
|
| 370 |
+
url = f"https://huggingface.co/datasets/{dataset_id}/raw/main/README.md"
|
| 371 |
+
response = client.get(url)
|
| 372 |
+
response.raise_for_status()
|
| 373 |
+
return response.text
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
# Create Gradio interface
|
| 377 |
+
with gr.Blocks(title="HuggingFace Search MCP Server") as demo:
|
| 378 |
+
gr.Markdown("# HuggingFace Search MCP Server")
|
| 379 |
+
gr.Markdown("This server provides semantic search capabilities for HuggingFace models and datasets.")
|
| 380 |
+
|
| 381 |
+
with gr.Tab("Search Datasets"):
|
| 382 |
+
gr.Interface(
|
| 383 |
+
fn=search_datasets,
|
| 384 |
+
inputs=[
|
| 385 |
+
gr.Textbox(label="Query", placeholder="Enter search query"),
|
| 386 |
+
gr.Slider(1, 100, value=5, step=1, label="Number of results"),
|
| 387 |
+
gr.Dropdown(["similarity", "likes", "downloads", "trending"], value="similarity", label="Sort by"),
|
| 388 |
+
gr.Number(value=0, label="Minimum likes"),
|
| 389 |
+
gr.Number(value=0, label="Minimum downloads")
|
| 390 |
+
],
|
| 391 |
+
outputs=gr.Markdown(label="Results"),
|
| 392 |
+
title="Search Datasets",
|
| 393 |
+
description="Search for datasets based on a text query"
|
| 394 |
)
|
| 395 |
+
|
| 396 |
+
with gr.Tab("Find Similar Datasets"):
|
| 397 |
+
gr.Interface(
|
| 398 |
+
fn=find_similar_datasets,
|
| 399 |
+
inputs=[
|
| 400 |
+
gr.Textbox(label="Dataset ID", placeholder="username/dataset-name"),
|
| 401 |
+
gr.Slider(1, 100, value=5, step=1, label="Number of results"),
|
| 402 |
+
gr.Dropdown(["similarity", "likes", "downloads", "trending"], value="similarity", label="Sort by"),
|
| 403 |
+
gr.Number(value=0, label="Minimum likes"),
|
| 404 |
+
gr.Number(value=0, label="Minimum downloads")
|
| 405 |
+
],
|
| 406 |
+
outputs=gr.Markdown(label="Results"),
|
| 407 |
+
title="Find Similar Datasets",
|
| 408 |
+
description="Find datasets similar to a specified dataset"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
)
|
| 410 |
+
|
| 411 |
+
with gr.Tab("Search Models"):
|
| 412 |
+
gr.Interface(
|
| 413 |
+
fn=search_models,
|
| 414 |
+
inputs=[
|
| 415 |
+
gr.Textbox(label="Query", placeholder="Enter search query"),
|
| 416 |
+
gr.Slider(1, 100, value=5, step=1, label="Number of results"),
|
| 417 |
+
gr.Dropdown(["similarity", "likes", "downloads", "trending"], value="similarity", label="Sort by"),
|
| 418 |
+
gr.Number(value=0, label="Minimum likes"),
|
| 419 |
+
gr.Number(value=0, label="Minimum downloads"),
|
| 420 |
+
gr.Number(value=0, label="Minimum parameter count"),
|
| 421 |
+
gr.Number(value=None, label="Maximum parameter count (leave empty for no limit)")
|
| 422 |
+
],
|
| 423 |
+
outputs=gr.Markdown(label="Results"),
|
| 424 |
+
title="Search Models",
|
| 425 |
+
description="Search for models based on a text query with optional parameter count filtering"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
)
|
| 427 |
+
|
| 428 |
+
with gr.Tab("Find Similar Models"):
|
| 429 |
+
gr.Interface(
|
| 430 |
+
fn=find_similar_models,
|
| 431 |
+
inputs=[
|
| 432 |
+
gr.Textbox(label="Model ID", placeholder="username/model-name"),
|
| 433 |
+
gr.Slider(1, 100, value=5, step=1, label="Number of results"),
|
| 434 |
+
gr.Dropdown(["similarity", "likes", "downloads", "trending"], value="similarity", label="Sort by"),
|
| 435 |
+
gr.Number(value=0, label="Minimum likes"),
|
| 436 |
+
gr.Number(value=0, label="Minimum downloads"),
|
| 437 |
+
gr.Number(value=0, label="Minimum parameter count"),
|
| 438 |
+
gr.Number(value=None, label="Maximum parameter count (leave empty for no limit)")
|
| 439 |
+
],
|
| 440 |
+
outputs=gr.Markdown(label="Results"),
|
| 441 |
+
title="Find Similar Models",
|
| 442 |
+
description="Find models similar to a specified model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
)
|
| 444 |
+
|
| 445 |
+
with gr.Tab("Trending Models"):
|
| 446 |
+
gr.Interface(
|
| 447 |
+
fn=get_trending_models,
|
| 448 |
+
inputs=[
|
| 449 |
+
gr.Slider(1, 100, value=10, step=1, label="Number of results"),
|
| 450 |
+
gr.Number(value=0, label="Minimum likes"),
|
| 451 |
+
gr.Number(value=0, label="Minimum downloads"),
|
| 452 |
+
gr.Number(value=0, label="Minimum parameter count"),
|
| 453 |
+
gr.Number(value=None, label="Maximum parameter count (leave empty for no limit)")
|
| 454 |
+
],
|
| 455 |
+
outputs=gr.Markdown(label="Results"),
|
| 456 |
+
title="Get Trending Models",
|
| 457 |
+
description="Get trending models with their summaries and optional filtering"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
)
|
| 459 |
+
|
| 460 |
+
with gr.Tab("Trending Datasets"):
|
| 461 |
+
gr.Interface(
|
| 462 |
+
fn=get_trending_datasets,
|
| 463 |
+
inputs=[
|
| 464 |
+
gr.Slider(1, 100, value=10, step=1, label="Number of results"),
|
| 465 |
+
gr.Number(value=0, label="Minimum likes"),
|
| 466 |
+
gr.Number(value=0, label="Minimum downloads")
|
| 467 |
+
],
|
| 468 |
+
outputs=gr.Markdown(label="Results"),
|
| 469 |
+
title="Get Trending Datasets",
|
| 470 |
+
description="Get trending datasets with their summaries"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
)
|
| 472 |
+
|
| 473 |
+
with gr.Tab("Download Model Card"):
|
| 474 |
+
gr.Interface(
|
| 475 |
+
fn=download_model_card,
|
| 476 |
+
inputs=gr.Textbox(label="Model ID", placeholder="username/model-name"),
|
| 477 |
+
outputs=gr.Textbox(label="Model Card Content", lines=20),
|
| 478 |
+
title="Download Model Card",
|
| 479 |
+
description="Download the README card for a HuggingFace model"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
)
|
| 481 |
+
|
| 482 |
+
with gr.Tab("Download Dataset Card"):
|
| 483 |
+
gr.Interface(
|
| 484 |
+
fn=download_dataset_card,
|
| 485 |
+
inputs=gr.Textbox(label="Dataset ID", placeholder="username/dataset-name"),
|
| 486 |
+
outputs=gr.Textbox(label="Dataset Card Content", lines=20),
|
| 487 |
+
title="Download Dataset Card",
|
| 488 |
+
description="Download the README card for a HuggingFace dataset"
|
| 489 |
)
|
| 490 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
if __name__ == "__main__":
|
| 493 |
+
# Launch with MCP server enabled
|
| 494 |
+
demo.launch(mcp_server=True)
|