Skip to content

Commit 18b2880

Browse files
committed
added huggingface tool to explore huggingface Hub
1 parent dd203e6 commit 18b2880

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from openagi.actions.base import ConfigurableAction
2+
from pydantic import Field
3+
import json
4+
5+
try:
6+
from huggingface_hub import HfApi
7+
except ImportError:
8+
raise Exception("Install huggingface_hub with cmd `pip install huggingface`")
9+
10+
class HuggingFaceTool(ConfigurableAction):
11+
"""
12+
Tool for exploring Hugging Face models and datasets.
13+
"""
14+
query: str = Field(..., description="Dataset or model name to search on Hugging Face.")
15+
16+
def execute(self) -> str:
17+
api = HfApi()
18+
results_dict = {}
19+
20+
models = api.list_models(search=self.query, limit=15)
21+
datasets = api.list_datasets(search=self.query, limit=15)
22+
23+
for model in models:
24+
results_dict[model.modelId] = f"https://huggingface.co/{model.modelId}"
25+
26+
for dataset in datasets:
27+
results_dict[dataset.id] = f"https://huggingface.co/datasets/{dataset.id}"
28+
29+
return json.dumps(results_dict)

0 commit comments

Comments
 (0)