2016-08-22 145 views
1

我下面这个例子,并在底部有一些代码 https://cloud.google.com/prediction/docs/developer-guide与谷歌应用程序引擎谷歌Prediction API进行

我使用的,而不是webapp2的烧瓶中,我的代码如下所示:

# [START app] 
import logging 
from oauth2client.appengine import AppAssertionCredentials 
from flask import Flask 
import httplib2, webapp2 
from oauth2client.appengine import AppAssertionCredentials 
from apiclient.discovery import build 

http = AppAssertionCredentials('https://www.googleapis.com/auth/prediction').authorize(httplib2.Http()) 
service = build('prediction', 'v1.6', http=http) 


app = Flask(__name__) 


@app.route('/') 
def hello(): 
    return 'Hello World1!' 

@app.route('/add') 
def something(): 
    class MakePrediction(): 
     def get(self): 
     result = service.hostedmodels().predict(project=PROJECT-NAME, hostedModelName=PROJECT-ID, body={'input' {'csvInstance': ['hello']}}).execute() 
     self.response.headers['Content-Type'] = 'text/plain' 
     self.response.out.write('Result: ' + repr(result)) 


@app.errorhandler(500) 
def server_error(e): 
    # Log the error and stacktrace. 
    logging.exception('An error occurred during a request.') 
    return 'An internal error occurred.', 500 
# [END app] 

我不断收到错误:

File "/Users/morganallen/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle 
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) 
    File "/Users/morganallen/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler 
    handler, path, err = LoadObject(self._handler) 
    File "/Users/morganallen/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject 
    obj = __import__(path[0]) 
    File "/Users/morganallen/Desktop/project/flask_app_engine/main.py", line 24 
    result = service.hostedmodels().predict(project='linear-yen-140912', hostedModelName='language-identifier', body={'input' {'csvInstance': ['hello']}}).execute() 
                                  ^
SyntaxError: invalid syntax 

我在做什么错?

回答

3

你缺少一个冒号上线24,其中小胡萝卜指向在堆栈跟踪:

File "/Users/morganallen/Desktop/project/flask_app_engine/main.py", line 24 
result = service.hostedmodels().predict(project='linear-yen-140912', hostedModelName='language-identifier', body={'input' {'csvInstance': ['hello']}}).execute() 
                                 ^

所以这里的解决办法是改变这样的:

body={'input' {'csvInstance': ['hello']}}).execute() 
      ^

对此:

body={'input' : {'csvInstance': ['hello']}}).execute() 
      ^

这应该解决语法错误。

SyntaxError: invalid syntax 

无论何时出现错误,请不要忽略编译器吐出的所有行。它会经常告诉你问题的确切路线,特别是在像这样的简单语法错误的情况下。