如何将机器学习模型部署到生产环境?

译文 精选
人工智能 机器学习
本文介绍了如何将模型从开发环境部署到生产环境。

译者 | 布加迪

审校 | 重楼

开发机器学习模型只完成了一半工作。除非部署到生产环境、提供业务价值,否则模型仍然毫无用处

知道如何部署自己的模型已成为任何数据科学家的一项基本技能,许多雇主已经要求我们能做到这一点。因此,对于任何级别的数据科学家来说,学习如何将模型部署到生产环境大有助益

本文探讨如何将机器学习模型部署到生产环境中。

机器学习模型准备

首先准备部署到生产环境中的模型。我们为整个教程设置虚拟环境。可以通过在终端中使用以下代码来实现这一点。

python -m venv myvirtualenv

在安装并激活虚拟环境之后,需要安装所需的软件包。创建requirements.txt文件,并用下面的库列表填充它。

pandas
scikit-learn
fastapi
pydantic
uvicorn
streamlit

requirements.txt准备就绪之后,我们必须使用以下代码安装它们。

pip install -r requirements.txt

一切准备就绪,我们将开始开发机器学习模型。在本教程中,我们将使用来自Kaggle糖尿病数据。把数据放在数据文件夹中。

然后,在app文件夹中创建一个名为train_model.py的文件。在train_model.py中,我们将使用下面的代码训练机器学习模型。

import pandas as pd
import joblib
from sklearn.linear_model import LogisticRegression

data = pd.read_csv("data\\diabetes.csv")
X = data.drop('Outcome', axis =1)
y = data['Outcome']
model = LogisticRegression()

model.fit(X, y)
joblib.dump(model, 'models\\logreg_model.joblib')

可以根据自己的喜好更改数据集的位置和模型路径。我将把模型放入模型的文件夹中。

我们将跳过所有的数据准备和模型评估,因为本文的目是将模型部署到生产环境中。当模型准备就绪后,我们将准备部署模型

模型部署

在本节中,我们将为模型预测创建API,并使用Docker部署它们,同时使用Streamlit前端测试它们。

首先确保已经安装了Docker桌面,我们将在本地测试它。

接下来,在app文件夹中创建一个名为main.py的文件,并用以下代码填充该文件以生成API。

from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd

# Load the logistic regression model
model = joblib.load('../models/logreg_model.joblib')

# Define the input data model
class DiabetesData(BaseModel):
 Pregnancies: int
 Glucose: int
 BloodPressure: int
 SkinThickness: int
 Insulin: int
 BMI: float
 DiabetesPedigreeFunction: float
 Age: int
app = FastAPI()

# Define prediction endpoint
@app.post("/predict")
def predict(data: DiabetesData):
 input_data = {
 'Pregnancies': [data.Pregnancies],
 'Glucose': [data.Glucose],
 'BloodPressure': [data.BloodPressure],
 'SkinThickness': [data.SkinThickness],
 'Insulin': [data.Insulin],
 'BMI': [data.BMI],
 'DiabetesPedigreeFunction': [data.DiabetesPedigreeFunction],
 'Age': [data.Age]
 }
 input_df = pd.DataFrame(input_data)

 # Make a prediction
 prediction = model.predict(input_df)
 result = "Diabetes" if prediction[0] == 1 else "Not Diabetes"
 return {"prediction": result}

此外,我们有一个前端web来试一试我们部署的API模型。为此,在app文件夹中创建一个名为frontend.py的文件。然后,用以下代码填充它们。

import streamlit as st
import requests
import json

API_URL = "http://localhost:8000/predict"

st.title("Diabetes Prediction App")
st.write("Enter the details below to make a prediction.")

pregnancies = st.number_input("Pregnancies", min_value=0, step=1)
glucose = st.number_input("Glucose", min_value=0, step=1)
blood_pressure = st.number_input("Blood Pressure", min_value=0, step=1)
skin_thickness = st.number_input("Skin Thickness", min_value=0, step=1)
insulin = st.number_input("Insulin", min_value=0, step=1)
bmi = st.number_input("BMI", min_value=0.0, step=0.1)
diabetes_pedigree_function = st.number_input("Diabetes Pedigree Function", min_value=0.0, step=0.1)
age = st.number_input("Age", min_value=0, step=1)

if st.button("Predict"):
 input_data = {
 "Pregnancies": pregnancies,
 "Glucose": glucose,
 "BloodPressure": blood_pressure,
 "SkinThickness": skin_thickness,
 "Insulin": insulin,
 "BMI": bmi,
 "DiabetesPedigreeFunction": diabetes_pedigree_function,
 "Age": age
 }

 response = requests.post(API_URL, data=json.dumps(input_data), headers={"Content-Type": "application/json"})

 if response.status_code == 200:
 prediction = response.json().get("prediction", "No prediction")
 st.success(f"Prediction: {prediction}")
 else:
 st.error("Error in making prediction. Please check your input data and try again.")

当一切准备就绪后,我们将创建Docker文件作为模型部署的基础。应该在文件中填写下面的代码。

FROM python:3.9-slim

WORKDIR /app

COPY app /app
COPY models /models

RUN pip install --no-cache-dir --upgrade pip && \
 pip install --no-cache-dir -r requirements.txt

EXPOSE 8000 8501

CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port 8000 & streamlit run frontend.py --server.port=8501 --server.enableCORS=false"]

我们将创建Docker文件已准备就绪的映像,然后通过容器部署模型。为此,在终端中运行以下代码来构建映像。

docker build -t diabetes-prediction-app .

上面的代码为我们的模型容器创建了Docker映像。然后,我们将使用以下代码为模型部署制作API。

docker run -d -p 8000:8000 -p 8501:8501 --name diabetes-prediction-container diabetes-prediction-app

一切准备就绪后,确保容器运行并使用下面的地址访问前端。

http://localhost:8501/

应该会看到如下图所示的前端。

如果一切顺利,恭喜刚刚将机器学习模型部署到生产环境中。

结论

在本文中,我们介绍了使用FastAPI和Docker将模型部署到生产环境中的简单方法。

当然,从维护模型和监测生产环境中模型的过程中,仍然有很多东西需要学习。但愿本文有所帮助!

原文标题:A Guide to Deploying Machine Learning Models to Production,作者:Cornellius Yudha Wijaya

责任编辑:华轩 来源: 51CTO
相关推荐

2020-11-10 08:00:00

机器学习技术科学

2018-01-08 09:09:46

机器学习模型NET

2019-08-08 08:00:00

深度学习机器学习神经网络

2015-03-18 10:29:01

私有云混合云云API

2019-10-23 08:00:00

Flask机器学习人工智能

2021-01-25 09:00:00

机器学习人工智能算法

2018-06-23 13:55:15

Apache SparPython数据

2020-06-10 07:46:39

机器学习预测性维护工业物联网

2024-09-09 11:45:15

ONNX部署模型

2024-02-20 15:17:35

机器学习模型部署

2012-08-17 11:04:57

IBMdW

2011-05-04 09:29:22

2023-02-07 16:36:34

机器学习Docker无服务器

2023-04-28 08:00:00

机器学习数据集

2021-05-12 08:00:00

深度学习人工智能设备

2010-08-11 15:35:47

Flex DataGr

2024-10-12 08:00:00

机器学习Docker

2022-09-07 08:00:00

机器学习MLFlow工具

2017-04-05 13:24:35

互联网

2021-12-09 09:53:00

人工智能机器学习网络安全
点赞
收藏

51CTO技术栈公众号