Shawn
Shawn

Reputation: 331

How to check character count for Google Translation API?

I am using the following code to translate using the google translation API

from google.cloud import translate_v2 as translate
translate_client = translate.Client(credentials=credentials)

# if isinstance(text, six.binary_type):
#     text = text.decode('utf-8')

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(
    text, target_language='en')

print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
    result['detectedSourceLanguage']))

How can I keep track as to how many characters a remaining or have been used till now? I have 1 million free characters.

Upvotes: 1

Views: 2483

Answers (2)

Albert Albesa
Albert Albesa

Reputation: 417

Even I do not think there is a direct way of requesting such information (apart from the console), there is a way of creating alerting policies internally.

You can set an alerting policy that is triggered for a certain number of requested bytes and apply 1 char = 8 bit = 1 byte.

In order to do that, you should go Monitoring -> Alerting -> Create New Policy ->

  ·Resource type: Consumed API
  ·Metric: Request sizes 
  ·Filter -> Service = translate.googleapis.com

and configure as much triggers as you like. I hope this finds well!

Upvotes: 3

Shubham Sharma
Shubham Sharma

Reputation: 792

Try storing the value in a file? Every time you translate n characters,

  1. read the count from file
  2. increment count by n
  3. write count back to the file

Upvotes: 1

Related Questions