使用 numpy c-extensions 在 gcloud 上部署 ML 模型失败
deploying an ML model on gcloud with numpy c-extensions failed
我正在尝试在线部署一个简单的 Machine 学习模型,以便其他人可以轻松地在线访问它。我试图将它部署在 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')
这是我的requirement.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.
有人愿意帮忙吗?非常感谢。
您必须在 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"
我正在尝试在线部署一个简单的 Machine 学习模型,以便其他人可以轻松地在线访问它。我试图将它部署在 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')
这是我的requirement.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.
有人愿意帮忙吗?非常感谢。
您必须在 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"