Request Format#

Let define format of stable diffusuion HERE

type TextPrompt struct {
Text string `json:"text"`
Weight int `json:"weight"`
}
type StableDiffusionRequest struct {
TextPrompts []TextPrompt `json:"text_prompts"`
CfgScale int `json:"cfg_scale"`
Seed int `json:"seed"`
Samples int `json:"samples"`
Style string `json:"style_preset"`
NumInferenceStep int `json:"num_inference_steps"`
Height int `json:"height"`
Width int `json:"width"`
}

Respons Format#

Let define response format

type StableDiffusionImage struct {
Seed int `json:"seed"`
Base64 string `json:"base64"`
}
type StableDiffusionResponse struct {
Result string `json:"result"`
Artifacts []StableDiffusionImage `json:"artifacts"`
}

Invoke Endpoint#

Let use sagemakerruntime HERE to invoke the deployed endpoint. First, create client configuration

config, error := config.LoadDefaultConfig(
context.Background(),
config.WithRegion("us-east-1"),
)
if error != nil {
fmt.Println(error)
}

Then create a sagemaker runtime client

client := sagemakerruntime.NewFromConfig(config)

Create a request with a simple prompt

body, error := json.Marshal(StableDiffusionRequest{
TextPrompts: []TextPrompt{
{
Text: "tiger and wife",
Weight: 1,
}},
CfgScale: 15,
Seed: 3,
Samples: 1,
Style: "anime",
NumInferenceStep: 30,
Height: 1024,
Width: 1024,
})
if error != nil {
fmt.Println(error)
}

Next, invoke the sagemaker endpoint

f error != nil {
fmt.Println(error)
}
client := sagemakerruntime.NewFromConfig(config)
output, error := client.InvokeEndpoint(
context.TODO(),
&sagemakerruntime.InvokeEndpointInput{
EndpointName: aws.String("sdxl-1-0-jumpstart-2024-01-24-09-54-52-906"),
ContentType: aws.String("application/json"),
Body: body,
},
)
if error != nil {
fmt.Println(error)
}

Finally parse response, decode the base64 data and save image to file

if error != nil {
fmt.Println(error)
}
var response StableDiffusionResponse
if error := json.Unmarshal(output.Body, &response); error != nil {
fmt.Println(error)
}
imageData, _ := base64.StdEncoding.DecodeString(response.Artifacts[0].Base64)
file, error := os.Create("hehe.jpg")
if error != nil {
fmt.Print(error)
}
file.Write([]byte(imageData))
file.Close()

The full code here

main.go
package stablediffusion
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sagemakerruntime"
)
type TextPrompt struct {
Text string `json:"text"`
Weight int `json:"weight"`
}
type StableDiffusionRequest struct {
TextPrompts []TextPrompt `json:"text_prompts"`
CfgScale int `json:"cfg_scale"`
Seed int `json:"seed"`
Samples int `json:"samples"`
Style string `json:"style_preset"`
NumInferenceStep int `json:"num_inference_steps"`
Height int `json:"height"`
Width int `json:"width"`
}
type StableDiffusionImage struct {
Seed int `json:"seed"`
Base64 string `json:"base64"`
}
type StableDiffusionResponse struct {
Result string `json:"result"`
Artifacts []StableDiffusionImage `json:"artifacts"`
}
func CallStableDiffusion() {
config, error := config.LoadDefaultConfig(
context.Background(),
config.WithRegion("us-east-1"),
)
if error != nil {
fmt.Println(error)
}
body, error := json.Marshal(StableDiffusionRequest{
TextPrompts: []TextPrompt{
{
Text: "tiger and wife",
Weight: 1,
}},
CfgScale: 15,
Seed: 3,
Samples: 1,
Style: "anime",
NumInferenceStep: 30,
Height: 1024,
Width: 1024,
})
if error != nil {
fmt.Println(error)
}
client := sagemakerruntime.NewFromConfig(config)
output, error := client.InvokeEndpoint(
context.TODO(),
&sagemakerruntime.InvokeEndpointInput{
EndpointName: aws.String("sdxl-1-0-jumpstart-2024-01-24-09-54-52-906"),
ContentType: aws.String("application/json"),
Body: body,
},
)
if error != nil {
fmt.Println(error)
}
var response StableDiffusionResponse
if error := json.Unmarshal(output.Body, &response); error != nil {
fmt.Println(error)
}
imageData, _ := base64.StdEncoding.DecodeString(response.Artifacts[0].Base64)
file, error := os.Create("hehe.jpg")
if error != nil {
fmt.Print(error)
}
file.Write([]byte(imageData))
file.Close()
}

Reference#