huggingface-gemma-recipes
は、Hugging Face が公式にメンテナンスしているオープンソースプロジェクトであり、Google Gemma シリーズモデルに関連する最小限のサンプルコードとチュートリアルをユーザーに提供することを目的としています。このプロジェクトの主な目標は、開発者が Gemma モデルの推論、微調整、およびさまざまな実際のアプリケーションシナリオを迅速に開始できるようにすることです。
このプロジェクトは、Gemma 3 シリーズモデルのマルチモーダル機能をサポートしています。
このプロジェクトは、統一されたモデル推論インターフェースを提供し、Gemma モデルの迅速なロードと使用をサポートします。
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
model_id = "google/gemma-3n-e4b-it" # または google/gemma-3n-e2b-it
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id).to(device)
def model_generation(model, messages):
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
)
input_len = inputs["input_ids"].shape[-1]
inputs = inputs.to(model.device, dtype=model.dtype)
with torch.inference_mode():
generation = model.generate(**inputs, max_new_tokens=32, disable_compile=False)
generation = generation[:, input_len:]
decoded = processor.batch_decode(generation, skip_special_tokens=True)
print(decoded[0])
# テキスト質疑応答
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What is the capital of France?"}
]
}
]
model_generation(model, messages)
# 音声テキスト変換
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "Transcribe the following speech segment in English:"},
{"type": "audio", "audio": "https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/speech.wav"},
]
}
]
model_generation(model, messages)
# 画像の説明
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": "https://huggingface.co/datasets/ariG23498/demo-data/resolve/main/airplane.jpg"},
{"type": "text", "text": "Describe this image."}
]
}
]
model_generation(model, messages)
このプロジェクトは、さまざまな微調整ソリューションとスクリプトを提供します。
# 依存関係のインストール
$ pip install -U -q -r requirements.txt
# コア依存関係のインストール
$ pip install -U -q transformers timm
# 完全な依存関係のインストール (微調整用)
$ pip install -U -q -r requirements.txt
huggingface-gemma-recipes/
├── notebooks/ # Jupyter ノートブックチュートリアル
│ └── fine_tune_gemma3n_on_t4.ipynb
├── scripts/ # 微調整スクリプト
│ ├── ft_gemma3n_image_vt.py
│ ├── ft_gemma3n_audio_vt.py
│ └── ft_gemma3n_image_trl.py
├── requirements.txt # 依存関係リスト
└── README.md # プロジェクトの説明
このプロジェクトは、Hugging Face が公式にメンテナンスしているオープンソースプロジェクトとして、次の利点があります。
huggingface-gemma-recipes
は、Gemma モデルの使用に完全なソリューションを提供する高品質のオープンソースプロジェクトです。初心者でも経験豊富な開発者でも、適切なリソースとガイダンスを見つけることができます。プロジェクトのマルチモーダルサポートと柔軟な微調整ソリューションにより、現在の AI 開発分野における重要なツールの 1 つとなっています。