Python Google Cloud Natural Language API returns B

2019-08-21 09:50发布

问题:

I'm working on a project using Python(3.6) and Django(2) in which I need to process all text files from a directory using Google's Natural Language API python's Client. At the moment, there are 30 text files in my folder for testing purpose, the programs perform sentiment_analysis on 28 files but when reached 29th file it returns an error like below:

raise HttpError(resp, content, uri=self.uri)

googleapiclient.errors.HttpError: https://language.googleapis.com/v1beta2/documents:analyzeSentiment?alt=json returned "Bad Gateway">

[19/Dec/2018 10:08:41] "POST / HTTP/1.1" 500 15145

Here's how I'm using the python's client:

def nlp_text_manager(text_path, name):
    text = text_path
    txt = Path(text_path).read_text(encoding='cp1252')
    service = discovery.build('language', 'v1beta2', credentials=credentials)
    service_request = service.documents().analyzeSentiment(
        body={
            'document': {
                'type': 'PLAIN_TEXT',
                'content': txt
            }
        }
    )
    response = service_request.execute()
    return response

The Traceback points out the response = service_request.execute() line inside the nlp_text_manager function.

What can be wrong here? Thanks in advance!