I tried to use Google Cloud Translate (advanced) API but failed...
It keeps showing me the error message. "Error: 403 Cloud IAM permission 'cloudtranslate.generalModels.predict' denied."
Here is my code. Please look over it and tell me the way to resolve the issue... ;(
I granted the permission on my account. (owner, API key manager, Cloud Translation API Manager)
But still doesn't work properly.
from google.cloud import translate_v3 as translate
from openpyxl import load_workbook
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
'./static/myauth-xxxxxx.json')
project_id = "myid"
source_language_code = "fr"
target_language_code = "en"
excel_file_path = "testfile/test2.xlsx"
client = translate.TranslationServiceClient()
location = "us-central1"
parent = f"projects/{project_id}/locations/{location}"
def translate_cell(text):
"""Translates a single cell of text."""
request = {
"parent": parent,
"contents": text,
"source_language_code" : source_language_code,
"target_language_code": target_language_code,
}
try:
response = client.translate_text(request=request)
# Print a message
print(f"Excel file translated to {target_language_code}.")
return response.translations[0].translated_text
except Exception as e:
print(f"Error translating cell: {text}. Error: {e}")
return text # Return original text in case of error
# Load the Excel workbook
workbook = load_workbook(excel_file_path)
# Iterate through sheets and cells
for sheet in workbook.worksheets:
for row in sheet.iter_rows(min_row=2):
for cell in row:
if cell.value:
original_text = cell.value
translated_text = translate_cell(original_text)
cell.value = translated_text
I searched on google and granted the Cloud Translation API Admin permission on my account but still...
[1]: https://i.stack.imgur.com/pDqYt.png