YAML Metadata
Warning:
empty or missing yaml metadata in repo card
(https://huggingface.co/docs/hub/model-cards#model-card-metadata)
Google Colabでの推論手順
この手順では、Hugging Face HubにアップロードされたLLMモデル (nagasahiro/llm-jp-3-13b-sft-07
)をGoogle Colab環境で読み込み、推論を実行する方法について説明します。
準備
- Google Colabへのログイン: GoogleアカウントでGoogle Colabにログインしてください。
- ノートブックの作成: 新しいPython 3のノートブックを作成します。
- シークレットの設定: Hugging Face のトークン (
HF_TOKEN
) を Google Colab のシークレットに登録してください。
シークレットの設定方法:
- Google Colab の左側のメニューから「シークレット」を選択します。
- 「シークレットを作成」をクリックし、名前 (
HF_TOKEN
) と値をそれぞれ入力して保存します。
推論の実行手順
以下の手順をGoogle Colabのコードセルに入力し、実行してください。
1. 必要なライブラリのインストール
%%capture
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
2. Hugging Face Hubへのログイン
Hugging Face Hubからモデルをダウンロードするために、認証を行います。以下のコードを実行し、Hugging Faceのトークンを入力してください。
from huggingface_hub import login
from google.colab import userdata
HF_TOKEN = userdata.get('HF_TOKEN')
login(HF_TOKEN)
3. モデルの準備
推論に使用するモデルをロードします。
from unsloth import FastLanguageModel
model_name = "nagasahiro/llm-jp-3-13b-sft-07"
max_seq_length = 2048
dtype = None
load_in_4bit = True
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = model_name,
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
)
FastLanguageModel.for_inference(model)
4. 推論の実行
推論を実行するコードです。プロンプトを変更することで、様々なタスクに対応できます。
import torch
prompt = "質問: 日本の首都は?\n回答:"
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=512, use_cache=True, do_sample=False, repetition_penalty=1.2)
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('回答:')[-1]
print(prediction)
補足事項
- この手順は Google Colab 環境で L4 GPU を用いて検証されました。
- Google Colab の環境によっては、ライブラリのインストールやモデルのダウンロードに時間がかかる場合があります。
- エラーが発生した場合は、エラーメッセージを確認し、手順を見直してください。
Inference Providers
NEW
This model is not currently available via any of the supported Inference Providers.
The model cannot be deployed to the HF Inference API:
The model has no library tag.