本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在 Amazon Bedrock 上调用 Stability.ai Stable Diffusion XL 生成图像
以下代码示例展示了如何在 Amazon Bedrock 上调用 Stability.ai Stable Diffusion XL 来生成图像。
- .NET
-
- AWS SDK for .NET
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 异步调用 Stability.ai Stable Diffusion XL 基础模型来生成图像。
/// <summary> /// Asynchronously invokes the Stability.ai Stable Diffusion XLmodel to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that describes the image Stability.ai Stable Diffusion XL has to generate.</param> /// <returns>A base-64 encoded image generated by model</returns> /// <remarks> /// The different model providers have individual request and response formats. /// For the format, ranges, and default values for Stability.ai Stable Diffusion XL, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html /// </remarks> public static async Task<string?> InvokeStableDiffusionXLG1Async(string prompt, int seed, string? stylePreset = null) { string stableDiffusionXLModelId = "stability.stable-diffusion-xl"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); var jsonPayload = new JsonObject() { { "text_prompts", new JsonArray() { new JsonObject() { { "text", prompt } } } }, { "seed", seed } }; if (!string.IsNullOrEmpty(stylePreset)) { jsonPayload.Add("style_preset", stylePreset); } string payload = jsonPayload.ToString(); try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = stableDiffusionXLModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { var results = JsonNode.ParseAsync(response.Body).Result?["artifacts"]?.AsArray(); return results?[0]?["base64"]?.GetValue<string>(); } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return null; }
-
有关 API 的详细信息,请参阅 AWS SDK for .NET API 参考InvokeModel中的。
-
- Java
-
- 适用于 Java 2.x 的 SDK
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 异步调用 Stability.ai Stable Diffusion XL 基础模型来生成图像。
/** * Asynchronously invokes the Stability.ai Stable Diffusion XL model to create * an image based on the provided input. * * @param prompt The prompt that guides the Stable Diffusion model. * @param seed The random noise seed for image generation (use 0 or omit * for a random seed). * @param stylePreset The style preset to guide the image model towards a * specific style. * @return A Base64-encoded string representing the generated image. */ public static String invokeStableDiffusion(String prompt, long seed, String stylePreset) { /* * The different model providers have individual request and response formats. * For the format, ranges, and available style_presets of Stable Diffusion * models refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html */ String stableDiffusionModelId = "stability.stable-diffusion-xl-v1"; BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); JSONArray wrappedPrompt = new JSONArray().put(new JSONObject().put("text", prompt)); JSONObject payload = new JSONObject() .put("text_prompts", wrappedPrompt) .put("seed", seed); if (stylePreset != null && !stylePreset.isEmpty()) { payload.put("style_preset", stylePreset); } InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(stableDiffusionModelId) .contentType("application/json") .accept("application/json") .build(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); String base64ImageData = ""; try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); base64ImageData = responseBody .getJSONArray("artifacts") .getJSONObject(0) .getString("base64"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return base64ImageData; }
调用 Stability.ai Stable Diffusion XL 基础模型来生成图像。
/** * Invokes the Stability.ai Stable Diffusion XL model to create an image based * on the provided input. * * @param prompt The prompt that guides the Stable Diffusion model. * @param seed The random noise seed for image generation (use 0 or omit * for a random seed). * @param stylePreset The style preset to guide the image model towards a * specific style. * @return A Base64-encoded string representing the generated image. */ public static String invokeStableDiffusion(String prompt, long seed, String stylePreset) { /* * The different model providers have individual request and response formats. * For the format, ranges, and available style_presets of Stable Diffusion * models refer to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html */ String stableDiffusionModelId = "stability.stable-diffusion-xl"; BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); JSONArray wrappedPrompt = new JSONArray().put(new JSONObject().put("text", prompt)); JSONObject payload = new JSONObject() .put("text_prompts", wrappedPrompt) .put("seed", seed); if (!(stylePreset == null || stylePreset.isEmpty())) { payload.put("style_preset", stylePreset); } InvokeModelRequest request = InvokeModelRequest.builder() .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(stableDiffusionModelId) .contentType("application/json") .accept("application/json") .build(); InvokeModelResponse response = client.invokeModel(request); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); String base64ImageData = responseBody .getJSONArray("artifacts") .getJSONObject(0) .getString("base64"); return base64ImageData; }
-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考InvokeModel中的。
-
- PHP
-
- 适用于 PHP 的 SDK
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 调用 Stability.ai Stable Diffusion XL 基础模型来生成图像。
public function invokeStableDiffusion(string $prompt, int $seed, string $style_preset) { # The different model providers have individual request and response formats. # For the format, ranges, and available style_presets of Stable Diffusion models refer to: # https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html $base64_image_data = ""; try { $modelId = 'stability.stable-diffusion-xl'; $body = [ 'text_prompts' => [ ['text' => $prompt] ], 'seed' => $seed, 'cfg_scale' => 10, 'steps' => 30 ]; if ($style_preset) { $body['style_preset'] = $style_preset; } $result = $this->bedrockRuntimeClient->invokeModel([ 'contentType' => 'application/json', 'body' => json_encode($body), 'modelId' => $modelId, ]); $response_body = json_decode($result['body']); $base64_image_data = $response_body->artifacts[0]->base64; } catch (Exception $e) { echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n"; } return $base64_image_data; }
-
有关 API 的详细信息,请参阅 AWS SDK for PHP API 参考InvokeModel中的。
-
- Python
-
- SDK for Python (Boto3)
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 调用 Stability.ai Stable Diffusion XL 基础模型来生成图像。
# Use the native inference API to create an image with Stability.ai Stable Diffusion import base64 import boto3 import json import os import random # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Stable Diffusion XL 1. model_id = "stability.stable-diffusion-xl-v1" # Define the image generation prompt for the model. prompt = "A stylized picture of a cute old steampunk robot." # Generate a random seed. seed = random.randint(0, 4294967295) # Format the request payload using the model's native structure. native_request = { "text_prompts": [{"text": prompt}], "style_preset": "photographic", "seed": seed, "cfg_scale": 10, "steps": 30, } # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract the image data. base64_image_data = model_response["artifacts"][0]["base64"] # Save the generated image to a local folder. 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"stability_{i}.png")): i += 1 image_data = base64.b64decode(base64_image_data) image_path = os.path.join(output_dir, f"stability_{i}.png") with open(image_path, "wb") as file: file.write(image_data) print(f"The generated image has been saved to {image_path}")
-
有关 API 的详细信息,请参阅适用InvokeModel于 Python 的AWS SDK (Boto3) API 参考。
-
- SAP ABAP
-
- SDK for SAP ABAP
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 调用 Stability.ai Stable Diffusion XL 基础模型来生成图像。
"Stable Diffusion Input Parameters should be in a format like this: * { * "text_prompts": [ * {"text":"Draw a dolphin with a mustache"}, * {"text":"Make it photorealistic"} * ], * "cfg_scale":10, * "seed":0, * "steps":50 * } TYPES: BEGIN OF prompt_ts, text TYPE /aws1/rt_shape_string, END OF prompt_ts. DATA: BEGIN OF ls_input, text_prompts TYPE STANDARD TABLE OF prompt_ts, cfg_scale TYPE /aws1/rt_shape_integer, seed TYPE /aws1/rt_shape_integer, steps TYPE /aws1/rt_shape_integer, END OF ls_input. APPEND VALUE prompt_ts( text = iv_prompt ) TO ls_input-text_prompts. ls_input-cfg_scale = 10. ls_input-seed = 0. "or better, choose a random integer. ls_input-steps = 50. DATA(lv_json) = /ui2/cl_json=>serialize( data = ls_input pretty_name = /ui2/cl_json=>pretty_mode-low_case ). TRY. DATA(lo_response) = lo_bdr->invokemodel( iv_body = /aws1/cl_rt_util=>string_to_xstring( lv_json ) iv_modelid = 'stability.stable-diffusion-xl-v0' iv_accept = 'application/json' iv_contenttype = 'application/json' ). "Stable Diffusion Result Format: * { * "result": "success", * "artifacts": [ * { * "seed": 0, * "base64": "iVBORw0KGgoAAAANSUhEUgAAAgAAA.... * "finishReason": "SUCCESS" * } * ] * } TYPES: BEGIN OF artifact_ts, seed TYPE /aws1/rt_shape_integer, base64 TYPE /aws1/rt_shape_string, finishreason TYPE /aws1/rt_shape_string, END OF artifact_ts. DATA: BEGIN OF ls_response, result TYPE /aws1/rt_shape_string, artifacts TYPE STANDARD TABLE OF artifact_ts, END OF ls_response. /ui2/cl_json=>deserialize( EXPORTING jsonx = lo_response->get_body( ) pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = ls_response ). IF ls_response-artifacts IS NOT INITIAL. DATA(lv_image) = cl_http_utility=>if_http_utility~decode_x_base64( ls_response-artifacts[ 1 ]-base64 ). ENDIF. CATCH /aws1/cx_bdraccessdeniedex INTO DATA(lo_ex). WRITE / lo_ex->get_text( ). WRITE / |Don't forget to enable model access at https://console.aws.amazon.com/bedrock/home?#/modelaccess|. ENDTRY.
调用 Stability.ai Stable Diffusion XL 基础模型,使用 L2 高级客户端生成图像。
TRY. DATA(lo_bdr_l2_sd) = /aws1/cl_bdr_l2_factory=>create_stable_diffusion_10( lo_bdr ). " iv_prompt contains a prompt like 'Show me a picture of a unicorn reading an enterprise financial report'. DATA(lv_image) = lo_bdr_l2_sd->text_to_image( iv_prompt ). CATCH /aws1/cx_bdraccessdeniedex INTO DATA(lo_ex). WRITE / lo_ex->get_text( ). WRITE / |Don't forget to enable model access at https://console.aws.amazon.com/bedrock/home?#/modelaccess|. ENDTRY.
-
有关 API 的详细信息,请参阅适用InvokeModel于 S AP 的AWS SDK ABAP API 参考。
-
有关 S AWS DK 开发者指南和代码示例的完整列表,请参阅将此服务与 AWS SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。