How to create an AI Avatar in Go using Simli

Creating a real-time video AI avatar for interaction is a great way to blend technology and human-like communication. The exciting part is that it can be used in various aspects, such as customer support, virtual assistants, education, and more. Simli provides this. It is an AI video avatar-generating platform that allows you to create lip-synced AI avatars with lifelike, animated characters, realistic head movements, and synchronized speech. Now, you can utilize its product and build in Go! In this tutorial, we’ll take a look at how to generate an AI avatar in Go using Simli. Prerequisites Before we proceed, ensure that you have the following: Basic knowledge of Go and its concepts. Go version 1.22 or higher. A Simli account. An audio file. Set up your Environment To get started, go to your dashboard and navigate to the API KEYS section. There, you’ll see your API key. Copy it and store it somewhere, as you’ll need it during the building process. Next, you’ll need an AI avatar. You can choose to create your own avatar or choose from the list in your dashboard. For the project, we’ll choose an avatar from the one assigned to us. Navigate to your avatars. There, you’ll see the list of avatars you have: Copy the ID of your desired avatar and store it somewhere. Now you’re ready to start building! Build the Project Open up a terminal in your code editor and navigate to the folder for the project. You need to initialize a new Go module. Run this command: go mod init simli-avatar Next, you need to install Simli sdk. While there isn't an official Simli sdk for Go yet, There’s a Go library you can use. Add this library to your project using this command: go get github.com/oyedeletemitope/simli-go-sdk This will install the library that you’ll need to use Simli. Import Required Packages and Set Data Structure for Response You need to import the necessary packages for file handling, HTTP communication, JSON encoding/decoding, environment variables, and working with Simli's SDK. Create a main.go file and add the following code: import ( "bytes" "encoding/base64" "encoding/json" "fmt" "log" "net/http" "os" "os/exec" "github.com/oyedeletemitope/simli-go-sdk/client" "github.com/oyedeletemitope/simli-go-sdk/models" ) Below is the list of packages imported and what they do: encoding/base64: To encode binary data into Base64 format. encoding/json: To work with JSON data. fmt: For formatted I/O operations. log: For logging errors or information. net/http: To handle HTTP server and client operations. os: For file and environment operations. simli-go-sdk: The library for interacting with Simli's API. You also need to create the data structure for the response: type GenerateAvatarResponse struct { HLSURL string `json:"hls_url"` MP4URL string `json:"mp4_url"` } Here, we created a struct that defines the structure of the HTTP response sent back to the client. Generate the AI Avatar You need to create a handler to generate the avatar. It will handle HTTP requests for avatar generation by processing a PCM file and sending it to the Simli API. Add this code: func generateAvatarHandler(w http.ResponseWriter, r *http.Request) { apiKey := "your-simli-api-key" faceID := "6ebf0aa7-6fed-443d-a4c6-fd1e3080b215" // Path to the PCM file pcmFile := "./audio/audio.pcm" // Read the PCM file audioData, err := os.ReadFile(pcmFile) if err != nil { http.Error(w, fmt.Sprintf("Failed to read PCM audio file: %v", err), http.StatusInternalServerError) return } // Convert PCM audio to Base64 audioBase64 := base64.StdEncoding.EncodeToString(audioData) // Initialize Simli client c := client.NewClient(apiKey) // Prepare audio-to-video request audioReq := models.AudioToVideoRequest{ SimliAPIKey: apiKey, FaceID: faceID, AudioBase64: audioBase64, AudioFormat: "pcm16", AudioSampleRate: 16000, AudioChannelCount: 1, } // Generate avatar resp, err := c.AudioToVideo(audioReq) if err != nil { http.Error(w, fmt.Sprintf("Failed to generate avatar: %v", err), http.StatusInternalServerError) return } // Respond with video URLs json.NewEncoder(w).Encode(GenerateAvatarResponse{ HLSURL: resp.HLSURL, MP4URL: resp.MP4URL, }) } Add the Main Function Lastly, on the main.go file, you need to create the main function that’ll take in the avatar generation function, set up the HTTP server, define routes, and start serving API requests and static frontend files. Add the code right after the avatar generat

Jan 16, 2025 - 18:10
How to create an AI Avatar in Go using Simli

Creating a real-time video AI avatar for interaction is a great way to blend technology and human-like communication. The exciting part is that it can be used in various aspects, such as customer support, virtual assistants, education, and more.

Simli provides this. It is an AI video avatar-generating platform that allows you to create lip-synced AI avatars with lifelike, animated characters, realistic head movements, and synchronized speech. Now, you can utilize its product and build in Go!

In this tutorial, we’ll take a look at how to generate an AI avatar in Go using Simli.

Prerequisites

Before we proceed, ensure that you have the following:

  • Basic knowledge of Go and its concepts.
  • Go version 1.22 or higher.
  • A Simli account.
  • An audio file.

Set up your Environment

To get started, go to your dashboard and navigate to the API KEYS section. There, you’ll see your API key. Copy it and store it somewhere, as you’ll need it during the building process.

Get your API key

Next, you’ll need an AI avatar. You can choose to create your own avatar or choose from the list in your dashboard. For the project, we’ll choose an avatar from the one assigned to us. Navigate to your avatars. There, you’ll see the list of avatars you have:

Simli AI avatars

Copy the ID of your desired avatar and store it somewhere.

Now you’re ready to start building!

Build the Project

Open up a terminal in your code editor and navigate to the folder for the project.

