×
Community Blog Generative AI with Function Compute

Generative AI with Function Compute

This post explains how to set up your own AI image generation service on Function Compute using Terraform and DashScope, our Model-as-a-Service (MaaS).

In this article, I will explain how to set up your own GenAI application on Function Compute using DashScope, our Model-as-a-Service (MaaS). The application will be based on Gradio, an open source framework that allows you to quickly create Machine Learning (ML) apps. The infrastructure will be deployed with Terraform.

The full application code can be found here.

For a live demo, please click here.

The final application will look like this:

example_demo

DashScope and Alibaba Cloud Model Studio

Before we start building our application, let's take a closer look at DashScope. As I mentioned earlier, DashScope is our Model-as-a-Service (MaaS). But what exactly is a MaaS?

In basic terms, a MaaS is a service model where machine learning models are deployed and hosted on the cloud, allowing you to use these models for your own applications through an exposed API. The biggest advantage of this is that you do not have to build or maintain the underlying infrastructure itself, which often requires instances with powerful Graphical Processing Units (GPUs). With a MaaS, you only pay for the resources that you use, which could be the length of the response or the amount of images that your model generated.

DashScope offers a wide variety of different models that you can use, including our homegrown models Qwen (our LLM) and Wanxiang (our text-to-image model). These have already been pre-trained, so you can directly start to use them in your code. Of course, you might want to refine these models according to your own needs and business logic. This is possible by fine-tuning the model through the DashScope API.

Currently, DashScope is only available on the mainland China version of Alibaba Cloud. However, an international version is being worked on and will be integrated into Alibaba Cloud Model Studio.  Alibaba Cloud Model Studio is a one-stop generative AI development platform that provides access to many different foundational models that can be used out of the box and trained according to your own specifications. It streamlines the entire development process, from model training and fine-tuning to evaluation and deployment.

If you want to call DashScope in an application, you will need to use the DashScope SDK. The SDK is currently available in 2 languages: Python and Java. You can find installation instructions here.

Using the SDK is quite straightforward: you set your API key through the DASHSCOPE_API_KEYenvironment variable or in your application code, import the dashscope library in your application, and you're ready to go! For example, the code snippet below is a Python script that generates 4 different images, using only a simple prompt:

import dashscope

dashscope.api_key = "<YOUR_API_KEY>"

def generate_images(prompt, image_count):
    task = dashscope.ImageSynthesis.async_call(
        model=dashscope.__name__ImageSynthesis.Models.wanx_v1,
        prompt=prompt,
        n=image_count,
    )
    response = dashscope.ImageSynthesis.wait(task)
    images = [image.url for image in response.output.results]
    return images


if __name__ == "__main__":
    prompt = "futuristic city landscape, spaceship, abandoned planet"
    images = generate_images(prompt, image_count=4)   
    for image in images:
        print(image)

In this post, we will be using the Tongyi Wanxiang API of DashScope, but there are many more models that you can experiment with! You can find an overview of all the different models that the SDK supports here (currently only in Chinese).

Building your application

Now that we have an idea what a MaaS entails and how to call the DashScope SDK, we can start to use these tools in our own application!

Prerequisites

To complete all the steps in this post, you will require the following:

  1. Download and install Terraform. See here on how to set up Terraform on your local operating system.
  2. An Alibaba Cloud RAM user or role that has the necessary rights to deploy your application on your account and connect to the Container Registry.
  3. Activate a Personal Instance on Container Registry and register your preferred RAM user on Container Registry. You can activate and register a user here.
  4. Own a domain name that you can use as the application endpoint. If you do not have one yet, you can easily register for one through Alibaba cloud here.
  5. Have a DashScope API key. You can get one here.
  6. (Optional) Have an SSL certificate for your domain in order to enable HTTPS. You can use your own certificate, or you can buy one from the Alibaba Cloud Certificate Management service.

Solution overview

Our solution consists of the following components:

  1. Container Registry repository. This repository will contain the Docker image that our Function Compute function runs.
  2. Function Compute function. This function will run our Gradio web app and will serve as the main entry point for our end users.
  3. CloudOps Orchestration Service Secret Parameter. This parameter will be used to store the DashScope API key.
  4. DashScope's Tongyi Wanxiang API. This pre-trained model will be able to generate images based on our prompts.

The final architecture will look like this:

genai_with_fc_drawio

Walkthrough

Setting up the project
Clone the following repository: https://github.com/dmolenaars/serverless-image-generation.git

Change into the downloaded repository: cd serverless-image-generation. The repository will look like this:

