我正在尝试无服务器部署API ......并且在设置阶段陷入困境。
我正在尝试从zappa init
运行cmd
,但我收到了一个奇怪的错误。
该命令在我第一次运行时实际工作(我在最后一步取消了,意识到我想先设置我的S3存储桶)。
当我再次尝试时,在安装和配置awscli之后,然后将flask安装到我的虚拟环境中,我收到以下错误:
Your Zappa deployments will need to be uploaded to a private S3 bucket.
If you don't have a bucket yet, we'll create one for you too.
What do you want call your bucket? (default 'zappa-hohr3kxh7'):
It looks like this is a Flask application.
Oh no! An error occurred! :(
==============
Traceback (most recent call last):
File "c:\users\joshu\documents\code\machine-learning\boston-housing-price\app\env\venv\lib\site-packages\zappa\cli.py", line 2610, in handle
sys.exit(cli.handle())
File "c:\users\joshu\documents\code\machine-learning\boston-housing-price\app\env\venv\lib\site-packages\zappa\cli.py", line 476, in handle
self.init()
File "c:\users\joshu\documents\code\machine-learning\boston-housing-price\app\env\venv\lib\site-packages\zappa\cli.py", line 1604, in init
matches = detect_flask_apps()
File "c:\users\joshu\documents\code\machine-learning\boston-housing-price\app\env\venv\lib\site-packages\zappa\utilities.py", line 143, in detect_flask_apps
lines = f.readlines()
File "c:\users\joshu\documents\code\machine-learning\boston-housing-price\app\env\venv\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 301: character maps to <undefined>
我正在运行Windows 10,Python 3.6 Anaconda发行版。
我的大多数代码都在api.py文件中,除了有一个index.html文件。这是api.py代码:
from flask import Flask, render_template, request, jsonify
from sklearn.externals import joblib
import boto3
import numpy as np
import pickle
app = Flask(__name__)
BUCKET_NAME = 'ml-boston-housing'
MODEL_FNAME = 'model.pkl'
S3 = boto3.client('s3', region_name='us-east-1')
# memoized annotation, caches model file after it's pulled
def memoize(f):
memo = {}
def helper(x):
if x not in memo:
memo[x] = f(x)
return memo[x]
return helper
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def make_prediction():
if request.method == 'POST':
app.logger.info("{} request received from: {}".format(
request.method, request.remote_addr))
mdl = load_model(MODEL_FNAME)
input_data = np.zeros((1, 13))
if request.json or 'data' in request.json:
for i, k in enumerate(request.get_json()):
input_data[0, i] = request.form.get(k, 0)
result = np.squeeze(mdl.predict(input_data))
return jsonify({'result': result})
else:
for i, k in enumerate(request.form.keys()):
input_data[0, i] = request.form.get(k, 0)
result = np.squeeze(mdl.predict(input_data))
return render_template('index.html', label=result)
@memoize
def load_model(key):
response = S3.get_object(Bucket=BUCKET_NAME, Key=key)
modelstr = response['Body'].read()
model = pickle.load(modelstr)
return model
if __name__ == '__main__':
app.run(host='0.0.0.0')
答案 0 :(得分:1)
有点想通了,当Zappa init
运行时,它会在 utilities.py 文件中调用def detect_flask_apps()
函数。此函数读取项目目录中的每个python文件,包括虚拟环境文件夹中的库。
有一个名为functools.py的库必须具有与默认编码不同的字符编码,因为
with open(full, 'r') as f:
lines = f.readlines()
阅读~/env/venv/Lib/functools.py
时失败。
修复是指定编码:
with open(full, 'r', encoding='utf-8') as f:
lines = f.readlines()
zappa/utilities.py -- line 148
中的