Create tools.py
Browse files
tools.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import DuckDuckGoSearchTool
|
| 2 |
+
from smolagents import Tool
|
| 3 |
+
import random
|
| 4 |
+
from huggingface_hub import list_models
|
| 5 |
+
|
| 6 |
+
class HubStatsTool(Tool):
|
| 7 |
+
name = "hub_stats"
|
| 8 |
+
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 9 |
+
inputs = {
|
| 10 |
+
"author": {
|
| 11 |
+
"type": "string",
|
| 12 |
+
"description": "The username of the model author/organization to find models from."
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
output_type = "string"
|
| 16 |
+
|
| 17 |
+
def forward(self, author: str):
|
| 18 |
+
try:
|
| 19 |
+
# List models from the specified author, sorted by downloads
|
| 20 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 21 |
+
|
| 22 |
+
if models:
|
| 23 |
+
model = models[0]
|
| 24 |
+
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 25 |
+
else:
|
| 26 |
+
return f"No models found for author {author}."
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error fetching models for {author}: {str(e)}"
|