Weitere AWS SDK-Beispiele sind im GitHub Repo AWS Doc SDK Examples
Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.
Rufen Sie Stability.ai Stable Image Core auf Amazon Bedrock auf, um ein Bild zu generieren
Die folgenden Codebeispiele zeigen, wie Stability.ai Stable Image Core auf Amazon Bedrock aufgerufen wird, um ein Bild zu generieren.
- .NET
-
- SDK für .NET
-
Anmerkung
Es gibt noch mehr dazu. GitHub Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Erstellen Sie ein Image mit Stable Image Core.
/// <summary> /// Asynchronously invokes the Stability.ai Stable Image Core model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that describes the image Stability.ai Stable Image Core 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 Image Core, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-diffusion-stable-image-core-text-image-request-response.html /// </remarks> public static async Task<string?> InvokeStableImageCoreAsync(string prompt, int seed) { string stableImageCoreModelId = "stability.stable-image-core-v1:1"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USWest2); string payload = new JsonObject() { { "prompt", prompt }, { "aspect_ratio", "1:1" }, { "seed", seed }, { "output_format", "png" } }.ToJsonString(); try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = stableImageCoreModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { var results = JsonNode.ParseAsync(response.Body).Result?["images"]?.AsArray(); return results?[0]?.GetValue<string>(); } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return null; }-
Einzelheiten zur API finden Sie InvokeModelin der AWS SDK für .NET API-Referenz.
-
- SDK für .NET (v4)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Erstellen Sie ein Image mit Stable Image Core.
/// <summary> /// Asynchronously invokes the Stability.ai Stable Image Core model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that describes the image Stability.ai Stable Image Core 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 Image Core, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-diffusion-stable-image-core-text-image-request-response.html /// </remarks> public static async Task<string?> InvokeStableImageCoreAsync(string prompt, int seed) { string stableImageCoreModelId = "stability.stable-image-core-v1:1"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USWest2); string payload = new JsonObject() { { "prompt", prompt }, { "aspect_ratio", "1:1" }, { "seed", seed }, { "output_format", "png" } }.ToJsonString(); try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = stableImageCoreModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { var results = JsonNode.ParseAsync(response.Body).Result?["images"]?.AsArray(); return results?[0]?.GetValue<string>(); } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return null; }-
Einzelheiten zur API finden Sie InvokeModelin der AWS SDK für .NET API-Referenz.
-
- Java
-
- SDK für Java 2.x
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Erstellen Sie ein Bild mit Stable Diffusion.
// Create an image with Stability AI Stable Image Core. import org.json.JSONObject; import org.json.JSONPointer; import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; import java.math.BigInteger; import java.security.SecureRandom; import static com.example.bedrockruntime.libs.ImageTools.displayImage; public class InvokeModel { public static String invokeModel() { // Create a Bedrock Runtime client in the AWS Region you want to use. // Replace the DefaultCredentialsProvider with your preferred credentials provider. var client = BedrockRuntimeClient.builder() .credentialsProvider(DefaultCredentialsProvider.create()) .region(Region.US_WEST_2) .build(); // Set the model ID, e.g., Stable Image Core. var modelId = "stability.stable-image-core-v1:1"; // The InvokeModel API uses the model's native payload. // Learn more about the available inference parameters and response fields at: // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-diffusion-stable-image-core-text-image-request-response.html var nativeRequestTemplate = """ { "prompt": "{{prompt}}", "aspect_ratio": "1:1", "seed": {{seed}}, "output_format": "png" }"""; // Define the prompt for the image generation. var prompt = "A stylized picture of a cute old steampunk robot"; // Get a random seed for the image generation (max. 4,294,967,294). var seed = new BigInteger(31, new SecureRandom()); // Embed the prompt and seed in the model's native request payload. String nativeRequest = nativeRequestTemplate .replace("{{prompt}}", prompt) .replace("{{seed}}", seed.toString()); try { // Encode and send the request to the Bedrock Runtime. var response = client.invokeModel(request -> request .body(SdkBytes.fromUtf8String(nativeRequest)) .modelId(modelId) ); // Decode the response body. var responseBody = new JSONObject(response.body().asUtf8String()); // Retrieve the generated image data from the model's response. var base64ImageData = new JSONPointer("/images/0") .queryFrom(responseBody) .toString(); return base64ImageData; } catch (SdkClientException e) { System.err.printf("ERROR: Can't invoke '%s'. Reason: %s", modelId, e.getMessage()); throw new RuntimeException(e); } } public static void main(String[] args) { System.out.println("Generating image. This may take a few seconds..."); String base64ImageData = invokeModel(); displayImage(base64ImageData); } }-
Einzelheiten zur API finden Sie InvokeModelin der AWS SDK for Java 2.x API-Referenz.
-
- PHP
-
- SDK für PHP
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Erstellen Sie ein Bild mit Stable Diffusion.
public function invokeStableDiffusion(string $prompt, int $seed = 0, string $aspect_ratio = '1:1') { // The different model providers have individual request and response formats. // For the format, ranges, and available parameters 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-image-core-v1:1'; $body = [ 'prompt' => $prompt, 'aspect_ratio' => $aspect_ratio, 'seed' => $seed, 'output_format' => 'png', ]; $result = $this->bedrockRuntimeClient->invokeModel([ 'contentType' => 'application/json', 'body' => json_encode($body), 'modelId' => $modelId, ]); $response_body = json_decode($result['body']); $base64_image_data = $response_body->images[0]; } catch (Exception $e) { echo "Error: ({$e->getCode()}) - {$e->getMessage()}\n"; } return $base64_image_data; }-
Einzelheiten zur API finden Sie InvokeModelin der AWS SDK für PHP API-Referenz.
-
- Python
-
- SDK für Python (Boto3)
-
Anmerkung
Es gibt noch mehr dazu GitHub. Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Erstellen Sie ein Bild mit Stable Diffusion.
# Use the native inference API to create an image with Stability AI Stable Image Core 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-west-2") # Set the model ID, e.g., Stable Image Core. model_id = "stability.stable-image-core-v1:1" # 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 = { "prompt": prompt, "aspect_ratio": "1:1", "seed": seed, "output_format": "png", } # 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["images"][0] # 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}")-
Einzelheiten zur API finden Sie InvokeModelin AWS SDK for Python (Boto3) API Reference.
-
- SAP ABAP
-
- SDK für SAP ABAP
-
Anmerkung
Es gibt noch mehr dazu. GitHub Hier finden Sie das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-
einrichten und ausführen. Erstellen Sie ein Bild mit Stable Diffusion.
"Stable Image Core Input Parameters should be in a format like this: * { * "prompt": "Draw a dolphin with a mustache, photorealistic", * "aspect_ratio": "1:1", * "seed": 0, * "output_format": "png" * } DATA: BEGIN OF ls_input, prompt TYPE /aws1/rt_shape_string, aspect_ratio TYPE /aws1/rt_shape_string, seed TYPE /aws1/rt_shape_integer, output_format TYPE /aws1/rt_shape_string, END OF ls_input. ls_input-prompt = iv_prompt. ls_input-aspect_ratio = '1:1'. ls_input-seed = 0. "or better, choose a random integer. ls_input-output_format = 'png'. 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-image-core-v1:1' iv_accept = 'application/json' iv_contenttype = 'application/json' ). "Stable Image Core Result Format: * { * "seeds": ["0"], * "finish_reasons": [null], * "images": ["iVBORw0KGgoAAAANSUhEUgAAAgAAA...."] * } DATA: BEGIN OF ls_response, images TYPE STANDARD TABLE OF /aws1/rt_shape_string, 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-images IS NOT INITIAL. DATA(lv_image) = cl_http_utility=>if_http_utility~decode_x_base64( ls_response-images[ 1 ] ). 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.Rufen Sie das Stability.ai Stable Image Core Foundation-Modell auf, um Bilder mit dem L2-High-Level-Client zu generieren.
TRY. DATA(lo_bdr_l2_sd) = /aws1/cl_bdr_l2_factory=>create_stable_diffusion_xl_1( 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.-
Einzelheiten zur API finden Sie InvokeModelin der API-Referenz zum AWS SDK für SAP ABAP.
-