【Stable Diffusion】呪文(プロンプト)リストの自動生成

【Stable Diffusion】呪文(プロンプト)リストの自動生成 プログラミング

「Stable Diffusionの呪文作成を自動化したい」
「MagicPromptをもっと効果的に活用したい」

このような場合には、この記事の内容が参考になります。
この記事では、Stable Diffusionのプロンプト一覧を自動で作成する方法を解説しています。

本記事の内容

  • プロンプト作成の効率化
  • 呪文リストの自動生成プログラム
  • 呪文リストで生成した画像

それでは、上記に沿って解説していきます。

プロンプト作成の効率化

プロンプトの作成は、時間のかかる作業です。
だからこそ、MagicPromptのような便利ツール(Webアプリ)が求められます。

上記の記事では、MagicPromptをローカル環境で動かす方法を解説しています。
HuggingFaceサイト上のデモ環境とは異なり、ローカル環境であれば使いたい放題です。

ただ、Webアプリは1度に4つのプロンプトしか表示されません。
それに、毎回コピーして利用するのは面倒です。

そこで、デモプログラムの改修を考えました。
結論から言うと、改修できました。

Webアプリではなく、単純なPythonスクリプトに変更しています。
画面にプロンプトを表示するのではなく、テキストファイルにプロンプトを書き込むようにしました。

つまり、呪文リストの自動生成ということです。
あと、最大4個から何個でも指定できるようにもしています。

100個の呪文が一気に欲しければ、それも可能です。
以下では、改修したプログラムについて説明します。

呪文リストの自動生成プログラム

呪文リストの自動生成プログラムについて説明します。

ここで説明するプログラムを動かすには、MagicPromptが動くことが前提です。
上記で載せた記事をもとに、ローカル環境で動くようにしておいてください。

では、コードから見ていきます。
以下が、呪文リストを自動生成するプログラムのコードになります。

import random
import re
from transformers import pipeline, set_seed

prompt = "dog"
count = 10

gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
with open("ideas.txt", "r") as f:
    line = f.readlines()


def generate(row_count, starting_text):
    seed = random.randint(100, 1000000)
    set_seed(seed)

    if starting_text == "":
        starting_text: str = line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize()
        starting_text: str = re.sub(r"[,:\-–.!;?_]", '', starting_text)

    response = gpt2_pipe(starting_text, max_length=random.randint(60, 90), num_return_sequences=row_count)
    response_list = []
    for x in response:
        resp = x['generated_text'].strip()
        if resp != starting_text and len(resp) > (len(starting_text) + 4) and resp.endswith(
                (":", "-", "—")) is False:
            response_list.append(resp)

    response_end = "\n".join(response_list)
    response_end = re.sub('[^ ]+\.[^ ]+', '', response_end)
    response_end = response_end.replace("<", "").replace(">", "")

    return response_end


res = generate(count, prompt)

file_path = "prompt.txt"
with open(file_path, mode="w", encoding="utf-8") as f:
    f.write(res)

ポイントは、次の部分です。

prompt = "dog" 
count = 10

これは、「dog」についてのプロンプトを「10」個作成することを指定しています。
ここの値を変更すれば、好きなワードで好きな数だけ呪文を生成できます。

ただし、呪文の数は指定した値になるとは限りません。
と言っても、1個少ない程度です。

では、コードを実行をしてみましょう。
処理が完了すると、「prompt.txt」がディレクトリ上に存在することを確認できます。

prompt.txt

dog and alex grey style, soft lighting, trending on artstation, 4 k, hyperrealistic, focused, extreme details, unreal engine 5, cinematic, masterpiece 
dog, by artgerm, digital art, trending on artstation 
dog, intricate, elegant, highly detailed, centered, digital painting, artstation, concept art, smooth, sharp focus, illustration, artgerm, tomasz alen kopera, peter mohrbacher, donato giancola, joseph christian leyendecker, wlop, 
dog, elegant, highly detailed, digital painting, artstation, concept art, matte, sharp focus, illustration 
dog and a tall attractive korean actress wearing elaborate red-sleeved top hat, open mouth, round face, large lips, intricate, elegant, highly detailed, digital painting, artstation, concept art, matte, sharp focus, illustration, art by Artgerm and Greg Rutkowski and Alphonse Mucha 
dog, cinematic lighting, highly detailed, digital painting, 8 k concept art, art by guweiz and z. w. gu, masterpiece, trending on artstation, 8 k 
dog, intricate, dystopian, sci-fi, extremely detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, intimidating lighting, incredible art by Artgerm and Vincent di Fate 
dog and a young woman with brown hair, at a hipster coffee shop, disco elysium, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, art by artgerm and greg rutkowski and alphonse mucha, frank frazetta 
dog portrait art by donato giancola and greg rutkowski, realistic face, digital art, trending on artstation, symmetry!!, skull helmet 
dog with a bowl of worms, by zdzislaw beksinski, by dariusz zawadzki, by wayne barlowe, gothic, surrealism, cosmic horror, lovecraftian, cold hue's, warm tone gradient background, concept art, beautiful composition

生成される呪文は、毎回その内容が異なります。
そこは、MagicPromptと同じです。

今回は、指定した数だけ呪文が生成できたようです。
運が悪いと、1個少ないことになります。

折角なので、最後にこの呪文リストで生成した画像を載せておきます。

呪文リストで生成した画像

呪文リストを用いて、Stable Diffusionで自動的に絵を作成しました。
「prompt.txt」をPythonで読み込んで、一気に10枚の画像を生成しています。

合計で3回、その処理を実行しています。

1回目

2回目

3回目

画像を呪文と比較してみると、答え合わせのようなことが可能です。
なお、実行毎にseedの値は変更しています。

「7」と「10」(「prompt.txt」の7行目と10行目)なんて、決して自力では生成できる呪文ではありません。
いや、せいぜい2行目のプロンプトを自力で生成できるぐらいでしょうか・・・

タイトルとURLをコピーしました