使用 Flask 在 gcloud 应用程序中加载大文件

Loading a big file in a gcloud application with Flask

我在使用 scikit 学习模型的 Google 应用引擎上上传了一个 Flask 应用程序。我希望 pickle 文件只加载一次,但我不知道在哪里以及如何导入它。

我尝试在应用程序脚本的最开始导入模型,但随后网站出现错误。

main.py :


clf = joblib.load(open("static/troll_model.pkl", "rb"))

application = Flask(__name__)

@application.route("/")
def home():
    return render_template('index.html')

@application.route('/predict', methods=['POST'])
def predict():
    if request.method == "POST":
        message = request.form['message']
        data = [message]
        pred = clf.predict(data)

    return render_template("result.html", prediction=pred)

app.yaml :

runtime: python37
entrypoint: gunicorn -b :$PORT main:application

handlers:
- url: /static
  static_dir: static

- url : /.*
  script: auto

目前,每次客户询问答案时都会导入模型,它可以工作,但应用程序变得非常滞后。我只想导入一次文件。

如果使用flex gcloud app engine环境就可以正常工作。 flex 环境允许使用本地文件(这里是 scikit learn 模型),而 strict 环境则不允许。