简体   繁体   English

顶点ai中的自定义容器部署

[英]Custom Container deployment in vertex ai

I am trying to deploy my custom container in vertex ai endpoint for predictions.我正在尝试在顶点 ai 端点中部署我的自定义容器以进行预测。 The contents of the application are as follows.申请书内容如下。

  1. Flask - app.py Flask - 应用程序.py
import pandas as pd
from flask import Flask, jsonify,request
import tensorflow
import pre_process
import post_process


app = Flask(__name__)


@app.route('/predict',methods=['POST'])
def predict():
    req = request.json.get('instances')
    
    input_data = req[0]['email']

    #preprocessing
    text = pre_process.preprocess(input_data)
    vector = pre_process.preprocess_tokenizing(text)

    model = tensorflow.keras.models.load_model('model')

    #predict
    prediction = model.predict(vector)

    #postprocessing
    value = post_process.postprocess(list(prediction[0])) 
    
    return jsonify({'output':{'doc_class':value}})


if __name__=='__main__':
    app.run(host='0.0.0.0')
  1. Dockerfile Dockerfile
FROM python:3.7

WORKDIR /app

COPY . /app

RUN pip install --trusted-host pypi.python.org -r requirements.txt 


CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:app"]

EXPOSE 5050
  1. pre_process.py预处理程序.py
#import 
import pandas as pd
import pickle
import re
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences


def preprocess(text):
    """Do all the Preprocessing as shown above and
    return a tuple contain preprocess_email,preprocess_subject,preprocess_text for that Text_data"""
         
    
    #After you store it in the list, Replace those sentances in original text by space.
    text = re.sub("(Subject:).+"," ",text,re.I)
    
    #Delete all the sentances where sentence starts with "Write to:" or "From:".
    text = re.sub("((Write to:)|(From:)).+","",text,re.I)
    
    #Delete all the tags like "< anyword >"
    text = re.sub("<[^><]+>","",text)
    
    #Delete all the data which are present in the brackets.
    text = re.sub("\([^()]+\)","",text)
    
    #Remove all the newlines('\n'), tabs('\t'), "-", "".
    text = re.sub("[\n\t\\-]+","",text)
    
    #Remove all the words which ends with ":".
    text = re.sub("(\w+:)","",text)
    
    #Decontractions, replace words like below to full words.

    lines = re.sub(r"n\'t", " not", text)
    lines = re.sub(r"\'re", " are", lines)
    lines = re.sub(r"\'s", " is", lines)
    lines = re.sub(r"\'d", " would", lines)
    lines = re.sub(r"\'ll", " will", lines)
    lines = re.sub(r"\'t", " not", lines)
    lines = re.sub(r"\'ve", " have", lines)
    lines = re.sub(r"\'m", " am", lines)
    text = lines
    
        #replace numbers with spaces
    text = re.sub("\d+"," ",text)
    
        # remove _ from the words starting and/or ending with _
    text = re.sub("(\s_)|(_\s)"," ",text)
    
        #remove 1 or 2 letter word before _
    text = re.sub("\w{1,2}_","",text)
    
        #convert all letters to lowercase and remove the words which are greater 
        #than or equal to 15 or less than or equal to 2.
    text = text.lower()
    
    text =" ".join([i for i in text.split() if len(i)<15 and len(i)>2])
    
    #replace all letters except A-Z,a-z,_ with space
    preprocessed_text = re.sub("\W+"," ",text)

    return preprocessed_text

def preprocess_tokenizing(text):
        
    #from tf.keras.preprocessing.text import Tokenizer
    #from tf.keras.preprocessing.sequence import pad_sequences
    
    tokenizer = pickle.load(open('tokenizer.pkl','rb'))

    max_length = 1019
    tokenizer.fit_on_texts([text])
    encoded_docs = tokenizer.texts_to_sequences([text])
    text_padded = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
    
    return text_padded
  1. post_process.py后处理.py
def postprocess(vector):
    index = vector.index(max(vector))
    classes = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
    return classes[index]
  1. requirements.txt要求.txt
gunicorn
pandas==1.3.3
numpy==1.19.5
flask
flask-cors
h5py==3.1.0
scikit-learn==0.24.2
tensorflow==2.6.0

  1. model model

  2. tokenizer.pkl分词器.pkl

I am following this blog vertex ai deployment for gcloud console commands to containerise and deploy the model to endpoint.But the model is taking forever to get deployed and ultimately fails to get deployed.我正在关注此博客vertex ai deployment for gcloud console 命令以将 model 容器化并部署到端点。但是 model 需要很长时间才能部署并最终无法部署。

After running the container in local host, it runs as expected but it is not getting deployed into vertex ai endpoint.在本地主机中运行容器后,它按预期运行但未部署到顶点 ai 端点。 I don't understand whether the problem is in flask app.py or Dockerfile or whether the problem lies somewhere else.我不明白问题是在 flask app.py 还是 Dockerfile 还是其他地方。

I was able to resolve this issue by adding health route to http server.我能够通过将健康路由添加到 http 服务器来解决此问题。 I added the following piece of code in my flask app.我在我的 flask 应用程序中添加了以下代码。

@app.route('/healthz')
def healthz():
    return "OK"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vertex AI 自定义容器部署 - Vertex AI Custom Container deployment 使用自定义训练容器和 model 服务容器构建 Vertex AI 管道 - Constructing a Vertex AI Pipeline with a custom training container and a model serving container GCP Vertex AI 托管笔记本无法使用自定义容器 - GCP Vertex AI Managed Notebook cannot use custom container 在 Google Cloud Vertex AI 上部署客户处理程序 - Deployment with customer handler on Google Cloud Vertex AI 使用 Vertex AI Pipelines 通过 GPU 运行自定义 Docker 容器 - 如何安装 NVIDIA 驱动程序? - Running custom Docker container with GPU using Vertex AI Pipelines - how to install NVIDIA driver? 我如何从自定义容器中获取 output 并传递到 Vertex AI/Kubeflow 管道中的下一个管道? - How do i get the output from a custom container and pass to next pipeline in Vertex AI/Kubeflow pipeline? memory 中的 Model 服务器容器 - Vertex AI - Model server container out of memory - Vertex AI Vertex AI 管道导入自定义模块 - Vertex AI pipelines import custom modules Google Cloud Platform - 使用自定义数据格式进行 Vertex AI 训练 - Google Cloud Platform - Vertex AI training with custom data format 使用自定义 model 为 Vertex AI 批量预测返回置信度分数 - Return confidence score with custom model for Vertex AI batch predictions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM