How to use Stable Diffusion on Cloud Run

Google Cloud Tech · Intermediate ·🎨 Image & Video AI ·1y ago

Key Takeaways

This video demonstrates how to use Stable Diffusion on Cloud Run, an open-source model for generating images from text prompts, by setting up the model with TorchServe and deploying it on Cloud Run with GPU support.

Full Transcript

now that I can use gpus serverless in Cloud run what can I do with it good question you can run different op Source models on it like stable diffusion XL for image generation and let me show you [Music] how welcome to the show Lisa what do you do here at Google Cloud I'm a product manager at Google coud focusing on C run and gke and my years of experience in the C industry have given me a deep understanding of the challenges Enterprises face I am passionate about using this knowledge to drive product development and ensure a simless user experience for our customers seamless user experience I love it uh so what are we building today Lisa we're building a simple AI inference application that generates image it uses the stable diffusion Excel model with torch serf running similarly on carun once deployed the application will allow you to generate images simply by providing a text prompt describing the image you want and here's an example of what it can do but you could also use Google's hosted image and model for image generation uh why are we using stable diffusion Google's imj model is really good good it's hosted by Google and generates photo realistic images it's easy to call from your code so you can get your application to Market quickly if you use that on the other hand if you host your own model or open models on carun you get more control you get more transparency and customization and that's what we're doing today ah I see so you said before that we're going to set up stable diffusion excel in Cloud run uh can viewers try for themselves yeah of course you can follow along in the collab Martin can you please make sure to include the link in the show notes okay we'll do all right the first step is to enable the right apis in your Google call project and I have done that in my project but the collab list all the commands for this got it next I will create a torch serve app torch serve is an open source model serving library that makes my life easier to work with the stable diffusion Excel model and the stable diffusion Excel model will run in the GPU enabled cand service then I will create a requirements file that list all the libraries the application will use I will add a file called config do properties that gives torch serve is configuration now it's time to add the main code of the application I'll create a new file called stable diffusion Handler and paste the code from the cab in there whoa that's a lot of code uh where does the image generation happen here in the inference method and that's where the real action happens this inference method takes a list of input promps and generate images using the loaded models it first used the pipeline to generate a initial image from the pump and then it used the refiner to refine the image and remove the artifacts ah got it next I will create a shell script file that starts the torch serve application I'll will ask cun to call this show script Cloud run runs and scales containers for us in the cloud so I need a Docker file to build that container here at the end of the docker file I'm asking Cloud run to execute the show script that starts the torch serve application ah understood and now it's time to deploy the app yeah that is right it takes some time to download a stable diffusion Excel model from the internet so to speed things up let's run this two commands to set up a Callet and that will increase the bandwidth to huging face website where the model is stored sounds useful uh deployment time yeah it is first I will run gcal builds Summit to build a container and this command will build a container in the cloud on a Google host dat to build server it will take a while so let's take a te break and we're back the build was completed successfully now I will run Gard run deploy to deploy the build container and give it a public https edges and it looks like you're setting up cloudron to use gpus here Lisa yeah GPU equals one means that the service will use one GPU and then GPU type flag specified the service will Ed Nvidia L4 GPU let's run that command it will take a minute to complete looks like the deployment finished it did let's try it out first let's put the service URL in an environment variable next let's set up environment variable for the prompt and I'll ask for a image of a dog running in the park since the dog is my favorite pet and then I will run this Cur command to send a request to my app with the prom and this will take a minute also due to the cloud run service code start and it's done here's the image very nice um could the dog wear a pink shirt and sunglasses sure let's do it I'll send another request to the app with a new prompt and here you are this is the image how do you like it Martin it looks great Lisa uh well thank you for showing us this Lisa yes thanks for having me Martin and thank you everyone for watching if you have any questions for Lisa or me please add them in the comments also please let me know what you thought of this episode I read every single comment I can't wait to see what you build [Music]

Original Description

Codelab → https://goo.gle/41tWN1W Discover how to use GPUs in Cloud Run for running Stable Diffusion, an open-source model for generating images from text prompts. Join Googlers Martin Omander and Lisa Shen as they demonstrate how to set up Stable Diffusion, package it with TorchServe, and deploy it on Cloud Run for on demand image generation. Chapters: 0:00 - Intro 0:53 - Building an image generation app 2:10 - Creating a TorchServe app 3:02 - Inference method for image generation 3:55 - Deploying the app 5:08 - Testing the app 5:58 - Conclusion Watch more Serverless Expeditions → https://goo.gle/ServerlessExpeditions Subscribe to Google Cloud Tech → https://goo.gle/GoogleCloudTech #ServerlessExpeditions #GoogleCloud Speaker: Martin Omander, Lisa Shen Products Mentioned: Cloud Run, Gemini
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Google Cloud Tech · Google Cloud Tech · 0 of 60

