Spaces:
Running
Running
File size: 1,983 Bytes
da8e881 6bd388c da8e881 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
from google.cloud import translate
import yaml
class GoogleAuthTranslation:
def __init__(self, project_id, yaml_path='lang.yaml'):
self.translator = translate.TranslationServiceClient()
self.location = "global"
self.parent = f"projects/{project_id}/locations/{self.location}"
with open(yaml_path) as f:
self.supporting_languages = yaml.load(f, Loader=yaml.FullLoader)
def _detect(self, query):
response = self.translator.detect_language(
request={
"parent": self.parent,
"content": query,
"mime_type": "text/plain", # mime types: text/plain, text/html
}
)
for language in response.languages:
# First language is the most confident one
return language.language_code
def _get_dest_from_lang(self, lang):
try:
return self.supporting_languages[lang]['google_dest']
except KeyError as e:
raise e
def _get_lang_from_dest(self, dest):
for key in self.supporting_languages:
if self.supporting_languages[key]['google_dest'] == dest:
return key
raise RuntimeError(f"Detected langauge is not supported in our multilingual TTS. |\n Code: {dest} | See https://cloud.google.com/translate/docs/languages")
def translate(self, query, lang):
dest = self._get_dest_from_lang(lang)
response = self.translator.translate_text(
request={
"parent": self.parent,
"contents": [query],
"mime_type": "text/plain", # mime types: text/plain, text/html
"target_language_code": dest,
}
)
return " ".join([translation.translated_text for translation in response.translations])
def detect(self, query):
dest = self._detect(query)
return self._get_lang_from_dest(dest)
|