You need to initialize a new Go module. Run this command:

go mod init simli-avatar

Next, you need to install Simli sdk. While there isn't an official Simli sdk for Go yet, There’s a Go library you can use. Add this library to your project using this command:

go get github.com/oyedeletemitope/simli-go-sdk

This will install the library that you’ll need to use Simli.

Import Required Packages and Set Data Structure for Response

You need to import the necessary packages for file handling, HTTP communication, JSON encoding/decoding, environment variables, and working with Simli's SDK. Create a main.go file and add the following code:


import (
   "bytes"
   "encoding/base64"
   "encoding/json"
   "fmt"
   "log"
   "net/http"
   "os"
   "os/exec"
 "github.com/oyedeletemitope/simli-go-sdk/client"
 "github.com/oyedeletemitope/simli-go-sdk/models"
)

Below is the list of packages imported and what they do:

  • encoding/base64: To encode binary data into Base64 format.
  • encoding/json: To work with JSON data.
  • fmt: For formatted I/O operations.
  • log: For logging errors or information.
  • net/http: To handle HTTP server and client operations.
  • os: For file and environment operations.
  • simli-go-sdk: The library for interacting with Simli's API.

You also need to create the data structure for the response:


type GenerateAvatarResponse struct {
        HLSURL string `json:"hls_url"`
        MP4URL string `json:"mp4_url"`
}

Here, we created a struct that defines the structure of the HTTP response sent back to the client.

Generate the AI Avatar

You need to create a handler to generate the avatar. It will handle HTTP requests for avatar generation by processing a PCM file and sending it to the Simli API. Add this code:


func generateAvatarHandler(w http.ResponseWriter, r *http.Request) {
        apiKey := "your-simli-api-key"
        faceID := "6ebf0aa7-6fed-443d-a4c6-fd1e3080b215"
        // Path to the PCM file
        pcmFile := "./audio/audio.pcm"
        // Read the PCM file
        audioData, err := os.ReadFile(pcmFile)
        if err != nil {
                http.Error(w, fmt.Sprintf("Failed to read PCM audio file: %v", err), http.StatusInternalServerError)
                return
        }
        // Convert PCM audio to Base64
        audioBase64 := base64.StdEncoding.EncodeToString(audioData)
        // Initialize Simli client
        c := client.NewClient(apiKey)
        // Prepare audio-to-video request
        audioReq := models.AudioToVideoRequest{
                SimliAPIKey:       apiKey,
                FaceID:            faceID,
                AudioBase64:       audioBase64,
                AudioFormat:       "pcm16",
                AudioSampleRate:   16000,
                AudioChannelCount: 1,
        }
        // Generate avatar
        resp, err := c.AudioToVideo(audioReq)
        if err != nil {
                http.Error(w, fmt.Sprintf("Failed to generate avatar: %v", err), http.StatusInternalServerError)
                return
        }
        // Respond with video URLs
        json.NewEncoder(w).Encode(GenerateAvatarResponse{
                HLSURL: resp.HLSURL,
                MP4URL: resp.MP4URL,
        })
}

Add the Main Function

Lastly, on the main.go file, you need to create the main function that’ll take in the avatar generation function, set up the HTTP server, define routes, and start serving API requests and static frontend files. Add the code right after the avatar generation function:


func main() {
   // Serve the generate-avatar endpoint
   http.HandleFunc("/generate-avatar", generateAvatarHandler)

   // Serve static HTML for the frontend
   fs := http.FileServer(http.Dir("./static"))
   http.Handle("/", fs)

   // Start the server
   port := ":8080"
   fmt.Printf("Server is running at http://localhost%s\n", port)
   log.Fatal(http.ListenAndServe(port, nil))
}

The code above initializes and starts an HTTP server that handles requests for the application. It defines the /generate-avatar route, which links to the handler responsible for processing audio files and generating avatar videos via the Simli API.

It also serves static HTML files from the ./static directory (which we’ll create shortly) to provide a simple frontend interface for users. Finally, the server runs on port 8080, which logs its address for easy accessibility to test or deploy.

Create the Static File

Now, you need to create the static file to serve as the frontend interface. Create a folder called static, and inside it, create an index.html file. Add this:



 lang="en">
 
    charset="UTF-8" />
    name="viewport" content="width=device-width, initial-scale=1.0" />
   </span>Avatar Video<span class="nt">
   
 
 
   class="bg-gray-100 min-h-screen flex flex-col items-center justify-center p-4"
 >
    class="max-w-lg w-full bg-white shadow-lg rounded-lg p-6 space-y-6">
      class="text-2xl font-bold text-gray-800 text-center">
       Generated Avatar Video
     
     
       id="generate"
       class="w-full bg-blue-500 hover:bg-blue-600 text-white font-semibold py-2 px-4 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-400"
     >
       Generate Avatar
     
     
class="text-lg font-semibold text-gray-700 text-center"> Video Playback id="avatar-video" controls class="w-full mt-4 rounded-lg hidden" >

Test the application

Now that we’re done, let’s test the application by starting it up. Run this command:

go run main. Go

Open http://localhost:8080/ in your local browser and test it out:

Wasn't so hard, was it?

That's how to create an AI avatar in Go using Simli

In this tutorial, we looked at how to create an AI avatar in Go through a library that uses Simli sdk for AI. It's important to note that the sdk is still in early development, and more Simli features are still being added.

Please share if you found this helpful.