Stability.ai Diffusion 1.0 文本到图像 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Stability.ai Diffusion 1.0 文本到图像

Stability.ai Diffusion 1.0 模型具有以下推理参数和模型响应,用于进行文本到图像的推理调用。

请求和响应

请求正文在请求body字段中传递给InvokeModelInvokeModelWithResponseStream

有关更多信息,请参阅 https://platform.stability.ai/docs/api-reference#tag/v1generation

Request

Stability.ai Diffusion 1.0 模型具有以下推理参数,用于进行文本到图像的推理调用。

{ "text_prompts": [ { "text": string, "weight": float } ], "height": int, "width": int, "cfg_scale": float, "clip_guidance_preset": string, "sampler": string, "samples", "seed": int, "steps": int, "style_preset": string, "extras" :JSON object }
  • text_prompts(必要)– 用于生成的文本提示数组。每个元素都是一个 JSON 对象,其中包含一个提示和该提示的权重。

    • text – 要传递给模型的提示。

      最低 最高

      0

      2000

    • weight(可选)– 模型应当应用于提示的权重。小于零的值表示否定提示。否定提示用于告知模型避免某些概念。weight 的默认值为 1。

  • cfg_scale –(可选)确定最终图像对提示的描绘程度。使用较小的数字可提高生成内容的随机性。

    最低 最高 默认

    0

    35

    7

  • clip_guidance_preset –(可选)枚举:FAST_BLUE, FAST_GREEN, NONE, SIMPLE SLOW, SLOWER, SLOWEST

  • height –(可选)要生成的图像的高度,以像素为单位,增量可被 64 整除。

    值必须为下列项之一:1024x1024, 1152x896, 1216x832, 1344x768, 1536x640, 640x1536, 768x1344, 832x1216, 896x1152

  • width –(可选)要生成的图像的宽度,以像素为单位,增量可被 64 整除。

    值必须为下列项之一:1024x1024, 1152x896, 1216x832, 1344x768, 1536x640, 640x1536, 768x1344, 832x1216, 896x1152

  • sampler –(可选)用于扩散过程的采样器。如果省略此值,模型会自动为您选择合适的采样器。

    枚举:DDIM, DDPM, K_DPMPP_2M, K_DPMPP_2S_ANCESTRAL, K_DPM_2, K_DPM_2_ANCESTRAL, K_EULER, K_EULER_ANCESTRAL, K_HEUN K_LMS

  • samples –(可选)要生成的图像数量。目前,Amazon Bedrock 支持生成一个图像。如果为 samples 提供一个值,该值必须为 1。

    默认 最低 最高

    1

    1

    1

  • seed –(可选)种子决定初始噪声设置。使用与上一次运行时相同的种子和相同的设置,以使推理可以创建相似的图像。如果不设置此值,或者此值为 0,它会被设置为一个随机数字。

    最低 最高 默认

    0

    4294967295

    0

  • steps –(可选)生成步骤决定对图像进行采样的次数。步骤越多,结果就越准确。

    最低 最高 默认

    10

    150

    30

  • style_preset(可选)– 用于将图像模型向特定样式引导的样式预设。此样式预设列表可能会发生变化。

    枚举:3d-model, analog-film, anime, cinematic, comic-book, digital-art, enhance, fantasy-art, isometric, line-art, low-poly, modeling-compound, neon-punk, origami, photographic, pixel-art, tile-texture

  • extras(可选)– 传递给引擎的额外参数。请谨慎使用。这些参数用于开发中或实验性的功能,可能会有变更,恕不另行通知。

Response

Stability.ai Diffusion 1.0 模型将返回以下字段,用于进行文本到图像的推理调用。

{ "result": string, "artifacts": [ { "seed": int, "base64": string, "finishReason": string } ] }
  • result – 操作的结果。如果成功,响应为 success

  • artifacts – 一个图像数组,每个请求的图像对应一个。

    • seed – 用于生成图像的种子值。

    • base64 – 模型生成的 base64 编码图像。

    • finishedReason – 图像生成过程的结果。有效值为:

      • SUCCESS – 图像生成过程成功。

      • ERROR – 出现错误。

      • CONTENT_FILTERED – 内容筛选器对图像进行了筛选,图像可能会变得模糊。

代码示例

以下示例介绍如何使用 Stability.ai Diffusion 1.0 模型和按需吞吐量运行推理。该示例将向模型提交一个文本提示,然后从模型中检索响应,最后显示图像。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an image with SDXL 1.0 (on demand). """ import base64 import io import json import logging import boto3 from PIL import Image from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by SDXL" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_image(model_id, body): """ Generate an image using SDXL 1.0 on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: image_bytes (bytes): The image generated by the model. """ logger.info("Generating image with SDXL model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = bedrock.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) print(response_body['result']) base64_image = response_body.get("artifacts")[0].get("base64") base64_bytes = base64_image.encode('ascii') image_bytes = base64.b64decode(base64_bytes) finish_reason = response_body.get("artifacts")[0].get("finishReason") if finish_reason == 'ERROR' or finish_reason == 'CONTENT_FILTERED': raise ImageError(f"Image generation error. Error code is {finish_reason}") logger.info("Successfully generated image withvthe SDXL 1.0 model %s", model_id) return image_bytes def main(): """ Entrypoint for SDXL example. """ logging.basicConfig(level = logging.INFO, format = "%(levelname)s: %(message)s") model_id='stability.stable-diffusion-xl-v1' prompt="""Sri lanka tea plantation.""" # Create request body. body=json.dumps({ "text_prompts": [ { "text": prompt } ], "cfg_scale": 10, "seed": 0, "steps": 50, "samples" : 1, "style_preset" : "photographic" }) try: image_bytes=generate_image(model_id = model_id, body = body) image = Image.open(io.BytesIO(image_bytes)) image.show() except ClientError as err: message=err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print(f"Finished generating text with SDXL model {model_id}.") if __name__ == "__main__": main()