I have a JSON file named region_descriptions.json available in this link. This file is not loading properly in notepad++ in my windows(Since it is a huge file). The file is partially loading in google chrome. This file is a dataset for my dense-captioning task and I need to write a python script to translate every "phrase" in it to hindi for my purpose.
I navigated in power shell to the directory where my json file is there and then setup the environment variable using:
>>$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Preeti\Downloads\Compressed\region_descriptions.json"
After this I tried to open jupyter notebook in the same directory and run the code:
import ijson
from google.cloud import translate
translate_client = translate.Client()
parser = ijson.items(open("region_descriptions.json"), "item.regions.item")
maxTranslations = 100;
for region in parser:
translation = translate_client.translate(region["phrase"], target_language="hi")
print(region["phrase"])
print(translation['translatedText'])
maxTranslations-=1
if maxTranslations==0:
break
but the jupyter notebook is giving me an error that:
AttributeError Traceback (most recent call last)
<ipython-input-1-5fa13c6f3710> in <module>
2 from google.cloud import translate
3
----> 4 translate_client = translate.Client()
5
6 parser = ijson.items(open("region_descriptions.json"), "item.regions.item")
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\cloud\translate_v2\client.py in __init__(self, target_language, credentials, _http, client_info)
75 ):
76 self.target_language = target_language
---> 77 super(Client, self).__init__(credentials=credentials, _http=_http)
78 self._connection = Connection(self, client_info=client_info)
79
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\cloud\client.py in __init__(self, credentials, _http)
128 raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
129 if credentials is None and _http is None:
--> 130 credentials, _ = google.auth.default()
131 self._credentials = google.auth.credentials.with_scopes_if_required(
132 credentials, self.SCOPE
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in default(scopes, request)
303
304 for checker in checkers:
--> 305 credentials, project_id = checker()
306 if credentials is not None:
307 credentials = with_scopes_if_required(credentials, scopes)
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in _get_explicit_environ_credentials()
163 if explicit_file is not None:
164 credentials, project_id = _load_credentials_from_file(
--> 165 os.environ[environment_vars.CREDENTIALS])
166
167 return credentials, project_id
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in _load_credentials_from_file(filename)
100 # The type key should indicate that the file is either a service account
101 # credentials file or an authorized user credentials file.
--> 102 credential_type = info.get('type')
103
104 if credential_type == _AUTHORIZED_USER_TYPE:
AttributeError: 'list' object has no attribute 'get'
Can someone please help me to write a python script to translate all the phrases in the json file to hindi or help me overcome my error? I would strongly recommend to download the json file from the link given for better understanding of what "phrase" I am referring to.