How to use google cloud natural language api using python?

352 Views Asked by At

When I run code for google cloud natural language using python on local system , code is execute successfully , but When I run the same code on some network restrictions(may be office network), it show me error. I already created google service account and enabled google cloud language API.

Error: Contiue show like this E0430 10:49:06.368000000 7700 src/core/tsi/ssl_transport_security.cc:1504] Handshake failed with fatal error SSL_ERROR_SSL: error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED.

Code :

from google.cloud import language_v1
client = language_v1.LanguageServiceClient.from_service_account_json("json file path")
text = "Hello, world!"
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
sentiment = client.analyze_sentiment(request={"document": document}).document_sentiment
print("Text: {}".format(text))
print("Sentiment: {}, {}".format(sentiment.score, sentiment.magnitude))

When I force to stop execution it show like this error message :

sentiment = client.analyze_sentiment(request={"document": document}).document_sentiment File "google_sentiment_analysis\sent_venv\lib\site-packages\google\cloud\language_v1\services\language_service\client.py", line 477, in analyze_sentiment response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) File "google_sentiment_analysis\sent_venv\lib\site-packages\google\api_core\gapic_v1\method.py", line 154, in call return wrapped_func(*args, **kwargs) File "google_sentiment_analysis\sent_venv\lib\site-packages\google\api_core\retry.py", line 283, in retry_wrapped_func return retry_target( File "google_sentiment_analysis\sent_venv\lib\site-packages\google\api_core\retry.py", line 205, in retry_target raise exceptions.RetryError( google.api_core.exceptions.RetryError: Deadline of 600.0s exceeded while calling target function, last exception: 503 failed to connect to all addresses

Please tell me what type of permission is required in google cloud or anything else? Thanks.

1

There are 1 best solutions below

1
On

Just a typo. This:

client = language_v1.LanguageServiceClient.from_service_account_json("json file path")

Should be this:

client = language_v1.LanguageServiceClient().from_service_account_json("json file path")