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