Here’s a step-by-step guide to deploying your Gradio application on Google Cloud Platform (GCP) using Cloud Run, including all necessary steps, configurations, and commands.
- A Google Cloud account.
- Google Cloud SDK installed (if deploying from local).
- Docker installed (if building the image locally).
- Your Gradio app code ready.
-
Create a New Project:
- Go to the Google Cloud Console.
- Click on the project dropdown at the top and select New Project.
- Enter a project name and click Create.
-
Set the Project ID:
- After creating the project, take note of your Project ID. You’ll use it in commands.
-
Enable Billing:
- Ensure that billing is enabled for your project. You can set this up in the Billing section of the Cloud Console.
-
Enable Required APIs:
- Go to the APIs & Services > Library.
- Enable the Cloud Run API and the Cloud Build API.
-
Organize Your Files:
- Structure your application with the following files:
/app ├── gradio-app.py └── requirements.txt Dockerfile
- Structure your application with the following files:
-
Create
requirements.txt
:- List all necessary Python packages in
requirements.txt
, including Gradio and any other dependencies.
- List all necessary Python packages in
-
Create Dockerfile:
- Use the following Dockerfile:
# Use an official Python runtime as a parent image FROM python:3.10-slim # Set the working directory in the container WORKDIR /app # Install system dependencies required by OpenCV and other libraries RUN apt-get update && apt-get install -y \ libgl1-mesa-glx \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender1 # Upgrade pip and setuptools to avoid dependency issues RUN pip install --upgrade pip setuptools # Copy requirements.txt first COPY requirements.txt /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Copy the application code incrementally COPY app/ /app # Expose the port defined by the PORT environment variable EXPOSE 8080 # Define environment variable for Gradio app ENV GRADIO_SERVER_NAME="0.0.0.0" # Set the PORT environment variable to 8080 (default for Cloud Run) ENV PORT=8080 # Run the application CMD ["python", "gradio-app.py"]
- Use the following Dockerfile:
-
Open Cloud Shell (or your terminal if you have the SDK installed):
- Click the Activate Cloud Shell button in the Google Cloud Console or open your terminal.
-
Authenticate with GCP (if using local terminal):
gcloud auth login
-
Set the Project:
gcloud config set project YOUR_PROJECT_ID
-
Build the Docker Image:
- If using Cloud Shell, you can use the built-in Docker, or if using local Docker:
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/gradio-app
- This command builds the Docker image and uploads it to Google Container Registry.
-
Deploy to Cloud Run:
gcloud run deploy gradio-app \ --image gcr.io/YOUR_PROJECT_ID/gradio-app \ --platform managed \ --region YOUR_REGION \ --allow-unauthenticated
- Replace
YOUR_REGION
with the preferred region (e.g.,us-central1
).
- Replace
-
Confirm Deployment:
- Once the deployment is complete, you’ll see a URL where your Gradio app is hosted.
- Open the URL provided in the deployment confirmation to access your Gradio app.
- To avoid incurring charges, remember to delete your Cloud Run service and any other associated resources when you’re done testing.
- Navigate to Cloud Run in the Google Cloud Console.
- Select your service and click Delete.
- Memory and Scaling: Adjust the memory limit and scaling settings during deployment if your app needs more resources.
- Environment Variables: Set any environment variables your app requires using the
--set-env-vars
flag during deployment.
By following these steps, you should be able to successfully deploy your Gradio application on Google Cloud Platform using Cloud Run. If you encounter any issues, consult the GCP documentation for further assistance.