尝试将流式应用程序部署到 Heroku 时出现 AttributeError
Posted
技术标签:
【中文标题】尝试将流式应用程序部署到 Heroku 时出现 AttributeError【英文标题】:AttributeError when trying to deploy streamlit app to Heroku 【发布时间】:2021-01-19 22:10:41 【问题描述】:我有一个简单的流线型应用程序,其中包括存储为用于预测的泡菜文件的转换 + 估计器。当我部署到本地主机时,该应用程序运行良好。部署到 Heroku 时,Web 布局可以工作,但预测应用程序会生成错误“AttributeError: 'ColumnTransformer' object has no attribute '_feature_names_in'。 我使用了下面的 requirements.txt: “numpy==1.17.2 pandas==0.25.1 streamlit==0.67.1 Pillow==7.2.0 scikit_learn==0.23.2”
由 pipreqs 生成。
从已发布的类似问题的答案中,我推测这可能是由于 sklearn 版本无法使用。但不知道如何纠正。
Heruko 的错误信息如下:
AttributeError:“ColumnTransformer”对象没有“__feature_names_in”
Here is the code for app.py:
import pandas as pd
import numpy as np
import pickle
import streamlit as st
from PIL import Image
#from sklearn.preprocessing import OneHotEncoder
from sklearn.base import BaseEstimator, TransformerMixin
#from sklearn.impute import SimpleImputer
#from sklearn.pipeline import Pipeline
#from sklearn.preprocessing import MinMaxScaler
#from sklearn.compose import ColumnTransformer
import warnings
warnings.filterwarnings('ignore')
acc_ix, wt_ix, hpower_ix, cyl_ix = 4, 3, 2, 0
##custom class inheriting the BaseEstimator and TransformerMixin
class CustomAttrAdder(BaseEstimator, TransformerMixin):
def __init__(self, acc_and_power=True):
self.acc_and_power = acc_and_power # new optional variable
def fit(self, X, y=None):
return self # nothing else to do
def transform(self, X):
wt_and_cyl = X[:, wt_ix] * X[:, cyl_ix] # required new variable
if self.acc_and_power:
acc_and_power = X[:, acc_ix] * X[:, hpower_ix]
return np.c_[X, acc_and_power, wt_and_cyl] # returns a 2D array
return np.c_[X, wt_and_cyl]
def predict_mpg_web1(config,regressor):
if type(config)==dict:
df=pd.DataFrame(config)
else:
df=config
# Note the model is in the form of pipeline_m, including both transforms and the estimator
# The config is with Origin already in country code
y_pred=regressor.predict(df)
return y_pred
# this is the main function in which we define our webpage
def main():
# giving the webpage a title
#st.title("MPG Prediction")
st.write("""
# MPG Prediction App
based on a Random Forest Model built from
"http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
""")
# here we define some of the front end elements of the web page like
# the font and background color, the padding and the text to be displayed
html_temp = """
<div style ="background-color:yellow;padding:13px">
<h1 style ="color:black;text-align:center;">What is the mpg of my car? </h1>
</div>
"""
# this line allows us to display the front end aspects we have
# defined in the above code
st.markdown(html_temp, unsafe_allow_html = True)
# the following lines create dropdowns and nueemric sliders in which the user can enter
# the data required to make the prediction
st.sidebar.header('Set My Car Configurations')
Orig = st.sidebar.selectbox("Select Car Origin",("India", "USA", "Germany"))
Cyl = st.sidebar.slider('Cylinders', 3, 6, 8)
Disp = st.sidebar.slider('Displacement', 68.0, 455.0, 193.0)
Power = st.sidebar.slider('Horsepower', 46.0, 230.0, 104.0)
WT = st.sidebar.slider(' Weight', 1613.0, 5140.0, 2970.0)
Acc = st.sidebar.slider('Acceleration', 8.0, 25.0, 15.57)
MY = st.sidebar.slider('Model_Year', 70, 82, 76)
image = Image.open('car.jpg')
st.image(image, caption='MPG Prediction',
use_column_width=True)
st.subheader("Click the 'Predict' button below")
# loading the saved model
pickle_in = open('final_model.pkl', 'rb')
regressor=pickle.load(pickle_in)
result =""
# the below line ensures that when the button called 'Predict' is clicked,
# the prediction function defined above is called to make the prediction
# and store it in the variable result
# Set up the Vehicale configurations
vehicle="Origin": [Orig], "Cylinders": [Cyl], "Displacement": Disp, "Horsepower": [Power],
"Weight":[WT], "Acceelation": [Acc], "Model Year": [MY]
if st.button("Predict"):
result = predict_mpg_web1(vehicle, regressor)
mpg=int(result[0])
st.success('The prediction is '.format(mpg))
if __name__=='__main__':
main()
【问题讨论】:
【参考方案1】:您是否尝试使用尚未安装的ColumnTransformer
调用predict
?
_feature_names_in
属性在 fit_transform
调用中设置。我有相同的sklearn版本并且该方法存在,所以恕我直言,版本应该不是问题
【讨论】:
感谢您回答我的问题。存储为 pickle 文件的管道是管道安装了训练数据后的结果。此管道和其他文件:app.py、Procfile 和 Requirments.txt 已通过运行在本地主机中进行了测试:streamlit run app.py 并且它使用 Predict 生成的正确值的文件。【参考方案2】:我解决了这个问题。事实证明,保存模型的泡菜文件不知何故损坏了。我重新生成了模型并且部署工作正常。 感谢任何花时间审查我的问题的人。 阿波罗。
【讨论】:
以上是关于尝试将流式应用程序部署到 Heroku 时出现 AttributeError的主要内容,如果未能解决你的问题,请参考以下文章
部署到 heroku 时出现错误“无法加载资源 404”bundle.js
尝试将 AWS S3 数据库备份到 heroku postgres 时出现 403 错误
在 Heroku 上部署 ktor 应用程序时出现 JDBC_DATABASE_URL 错误