← Previous Next →
1 I’m going for it #GoogleCloudCertified
I’m going for it #GoogleCloudCertified
Google Cloud Tech
2 I had to get #GoogleCloudCertified
I had to get #GoogleCloudCertified
Google Cloud Tech
3 Be better overall at what you do #GoogleCloudCertified
Be better overall at what you do #GoogleCloudCertified
Google Cloud Tech
4 Cloud Monitoring on our radar #Analysis #Uptime
Cloud Monitoring on our radar #Analysis #Uptime
Google Cloud Tech
5 Introduction to Generative AI Studio
Introduction to Generative AI Studio
Google Cloud Tech
6 How to use Github Actions with Google's Workload Identity Federation
How to use Github Actions with Google's Workload Identity Federation
Google Cloud Tech
7 Introduction to Responsible AI
Introduction to Responsible AI
Google Cloud Tech
8 Networking updates and CDMC-certified architecture
Networking updates and CDMC-certified architecture
Google Cloud Tech
9 Create and use a Cloud Storage bucket
Create and use a Cloud Storage bucket
Google Cloud Tech
10 How to digitize text from documents
How to digitize text from documents
Google Cloud Tech
11 Faster analytical queries with AlloyDB
Faster analytical queries with AlloyDB
Google Cloud Tech
12 Next ‘23 sessions and FaaS Wave
Next ‘23 sessions and FaaS Wave
Google Cloud Tech
13 Introduction to Assured Open Source Software
Introduction to Assured Open Source Software
Google Cloud Tech
14 BigQuery Cost Optimization: Storage
BigQuery Cost Optimization: Storage
Google Cloud Tech
15 BigQuery Cost Optimization: Compute
BigQuery Cost Optimization: Compute
Google Cloud Tech
16 BigQuery Cost Optimization: Select Queries
BigQuery Cost Optimization: Select Queries
Google Cloud Tech
17 Remote Field Equipment Management with Manufacturing Data Engine
Remote Field Equipment Management with Manufacturing Data Engine
Google Cloud Tech
18 Supercharging your applications with Cloud SQL Enterprise Plus
Supercharging your applications with Cloud SQL Enterprise Plus
Google Cloud Tech
19 Vector Support on our radar #GenAI
Vector Support on our radar #GenAI
Google Cloud Tech
20 Architecting a blockchain startup with Google Cloud
Architecting a blockchain startup with Google Cloud
Google Cloud Tech
21 Kubernetes and multitasking updates!
Kubernetes and multitasking updates!
Google Cloud Tech
22 GKE: Using Kubernetes Events
GKE: Using Kubernetes Events
Google Cloud Tech
23 How to configure firewall rules for Cloud Composer
How to configure firewall rules for Cloud Composer
Google Cloud Tech
24 Vertex AI Embeddings API + Matching Engine: Grounding LLMs made easy
Vertex AI Embeddings API + Matching Engine: Grounding LLMs made easy
Google Cloud Tech
25 Geospatial analytics on our radar #EarthEngine #BigQuery
Geospatial analytics on our radar #EarthEngine #BigQuery
Google Cloud Tech
26 Ensuring requests are set in Kubernetes
Ensuring requests are set in Kubernetes
Google Cloud Tech
27 Cloud Next 2023, Google research program, and more!
Cloud Next 2023, Google research program, and more!
Google Cloud Tech
28 How to migrate projects between organizations with Resource Manager
How to migrate projects between organizations with Resource Manager
Google Cloud Tech
29 How to run #MySQL in Google Cloud
How to run #MySQL in Google Cloud
Google Cloud Tech
30 #GenerativeAI for enterprises and #Next2023
#GenerativeAI for enterprises and #Next2023
Google Cloud Tech
31 How Google Photos scales to store 4 trillion photos and videos
How Google Photos scales to store 4 trillion photos and videos
Google Cloud Tech
32 Google Cross-Cloud Interconnect (Demo 2)
Google Cross-Cloud Interconnect (Demo 2)
Google Cloud Tech
33 GKE Cost Optimization Golden Signals: Introduction
GKE Cost Optimization Golden Signals: Introduction
Google Cloud Tech
34 GKE Cost Optimization Golden Signals: Workload Rightsizing
GKE Cost Optimization Golden Signals: Workload Rightsizing
Google Cloud Tech
35 GKE Load Balancing: Overview
GKE Load Balancing: Overview
Google Cloud Tech
36 GKE Load Balancing: Best Practices
GKE Load Balancing: Best Practices
Google Cloud Tech
37 Disaster Recovery in GKE
Disaster Recovery in GKE
Google Cloud Tech
38 How to configure IP masquerade agent in GKE Standard clusters
How to configure IP masquerade agent in GKE Standard clusters
Google Cloud Tech
39 Enable and use GKE Control plane logs
Enable and use GKE Control plane logs
Google Cloud Tech
40 Compliance in Australia with Assured Workloads
Compliance in Australia with Assured Workloads
Google Cloud Tech
41 Creating budgets and budget alerts in Google Cloud #FinOps
Creating budgets and budget alerts in Google Cloud #FinOps
Google Cloud Tech
42 Cloud SQL Enterprise Plus on our radar #mySQL
Cloud SQL Enterprise Plus on our radar #mySQL
Google Cloud Tech
43 What's Next for Google Cloud?
What's Next for Google Cloud?
Google Cloud Tech
44 How Loveholidays scaled with Contact Center AI
How Loveholidays scaled with Contact Center AI
Google Cloud Tech
45 What is fleet team management in GKE?
What is fleet team management in GKE?
Google Cloud Tech
46 Troubleshoot VPC Network Peering
Troubleshoot VPC Network Peering
Google Cloud Tech
47 Introduction to DocAI and Contact Center AI
Introduction to DocAI and Contact Center AI
Google Cloud Tech
48 Cloud Run Direct VPC egress explained
Cloud Run Direct VPC egress explained
Google Cloud Tech
49 Database deployment options in GKE
Database deployment options in GKE
Google Cloud Tech
50 Analyze cloud billing data with #BigQuery
Analyze cloud billing data with #BigQuery
Google Cloud Tech
51 Tips to becoming a world-class Prompt Engineer
Tips to becoming a world-class Prompt Engineer
Google Cloud Tech
52 Serverless is simple. Do I need CI/CD?
Serverless is simple. Do I need CI/CD?
Google Cloud Tech
53 Accelerating model deployment with MLOps
Accelerating model deployment with MLOps
Google Cloud Tech
54 How Hawaii's Department of Human Services scaled with CCAI
How Hawaii's Department of Human Services scaled with CCAI
Google Cloud Tech
55 Pricing API on our #Radar
Pricing API on our #Radar
Google Cloud Tech
56 How Recommendations AI for Media can boost customer retention
How Recommendations AI for Media can boost customer retention
Google Cloud Tech
57 Troubleshooting: Node Not Ready Status
Troubleshooting: Node Not Ready Status
Google Cloud Tech
58 One weekend until Cloud Next 2023!
One weekend until Cloud Next 2023!
Google Cloud Tech
59 #GoogleCloudNext starts tomorrow!
#GoogleCloudNext starts tomorrow!
Google Cloud Tech
60 #GoogleCloudNext will be demand!
#GoogleCloudNext will be demand!
Google Cloud Tech

