5
votes

I want to use google cloud natural language API from local python code. Because of project constraints I can not run my code on GCP Platform. I have google cloud account and credits to enable and use the API. Does google allow to use the API to run on local platforms. Any sample code will be helpful.

3
You can call the API from just about anywhere. Here are some examples to try: cloud.google.com/natural-language/docs/samples - John Hanley

3 Answers

3
votes

1.Create or select a project.

gcloud projects create nat-lan-api
gcloud config set project nat-lan-api

2.Enable billing.

gcloud alpha billing projects link  nat-lan-api  --billing-account XXXXXX-XXXXXX-XXXXXX

3.Enable the Google Natural Language API for that project.

gcloud services enable  language.googleapis.com

3.Create a service account.

gcloud iam service-accounts create natural-language-api  --description "natural-language-api"  --display-name "natural-language-api"
gcloud iam service-accounts list

4.Download a private key as JSON.

gcloud iam service-accounts keys create key.json   --iam-account [email protected] 

5.Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

export GOOGLE_APPLICATION_CREDENTIALS="/Users/user/folder/key.json"

6.Install the client library.

pip install --upgrade google-cloud-language

7.Analyze some text.

cat natural.py
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

# Instantiates a client
client = language.LanguageServiceClient()

# The text to analyze
text = u'Hello, world!'
document = types.Document(
    content=text,
    type=enums.Document.Type.PLAIN_TEXT)

# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment

print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

8.Test.

python natural.py 
#Text: Hello, world!
#Sentiment: 0.30000001192092896, 0.30000001192092896
2
votes

Yes, Google allow to use the API from your local platforms. The steps is as follows

  1. You need to create a service account with the proper permission.
  2. Create a private key of that service account and keep it in your local machine.
  3. Using that private key generate the jwt token from jwt.io site.
  4. Use that jwt to call the access token API to get the access token.
  5. Use the access token call the language processing API.

I have tried for google DB migration API using Java technology. You can refer my code.

https://github.com/itssanjib/google-cloud-poc/tree/master/gcp-db-migration-poc

Please let me know, if any assistance is required.

1
votes

You can use the Natural Language client libraries to call the API from Python:

https://cloud.google.com/natural-language/docs/quickstart-client-libraries

Since you won't calling the API from Google Cloud Platform, you'll need to create a service account and use it to authenticate.