All Products
Search
Document Center

Platform For AI:Use AI Portrait SDK for Go

Last Updated:Dec 16, 2025

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

  1. Environment dependency: Go 1.18 or later.

  2. Install SDK for Go:

    • Run the go mod init project name command to create a project.

    • Run the go get github.com/aliyun/aliyun-pai-aiservice-go-sdk command 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.

  3. Initialize the client:

    1. 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=xxxxxxxxx

      Parameter

      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.

    2. 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.

image

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

    The following table describes the data field whose type is AIGCImageCheckData.

    Parameter

    Description

    Type

    CheckResults

    The detection result of each input image. Each image corresponds to a dictionary that contains the following keys: url, message, and frontal. The url key indicates the URL of the image, the message key indicates the detection details of the image, and the frontal key indicates whether the image is front-facing.

    List<AIGCImageCheckResult>

    CostTime

    The time required for the server to call the API.

    float64

    Images

    The URLs of the images that are checked.

    List<String>

    RequestId

    The ID of the request. The ID is the same as the value of the requestId parameter in the sample response.

    String

    The following table describes the check_results field whose type is AIGCImageCheckResult.

    Parameter

    Description

    Type

    Code

    The status code of the request. Valid values: 0 and 1. 0 indicates that the request is not completed and 1 indicates that the request is completed.

    The detection result of each input image.

    int

    Frontal

    Indicates whether the image is the front-facing.

    bool

    Message

    The details of the request status.

    string

    Url

    The URL of the image.

    string

    The following table describes the messages of the check_results field.

    Message

    Status code

    Description

    success

    1

    The image meets the requirements.

    Image decode error.

    2

    The image cannot be downloaded or decoded.

    Number of face is not 1.

    3

    The number of faces is not 1.

    Image detect error.

    4

    An error occurred while detecting the face.

    Image encoding error.

    5

    An error occurred while encoding a face as a feature vector. In most cases, the issue occurs because a face cannot be detected.

    This photo is not the same person in photos.

    6

    If only this error is reported, the faces in multiple images do not belong to the same person.

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

    The following table describes the data field whose type is AIGCImageTrainData.

    Parameter

    Description

    Type

    jobId

    The task ID.

    int32

    modelId

    The ID of the model that is trained, which is a string of 36 characters.

    string

    You need to use job_id to query training results.

    You need to use model_id to send a request for portrait creation.

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

    The following table describes the parameters in the Result field returned after the model training is completed.

    Parameter

    Description

    cost_time

    The time required for this training session.

    states

    The check result of each image.

    Each image corresponds to a dictionary, and each dictionary has three keys: url, message, and frontal. The url key indicates the URL of the image, the message key indicates the detection details of the image, and the frontal key indicates whether the image is front-facing.

    model_id

    The ID of the LoRA model, which is the same as the value of model_id obtained during the training request.

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

      The following table describes the parameters in the config field.

      Parameter

      Description

      Type

      Required

      Default value

      Valid value

      lora_weights

      The weight of LoRA.

      float

      No

      0.90

      0.5 to 1.0

      first_denoising_strength

      The intensity of the first image reconstruction.

      float

      No

      0.45

      0.0 to 1.0

      second_denoising_strength

      The intensity of the second image reconstruction.

      float

      No

      0.30

      0.0 to 1.0

      more_like_me

      The degree to which the image resembles the desired person.

      float

      No

      0.50

      0.0 to 1.0

      crop_face_preprocess

      Specifies whether to crop and then reconstruct the image. We recommend that you set this parameter to True for large images.

      bool

      No

      True

      True and False

      apply_face_fusion_before

      Specifies whether to perform the first portrait merging.

      bool

      No

      True

      True and False

      apply_face_fusion_after

      Specifies whether to perform the second portrait merging.

      bool

      No

      True

      True and False

      color_shift_middle

      Specifies whether to perform the first color correction.

      bool

      No

      True

      True and False

      color_shift_last

      Specifies whether to perform the second color correction.

      bool

      No

      True

      True and False

      background_restore

      Specifies whether to rebuild the background.

      bool

      No

      False

      True and False

      skin_retouching_bool

      Specifies whether to perform skin smoothing.

      If you set the parameter to True, the system smooths and brightens the skin. In most cases, this makes the image more attractive, but may excessively lighten the skin. If you set the parameter to False, the skin texture can be improved.

      bool

      No

      False

      True and False

      photo_enhancement_bool

      Specifies whether to enhance the portrait.

      If you set the parameter to True, the system restores the portrait or increases the image resolution to improve image quality.

      bool

      No

      True

      True and False

      photo_enhancement_method

      The method used to enhance the portrait.

      photo_fix is used for image restoration by fixing specific imperfections, which may result in a loss of skin texture due to some distortion.

      super_resolution is used for enhancing the resolution of an image, preserving more of the original appearance.

      string

      No

      photo_fix

      photo_fix and super_resolution

      makeup_transfer

      Specifies whether to perform makeup transfer.

      If you set the parameter to True, the system performs makeup transfer to prevent the image from being too plain. However, this may result in less resemblance of the image to the user.

      bool

      No

      False

      True and False

      makeup_transfer_ratio

      The intensity of makeup transfer.

      The larger the value, the higher the proportion of makeup transfer and the more closely the resulting makeup resembles the template. This may also result in less resemblance of the image to the user.

      float

      No

      0.5

      0.0 to 1.0

      face_shape_match

      Specifies whether to perform face shape compatibility.

      If you set the parameter to True, the face shape and skin texture more closely resemble the user.

      bool

      No

      False

      True and False

      ipa_control

      Specifies whether to use IP-Adapter.

      If you set the parameter to True, the generated portrait more closely resembles the reference image.

      bool

      No

      False

      True and False

      ipa_control_only

      If you set the parameter to True, you must configure the ipa_image_path parameter and you can directly perform prediction without training.

      bool

      No

      False

      True and False

      ipa_image_path

      The URL of the reference image.

      If you set the ipa_control_only parameter to True, you must configure this parameter.

      string

      No

      None

      A publicly accessible URL

      ipa_weight

      The weight of IP-Adapter.

      The larger the value is, the more closely the generated image resembles the user. However, an excessively large value may result in image distortion.

      float

      No

      0.5

      0.0 to 1.0

      style_name

      The style of the created portrait.

      string

      No

      Realistic

      Realistic and

      Anime

      lcm_accelerate

      Specifies whether to perform latent code manipulation (LCM) acceleration.

      bool

      No

      False

      True and False

      sharp_ratio

      The sharpness level. You can configure this parameter to enhance clarity.

      An excessively large value may result in image distortion.

      float

      No

      0.15

      0.0 to 1.0

      t2i_prompt

      The prompt. Configure this parameter if you want to use the text-to-image generation feature.

      If you configure the t2i_prompt parameter, you do not need to specify the URL path of the template.

      String

      No

      None

      N/A

    • 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.

      The following table describes the parameters in the config field.

      Parameter

      Description

      Type

      Required

      Default value

      Valid value

      lora_weights

      The weight of LoRA.

      The larger the value is, the more closely the generated image resembles the user. However, an excessively large value may result in image distortion.

      float

      No

      0.90

      0.5 to 1.0

      first_diffusion_steps

      The number of steps for the first diffusion.

      We recommend that you keep the parameter value unchanged. A significant decrease can cause the image to become distorted.

      int

      No

      50

      20 to 50

      first_denoising_strength

      The intensity of the first image reconstruction.

      The reconstruction intensity of the face. A greater value specifies the greater intensity of the face reconstruction, and the more closely the generated image resembles the user. However, a large value may result in an uncoordinated image.

      float

      No

      0.45

      0.0 to 1.0

      second_diffusion_steps

      The number of steps for the second diffusion.

      We recommend that you keep the parameter value unchanged. A significant decrease can cause the image to become distorted.

      int

      No

      30

      20 to 50

      second_denoising_strength

      The intensity of the second image reconstruction.

      The reconstruction intensity of the facial edges. An excessive large value may result in an uncoordinated image.

      float

      No

      0.30

      0.0 to 1.0

      more_like_me

      The degree to which the image resembles the desired person.

      The larger the value is, the more closely the generated image resembles the desired person. However, if the value is excessively large, the image may look unrealistic.

      float

      No

      0.60

      0.0 to 1.0

      crop_face_preprocess

      Specifies whether to crop and then reconstruct the image. We recommend that you set this parameter to True for large images.

      We recommend that you keep the parameter value unchanged.

      bool

      No

      True

      True and False

      apply_face_fusion_before

      Specifies whether to perform the first portrait merging.

      If you set the parameter to True, the system performs portrait merging. If you set the parameter to False, the similarity is reduced.

      bool

      No

      True

      True and False

      apply_face_fusion_after

      Specifies whether to perform the second portrait merging.

      If you set the parameter to True, the system performs portrait merging. If you set the parameter to False, the similarity is reduced.

      bool

      No

      True

      True and False

      color_shift_middle

      Specifies whether to perform the first color correction.

      If you set the parameter to True, the system performs color correction to increase the resemblance of the skin color of the generated image to the template. If you set the parameter to False, the system does not correct colors and color distortion may occur.

      bool

      No

      True

      True and False

      color_shift_last

      Specifies whether to perform the second color correction.

      If you set the parameter to True, the system performs color correction to increase the resemblance of the skin color of the generated image to the template. If you set the parameter to False, the system does not correct colors and color distortion may occur.

      bool

      No

      True

      True and False

      background_restore

      Specifies whether to rebuild the background.

      If you set the parameter to True, the background is reconstructed to be more natural. However, this feature changes the image background and increases the time consumption.

      bool

      No

      False

      True and False

      skin_retouching_bool

      Specifies whether to perform skin smoothing.

      If you set the parameter to True, the system smooths and brightens the skin. In most cases, this makes the image more attractive, but may excessively lighten the skin. If you set the parameter to False, the skin texture can be improved.

      bool

      No

      False

      True and False

      photo_enhancement_bool

      Specifies whether to enhance the portrait.

      If you set the parameter to True, the system restores the portrait or increases the image resolution to improve image quality.

      bool

      No

      True

      True and False

      photo_enhancement_method

      The method to enhance the portrait.

      photo_fix is used for image restoration by fixing specific imperfections, which may result in a loss of skin texture due to some distortion.

      super_resolution is used for enhancing the resolution of an image, preserving more of the original appearance.

      String

      No

      photo_fix

      photo_fix and super_resolution

      makeup_transfer

      Specifies whether to perform makeup transfer.

      If you set the parameter to True, the system performs makeup transfer to prevent the image from being too plain. However, this may result in less resemblance of the image to the user.

      bool

      No

      False

      True and False

      makeup_transfer_ratio

      The intensity of makeup transfer.

      The larger the value, the higher the proportion of makeup transfer and the more closely the resulting makeup resembles the template. This may also result in less resemblance of the image to the user.

      float

      No

      0.5

      0.0 to 1.0

      ipa_control

      Specifies whether to use IP-Adapter.

      If you set the parameter to True, the generated portrait more closely resembles the reference image.

      bool

      No

      False

      True and False

      ipa_control_only

      If you set the parameter to True, you must configure the ipa_image_path parameter and you can directly perform prediction without training.

      bool

      No

      False

      True and False

      ipa_image_path

      The URL of the reference portrait.

      If you set the ipa_control_only to True, you must configure this parameter.

      String

      No

      None

      A publicly accessible URL

      ipa_weight

      The weight of IP-Adapter.

      The larger the value is, the more closely the generated image resembles the user. However, an excessively large value may result in image distortion.

      float

      No

      0.5

      0.0 to 1.0

      lcm_accelerate

      Specifies whether to perform LCM acceleration.

      bool

      No

      False

      True and False

      sharp_ratio

      The sharpness level. You can configure this parameter to enhance clarity.

      An excessively large value may result in image distortion.

      float

      No

      0.15

      0.0 to 1.0

      scene_id

      If you want to use scene_lora, you need to configure the scene_id parameter.

      The scene ID is obtained by training. The model ID that is returned when you train the SDXL_LoRA model is the scene ID.

      String

      No

      None

      N/A

      t2i_prompt

      The prompt. Configure this parameter if you want to use the text-to-image generation feature.

      If you configure the t2i_prompt parameter, you do not need to specify the URL path of the template.

      String

      No

      None

      N/A

  • 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

    1. 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.

    2. 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.