You can use AI Portrait SDK for Go provided by Platform for AI (PAI) to call the algorithm service operations of the AI Portrait service to train models and create portraits. You can also use the SDK to configure a custom Low-Rank Adaptation (LoRA) model and create portraits based on a template image. This topic describes the required preparations before you can use the AI Portrait SDK for Go to call the related operations. This topic also provides examples on how to call the operations.
Prerequisites
A Go environment is prepared.
For model training and portrait creation, 5 to 20 training images and 1 template image are prepared. The following image formats are supported: .jpg, .jpeg, and .png. The image size must be greater than 512 x 512 pixels.
For a solo portrait, you only need a template image that contains the face of a person. The faces in multiple training images belong to the same person.
For a multi-person portrait, the template image must contain multiple faces, and the number of the faces must be the same as the value of the model_id parameter used for model training.
Preparations
Environment dependency: Go 1.18 or later.
Install SDK for Go:
Run the
go mod init project namecommand to create a project.Run the
go get github.com/aliyun/aliyun-pai-aiservice-go-sdkcommand to obtain the code of the SDK.Run the
import "github.com/aliyun/aliyun-pai-aiservice-go-sdk"command to introduce the SDK in the code.
Initialize the client:
Run the following shell command to write the HOST, AppId, and Token parameters to environment variables.
export HOST="http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com" export AppId=xxx export Token=xxxxxxxxxParameter
Description
HOST
The server address. Example:
http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.AppId
The application ID. After you activate the AI Portrait service, you can view the application ID on the AI Portrait page.
Token
The token. After you activate the AI Portrait service, you can view the token on the AI Portrait page.
Run the following code to initialize the client.
host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token)
Sample code
AI Portrait is a resource-intensive service, involving the model training and portrait creation processes. In most cases, model training requires several minutes, whereas portrait creation requires only dozens of seconds. The following figure shows the workflow of API calls.
The following section describes the sample code for operation requests, responses, and end-to-end requests.
Check request (client.AIGCApi.AigcImageCheck)
Sample request
package main import ( "fmt" "log" "os" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) // Check images. images := []string{"https://train/0.jpg","https://train/1.jpg","https://train/2.jpg","https://train/3.jpg"} response, err := client.AIGCApi.AigcImageCheck(images, "", nil) fmt.Println(response) if err != nil { log.Fatal(err) return } if response.Code != "OK" { log.Fatal("response error:%v", response) return } // Traverse the image check results. for _, result := range response.Data.CheckResults { fmt.Printf("code=%d\tfrontal=%v\turl=%s\n", result.Code, result.Frontal, result.Url) } }The following table describes the parameters in the preceding code.
Parameter
Description
Type
images
The URLs of the images that you want to check. Separate multiple URLs with commas (,).
[]string
Sample response
An AIGCImageCheckResponse object is returned. The following table describes the fields of the object.
Field
Description
Type
RequestId
The request ID.
string
Code
The status code of the request. Valid values: OK and error. OK indicates that the request is completed and error indicates that the request is not completed.
string
Message
The details of the request status. If the request is successful, success is returned. In other scenarios, a different message is returned.
string
Data
The details of the returned data.
AIGCImageCheckData
Model training initiation (client.AIGCApi.AigcImagesTrain)
Stable Diffusion 1.5 training
package main import ( "fmt" "log" "os" "time" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) images := []string{"https://train/0.jpg","https://train/1.jpg","https://train/2.jpg","https://train/3.jpg"} response, err := client.AIGCApi.AigcImagesTrain(images, "", nil) if err != nil { log.Fatal(err) } // The task ID. jobId := response.Data.JobId modelId := response.Data.ModelId fmt.Println(jobId) jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Message) if err != nil { log.Fatal(err) } for { jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Result) if err != nil { log.Fatal("get job fail", err) } if jobResponse.Data.Job.State == sdk.JOB_STATE_WAIT { fmt.Println("job running") } else if jobResponse.Data.Job.State == sdk.JOB_STATE_SUCCESS { // job success fmt.Println(jobResponse) fmt.Println("job success") break } else if jobResponse.Data.Job.State == sdk.JOB_STATE_FAILED { log.Fatal("job fail", err) return } time.Sleep(30 * time.Second) } fmt.Println(modelId) }Parameter
Description
Type
images
The URLs of the training images.
[]string
Stable Diffusion XL (SDXL) training
If you want to use SDXL, contact your account manager to activate the service and then use the service by specifying a model in the modelName parameter.
You can use SDXL for portrait training or scene LoRA training by specifying the parameters in configure.
package main import ( "fmt" "log" "os" "time" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) images := []string{"https://train/0.jpg","https://train/1.jpg","https://train/2.jpg","https://train/3.jpg"} modelName := "train_xl" configure := make(map[string]interface{}, 10) configure["train_scene_lora_bool"] = false configure["scene_lora_prompts"] = []string{"a photography of a woman with long blonde hair and a white dress", "a photography of a woman in a pink dress posing for a picture", "a photography of a woman in a black dress with a white background", "a photography of a woman with a frilly collar and suspenders", "a photography of a woman with a white dress and a white headpiece"} response, err := client.AIGCApi.AigcImagesTrain(images, modelName, configure) if err != nil { log.Fatal(err) } // The task ID. jobId := response.Data.JobId modelId := response.Data.ModelId fmt.Println(jobId) jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Message) if err != nil { log.Fatal(err) } for { jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Result) if err != nil { log.Fatal("get job fail", err) } if jobResponse.Data.Job.State == sdk.JOB_STATE_WAIT { fmt.Println("job running") } else if jobResponse.Data.Job.State == sdk.JOB_STATE_SUCCESS { // job success fmt.Println(jobResponse) fmt.Println("job success") break } else if jobResponse.Data.Job.State == sdk.JOB_STATE_FAILED { log.Fatal("job fail", err) return } time.Sleep(30 * time.Second) } fmt.Println(modelId) }Parameter
Description
Type
images
The URLs of the training images.
[]string
modelName
The name of the model. If you want to use SDXL, set the value to train_xl.
string
configure
The parameter configuration. Default value: nil.
map[string]interface{}
Parameter in the configure field
Description
Type
Required
Default value
Valid value
train_scene_lora_bool
Specifies whether to perform LoRA training on the scene.
bool
No
False
True and False
scene_lora_prompts
The prompts of LoRA training on the scene.
If you perform LoRA training on the scene, this parameter is required. The length of the list must be the same as the length of the image URL.
[]string
No
[]
N/A
Response description
An AIGCImageTrainResponse object is returned. The following table describes the fields of the object.
Field
Description
Type
RequestId
The ID of the request.
string
Code
The status code of the request. Valid values: OK and error. OK indicates that the request is completed and error indicates that the request is not completed.
string
Message
The details of the request status. If the request is successful, success is returned. In other scenarios, a different message is returned.
string
Data
The details of the returned data.
AIGCImageTrainData
Training result query (client.JobApi.GetAsyncJobWithId)
Sample request
package main import ( "fmt" "log" "os" "time" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) // The task ID. var jobId int32 = xxxxxx for { jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) if err != nil { log.Fatal("get job fail", err) } if jobResponse.Data.Job.State == sdk.JOB_STATE_WAIT { fmt.Println("job running") } else if jobResponse.Data.Job.State == sdk.JOB_STATE_SUCCESS { // job success fmt.Println(jobResponse) fmt.Println("job success") break } else if jobResponse.Data.Job.State == sdk.JOB_STATE_FAILED { log.Fatal("job fail", err) return } time.Sleep(30 * time.Second) } }The following table describes the parameters in the preceding code.
Parameter
Description
Type
Required
jobId
The task ID returned in the request response.
int32
Yes
Response description
An AsyncJobResponse object is returned. The following table describes the fields in the object.
Field
Description
Type
RequestId
The ID of the request.
string
Code
The status code of the request. Valid values: OK and error. OK indicates that the request is completed and error indicates that the request is not completed.
string
Message
The details of the request status. If the request is successful, success is returned. In other scenarios, a different message is returned.
string
Data
The details of the returned data.
AsyncJobData
The following table describes the data field whose type is AsyncJobData.
Parameter
Description
Type
State
The check result of each image.
int32
AppId
The application ID of the user.
string
Message
The details of the request status. If the request is successful, success is returned. In other scenarios, a different message is returned.
string
Result
The response of the model.
string
Portrait creation
Solo portrait creation (client.AIGCApi.AigcImagesCreate)
Stable Diffusion 1.5 prediction
package main import ( "encoding/base64" "log" "os" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) modelID := "xxxxxxxxxxxxxxx" template_image := "https://template_case.png" config := make(map[string]interface{}, 10) config["lora_weights"] = 0.9 config["first_denoising_strength"] = 1 config["second_denoising_strength"] = 1 config["more_like_me"] = 1 config["crop_face_preprocess"] = false config["apply_face_fusion_before"] = false config["apply_face_fusion_after"] = false config["color_shift_middle"] = false config["color_shift_last"] = false config["background_restore"] = true response, err := client.AIGCApi.AigcImagesCreate(modelID, image, "", config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(response.Data.Image) f, err_2 := os.Create("test.jpg") f.Write(image_decode) if err_2 != nil { log.Fatal(err) } }Parameter
Description
Type
Required
modelID
The ID of the LoRA model. Enter the value of model_id obtained from training.
string
Yes
template_image
The URL path of the template.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
SDXL prediction
If you want to use SDXL, contact your account manager to activate the service and then use the service by specifying a model in the modelName parameter.
package main import ( "encoding/base64" "log" "os" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) modelID := "xxxxxxxxxxxx" template_image := "https://template_case.png" modelName := "create_xl" config := make(map[string]interface{}, 10) // config["lora_weights"] = 0.9 // config["first_denoising_strength"] = 1 // config["second_denoising_strength"] = 1 // config["more_like_me"] = 1 // config["crop_face_preprocess"] = false // config["apply_face_fusion_before"] = false // config["apply_face_fusion_after"] = false // config["color_shift_middle"] = false // config["color_shift_last"] = false // config["background_restore"] = true response, err := client.AIGCApi.AigcImagesCreate(modelID, image, modelName, config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(response.Data.Image) f, err_2 := os.Create("test.jpg") f.Write(image_decode) if err_2 != nil { log.Fatal(err) } }Parameter
Type
Description
modelID
string
The ID of the LoRA model. Enter the value of model_id obtained from training. Set the value to "" if you set the ipa_control_only parameter to True.
template_image
string
The URL path of the template image.
If you use scene_lora or prompt to generate an image, set the parameter to t2i_generate.
modelName
string
The name of the model. If SDXL is used, set the value to create_xl.
config
map[string]interface{}
The returned configuration of the model. Default value: None.
Multi-person portrait creation (client.AIGCApi.AigcImagesCreateByMultiModelIds)
package main import ( "encoding/base64" "log" "os" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { // Obtain the HOST, AppId, and Token parameters from environment variables. host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) modelIDs := []string{"xxxxxx", "xxxxxx"} template_image := "https://template_case.png" config := make(map[string]interface{}, 10) config["lora_weights"] = 0.9 config["first_denoising_strength"] = 1 config["second_denoising_strength"] = 1 config["more_like_me"] = 1 config["crop_face_preprocess"] = false config["apply_face_fusion_before"] = false config["apply_face_fusion_after"] = false config["color_shift_middle"] = false config["color_shift_last"] = false config["background_restore"] = true response, err := client.AIGCApi.AigcImagesCreateByMultiModelIds(modelIDs, image, "", config) image_decode, _ := base64.StdEncoding.DecodeString(response.Data.Image) f, err := os.Create("test.jpg") f.Write(image_decode) if err != nil { log.Fatal(err) } }Parameter
Description
Type
Required
modelIds
The IDs of the LoRA models that are used to create the portrait.
[]string
Yes
template_image
The URL path of the template.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
Response description
An AIGCImageCreateResponse object is returned. The following table describes the fields in the object.
Field
Description
Type
RequestId
The ID of the request.
string
Code
The status code of the request. Valid values: OK and error. OK indicates that the request is completed and error indicates that the request is not completed.
string
Message
The details of the request status. If the request is successful, success is returned. In other scenarios, a different message is returned.
string
Data
The details of the returned data.
AIGCImageCreateData
The following table describes the data fields whose type is AIGCImageCreateData.
Parameter
Description
Type
CostTime
The time required for portrait creation.
Float
Image
The Base64-encoded image.
String
Description of related error codes
Error codes for service request:
HTTP status code
code
message
Description
400
PARAMETER_ERROR
not found appid
The application ID is invalid.
400
EXCEEDED_QUOTA_ERROR
exceeded quota
The quota for service calls of the account is used up.
401
PARAMETER_ERROR
sign error
The token is invalid.
404
PARAMETER_ERROR
model not found
The requested model service is not deployed.
Error codes for result queries:
HTTP status code
code
message
Description
462
error
Invalid input data. Please check the input dict.
An error occurred while parsing the input data.
462
error
Image not provided. Please check the template_image.
The template image that is used for portrait creation is not provided.
462
error
Prompts get error. Please check the model_id.
The format of the model ID is invalid.
462
error
Roop image decode error. Please check the user's lora is trained or not.
The Roop image does not exist. Check whether the model is trained.
462
error
Template image decode error. Please Give a new template.
An error occurred while decoding the template image. Provide a new template image.
462
error
There is not face in template. Please Give a new template.
No face exists in the template image. Provide a new template image.
462
error
Template image process error. Please Give a new template.
An error occurred while preprocessing the template image. Provide a new template image.
469
error
First Face Fusion Error, Can't get face in template image.
An error occurred during the first portrait merging.
469
error
First Stable Diffusion Process error. Check the webui status.
An error occurred during the first image generation by using Stable Diffusion.
469
error
Second Face Fusion Error, Can't get face in template image.
An error occurred during the second portrait merging.
469
error
Second Stable Diffusion Process error. Check the webui status.
An error occurred during the second image generation by using Stable Diffusion.
469
error
Please confirm if the number of faces in the template corresponds to the user ID.
The number of user IDs that you provided does not match the number of faces.
469
error
Third Stable Diffusion Process error. Check the webui status.
An error occurred while preprocessing the background. Provide a new template image.
500
error
Face id image decode error. Please check the user's lora is trained or not.
An error occurred while decoding the training image that you uploaded. Check whether the model is trained.
Sample code of end-to-end portrait creation
Stable Diffusion 1.5
package main import ( "encoding/base64" "fmt" "log" "os" "time" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) images := []string{"https://train/0.jpg","https://train/1.jpg","https://train/2.jpg","https://train/3.jpg"} trainresponse, err := client.AIGCApi.AigcImagesTrain(images, "", nil) if err != nil { log.Fatal(err) } jobId := trainresponse.Data.JobId modelId := trainresponse.Data.ModelId fmt.Println(jobId) jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Message) if err != nil { log.Fatal(err) } for { jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Result) if err != nil { log.Fatal("get job fail", err) } if jobResponse.Data.Job.State == sdk.JOB_STATE_WAIT { fmt.Println("job running") } else if jobResponse.Data.Job.State == sdk.JOB_STATE_SUCCESS { // job success fmt.Println(jobResponse) fmt.Println("job success") break } else if jobResponse.Data.Job.State == sdk.JOB_STATE_FAILED { log.Fatal("job fail", err) return } time.Sleep(30 * time.Second) } fmt.Println(modelId) template_image := "https://template_case.png" config := make(map[string]interface{}, 10) config["lora_weights"] = 0.9 config["first_denoising_strength"] = 1 config["second_denoising_strength"] = 1 config["more_like_me"] = 1 config["crop_face_preprocess"] = false config["apply_face_fusion_before"] = false config["apply_face_fusion_after"] = false config["color_shift_middle"] = false config["color_shift_last"] = false config["background_restore"] = true createresponse, err := client.AIGCApi.AigcImagesCreate(modelId, template_image, "", config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(createresponse.Data.Image) f, err := os.Create("test.jpg") f.Write(image_decode) if err != nil { log.Fatal(err) } }Parameter
Description
Type
Required
images
The URLs of the training images.
[]string
Yes
template_image
The URL path of the template.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
SDXL
package main import ( "encoding/base64" "fmt" "log" "os" "time" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) images := []string{ "https://xxx/0.jpg", "https://xxx/1.jpg", "https://xxx/2.jpg"} trainresponse, err := client.AIGCApi.AigcImagesTrain(images, "train_xl", nil) if err != nil { log.Fatal(err) } jobId := trainresponse.Data.JobId modelId := trainresponse.Data.ModelId fmt.Println(jobId) jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Message) if err != nil { log.Fatal(err) } for { jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Result) if err != nil { log.Fatal("get job fail", err) } if jobResponse.Data.Job.State == sdk.JOB_STATE_WAIT { fmt.Println("job running") } else if jobResponse.Data.Job.State == sdk.JOB_STATE_SUCCESS { // job success fmt.Println(jobResponse) fmt.Println("job success") break } else if jobResponse.Data.Job.State == sdk.JOB_STATE_FAILED { log.Fatal("job fail", err) return } time.Sleep(30 * time.Second) } fmt.Println(modelId) template_image = "https://demo.jpg" config := make(map[string]interface{}, 10) createresponse, err := client.AIGCApi.AigcImagesCreate(modelId, template_image, "create_xl", config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(createresponse.Data.Image) f, err := os.Create("test.jpg") f.Write(image_decode) if err != nil { log.Fatal(err) } }Parameter
Description
Type
Required
images
The URLs of the training images.
[]string
Yes
template_image
The URL path of the template.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
AI portrait creation based on a single reference image (without the need for model training)
package main import ( "encoding/base64" "log" "os" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) template_image = "https://demo.jpg" ref_image = "https://reference.jpg" config := make(map[string]interface{}, 10) config["ipa_control_only"] = true config["ipa_weight"] = 0.6 config["ipa_image_path"] = ref_image createresponse, err := client.AIGCApi.AigcImagesCreate("", template_image, "", config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(createresponse.Data.Image) f, err := os.Create("test.jpg") f.Write(image_decode) if err != nil { log.Fatal(err) } }Parameter
Description
Type
Required
template_image
The URL path of the template.
string
Yes
ref_image
The URL path of the reference image.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
Template creation link based on a prompt (without the need for a template image)
package main import ( "encoding/base64" "fmt" "log" "os" "time" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) images := []string{"https://xxx/0.jpg", "https://xxx/1.jpg", "https://xxx/2.jpg"} trainresponse, err := client.AIGCApi.AigcImagesTrain(images, "", nil) if err != nil { log.Fatal(err) } jobId := trainresponse.Data.JobId modelId := trainresponse.Data.ModelId fmt.Println(jobId) jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Message) if err != nil { log.Fatal(err) } for { jobResponse, err := client.JobApi.GetAsyncJobWithId(jobId) fmt.Println(jobResponse.Data.Job.Result) if err != nil { log.Fatal("get job fail", err) } if jobResponse.Data.Job.State == sdk.JOB_STATE_WAIT { fmt.Println("job running") } else if jobResponse.Data.Job.State == sdk.JOB_STATE_SUCCESS { // job success fmt.Println(jobResponse) fmt.Println("job success") break } else if jobResponse.Data.Job.State == sdk.JOB_STATE_FAILED { log.Fatal("job fail", err) return } time.Sleep(30 * time.Second) } fmt.Println(modelId) template_image = "https://demo.jpg" t2i_prompt := "(portrait:1.5), 1girl, bokeh, bouquet, brown_hair, cloud, flower, hairband, hydrangea, lips, long_hair, outdoors, sunlight, white_flower, white_rose, green sweater, sweater, (cloth:1.0), (best quality), (realistic, photo-realistic:1.3), film photography, minor acne, (portrait:1.1), (indirect lighting), extremely detailed CG unity 8k wallpaper, huge filesize, best quality, realistic, photo-realistic, ultra high res, raw photo, put on makeup" config := make(map[string]interface{}, 10) config["t2i_prompt"] = t2i_prompt createresponse, err := client.AIGCApi.AigcImagesCreate(modelId, template_image, "", config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(createresponse.Data.Image) f, err := os.Create("test.jpg") f.Write(image_decode) if err != nil { log.Fatal(err) } }Parameter
Description
Type
Required
images
The URLs of the images that are used for LoRA training.
[]string
Yes
template_image
The URL path of the template.
string
Yes
t2i_prompt
The prompt.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
AI portrait creation based on a prompt and a template image that is generated based on a single reference image without the need for a template image or model training
package main import ( "encoding/base64" "log" "os" sdk "github.com/aliyun/aliyun-pai-aiservice-go-sdk" ) func main() { host := os.Getenv("Host") appId := os.Getenv("AppId") token := os.Getenv("Token") client := sdk.NewClient(host, appId, token) template_image = "https://demo.jpg" ref_image = "https://reference.jpg" t2i_prompt := "(portrait:1.5), 1girl, bokeh, bouquet, brown_hair, cloud, flower, hairband, hydrangea, lips, long_hair, outdoors, sunlight, white_flower, white_rose, green sweater, sweater, (cloth:1.0), (best quality), (realistic, photo-realistic:1.3), film photography, minor acne, (portrait:1.1), (indirect lighting), extremely detailed CG unity 8k wallpaper, huge filesize, best quality, realistic, photo-realistic, ultra high res, raw photo, put on makeup" config := make(map[string]interface{}, 10) config["ipa_control_only"] = true config["ipa_weight"] = 0.6 config["ipa_image_path"] = ref_image config["t2i_prompt"] = t2i_prompt createresponse, err := client.AIGCApi.AigcImagesCreate("", template_image, "", config) if err != nil { log.Fatal(err) } image_decode, _ := base64.StdEncoding.DecodeString(createresponse.Data.Image) f, err := os.Create("test.jpg") f.Write(image_decode) if err != nil { log.Fatal(err) } }Parameter
Description
Type
Required
ref_image
The URL path of the reference image.
string
Yes
template_image
The URL path of the template.
string
Yes
t2i_prompt
The prompt.
string
Yes
config
The returned configurations. Default value: nil.
map[string]interface{}
Yes
References
For more information, see GitHub.