はじめに
AWS BedrockのStable Diffusion XLで画像生成をやっていきます。こちらの記事を参考にしています。
導入
1.IAMロールを作成
2.アクセスキーとシークレットキーをメモ
3.ポリシーを作成
1{2 "Version": "2012-10-17",3 "Statement": [4 {5 "Sid": "Statement1",6 "Effect": "Allow",7 "Action": [8 "bedrock:*"9 ],10 "Resource": "*"11 }12 ]13}4.AWS BedrockのModel access->EditからStable Diffusion XLにチェック
5.AWS CLIをインストール
6.コマンドプロンプトを開く
1C:\Users\admin>aws --version2aws-cli/2.13.30 Python/3.11.6 Windows/10 exe/AMD64 prompt/off7.aws configureでアクセスキーとシークレットキーを設定
1C:\Users\admin>aws configure2AWS Access Key ID [****************]: 3AWS Secret Access Key [****************]: 4Default region name [ap-northeast-1]:5Default output format [None]: json8.anaconda promptを開き、Python 3.11環境を作成
1(base) C:\Users\admin\Documents\PythonProjects>conda create -n py311 python=3.112(base) C:\Users\admin\Documents\PythonProjects>conda activate py3119.ライブラリのインストール
1(py311) C:\Users\admin\Documents\PythonProjects>pip install --no-build-isolation --force-reinstall boto3>=1.28.57 awscli>=1.29.57 botocore>=1.31.572(py311) C:\Users\admin\Documents\PythonProjects>pip install pillow==9.510.bedrockのツールをダウンロード
10.bedrockのツールをダウンロード
11.utilsをプロジェクトフォルダに置く
1# Python Built-Ins:2import base643import io4import json5import os6import sys78# External Dependencies:910import boto311from PIL import Image1213module_path = ".."14sys.path.append(os.path.abspath(module_path))15from utils import bedrock, print_ww1617# ---- ⚠️ Un-comment and edit the below lines as needed for your AWS setup ⚠️ ----1819os.environ["AWS_DEFAULT_REGION"] = "us-east-1" # E.g. "us-east-1"2021# os.environ["AWS_PROFILE"] = ""2223# os.environ["BEDROCK_ASSUME_ROLE"] = "arn:aws:iam::785208610362:user/s-fujimoto" # E.g. "arn:aws:iam::xxxxxxxxxxxx:role/xxxxxx" # E.g.2425boto3_bedrock = bedrock.get_bedrock_client(26 assumed_role=os.environ.get("BEDROCK_ASSUME_ROLE", None),27 region=os.environ.get("AWS_DEFAULT_REGION", None),28)2930print(3132 "1 if the image is generated using a prompt, 2 if the image is generated using an image and a prompt."33)34choice = int(input("Enter Choice :"))35print(choice)3637if choice == 1:38 print("please enter the Prompt"),39 prompt = str(input())40 print(41 "Please enter 3 negative Prompt eg:poorly rendered,poor background details,poorly drawn face"42 )43 negative_prompts = []44 n = 345 for i in range(0, n):4647# ele = [input(), str(input()),]4849 ele = str(input())50 negative_prompts.append(ele)51 print(negative_prompts)52 style_preset = "photographic"53 request = json.dumps(54 {55 "text_prompts": (56 [{"text": prompt, "weight": 1.0}]57 + [58 {"text": negprompt, "weight": -1.0}59 for negprompt in negative_prompts60 ]61 ),62 "cfg_scale": 5,63 "seed": 5450,64 "steps": 70,65 "style_preset": style_preset,66 }67 )68 modelId = "stability.stable-diffusion-xl"6970 response = boto3_bedrock.invoke_model(body=request, modelId=modelId)71 response_body = json.loads(response.get("body").read())7273 print(response_body["result"])74 base_64_img_str = response_body["artifacts"][0].get("base64")75 print(f"{base_64_img_str[0:80]}...")7677 os.makedirs("data", exist_ok=True)78 image_1 = Image.open(79 io.BytesIO(base64.decodebytes(bytes(base_64_img_str, "utf-8")))80 )81 image_1.save("data/image_1.png")82 image_18384if choice == 2:85 print("please enter the image file path"),86 image_path = str(input())87 print("please enter the Prompt"),88 change_prompt = str(input())89 print(90 "Please enter 3 negative Prompt eg:poorly rendered,poor background details,poorly drawn face"91 )92 negative_prompts = []93 n = 394 for i in range(0, n):9596# ele = [input(), str(input()),]9798 ele = str(input())99 negative_prompts.append(ele)100 print(negative_prompts)101 style_preset = "photographic"102103 def image_to_base64(img) -> str:104 """Convert a PIL Image or local image file path to a base64 string for Amazon Bedrock"""105 if isinstance(img, str):106 if os.path.isfile(img):107 print(f"Reading image from file: {img}")108 with open(img, "rb") as f:109 return base64.b64encode(f.read()).decode("utf-8")110 else:111 raise FileNotFoundError(f"File {img} does not exist")112 elif isinstance(img, Image.Image):113 print("Converting PIL Image to base64 string")114 buffer = io.BytesIO()115 img.save(buffer, format="PNG")116 return base64.b64encode(buffer.getvalue()).decode("utf-8")117 else:118 raise ValueError(f"Expected str (filename) or PIL Image. Got {type(img)}")119120 original_image = Image.open(image_path)121 image_1 = original_image.resize((512, 512))122 init_image_b64 = image_to_base64(image_1)123 print(init_image_b64[:80] + "...")124125 request = json.dumps(126 {127 "text_prompts": (128 [{"text": change_prompt, "weight": 1.0}]129 + [130 {"text": negprompt, "weight": -1.0}131 for negprompt in negative_prompts132 ]133 ),134 "cfg_scale": 10,135 "init_image": init_image_b64,136 "seed": 321,137 "start_schedule": 0.6,138 "steps": 50,139 "style_preset": style_preset,140 }141 )142 modelId = "stability.stable-diffusion-xl"143144 response = boto3_bedrock.invoke_model(body=request, modelId=modelId)145 response_body = json.loads(response.get("body").read())146147 print(response_body["result"])148 image_2_b64_str = response_body["artifacts"][0].get("base64")149 print(f"{image_2_b64_str[0:80]}...")150151 image_2 = Image.open(152 io.BytesIO(base64.decodebytes(bytes(image_2_b64_str, "utf-8")))153 )154 image_2.save("data/image.png")実行
オプション1は、プロンプトから画像生成します
1(py311) C:\Users\admin\Documents\PythonProjects\Bedrocks>python app.py2Create new client3 Using region: us-east-14boto3 Bedrock client successfully created!5bedrock-runtime(https://bedrock-runtime.us-east-1.amazonaws.com)61 if the image is generated using a prompt, 2 if the image is generated using an image and a prompt.7Enter Choice :1819please enter the Prompt10japanese mountain11Please enter 3 negative Prompt eg:poorly rendered,poor background details,poorly drawn face12poorly rendered13poor background details14poorly drawn face15['poorly rendered', 'poor background details', 'poorly drawn face']16success17iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAADWmVYSWZNTQAqAAAACAAGAQAABAAAAAEA...
オプションの2は、画像とプロンプトを入力して画像生成します
1(py311) C:\Users\admin\Documents\PythonProjects\Bedrocks>python app.py2Create new client3 Using region: us-east-14boto3 Bedrock client successfully created!5bedrock-runtime(https://bedrock-runtime.us-east-1.amazonaws.com)61 if the image is generated using a prompt, 2 if the image is generated using an image and a prompt.7Enter Choice :2829please enter the image file path10image.jpg11please enter the Prompt12cute girl13Please enter 3 negative Prompt eg:poorly rendered,poor background details,poorly drawn face14poorly rendered15poor background details16poorly drawn face17['poorly rendered', 'poor background details', 'poorly drawn face']18Converting PIL Image to base64 string19iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAABdmlDQ1BJQ0MgUHJvZmlsZQAAeJylkLFL...20success21iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAACj0GVYSWZNTQAqAAAACAAGAQAABAAAAAEA...
input image