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.
To use Google Cloud APIs in your applications, you first need to have a Google account. This allows you to use Google developer products, including Google Cloud console, gcloud CLI, Cloud Logging, and Cloud Monitoring.
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
Yes, Google allow to use the API from your local platforms. The steps is as follows
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With