如何检查Google Translation API的字符数? [英] How to check character count for Google Translation API?
问题描述
我正在使用以下代码使用Google翻译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']))
我如何跟踪剩余或到目前为止使用了多少个字符?我有100万个免费字符.
How can I keep track as to how many characters a remaining or have been used till now? I have 1 million free characters.
推荐答案
即使我不认为有直接请求此类信息的方法(除了控制台),还是有一种在内部创建警报策略的方法.
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.
您可以设置针对一定数量的请求字节触发的警报策略,并应用1个char = 8位= 1个字节.
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!
这篇关于如何检查Google Translation API的字符数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!