Llama3.2を試してみて、初心者が最初に気をつけるべきだと感じたポイント【Python】

LLM

最近、Llama3.2が出てLLM界隈はじめ、AI界隈全体で話題となっています。

今回はLlama3.2を試そうと動かしたときに躓いた、ちょっとしたポイントについてお話します。

Llama3.2を試す

サンプルコード

HuggingFaceのLlama3.2公式ページでは、以下のサンプルコードが紹介されています。

import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor

model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"

model = MllamaForConditionalGeneration.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_id)

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"
image = Image.open(requests.get(url, stream=True).raw)

messages = [
    {"role": "user", "content": [
        {"type": "image"},
        {"type": "text", "text": "If I had to write a haiku for this one, it would be: "}
    ]}
]
input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(
    image,
    input_text,
    add_special_tokens=False,
    return_tensors="pt"
).to(model.device)

output = model.generate(**inputs, max_new_tokens=30)
print(processor.decode(output[0]))

内容としては、以下の画像から俳句(英語)を作ったらどうなるのか、というものになります。

注意事項

動作にあたっては、MllamaForConditionalGenerationという最近追加されたクラスを使用するため、transformers >= 4.45.0が必要になります。
transformersのバージョンが古い方は、pip install –upgrade transformersで必ずバージョンアップしておきましょう。

エラー対応

エラー内容

・・・というのが一般的な記事ですが、初心者が試すとこんなエラーが出てきます。

OSError: You are trying to access a gated repo.
Make sure to have access to it at https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct.
403 Client Error.

Llama3.2へのアクセス権がないということで、アクセストークンが必要になります。
詳しくは以下の記事を御覧ください。

解決策

上の紹介記事では説明しきれていない部分のみ、以下で説明します。

まず、公式ページから"Expand to review and access"をクリックします。

必要事項を入力して、チェックボックスにチェックを入れたら"Submit"します。

以下の画面が出てきますが、よく見ると「リクエストのレビュー待ちだよ」的なことが書かれています。
だいたい30分くらい経ったらレビューが完了するので、それまでは待つしかありません。

レビュー完了後、ページを更新すると以下の表示が出てきます。

そうすると、先ほどのサンプルコードが実行できるようになっているはずです。
※それでも動かない!という方は、先ほど紹介した記事からHuggingFace認証の方法をご確認ください。

出力結果

ちなみに出力結果はこんな感じになりました。

<|begin_of_text|><|begin_of_text|><|start_header_id|>user<|end_header_id|>

<|image|>If I had to write a haiku for this one, it would be: <|eot_id|><|start_header_id|>assistant<|end_header_id|>

Here is a haiku for the image:

A rabbit in a coat so fine,
Strolling down the dirt path home,
Springtime's gentle breeze

以上です。

スポンサーリンク

LLM

Posted by このめ