├── Dockerfile
├── main.tf
├── modules
│   ├── container_registry
│   │   ├── main.tf
│   │   └── variables.tf
│   └── image_generator
│       ├── main.tf
│       └── variables.tf
├── src
│   ├── app.py
│   └── requirements.txt
└── variables.tf

Our application contains 2 modules, container_registry and image_generator. container_registry contains the container registry where our Function Compute images will be hosted. image_generator contains the Function compute function and its dependencies.

Before we can start deploying our infrastructure, we will need to change some default project values. Open terraform.tfvars in your code editor and replace the default values as follows:

deployment_region: your preferred deployment region.
namespace_id: your preferred container registry namespace name. Note: this name must be globally unique!
domain_name : the domain name that you wish to use to access the application.

In addition, you will need to set the DashScope API key as a Terraform environment variable like follows:
export TF_VAR_api_key=<YOUR DASHSCOPE API KEY>

You can now initialize the project and the Alibaba Cloud provider with terraform init. If the operation was successful, Terraform will provide the following output: Terraform has been successfully initialized!

Deploying the container registry
Deploy the container registry with the following command:

terraform apply -target=module.container_registry

If Terraform cannot find any valid credentials, the following error will be displayed:

no valid credential sources for Terraform Alibaba Cloud Provider found.

If Terraform is able to use a set of valid credentials, a list of Container Registry resources that will be deployed will be displayed on the screen. Terraform will then ask you whether you wish to proceed:

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value:

Type yes to confirm the deployment. After the deployment has succeeded, Terraform will display something the following message:

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Congratulations, you have created an image registry in your account! We can now start pushing images to this registry.

Building and pushing an image
Execute the following command to log in to the registry, replacing the placeholder values with your own:

docker login --username=<RAM_USER_ID>@<ACCOUNT_ID> registry-intl.<REGION_ID>.aliyuncs.com

Enter your password when prompted.

Next, build the image, and push the image to the registry with the following command, replacing REGION_ID and NAMESPACE_ID with your own:

docker build . -t registry-intl.<REGION_ID>.aliyuncs.com/<NAMESPACE_ID>/serverless-image-generation:latest --platform linux/amd64
docker push registry-intl.<REGION_ID>.aliyuncs.com/<NAMESPACE_ID>/serverless-image-generation:latest

Deploying the application
All that's left is deploying the application itself! You can deploy the Function Compute resources with terraform apply.
Terraform will again ask you whether you wish to proceed:

Plan: 8 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value:

Type yes to confirm the deployment. After the deployment has succeeded, Terraform will display the following message:

Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Configuring the DNS record of your application domain name
Our application is now ready to be used, but we need to configure the DNS settings in order to make it publicly accessible.

Go to the DNS settings of your domain registrar. Add a CNAME record to the DNS settings of your domain name that points your root domain to the Function Compute endpoint. The endpoint is constructed as follows:<ACCOUNT_ID>.<REGION>.fc.aliyuncs.com

If your domain name is registered through Alibaba Cloud, you can use the Alibaba Cloud DNS console to add a new record as follows:

  1. Within the Alibaba Cloud DNS console, select Domain Name Resolution and click on your domain name.
  2. Finally, click on Add DNS Record. For Hostname, fill in @ . For Record Value, fill in your account's Function Compute endpoint.
  3. Click Ok to add the DNS record.
    Screenshot_2024_03_25_at_16_14_50

Congratulations! Your project is now deployed, correctly configured, and ready to be used. Navigate to your application in your browser using your domain name. If this is the first time that your application runs, it might take a little longer than usual as Function Compute has not cached your image yet. Depending on your DNS provider, it might also take some time before the DNS changes in the previous step are fully propagated.

Cleaning up

In order to avoid any unwanted charges (for example, due to DashScope invocations), you can run the following Terraform command to remove all resources:

terraform destroy

Terraform will ask you whether you wish to proceed:

Plan: 0 to add, 0 to change, 9 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: 

Type yes to confirm the plan. Terraform will display the following message:

Destroy complete! Resources: 9 destroyed.

Conclusion

In this post I have shown how you can rapidly develop and deploy GenAI applications using DashScope and Terraform. With DashScope, you can quickly make use of Alibaba Cloud's AI resources without having to maintain your own infrastructure or train your own models. In addition, Terraform allows you to efficiently deploy infrastructure in Alibaba Cloud, and roll back changes whenever necessary.

Curious about GenAI on Alibaba Cloud? Take a look at our offerings.

0 1 0
Share on

Daniel Molenaars

1 posts | 0 followers

You may also like

Daniel Molenaars

1 posts | 0 followers

Related Products