首页 > 解决方案 > 在本地运行烧瓶与在 AWS Elastic BeanStalk 上运行它 - 一些范围问题?

问题描述

以下代码在我的本地计算机上运行良好:

import tensorflow as tf
import tensorflow_hub as hub
import flask
from flask import request, jsonify

def get_embed_func():
    module_url = "https://tfhub.dev/google/universal-sentence-encoder/2"
    with tf.Graph().as_default():
        sentences = tf.placeholder(tf.string)
        embed = hub.Module(module_url)
        embeddings = embed(sentences)
        session = tf.train.MonitoredSession()
    return lambda x: session.run(embeddings, {sentences: x})


application = flask.Flask(__name__)

@application.route('/', methods=['GET'])
def home():
    return '''<h1>Some header</h1>
<p>A prototype API</p>'''


@application.route('/api/v1', methods=['GET'])
def encoding():
    return jsonify(str(embed_func(['Sample sentence'])[0][0]))


if __name__ == '__main__':
    embed_func = get_embed_func()  
    application.run()

但是在 AWS Elastic BeanStalk 上它会产生错误:

[Mon May 13 20:10:55.391791 2019] [:error] [pid 3387] WARNING: Logging before flag parsing goes to stderr.
[Mon May 13 20:10:55.391931 2019] [:error] [pid 3387] W0513 20:10:55.391691 140563746686720 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
[Mon May 13 20:11:41.111770 2019] [:error] [pid 3387] E0513 20:11:41.108649 140563780257536 app.py:1761] Exception on /api/v1 [GET]
[Mon May 13 20:11:41.111801 2019] [:error] [pid 3387] Traceback (most recent call last):
[Mon May 13 20:11:41.111804 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
[Mon May 13 20:11:41.111807 2019] [:error] [pid 3387]     response = self.full_dispatch_request()
[Mon May 13 20:11:41.111810 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
[Mon May 13 20:11:41.111836 2019] [:error] [pid 3387]     rv = self.handle_user_exception(e)
[Mon May 13 20:11:41.111839 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
[Mon May 13 20:11:41.111842 2019] [:error] [pid 3387]     reraise(exc_type, exc_value, tb)
[Mon May 13 20:11:41.111844 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/_compat.py", line 35, in reraise
[Mon May 13 20:11:41.111847 2019] [:error] [pid 3387]     raise value
[Mon May 13 20:11:41.111859 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
[Mon May 13 20:11:41.111862 2019] [:error] [pid 3387]     rv = self.dispatch_request()
[Mon May 13 20:11:41.111864 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
[Mon May 13 20:11:41.111866 2019] [:error] [pid 3387]     return self.view_functions[rule.endpoint](**req.view_args)
[Mon May 13 20:11:41.111869 2019] [:error] [pid 3387]   File "/opt/python/current/app/application.py", line 28, in endoding
[Mon May 13 20:11:41.111871 2019] [:error] [pid 3387]     return jsonify(str(embed_func(['Sample sentence'])[0][0]))
[Mon May 13 20:11:41.111875 2019] [:error] [pid 3387] NameError: name 'embed_func' is not defined
[Mon May 13 20:11:41.111883 2019] [:error] [pid 3387] 

基本上,以下行要么未执行,要么在 flask scope 中不可见embed_func = get_embed_func()。为什么本地执行和 AWS 之间存在差异?有什么解决办法吗?

标签: pythonamazon-web-servicesscopeamazon-elastic-beanstalk

解决方案


没有调用 main 方法,这就是您收到此错误的原因。有没有理由def encoding()使用embed_func变量而不是get_embed_func()直接引用方法?

如果您将其更改为以下内容,您可能会克服此错误:

@application.route('/api/v1', methods=['GET'])
def encoding():
    return jsonify(str(get_embed_func(['Sample sentence'])[0][0]))

推荐阅读