モデルのデプロイ
MLOps Desktopには、モデルサービング用のHTTPサーバーが組み込まれています。学習済みモデルをデプロイし、REST API経由で予測を取得できます。追加のインフラストラクチャは不要です。
- 学習済みモデル(クイックスタートを参照)
- Pythonパッケージ:
pip install fastapi uvicorn slowapi - ONNX使用時(オプション):
pip install onnxruntime
Servingタブの使用
Section titled “Servingタブの使用”-
Servingタブを開く
出力パネルのServingタブをクリックします。
-
モデルを選択
以下から選択できます。
- Modelsタブに登録されたモデル
- 現在のセッションで学習したモデル
複数バージョンがある場合は特定のバージョンを選択します。
-
サーバーを設定
Configureボタン(歯車アイコン)をクリックします。
設定 デフォルト 説明 Host 0.0.0.0 リッスンアドレス Port 8000 HTTPポート Use ONNX Runtime Off 高速推論を有効化 -
サーバーを起動
Start Serverをクリックします。
ステータスが変化します:
Stopped → Starting → RunningサーバーURL:
http://localhost:8000 -
予測を実行
curl、Python、JavaScriptなど任意のHTTPクライアントを使用できます。
プレイグラウンドパネル
Section titled “プレイグラウンドパネル”Playgroundパネルでは、コードを書いたり外部ツールを使わずに、モデルをインタラクティブにテストできます。
- プレイグラウンドを開く — 出力パネルのPlaygroundタブをクリックします(Servingタブの隣)。
- 特徴量を入力 — モデルが必要とする各特徴量の入力フィールドに値を入力します。
- 予測を実行 — プレイグラウンドが実行中のサーバーにリクエストを送信し、結果をインラインで表示します。
- バッチテスト — CSVファイルをアップロードして、複数行の予測を一度に実行できます。結果はCSVとしてダウンロード可能です。
APIエンドポイント
Section titled “APIエンドポイント”サーバー起動後、以下のエンドポイントが利用可能です。
ヘルスチェック
Section titled “ヘルスチェック”curl http://localhost:8000/healthレスポンス:
{"status": "healthy", "model": "RandomForestClassifier"}curl -X POST http://localhost:8000/predict \ -H "Content-Type: application/json" \ -d '{"features": [[5.1, 3.5, 1.4, 0.2]]}'分類の場合:
{ "predictions": [0], "probabilities": [[0.98, 0.01, 0.01]]}回帰の場合:
{ "predictions": [24.5]}複数サンプルを一度に送信できます。
curl -X POST http://localhost:8000/predict \ -H "Content-Type: application/json" \ -d '{ "features": [ [5.1, 3.5, 1.4, 0.2], [6.2, 3.4, 5.4, 2.3], [4.9, 2.5, 4.5, 1.7] ] }'APIドキュメント
Section titled “APIドキュメント”FastAPIが自動生成するドキュメントにアクセスできます。
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
リクエストメトリクス
Section titled “リクエストメトリクス”Servingタブにリアルタイムメトリクスが表示されます。
| メトリクス | 説明 |
|---|---|
| Total Requests | サーバー起動からの総リクエスト数 |
| Success Rate | 成功レスポンス(2xx)の割合 |
| Avg Latency | 平均応答時間 |
| Requests/min | 現在のスループット |
最近のリクエストログも表示されます。
ONNX Runtime
Section titled “ONNX Runtime”ONNX Runtimeを使用すると、推論が高速化されます(ツリーベースモデルで2-10倍)。
pip install onnxruntimeを実行- ModelExporterノードでONNX形式にエクスポート
- Serving設定でUse ONNX Runtimeを有効化
.onnxモデルファイルを選択
Pythonからの利用
Section titled “Pythonからの利用”import requests
# 単一予測response = requests.post( "http://localhost:8000/predict", json={"features": [[5.1, 3.5, 1.4, 0.2]]})result = response.json()print(f"予測クラス: {result['predictions'][0]}")print(f"信頼度: {max(result['probabilities'][0]):.1%}")
# バッチ予測import pandas as pddf = pd.read_csv("new_data.csv")features = df.drop(columns=["target"]).values.tolist()
response = requests.post( "http://localhost:8000/predict", json={"features": features})predictions = response.json()["predictions"]JavaScriptからの利用
Section titled “JavaScriptからの利用”const response = await fetch('http://localhost:8000/predict', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ features: [[5.1, 3.5, 1.4, 0.2]] })});
const { predictions, probabilities } = await response.json();console.log(`予測: ${predictions[0]}`);本番環境へのエクスポート
Section titled “本番環境へのエクスポート”MLOps Desktop外での本番デプロイ用にモデルをエクスポートできます。
ModelExporterノードで以下の形式で保存できます。
.joblib— Pythonアプリケーション向け.onnx— クロスプラットフォーム、高速推論.pkl— Pythonネイティブ(セキュリティに注意)
独自のFastAPIサーバーを作成できます。
from fastapi import FastAPIimport joblibimport numpy as np
app = FastAPI()model = joblib.load("model.joblib")
@app.post("/predict")async def predict(data: dict): features = np.array(data["features"]) predictions = model.predict(features) return {"predictions": predictions.tolist()}起動:uvicorn server:app --host 0.0.0.0 --port 8000
必要なパッケージ
Section titled “必要なパッケージ”| パッケージ | 用途 | 必須 |
|---|---|---|
fastapi | Webフレームワーク | Yes |
uvicorn | ASGIサーバー | Yes |
slowapi | レート制限 | Yes |
onnxruntime | ONNX推論 | Optional |
一括インストール:
pip install fastapi uvicorn slowapi onnxruntimeServingタブは依存関係をチェックし、不足している場合は警告を表示します。
トラブルシューティング
Section titled “トラブルシューティング”「FastAPIがインストールされていません」
Section titled “「FastAPIがインストールされていません」”pip install fastapi uvicorn slowapiポートが使用中
Section titled “ポートが使用中”設定でポートを変更するか、既存のプロセスを停止します。
lsof -i :8000 # プロセスを確認kill -9 <PID> # 停止- ONNX Runtimeを有効化
- 単一リクエストではなくバッチ予測を使用
- モデルの複雑さを確認(大規模なRandom Forestは遅い)
ブラウザからのCORSエラー
Section titled “ブラウザからのCORSエラー”サーバーはデフォルトで全オリジンを許可しています。問題が続く場合はリクエストヘッダーを確認してください。
サーバーにはslowapiによる基本的なレート制限が含まれています(IP毎に100リクエスト/分)。共有環境での乱用を防止します。