我已经构建了一个烧瓶api,并将代码保存在一台名为app.py
的文件中。下面是python文件的内容。
import os
from flask import jsonify
from flask import request
from flask import Flask
from pycorenlp import StanfordCoreNLP
import logging
nlp = StanfordCoreNLP('http://localhost:9000')
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
def coref_derive(sentence):
#coreferences = nlp.annotate(sentence, properties={'annotators':'tokenize,ssplit,pos,lemma,ner,parse,coref','outputFormat': 'json','ner.useSUTime':'false'})['corefs']
coreferences = StanfordCoreNLP('http://localhost:9000').annotate(sentence, properties={'annotators':'tokenize,ssplit,pos,lemma,ner,parse,coref','outputFormat': 'json','ner.useSUTime':'false'})
return coreferences
@app.route('/sentiment',methods=['POST'])
def sentiment():
text = request.args.get('text')
mydict = coref_derive(text)
return jsonify(mydict)
if __name__ == "__main__":
# Run locally
app.run(debug = True)
我通过转到保存app.py
文件的文件夹来启动API。并输入python app.py
。
当我尝试通过以下代码向此API发送请求时
import requests
sentence="When planning our return trip to Kauai, husband and I were happy to read that all the renovations to the Koloa Landing Resort were completed. We then went ahead and booked a deluxe studio for mid-October. Upon our check in after a long flight and dark rainy drive from the airport, we were greeted warmly at the front desk. While we are platinum status, we weren't expecting a room upgrade, but much to our delight, was advised by the clerk that we received one. However, it wasn't until we got to our room, or should I say villa, how nice the upgrade was. It was a one bedroom, 1 1/2 bath, almost 1000 square feet corner villa with 2 full walls of windows in the lovely living room alone. The villa was beautiful. Our view of the smaller family pool and soccer field was so nice. Really enjoyed being in this area as it's quieter than the large main pool. This property is amazing - everything is well maintained and all of the staff that we interacted with were very nice."
requests.post('''http://localhost:5000/sentiment?text='''+sentence).text
我得到以下错误
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\azimul.haque\Downloads\flask\app_corefer.py", line 137, in sentiment
mydict = coref_derive(text)
File "C:\Users\azimul.haque\Downloads\flask\app_corefer.py", line 47, in coref_derive
coreferences = StanfordCoreNLP('http://localhost:9000').annotate(sentence, properties={'annotators':'tokenize,ssplit,pos,lemma,ner,parse,coref','outputFormat': 'json','ner.useSUTime':'false'})
File "C:\Python27\lib\site-packages\pycorenlp\corenlp.py", line 11, in annotate
assert isinstance(text, str)
AssertionError
对此有何帮助?