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:
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_KEY
environment 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).
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!
To complete all the steps in this post, you will require the following:
Our solution consists of the following components:
The final architecture will look like this:
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:
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.
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.
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.
A Guide to Ultra-Low Latency Crypto Trading on the Cloud: Part 1 - Infrastructure Fundamentals
2 posts | 1 followers
FollowFarruh - September 22, 2023
JJ Lim - December 29, 2023
Alibaba Cloud Indonesia - November 22, 2023
Rupal_Click2Cloud - October 19, 2023
PM - C2C_Yuan - August 14, 2023
Farruh - July 18, 2024
2 posts | 1 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreServerless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.
Learn MoreTop-performance foundation models from Alibaba Cloud
Learn MoreMore Posts by Daniel Molenaars