Record while it’s hot, for future self > record while it’s hot, for future self
0 – preface
Gradio for those of you in the field of artificial intelligence, let’s take a look at what Gradio is about.
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, in short, is about giving the models Gradio engineers trained in AI algorithms the ability to share with the masses.
Split from the technical side, it consists of three parts:
Front-end page + back-end interface + AI algorithm model reasoning
One thing Gradio has done is encapsulate these three parts into a Python interface that users can implement to expose their trained algorithmatic models to the masses as Web services.
1 – A simple Gradio procedure
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"). The launch (server_name = "0.0.0.0 server_port = 7000)Copy the code
It can be seen that gr.interface ().lanuch() combines front-end page, back-end service and AI algorithm model into one Interface, greatly reducing the difficulty of algorithm model landing, enabling AI algorithm engineers to rapidly deploy front-end and back-end and provide services without engineering ability.
2 – Deploy via Kubernetes
Gradio App can run directly on a physical machine in Python, but for easier portability and engineering management, Gradio App needs to be packaged as a Docker image and managed by Kubernetes.
The first is packaged as a Docker image:
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-compressed-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 # if PYTHONUNBUFFERED=1 # https://juejin.cn/post/7052286381724270600 ADD SRC. / SRC # hypothesis app. Py etc need to files in the SRC directory WORKDIR SRC ENTRYPOINT [" python ", "app.py" ]Copy the code
packaging
Docker build. -t gradio-demo:v0.0.1Copy the code
Next, configure the Kubernetes YAML file: 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; Gradio-demo name: gradio-demo namespace: openmmLab spec: rules: -host: app.xxx.com - backend: service: name: gradio-demo port: number: 7600 pathType: Prefix path: /gradio-demo(/|$)(.*)Copy the code
Start the
kubectl apply -f gradio-demo.yaml
Copy the code
Finally, after the startup is successful, you can directly access it through the domain name app.xxx.com/gradio-demo.
~~~~~~~~~~~~ advertising time ~~~~~~~~~~~~
Here’s our team of algorithms using gradio to write mmgen-Facestylor,
Welcome to use: app.openmmlab.com/facestylor/
Welcome to star: github.com/open-mmlab/…
The above.