Gradio app 基於 Kubernetes 部署實戰
趁熱記錄下,給未來的自己
0 - 前言
在人工智能領域的小夥伴,應該聽説過 Gradio 吧,在進入本文主題之前,先來了解下 gradio 是什麼,官網的介紹是:
Gradio is the fastest way to demo your machine learning model with a friendly web interface so that anyone can use it, anywhere!
簡而言之,Gradio App 就是給 AI 算法工程師訓練的模型賦予分享給大眾的能力。
從技術側拆分,由三個部分組成:
前端頁面 + 後端接口 + AI算法模型推理
Gradio 做了一件事情,就是將這三個部分封裝到一個 python 接口裏,用户通過實現其封裝的接口,將自己訓練的算法模型以 web 服務的形式展現給大眾使用。
1 - 一個簡單的 gradio 程序
下面這個demo程序來自於 gradio 官網:Gradio
```
app.py
import gradio as gr
def sketch_recognition(img): # Implement sketch recognition model here... # Return labels and confidences as dictionary
iface = gr.Interface(fn=sketch_recognition, inputs="sketchpad", outputs="label").launch(server_name="0.0.0.0", server_port=7000) ```
可以看到 gr.Interface().lanuch() 就是將前端頁面,後端服務以及 AI 算法模型三者結合到一個接口裏,極大的降低了算法模型落地的難度,使得 AI 算法工程師在不具備工程能力的情況下,也能拿快速部署前後端並提供服務。
2 - 通過Kubernetes部署
Gradio app 可以直接在物理機上通過 python 運行,不過為了更易於移植以及工程化管理,我們需要把gradio app 打包成 docker 鏡像,然後交由 kubernetes 進行管理。
首先是 打包成 docker 鏡像:
Dockerfile
ARG PYTORCH="1.6.0"
ARG CUDA="10.1"
ARG CUDNN="7"
FROM pytorch/pytorch:$ {PYTORCH} -cuda$ {CUDA} -cudnn$ {CUDNN} -devel
MAINTAINER arkMon
ENV TORCH_CUDA_ARCH_LIST="6.0 6.1 7.0+PTX"
ENV TORCH_NVCC_FLAGS="-Xfatbin -compress-all"
ENV CMAKE_PREFIX_PATH="(dirname(which conda))/../"
ENV TZ=Asia/Shanghai
ENV DEBIAN_FRONTEND=noninteractive
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ >/etc/timezone
RUN apt-get update && apt-get install -y git && apt-get install -y libgl1-mesa-glx && apt-get -y install libglib2.0-dev
RUN pip install cmake && pip install dlib && pip install wget
RUN pip install gradio
RUN export PYTHONUNBUFFERED=1 # 一定要加,不加可能出現啟動失敗的問題,原因見我另一篇文章: https://juejin.cn/post/7052286381724270600
ADD src ./src # 假設 app.py 等需要的文件在 src 目錄下
WORKDIR src
ENTRYPOINT [ "python", "app.py" ]
打包
docker build . -t gradio-demo:v0.0.1
接着,配置 kubernetes yaml 文件: gradio-demo.yaml
``` apiVersion: apps/v1 kind: Deployment metadata: name: gradio-demo namespace: openmmlab labels: k8s-app: inference-engine spec: selector: matchLabels: name: gradio-demo replicas: 1 template: metadata: labels: name: gradio-demo spec: restartPolicy: Always nodeSelector: workfor: inference hostNetwork: true terminationGracePeriodSeconds: 30 containers: - name: gradio-demo image: gradio-demo:v0.0.1 env: - name: PYTHONUNBUFFERED value: "1" resources: limits: memory: 20000Mi requests: memory: 2000Mi
apiVersion: v1 kind: Service metadata: name: gradio-demo namespace: openmmlab spec: type: ClusterIP selector: name: gradio-demo ports: - port: 7600 targetPort: 7600 protocol: TCP
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: kubernetes.io/ingress.class: "nginx" nginx.ingress.kubernetes.io/rewrite-target: /$2 nginx.ingress.kubernetes.io/configuration-snippet: | rewrite ^(/gradio-demo)$ $1/ redirect; # 一定要加這一句重定向,否則無法通過subpath的方式訪問 name: gradio-demo namespace: openmmlab spec: rules: - host: app.xxx.com http: paths: - backend: service: name: gradio-demo port: number: 7600 pathType: Prefix path: /gradio-demo(/|$)(.*) ```
啟動
kubectl apply -f gradio-demo.yaml
最後,啟動成功後,通過域名 http://app.xxx.com/gradio-demo 直接訪問即可。
~~~~~~~~~~~~ 廣吿時間 ~~~~~~~~~~~~
下面是我們團隊算法同學利用 gradio 寫的 MMGEN-Facestylor,換頭像神器,
歡迎使用: https://app.openmmlab.com/facestylor/
算法已開源,歡迎star:https://github.com/open-mmlab/MMGEN-FaceStylor
以上。