LoginSignup
9
1

More than 1 year has passed since last update.

DALL-E2もGoogle Colabで動かしたい!

Posted at

AI画像生成のQiita記事Stable DiffusionばっかりでDALL-E 2の記事少なくない!?
ということで、海外のGithubを参考にDALL-E 2をローカルで動かします。

DALL-E 2とは

https://openai.com/dall-e-2/
ここ最近流行りのAI画像生成のサービスの1つで、主に文章から画像を生成するAIを利用することができます。
類似したサービスないしプログラムだと「Midjourney」、「Stable Diffusion」、「NovelAI Diffusion」などが有名ですね。
最近「Azure OpenAI Service」や「Bing」にも組み込まれたり、「Microsoft Designer」にも利用され始めており、またHOTになってきているサービスです。

やり方

  1. DALL・Eに登録していない人は登録して、メールにて登録されたことを確認してください。
    https://labs.openai.com/waitlist

  2. 上のGitHubに書かれている方法を利用してトークンを取得します。これで利用の準備は完了です。
    sess-XXXXXXXXXXXXXXXXXXXX

  3. pipを使用し、DALL-E 2をインストールします。
    pip install dalle2

  4. コードを書いていきます。DALL-E 2ライブラリを読み込みます(2.で取得したトークン使用)。
    from dalle2 import Dalle2
    dalle = Dalle2("sess-xxxxxxxxxxxxxxxxxxxxxxxxxxxx")

  5. 画像を生成するコードを書きます。
    generations = dalle.generate("プロンプト")

実行した結果、以下の結果を得ることができます。

[
    {
        'id': 'generation-b3LLf5D59JwRQJSetvAONLOw',
        'object': 'generation', 
        'created': 1664740126, 
        'generation_type': 'ImageGeneration', 
        'generation': 
        {
            'image_path': 'https://openailabsprodscus.blob.core.windows.net/private/user-xxxxxx/generations/generation-xxxx/image.webp'
        }, 
        'task_id': 'task-k6i8TV46DDa2gKPqFlyZuJ6N', 
        'prompt_id': 'prompt-DiiBTh2I1xHhjWS9wf5J79Ti', 
        'is_public': False
    }
]

ダウンロードしたいときは
generations = dalle.generate_and_download("プロンプト")

その時はcontent/xxx.webpという形でcontentフォルダにwebp形式で保存されます。

実践

今回、butteflies flying in the skyというプロンプトを利用しました。

generation-3bl8iYHBorrTEFxzfP8uYr29.png generation-3bl8iYHBorrTEFxzfP8uYr29.png

それらしきものがちゃんと出力できましたね!
APIからの起動でもちゃんとクレジットは減るのでご注意を
image.png

それではよきAIライフを。

9
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
9
1