This video teaches how to set up and deploy Stable Diffusion on Cloud Run with GPU support, allowing users to generate images from text prompts. The process involves creating a TorchServe app, configuring the model, and deploying it on Cloud Run.

Key Takeaways
  1. Enable the necessary APIs in your Google Cloud project
  2. Create a TorchServe app
  3. Create a requirements file and config.properties file
  4. Add the main code for the application
  5. Create a shell script to start the TorchServe application
  6. Build a Docker container
  7. Deploy the app on Cloud Run with GPU support
💡 Using GPU support on Cloud Run can significantly speed up image generation tasks, making it a powerful tool for deploying AI models.

Related Reads

📰
How can I batch-generate 3D assets from prompts or images using an API, and which 3D generation APIs support batch generation?
Learn to batch-generate 3D assets from prompts or images using APIs for efficient pipeline creation
Reddit r/artificial
📰
How AI Head Swap Works: The Technology Behind Realistic AI Image Replacement
Learn how AI head swap technology works and its applications in image editing
Dev.to AI
📰
How I Built an AI Pet Portrait Generator That Turns Photos Into Art
Learn how to build an AI pet portrait generator that turns photos into art using deep learning techniques and Python libraries
Dev.to · William Li
📰
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Learn how to use Google's Squoosh codecs in the browser to compress images before upload and reduce costs
Medium · Programming

Chapters (7)

Intro
0:53 Building an image generation app
2:10 Creating a TorchServe app
3:02 Inference method for image generation
3:55 Deploying the app
5:08 Testing the app
5:58 Conclusion
Up next
Short.ai Review 2025: Turn Long Videos into Viral Shorts in One Click! 🔥
DroidCrunch
Watch →