背景先容
在2024年的亚马逊云科技re:Invent大会上提前预告发布的Stable Diffusion 3.5 Large,现在已经在Amazon Bedrock上线了!各位开辟者们现在可以使用该模型,根据文本提示词文生图生成高质量的图片,并且支持多种图片风格生成,助力媒体、游戏、广告和零售等行业的开辟者们加速概念艺术、视觉特效以及精修产品宣传图的生成创作。
2024年10月,图像生成模型厂商Stability AI发布了Stable Diffusion 3.5 Large,这是Stable Diffusion系列中现在最强盛的模型,拥有81亿个参数,值得一提的是该模型也是在Amazon SageMaker HyperPod上进行练习的,该模型可以生成极高的图像质量并具有精彩的提示词匹配本领。Stable Diffusion 3.5 Large可增效开辟者们在故事插画制作、概念艺术创作以及视觉特效应用的快速原型开辟。大家可以快速生成高质量的高达1兆像素图片,适用于媒体营销活动、交际媒体文章插图和广告,既可以节省时间和资源,又能以上场景中的加强图像创意效果。
在本系列的上篇中,我们先容了在亚马逊云科技控制台中,通过Stable Diffusion 3.5 Large模型生成图片的详细步骤使用,手把手和大家生成了一个赛博朋克风的图片。在本系列的下篇中,我们将通过API调用的方式进行图片生成,并展示Stable Diffusion 3.5 Large模型更多的图片生成场景。
如果调用Python SDK API生成图片?
下面小李哥会分享通过两种方式调用Stable Diffusion 3.5 Large生成图片。首先要先容的是通过aws cli下令行调用图片生成的api - invoke-model。我们接下来会在下令工具中通过一条下令获取生成的图像,并将输出的JSON格式相应以标准格式输出,并使用jq工具提取编码后的图像,最后将其解码并将结果写入img.png文件,直接打开即可获取输出的图像。
以下是 AWS CLI 下令的示例:
- $ aws bedrock-runtime invoke-model \
- --model-id stability.sd3-5-large-v1:0 \
- --body "{"prompt":"High-energy street scene in a neon-lit Tokyo alley at night, where steam rises from food carts, and colorful neon signs illuminate the rain-slicked pavement.","mode":"text-to-image","aspect_ratio":"1:1","output_format":"jpeg","seed":0}" \
- --cli-binary-format raw-in-base64-out \
- --region us-west-2 \
- /dev/stdout | jq -r '.images[0]' | base64 --decode > img.jpg
复制代码 如果大家希望在生成式AI应用中集成Stable Diffusion 3.5 Large模型,可以使用亚马逊云科技的SDK for Python(Boto3) 。接下来我将分享调用模型生成图片的代码段,使用的模型是Stable Image Ultra 1.1(该模型的底层架构即为Stable Diffusion 3.5 Large)。下面这个代码段会在请求中添加文本到图像的提示词,并调用Amazon Bedrock生成图像,使用的模型ID为“stability.stable-image-ultra-v1:1”。
- import base64
- import boto3
- import json
- import os
- MODEL_ID = "stability.stable-image-ultra-v1:1"
- bedrock_runtime = boto3.client("bedrock-runtime", region_name="us-west-2")
- print("Enter a prompt for the text-to-image model:")
- prompt = input()
- body = {
- "prompt": prompt,
- "mode": "text-to-image"
- }
- response = bedrock_runtime.invoke_model(modelId=MODEL_ID, body=json.dumps(body))
- model_response = json.loads(response["body"].read())
- base64_image_data = model_response["images"][0]
- i, output_dir = 1, "output"
- if not os.path.exists(output_dir):
- os.makedirs(output_dir)
- while os.path.exists(os.path.join(output_dir, f"img_{i}.png")):
- i += 1
- image_data = base64.b64decode(base64_image_data)
- image_path = os.path.join(output_dir, f"img_{i}.png")
- with open(image_path, "wb") as file:
- file.write(image_data)
- print(f"The generated image has been saved to {image_path}")
复制代码 该代码段生成的图像会被存储在本地文件体系的目录中,如果该目录不存在,程序会自动创建一个文件夹目录。为了制止覆盖已有的文件,代码会查抄目录中是否存在同名文件,并自动将新生成的图片命名为img_<number>.png 格式,确保文件名称的唯一性。
想要了解更多关于调用Bedrock上的模型生成内容的细节,大家可以访问Amazon Bedrock Invoke API代码示例页面,学习如何使用差别的编程语言来构建差别种类的生成式AI应用。
其他风格/场景图片生成展示
以下是使用 Stable Diffusion 3.5 Large 生成的一些其他场景的示例图片:
提示词1:生成学生正使用Amazon Bedrock的图片
Prompt: Full-body university students working on a tech project with the words Stable Diffusion 3.5 in Amazon Bedrock, cheerful cursive typography font in the foreground.
提示词2: 生成三种差别颜色的药剂
Prompt: Photo of three potions: the first potion is blue with the label "MANA", the second potion is red with the label "HEALTH", the third potion is green with the label " OISON". Old apothecary.
提示词3:生成玫瑰花摄影
Prompt: Photography, pink rose flowers in the twilight, glowing, tile houses in the background.
提示词4: 生成旅途冒险的3D卡通画面
Prompt: 3D animation scene of an adventurer traveling the world with his pet dog.
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |