【问题标题】:File not loading after deploying on Heroku. Runs fine on localhost and heroku local web在 Heroku 上部署后文件未加载。在 localhost 和 heroku 本地网络上运行良好
【发布时间】:2019-05-03 20:32:38
【问题描述】:

我使用 Flask 制作了一个 API,它使用预训练的 Scikit-learn 模型提供预测。该 API 在本地主机和 Heroku 本地 Web 上运行良好,但在我部署它时无法加载模型。 API 以 JSON 的形式返回预测。 {"prediction":123} 在本地运行时返回。部署时返回{"error":"Failed to load model"}

应用架构:

 - houseprediction.py
 - waitressServer.py
 - requirements.txt
 - Procfile
 - supp-files:
    - gbModel.pkl 
    - model_columns.pkl
    - sectorLabels.pkl

houseprediction.py:

from flask import Flask, jsonify, request
from sklearn.externals import joblib
import pandas as pd
import os

app = Flask(__name__)
@app.route('/predict', methods=['POST'])

def predict():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    try:
        gbModel = joblib.load("{}\\supp-files\\gbModel.pkl".format(dir_path))
    except:
        return jsonify({'error': 'Failed to load model'})
    try:
        model_columns = joblib.load("{}\\supp-files\\model_columns.pkl".format(dir_path))
    except:
        return jsonify({'error': 'Failed to load model columns'})
    try:
        lbl = joblib.load("{}\\supp-files\\sectorLabels.pkl".format(dir_path))
    except:
        return jsonify({'error': 'Failed to load sector labels'})
    json_ = request.get_json()
    query_df = pd.DataFrame(json_, index=[0])
    pd.options.display.max_columns = 50
    query_df['sector'] = lbl.transform([query_df['sector']])[0]
    print(query_df.dtypes)
    query = pd.get_dummies(query_df)
    for col in model_columns:
         if col not in query.columns:
              query[col] = 0
    print(query_df.shape)
    print(query_df.head(1))
    prediction = gbModel.predict(query)
    print(prediction)
    return jsonify({'prediction': prediction[0]})

@app.route('/')
def home():
    return "Welcome to House Prediction"

女服务员.py:

from waitress import serve
import os
import housepredictionServer     
serve(housepredictionServer.app, port=os.environ['PORT'])

过程文件:web: python waitressServer.py

如果还需要什么,请告诉我。

【问题讨论】:

  • 将所有路径更改为supp-files/{file}.pk1
  • @SmartManoj 哇哦,好用。我一直坚持这一点,我从来没有想过服务器主要使用基于 unix 的系统。谢谢。

标签: python heroku flask


【解决方案1】:

将所有路径更改为 supp-files/{file}.pk1,因为 heroku 是基于 unix 的系统

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 2014-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 2012-09-26
    • 2013-09-22
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多