本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Stability.ai Diffusion 1.0 影像至影像
Stability.ai Diffusion 1.0 模型具有下列推論參數和模型回應,用於影像到影像推論呼叫。
請求與回應
要求主體會在要求body
欄位中傳遞給InvokeModel或InvokeModelWithResponseStream。
如需詳細資訊,請參閱 https://platform.stability.ai/docs/api-reference#tag/v1generation/operation/ imageToImage
程式碼範例
下列範例展示如何使用 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 from a reference 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="""A space ship.""" # Read reference image from file and encode as base64 strings. with open("/path/to/image", "rb") as image_file: init_image = base64.b64encode(image_file.read()).decode('utf8') # Create request body. body=json.dumps({ "text_prompts": [ { "text": prompt } ], "init_image": init_image, "style_preset" : "isometric" }) 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()