huggingface
Hugging Faceの機械学習エコシステムを活用し、学習済みモデルのダウンロードから推論実行、カスタムモデルのトレーニング、そしてモデルハブへの公開までを包括的に支援するSkill。
📜 元の英語説明(参考)
Work with Hugging Face's ecosystem for machine learning — transformers library, model hub, tokenizers, inference pipelines, and fine-tuning. Covers downloading pre-trained models, running inference, training custom models, and publishing to the Hub.
🇯🇵 日本人クリエイター向け解説
Hugging Faceの機械学習エコシステムを活用し、学習済みモデルのダウンロードから推論実行、カスタムモデルのトレーニング、そしてモデルハブへの公開までを包括的に支援するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o huggingface.zip https://jpskill.com/download/14991.zip && unzip -o huggingface.zip && rm huggingface.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/14991.zip -OutFile "$d\huggingface.zip"; Expand-Archive "$d\huggingface.zip" -DestinationPath $d -Force; ri "$d\huggingface.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
huggingface.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
huggingfaceフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
Hugging Face
インストール
# コアライブラリのインストール
pip install transformers datasets tokenizers accelerate huggingface_hub
# Hugging Face Hub にログイン
huggingface-cli login
# または、トークンを環境変数として設定
export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxx"
Pipelines を使用したクイック推論
# quick_inference.py — ビルトインの pipelines を使用して推論を実行
from transformers import pipeline
# テキスト分類
classifier = pipeline("sentiment-analysis")
result = classifier("I love using Hugging Face!")
print(result) # [{'label': 'POSITIVE', 'score': 0.9998}]
# テキスト生成
generator = pipeline("text-generation", model="meta-llama/Llama-2-7b-chat-hf")
output = generator("Explain transformers in one sentence:", max_new_tokens=50)
print(output[0]["generated_text"])
# 固有表現認識
ner = pipeline("ner", grouped_entities=True)
entities = ner("Hugging Face is based in New York City.")
print(entities)
モデルと Tokenizer のロード
# load_model.py — より詳細な制御のために、モデルと tokenizer を手動でロード
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto", # GPU 全体に自動的に分散
load_in_4bit=True, # VRAM を削減するための量子化ロード
)
inputs = tokenizer("What is machine learning?", return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=100, temperature=0.7)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Tokenizer の使用法
# tokenizer_basics.py — デバッグと前処理のためのトークン化を理解する
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
text = "Hugging Face makes NLP easy!"
encoded = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
print(f"Input IDs: {encoded['input_ids']}")
print(f"Tokens: {tokenizer.convert_ids_to_tokens(encoded['input_ids'][0])}")
print(f"Token count: {len(encoded['input_ids'][0])}")
# バッチエンコード
texts = ["First sentence.", "Second longer sentence here."]
batch = tokenizer(texts, padding=True, truncation=True, return_tensors="pt")
Trainer API を使用したファイン・チューニング
# fine_tune.py — Trainer API を使用してカスタムデータセットでモデルをファイン・チューニング
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification,
TrainingArguments, Trainer
)
from datasets import load_dataset
import numpy as np
from sklearn.metrics import accuracy_score
model_name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
dataset = load_dataset("imdb")
def tokenize(batch):
return tokenizer(batch["text"], padding="max_length", truncation=True, max_length=256)
tokenized = dataset.map(tokenize, batched=True)
def compute_metrics(eval_pred):
logits, labels = eval_pred
preds = np.argmax(logits, axis=-1)
return {"accuracy": accuracy_score(labels, preds)}
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=16,
per_device_eval_batch_size=64,
eval_strategy="epoch",
save_strategy="epoch",
learning_rate=2e-5,
weight_decay=0.01,
logging_steps=100,
fp16=True,
push_to_hub=False,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized["train"].select(range(5000)),
eval_dataset=tokenized["test"].select(range(1000)),
compute_metrics=compute_metrics,
)
trainer.train()
trainer.evaluate()
LoRA / PEFT ファイン・チューニング
# lora_finetune.py — LoRA を使用したパラメータ効率の高いファイン・チューニング
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model, TaskType
import torch
model_name = "meta-llama/Llama-2-7b-hf"
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
r=16,
lora_alpha=32,
lora_dropout=0.05,
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# trainable params: 4,194,304 || all params: 6,742,609,920 || trainable%: 0.062
Datasets ライブラリ
# datasets_usage.py — カスタムデータセットのロード、処理、作成
from datasets import load_dataset, Dataset
# Hub からロード
squad = load_dataset("squad", split="train[:1000]")
print(squad[0])
# カスタム CSV/JSON のロード
dataset = Dataset.from_dict({
"text": ["Hello world", "Goodbye world"],
"label": [1, 0],
})
# ローカルに保存してロード
dataset.save_to_disk("./my_dataset")
loaded = Dataset.load_from_disk("./my_dataset")
# Hub にプッシュ
dataset.push_to_hub("my-username/my-dataset", private=True)
モデルを Hub に公開
# push_to_hub.py — トレーニング済みのモデルを保存して Hugging Face Hub で共有
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model_name = "my-org/my-fine-tuned-model"
# トレーニング後、モデルと tokenizer をプッシュ
model.push_to_hub(model_name, private=True)
tokenizer.push_to_hub(model_name, private=True)
# モデルカードを作成
from huggingface_hub import ModelCard
card = ModelCard.from_template(
card_data={"license": "mit", "datasets": ["imdb"], "language": "en"},
template_str="# My Model\n\nFine-tuned DistilBERT on IMDB.\n\n{{ card_data }}",
)
card.push_to_hub(model_name)
Inference API
# inference_api.py — 迅速なモデルアクセスにサーバーレス Inference API を使用
from huggingface_hub import InferenceClient
client = InferenceClient(token="hf_xxxxxxxxxxxx")
# テキスト生成
(原文がここで切り詰められています) 📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Hugging Face
Installation
# Install core libraries
pip install transformers datasets tokenizers accelerate huggingface_hub
# Login to Hugging Face Hub
huggingface-cli login
# Or set token as environment variable
export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxx"
Quick Inference with Pipelines
# quick_inference.py — Run inference using built-in pipelines
from transformers import pipeline
# Text classification
classifier = pipeline("sentiment-analysis")
result = classifier("I love using Hugging Face!")
print(result) # [{'label': 'POSITIVE', 'score': 0.9998}]
# Text generation
generator = pipeline("text-generation", model="meta-llama/Llama-2-7b-chat-hf")
output = generator("Explain transformers in one sentence:", max_new_tokens=50)
print(output[0]["generated_text"])
# Named entity recognition
ner = pipeline("ner", grouped_entities=True)
entities = ner("Hugging Face is based in New York City.")
print(entities)
Loading Models and Tokenizers
# load_model.py — Load a model and tokenizer manually for more control
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto", # Automatically distribute across GPUs
load_in_4bit=True, # Quantized loading for less VRAM
)
inputs = tokenizer("What is machine learning?", return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=100, temperature=0.7)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Tokenizer Usage
# tokenizer_basics.py — Understand tokenization for debugging and preprocessing
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
text = "Hugging Face makes NLP easy!"
encoded = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
print(f"Input IDs: {encoded['input_ids']}")
print(f"Tokens: {tokenizer.convert_ids_to_tokens(encoded['input_ids'][0])}")
print(f"Token count: {len(encoded['input_ids'][0])}")
# Batch encoding
texts = ["First sentence.", "Second longer sentence here."]
batch = tokenizer(texts, padding=True, truncation=True, return_tensors="pt")
Fine-Tuning with Trainer API
# fine_tune.py — Fine-tune a model on a custom dataset using the Trainer API
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification,
TrainingArguments, Trainer
)
from datasets import load_dataset
import numpy as np
from sklearn.metrics import accuracy_score
model_name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
dataset = load_dataset("imdb")
def tokenize(batch):
return tokenizer(batch["text"], padding="max_length", truncation=True, max_length=256)
tokenized = dataset.map(tokenize, batched=True)
def compute_metrics(eval_pred):
logits, labels = eval_pred
preds = np.argmax(logits, axis=-1)
return {"accuracy": accuracy_score(labels, preds)}
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=16,
per_device_eval_batch_size=64,
eval_strategy="epoch",
save_strategy="epoch",
learning_rate=2e-5,
weight_decay=0.01,
logging_steps=100,
fp16=True,
push_to_hub=False,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized["train"].select(range(5000)),
eval_dataset=tokenized["test"].select(range(1000)),
compute_metrics=compute_metrics,
)
trainer.train()
trainer.evaluate()
LoRA / PEFT Fine-Tuning
# lora_finetune.py — Parameter-efficient fine-tuning with LoRA
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model, TaskType
import torch
model_name = "meta-llama/Llama-2-7b-hf"
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name)
lora_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
r=16,
lora_alpha=32,
lora_dropout=0.05,
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# trainable params: 4,194,304 || all params: 6,742,609,920 || trainable%: 0.062
Datasets Library
# datasets_usage.py — Load, process, and create custom datasets
from datasets import load_dataset, Dataset
# Load from Hub
squad = load_dataset("squad", split="train[:1000]")
print(squad[0])
# Load custom CSV/JSON
dataset = Dataset.from_dict({
"text": ["Hello world", "Goodbye world"],
"label": [1, 0],
})
# Save and load locally
dataset.save_to_disk("./my_dataset")
loaded = Dataset.load_from_disk("./my_dataset")
# Push to Hub
dataset.push_to_hub("my-username/my-dataset", private=True)
Publishing Models to Hub
# push_to_hub.py — Save and share your trained model on Hugging Face Hub
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model_name = "my-org/my-fine-tuned-model"
# After training, push model and tokenizer
model.push_to_hub(model_name, private=True)
tokenizer.push_to_hub(model_name, private=True)
# Create a model card
from huggingface_hub import ModelCard
card = ModelCard.from_template(
card_data={"license": "mit", "datasets": ["imdb"], "language": "en"},
template_str="# My Model\n\nFine-tuned DistilBERT on IMDB.\n\n{{ card_data }}",
)
card.push_to_hub(model_name)
Inference API
# inference_api.py — Use the serverless Inference API for quick model access
from huggingface_hub import InferenceClient
client = InferenceClient(token="hf_xxxxxxxxxxxx")
# Text generation
response = client.text_generation(
"The future of AI is",
model="mistralai/Mistral-7B-Instruct-v0.2",
max_new_tokens=100,
)
# Image generation
image = client.text_to_image("A cat wearing a space suit")
image.save("space_cat.png")
# Embeddings
embeddings = client.feature_extraction("Hello world", model="sentence-transformers/all-MiniLM-L6-v2")
Key Concepts
- AutoClasses:
AutoModel,AutoTokenizerautomatically detect the right architecture - device_map="auto": Distributes model layers across available GPUs/CPU
- load_in_4bit/8bit: Quantization via bitsandbytes for reduced memory usage
- PEFT/LoRA: Train only a small fraction of parameters — faster and cheaper
- Datasets streaming:
load_dataset(..., streaming=True)for huge datasets without downloading - Trainer: High-level API handling training loops, evaluation, checkpointing, and logging