Search code examples
pythongoogle-cloud-platformtextnlpgoogle-translation-api

Google Translate Python package not working after X calls?


so I am translating almost 1755 English sentences (short ones each are less than 10 words). The code below works fine.

Problem faced: however, after translating almost 500 rows (sentences) in my data frame, it stops translating (without getting an error), and 'newLanguage' is the same as the original sentence. (I tried Italian 'it' instead of Arabic also faced the same problem).

Do I have a limit to the # of API calls to translate? Any ideas how to fix this ?

!pip install googletrans==3.1.0a0


from googletrans import Translator

translator = Translator()
backTrans_sentences=[]
backTrans_labels=[]

for sentence,label in zip(df_en_train['Sentence'],df_en_train['Labels']):
        newLanguage= translator.translate(text=sentence, dest='ar').text
        eng=translator.translate(text=newLanguage, dest='en').text
        backTrans_sentences.append(eng)
        backTrans_labels.append(label)
   

Solution

  • For each GCP API there's a limit to a number requests per minute. In this case this is a rate quota that applies:

    Rate quotas are typically used for limiting the number of requests you can make to an API or service. Rate quotas reset after a time interval that is specific to the service—for example, the number of API requests per day.

    In your case translation is handled by a translate.googleapis.com API.

    Go to your Quotas page and there you can see the numbers.

    You can also view the quotas for the API with the gcloud:

    gcloud alpha services quota list \
        --service=translate.googleapis.com \
        --consumer=projects/your-project-name
    

    However since this feature is in alpha it may not work as expected.

    You can also reqest higher quotas in your translate API quota page.

    To see which cap you're hitting go to Cloud Monitoring and follow the instructions described in the documentation.

    When you establish which quota you need to raise just edit the value and click submit request button.