> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/models/ja/supported-models/smolvlm2.md).

# SmolVLM2

SmolVLM2 は HuggingFace のコンパクトな視覚言語モデルです。画像とテキストプロンプトを受け取り、テキスト応答を返します。

{% hint style="info" %}
SmolVLM2 は Serverless Hosted API では利用できません。次の環境で実行してください [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) または [セルフホスト型 Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## コード例

{% stepper %}
{% step %}

### API キーを取得する

Roboflow アカウントを作成し、以下でキーを見つけます： [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) その後、シェルで利用できるようにします：

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### 依存関係をインストールする

以下をインストールします： [Inference SDK](https://docs.roboflow.com/deployment/self-hosted/self-hosted):

```bash
pip install -U inference-sdk supervision
```

{% endstep %}

{% step %}

### モデルを実行する

設定する `api_url` Dedicated Deployment URL またはローカルの推論サーバーに接続してください。

```python
import os
import supervision as sv
from inference_sdk import InferenceHTTPClient

image = sv.load_image_from_url("https://media.roboflow.com/quickstart/dog.jpeg")

client = InferenceHTTPClient(
    api_url="https://your-deployment.roboflow.cloud",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)
result = client.infer_lmm(
    image,
    model_id="smolvlm2",
    prompt="この画像を簡潔に説明してください。",
    max_new_tokens=64,
)
print(result["response"])
```

{% endstep %}
{% endstepper %}

上のコードはモデルの応答をターミナルに出力します:

```
男性が肩に犬を乗せて運んでいます。
```

<figure><img src="/files/240f842a980c044a24142bc421a7c2cb7108f650" alt=""><figcaption></figcaption></figure>

## 推論速度

レイテンシの測定条件： [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1x NVIDIA L4、バッチサイズ 1、固定プロンプトから greedy decoding でちょうど 128 トークンを生成して測定しています。レイテンシは出力長に比例して変わるため、他の長さの見積もりにはトークン/秒を使用してください。

<table data-search="false"><thead><tr><th>別名</th><th>レイテンシ、128 トークン（ms）</th><th>トークン/秒</th></tr></thead><tbody><tr><td><code>smolvlm2</code></td><td>3113</td><td>41</td></tr></tbody></table>

{% hint style="info" %}
設定する `api_url` をデプロイ先に合わせます：

* `http://localhost:9001` ローカルの [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) サーバー用。
* あなたの [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用の URL。
  {% endhint %}

## Inference（セルフホスト型）で使用する

SmolVLM2 は、以下を使って直接読み込むこともできます: [`inference`](https://docs.roboflow.com/deployment/self-hosted/self-hosted) VQA、文書 OCR、文書 VQA、物体カウント用のパッケージ。

{% stepper %}
{% step %}

### パッケージをインストールする

```bash
pip install "inference[transformers]"
```

以下を使用してください： `inference-gpu[transformers]` GPU マシン上で。
{% endstep %}

{% step %}

### モデルを実行する

```python
from PIL import Image

from inference.models.smolvlm.smolvlm import SmolVLM

model = SmolVLM(api_key="YOUR_API_KEY")

image = Image.open("dog.jpeg")
result = model.predict(image, "この画像には犬が何匹いますか？")

print(result)
```

{% endstep %}
{% endstepper %}

### Workflows の実行モード

以下で使用する場合： [Workflow](https://docs.roboflow.com/workflows)、SmolVLM2 は 2 つのモードのいずれかで動作します:

* **ローカル実行**：モデルは Inference サーバー上で実行されます（GPU 推奨）。
* **リモート実行**：モデルは、以下を介してリモート Inference サーバー上で HTTP 経由で呼び出されます： `infer_lmm()` クライアントメソッド。
