hugging-face-tool-builder
当用户需要构建工具、脚本或完成任务时,利用Hugging Face API数据提供帮助,尤其适用于涉及模型和数据集的场景。
npx skills add huggingface/skills --skill hugging-face-tool-builderBefore / After 效果对比
1 组1我需要频繁地从Hugging Face模型库中获取信息、测试模型或处理数据,但每次都手动调用API或使用网页界面,效率低下且容易出错。1通过Hugging Face Tool Builder技能,我构建了一个可重用的Python脚本,它能够自动化地调用Hugging Face API来获取模型信息、执行推理或处理数据集。这大大简化了我的工作流程,提高了开发和研究效率。
2
3```python
4# 改进前:手动查询Hugging Face模型信息
5# 1. 访问huggingface.co
6# 2. 搜索模型,复制模型ID
7# 3. 手动调用API或在网页上查看详情
8
9# 改进后:使用Python脚本自动化查询
10from huggingface_hub import HfApi
11
12def get_model_info(model_id):
13 api = HfApi()
14 try:
15 model_card = api.get_model_info(model_id)
16 print(f"模型名称: {model_card.id}")
17 print(f"作者: {model_card.author}")
18 print(f"下载量: {model_card.downloads}")
19 print(f"标签: {', '.join(model_card.tags)}")
20 return model_card
21 except Exception as e:
22 print(f"获取模型信息失败: {e}")
23 return None
24
25# 示例:获取'bert-base-uncased'模型的信息
26model_details = get_model_info('bert-base-uncased')
27```description SKILL.md
name: hugging-face-tool-builder description: Use this skill when the user wants to build tool/scripts or achieve a task where using data from the Hugging Face API would help. This is especially useful when chaining or combining API calls or the task will be repeated/automated. This Skill creates a reusable script to fetch, enrich or process data.
Hugging Face API Tool Builder
Your purpose is now is to create reusable command line scripts and utilities for using the Hugging Face API, allowing chaining, piping and intermediate processing where helpful. You can access the API directly, as well as use the hf command line tool. Model and Dataset cards can be accessed from repositories directly.
Script Rules
Make sure to follow these rules:
- Scripts must take a
--helpcommand line argument to describe their inputs and outputs - Non-destructive scripts should be tested before handing over to the User
- Shell scripts are preferred, but use Python or TSX if complexity or user need requires it.
- IMPORTANT: Use the
HF_TOKENenvironment variable as an Authorization header. For example:curl -H "Authorization: Bearer ${HF_TOKEN}" https://huggingface.co/api/. This provides higher rate limits and appropriate authorization for data access. - Investigate the shape of the API results before commiting to a final design; make use of piping and chaining where composability would be an advantage - prefer simple solutions where possible.
- Share usage examples once complete.
Be sure to confirm User preferences where there are questions or clarifications needed.
Sample Scripts
Paths below are relative to this skill directory.
Reference examples:
references/hf_model_papers_auth.sh— usesHF_TOKENautomatically and chains trending → model metadata → model card parsing with fallbacks; it demonstrates multi-step API usage plus auth hygiene for gated/private content.references/find_models_by_paper.sh— optionalHF_TOKENusage via--token, consistent authenticated search, and a retry path when arXiv-prefixed searches are too narrow; it shows resilient query strategy and clear user-facing help.references/hf_model_card_frontmatter.sh— uses thehfCLI to download model cards, extracts YAML frontmatter, and emits NDJSON summaries (license, pipeline tag, tags, gated prompt flag) for easy filtering.
Baseline examples (ultra-simple, minimal logic, raw JSON output with HF_TOKEN header):
references/baseline_hf_api.sh— bashreferences/baseline_hf_api.py— pythonreferences/baseline_hf_api.tsx— typescript executable
Composable utility (stdin → NDJSON):
references/hf_enrich_models.sh— reads model IDs from stdin, fetches metadata per ID, emits one JSON object per line for streaming pipelines.
Composability through piping (shell-friendly JSON output):
references/baseline_hf_api.sh 25 | jq -r '.[].id' | references/hf_enrich_models.sh | jq -s 'sort_by(.downloads) | reverse | .[:10]'references/baseline_hf_api.sh 50 | jq '[.[] | {id, downloads}] | sort_by(.downloads) | reverse | .[:10]'printf '%s\n' openai/gpt-oss-120b meta-llama/Meta-Llama-3.1-8B | references/hf_model_card_frontmatter.sh | jq -s 'map({id, license, has_extra_gated_prompt})'
High Level Endpoints
The following are the main API endpoints available at https://huggingface.co
/api/datasets
/api/models
/api/spaces
/api/collections
/api/daily_papers
/api/notifications
/api/settings
/api/whoami-v2
/api/trending
/oauth/userinfo
Accessing the API
The API is documented with the OpenAPI standard at https://huggingface.co/.well-known/openapi.json.
IMPORTANT: DO NOT ATTEMPT to read https://huggingface.co/.well-known/openapi.json directly as it is too large to process.
IMPORTANT Use jq to query and extract relevant parts. For example,
Command to Get All 160 Endpoints
curl -s "https://huggingface.co/.well-known/openapi.json" | jq '.paths | keys | sort'
Model Search Endpoint Details
curl -s "https://huggingface.co/.well-known/openapi.json" | jq '.paths["/api/models"]'
You can also query endpoints to see the shape of the data. When doing so constrain results to low numbers to make them easy to process, yet representative.
Using the HF command line tool
The hf command line tool gives you further access to Hugging Face repository content and infrastructure.
❯ hf --help
Usage: hf [OPTIONS] COMMAND [ARGS]...
Hugging Face Hub CLI
Options:
--help Show this message and exit.
Commands:
auth Manage authentication (login, logout, etc.).
cache Manage local cache directory.
download Download files from the Hub.
endpoints Manage Hugging Face Inference Endpoints.
env Print information about the environment.
jobs Run and manage Jobs on the Hub.
repo Manage repos on the Hub.
repo-files Manage files in a repo on the Hub.
upload Upload a file or a folder to the Hub.
upload-large-folder Upload a large folder to the Hub.
version Print information about the hf version.
The hf CLI command has replaced the now deprecated huggingface_hub CLI command.
forum用户评价 (0)
发表评价
暂无评价,来写第一条吧
统计数据
用户评分
为此 Skill 评分