DEV Community

JH5
JH5

Posted on • Originally published at Medium

在 Cloud Run 上部署Gradio 應用

在 Cloud Run 上部署Gradio 應用

Cloud Run 介紹的第一篇,最近在用Cloud Run部署一些手邊的玩具,陸續再把note更新上來,Cloud Run 是一個全托管的運算平台,可讓您執行透過 HTTP 請求觸發的Stateless容器,主要是基於開源專案 Knative 建構,因為也支援Container 部署,可以讓您的應用程式具有高度可攜性,身為一個懶惰的錢端工程師,Cloud Run 會幫你處理所有基礎架構的管理,讓我們能專心的寫Code,且初期不需要花太多心力與金錢在deploy上。

Gradio 則是一個優秀的 Python 函式庫,可以幫您的機器學習模型或任意 Python 函式建立簡單易用的 Web UI,特別是這兩年LLM模型需要介面來串接測試,它也提供了各式現成的template可以直接應用,還支援share link 可以隨時丟給其他人一起測試。

接下來我們會利用一個簡單的 “Hello World” Gradio 應用程式,並部署到 Cloud Run來認識一下相關的流程。

在開始後面的步驟前,要先利用 gcloud auth listgcloud config list project 來確認您的帳號與專案 ID 已正確設定。

啟用必要的 API

為了讓 Cloud Run 能夠從原始碼建置並部署容器,我們需要啟用幾個關鍵的 API。

gcloud services enable \  
  artifactregistry.googleapis.com \  
  cloudbuild.googleapis.com \  
  run.googleapis.com
Enter fullscreen mode Exit fullscreen mode

撰寫您的 Gradio 應用程式

在 Cloud Shell 中,建立一個新的資料夾並進入該目錄:

mkdir gradio-demo && cd gradio-demo
Enter fullscreen mode Exit fullscreen mode

建立 requirements.txt 檔案並加入 gradio

gradio==4.44.1  
gunicorn==22.0.0  
uvicorn==0.30.3
Enter fullscreen mode Exit fullscreen mode

建立一個 Dockerfile,並加入下面的內容

# This one is a good choice for Python applications.  
FROM python:3.10-slim  

# Install system-level dependencies required by Gradio and its components.  
# This includes ffmpeg for audio processing.  
RUN apt-get update && apt-get install -y --no-install-recommends ffmpeg && rm -rf /var/lib/apt/lists/*  

# Set the working directory in the container  
WORKDIR /app  

# Copy the requirements file into the container  
COPY requirements.txt .  

# Install the Python dependencies  
# Using --no-cache-dir is a good practice for smaller image sizes  
RUN pip install --no-cache-dir -r requirements.txt  

# Copy the rest of the application code  
COPY . .  

# Set the environment variable for the port as expected by Cloud Run  
# Cloud Run will automatically provide this variable.  
ENV PORT 8080  

# Expose the port. This is good practice for documentation.  
CMD gunicorn -w 1 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:${PORT} app:demo
Enter fullscreen mode Exit fullscreen mode

建立一個名為 app.py 的檔案,並加入以下的gradio範例:

import gradio as gr  
import os  

def hello(name):  
    """根據輸入的名字回傳一個友善的問候語。"""  
    return "Hello " + name  

# 建立 Gradio 介面  
demo = gr.Interface(  
    fn=hello,  
    inputs=[gr.Textbox(label="Your Name")],  
    outputs=["text"],  
    title="Hello World ",  
    description=(  
        "在下方輸入您的名字並點擊 'Submit',"  
    ),  
    theme="soft",  
)  

# 啟動應用程式  
# 在 Cloud Run 中,Gradio 會自動偵測環境並使用正確的埠號  
# 為了本地測試,我們也可以明確指定  
if __name__ == "__main__":  
    port = int(os.environ.get("PORT", 8080))  
    demo.launch(server_name="0.0.0.0", server_port=port, share=True)
Enter fullscreen mode Exit fullscreen mode

這段程式碼有一個簡單的函式 hello,並使用 gradio.Interface 將其包裝成一個 Web UI,裡面定義了介面上的component與參考設定。

利用Gradio Share link測試應用程式

建立並啟用虛擬環境:

uv venv source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

安裝相關套件:

uv pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

啟動應用程式:

python app.py  

# 會看到類似以下的輸出:  
# Running on local URL:  http://0.0.0.0:8080  
# Running on public URL: https://xxxxxxx9fdf5240f6.gradio.live  
# This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)
Enter fullscreen mode Exit fullscreen mode

部署到 Cloud Run

萬事俱備,只欠東風!現在我們可以用一個指令將應用程式部署到 Cloud Run。

設定部署區域與專案:

REGION=asia-east1
Enter fullscreen mode Exit fullscreen mode

執行部署指令:

gcloud run deploy gradio-demo --source . --region $REGION --allow-unauthenticated --clear-base-image
Enter fullscreen mode Exit fullscreen mode

指令參數:

  • --source .: 指示 Cloud Build 使用當前目錄的原始碼進行建置。

  • --region $REGION: 指定部署的區域,這邊我們預設佈在台灣。

  • --allow-unauthenticated: 允許公開存取您的服務,不建議在正式環境使用,關於Cloud Run的存取權限後面再用其他篇幅來談。

通常部署過程會自動建置容器映像檔並將其推送到 Artifact Registry,然後在 Cloud Run 上啟動服務,整個過程大概需要幾分鐘,等到部署成功後,指令列會輸出您的服務 URL,類似下面這樣:

Building using Dockerfile and deploying container to Cloud Run service [gradio-demo] in project [xxxxxxx] region [asia-east1]  
OK Building and deploying... Done.                                                                                                                                             
  OK Uploading sources...                                                                                                                                                      
  OK Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds;region=asia-east1/xxxx].                                                                                                                                                                       
  OK Creating Revision...                                                                                                                                                      
  OK Routing traffic...                                                                                                                                                        
  OK Setting IAM Policy...                                                                                                                                                     
Done.                                                                                                                                                                          
Service [gradio-demo] revision [gradio-demo-00001-xxx] has been deployed and is serving 100 percent of traffic.  
Service URL: https://gradio-demo-xxxx.asia-east1.run.app
Enter fullscreen mode Exit fullscreen mode

接著就可以在在瀏覽器中打開這個 URL,便可以看到您部署在雲端的 Gradio 應用程式!

清理資源

為了節省不必要的費用,可以刪除本次建立的相關資源:

# 刪除 Cloud Run 服務  
gcloud run services delete gradio-demo --region $REGION  

# 刪除 Artifact Registry 中為了部署而建立的映像檔  
gcloud artifacts repositories delete cloud-run-source-deploy --location $REGION
Enter fullscreen mode Exit fullscreen mode

如果您想刪除整個專案,可以在 Cloud 控制台的 IAM 與管理 > 設定頁面中進行。

其他的討論就後面的篇幅再來寫XD

#ai#cloud-run#python

Top comments (0)