0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Stable Diffusion 3 APIの使い方

Last updated at Posted at 2024-06-20

Stable Diffusion 3 APIの使い方

※2024/7月時点でのStable Diffusion 3 APIの利用方法になります。画像生成AIは進化の早い業界なので、最新のキャッチアップは公式サイトで確認ください。

Stability AIのアカウントにサインインし、APIキーを取得 

まずは、Stability AIのアカウントにアクセスしてサインアップ)します。

スクリーンショット 2024-06-19 21.53.34.png

  1. API keysをクリック
  2. API keyを取得する

価格について

サービス名 説明 価格 (クレジット)
Ultra 最新のモデル(Stable Diffusion 3 など)を活用した最も強力で柔軟なワークフロー 8
Core 高品質な画像を迅速に生成するための高度なワークフロー 3
Stable Diffusion 3 Medium 20億パラメータの Stable Diffusion 3 のバリアント、最新のベースモデル 3.5
Stable Diffusion 3 Large 80億パラメータの Stable Diffusion 3 のバリアント、最新のベースモデル 6.5
Stable Diffusion 3 Large Turbo Stable Diffusion 3 Large のターボバリアント 4
SDXL 1.0 レガシーベースモデル - クラシックでシンプルな画像生成 0.2-0.6
SD 1.6 レガシー柔軟解像度ベースモデル 0.2-1.0

クレジットについて

アカウントを新規作成した場合は25クレジットが無料付与されます。

$10ドル支払えば、1,000クレジットが購入できます。

スクリーンショット 2024-06-19 21.59.40.png

APIで画像作成する

基本的には公式リファレンス通りに進めるだけです。

必要な環境の準備

Node.jsのインストール

まだインストールしていない場合は、Node.jsの公式サイトからNode.jsをダウンロードしてインストールします。

依存パッケージのインストール

axiosとform-dataパッケージをプロジェクトにインストールします。

npm install axios form-data

プログラム実行

import fs from "node:fs";
import axios from "axios";
import FormData from "form-data";

const payload = {
  prompt: "Lighthouse on a cliff overlooking the ocean",
  output_format: "jpeg"
};

const response = await axios.postForm(
  `https://api.stability.ai/v2beta/stable-image/generate/sd3`,
  axios.toFormData(payload, new FormData()),
  {
    validateStatus: undefined,
    responseType: "arraybuffer",
    headers: { 
      Authorization: `Bearer 取得したAPIキー`, 
      Accept: "image/*" 
    },
  },
);

if(response.status === 200) {
  fs.writeFileSync("./lighthouse.jpeg", Buffer.from(response.data));
} else {
  throw new Error(`${response.status}: ${response.data.toString()}`);
}

上記のコードを実行して、画像が作成されればOK

実際に作成された画像
lighthouse.jpeg

実行した時に下記のエラーになる場合

➜  Desktop node stablediffusion.js
(node:94696) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/pc-m12/Desktop/stablediffusion.js:1
import fs from "node:fs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1124:15)
    at Module._compile (node:internal/modules/cjs/loader:1160:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47

package.jsonファイルに"type": "module"を追加すれば解決します。

{
  "type": "module",
  "dependencies": {
    "axios": "^1.7.2",
    "form-data": "^4.0.0"
  }
}

まとめ

思ったより簡単に利用できた。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?