在具有numpy c-extensions的gcloud上部署ML模型失败

我正在尝试在线部署一个简单的机器学习模型,以便其他人可以轻松地在线访问它。我尝试将其本地部署在LocalHost上,并且运行良好。所以现在,我正尝试使用gcloud将其部署为Web应用程序。

我成功地遵循了 https://www.freecodecamp.org/news/how-to-build-a-web-application-using-flask-and-deploy-it-to-the-cloud-3551c985e492/,尽管它不是ML模型。

这是我的项目目录中的视图! (请点击查看)(https://drive.google.com/open?id=1AvbZ4ERRsiS19exGPgOm8LfVjk_GWjM8

我使用的是Mac,所以我的python是2.7,但是当我使用Jupyter Notebook时,我也使用python 3.7。我主要通过Anaconda在Notebook上开发东西。

这是main.py:

import numpy as np
from flask import flask,request,jsonify,render_template
import pickle

app = flask(__name__)
model = pickle.load(open('model.pkl','rb'))

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

@app.route('/predict',methods=['POST'])
def predict():

    int_features = [int(x) for x in request.form.values()]
    final_features = [np.array(int_features)]
    prediction = model.predict(final_features)

    output = round(prediction[0],2)

    return render_template('index.html',prediction_text='The Forecast is  {}'.format(output))

@app.route('/results',methods=['POST'])
def results():

    data = request.get_json(force=True)
    prediction = model.predict([np.array(list(data.values()))])

    output = prediction[0]
    return jsonify(output)

if __name__ == "__main__":
    app.run(debug=True)

这是我的app.yml:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /static
  static_dir: static
- url: /.*
  script: main.app

libraries:
  - name: ssl
    version: latest

我的appengine_conengine.py:

from google.appengine.ext import vendor
vendor.add('lib')

这是我的要求。txt

flask
Werkzeug
numpy
sklearn

然后我运行这个:

pip install -t lib -r requirements.txt

将4个必需的库放在名为“ lib”的文件夹中。之所以这样做,是因为当我在Virtualenv中测试运行main.py时,它需要flask,numpy和sklearn才能成功部署在Localhost:5000上。

但是,当我跑步时:

gcloud app deploy 

上传我的项目并将其部署到gcloud,它显示如下错误:

1,in <module>
    import numpy as np
  File "/base/data/home/apps/n~sales-forecast-3mv3/20191110t154452.422348864415547477/lib/numpy/__init__.py",line 142,in <module>
    from . import core
  File "/base/data/home/apps/n~sales-forecast-3mv3/20191110t154452.422348864415547477/lib/numpy/core/__init__.py",line 47,in <module>
    raise ImportError(msg)
ImportError: 

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that,then:
  1. Check that you expected to use Python2.7 from "/base/alloc/tmpfs/dynamic_runtimes/python27g/79cfdbb680326abd/python27/python27_dist/python",and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.17.3" you're trying to use.
  2. If (1) looks fine,you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source,your compiler versions and ideally a build log

- If you're working with a numpy git repository,try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

有人可以帮忙吗?非常感谢。

hf2baobei 回答:在具有numpy c-extensions的gcloud上部署ML模型失败

您必须在app.yaml中指定numpy库,如下所示:

app.yaml

runtime: python27
api_version: 1
threadsafe: true

handlers:
  - url: /static
    static_dir: static
  - url: /.*
    script: main.app

libraries:
  - name: ssl
    version: latest
  - name: numpy
    version: "1.6.1"
本文链接:https://www.f2er.com/3129832.html

大家都在问