Final_Assignment / tools /get_hub_stats.py
Alfred828's picture
Create tools/get_hub_stats.py
16d2cc0 verified
raw
history blame contribute delete
828 Bytes
from huggingface_hub import list_models
from pydantic import BaseModel, Field
class HubStatInput(BaseModel):
author: str = Field(description="author on the Hugging Face Hub")
def get_hub_stats(author: str) -> str:
"""Fetches the most downloaded model from a specific author on the Hugging Face Hub."""
try:
# List models from the specified author, sorted by downloads
models = list(
list_models(author=author, sort="downloads", direction=-1, limit=1)
)
if models:
model = models[0]
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
else:
return f"No models found for author {author}."
except Exception as e:
return f"Error fetching models for {author}: {str(e)}"