from pprint import pprint # from conversion.translation.swagger_client.rest import ApiException from requests.exceptions import SSLError # import swagger_client import time import boto3 # from ibm_cloud_sdk_core.authenticators import IAMAuthenticator # from ibm_watson import LanguageTranslatorV3 from google.cloud import translate_v2 as Translate import os import requests import uuid import json from MNF.settings import BasePath basePath = BasePath() # google api creds and setup os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = rf"{basePath}/MNF/json_keys/authentication.json" translate_client = Translate.Client() # ibm api creds and setup # authenticator = IAMAuthenticator( # 'xy2fnVsvzYpzYbWOkhmlNksuWNnp_wUylTtW4Sdf-w-5') # language_translator = LanguageTranslatorV3( # version='2018-05-01', authenticator=authenticator) # language_translator.set_service_url( # 'https://api.eu-gb.language-translator.watson.cloud.ibm.com/instances/0141e54b-1733-4ff0-8290-005d13a4fa7d') # aws api creds and setup aws_json_path = basePath + "/MNF/json_keys" with open(rf"{aws_json_path}/keys_aws.json") as f: keys1 = json.load(f) session = boto3.Session(aws_access_key_id=keys1["aws_access_key_id"], aws_secret_access_key=keys1["aws_secret_access_key"], region_name=keys1["region_name"]) translate_aws = session.client( service_name='translate', region_name='us-east-2', use_ssl=True) # azure api creds and setup subscription_key = "959354878e73458e898a69f1f5887b69" endpoint = "https://api.cognitive.microsofttranslator.com" location = "eastus" path = '/translate' constructed_url = endpoint + path headers = { 'Ocp-Apim-Subscription-Key': subscription_key, 'Ocp-Apim-Subscription-Region': location, 'Content-type': 'application/json', 'X-ClientTraceId': str(uuid.uuid4()) } constructed_url1 = "https://api.cognitive.microsofttranslator.com/transliterate?api-version=3.0" # lingvanex api creds and setup # configuration = swagger_client.Configuration() # configuration.api_key['Authorization'] = \ # 'a_7KrNVLqseF2tNd8ewbXl74DF01n0g5fyt6SzPTk3p8wlHTaIMglzVmgUVLtgeCMHJ7MDMpKyFVtEspUf' # api_instance = swagger_client.TranslateApi( # swagger_client.ApiClient(configuration)) # yandex api creds and setup url = "https://translate.yandex.net/api/v1.5/tr.json/translate" # -> IBM Deprecated on 10 June 2024 so removed it # -> Directly Usable IBM api for translation # def ibm_watson(text, source_lang, target_lang): # model_id = source_lang+"-"+target_lang # translation = language_translator.translate( # text=text, model_id=model_id).get_result() # return translation['translations'][0]['translation'] def google_simple(text, source_lang, target_lang): try: result = translate_client.translate( text, source_language=source_lang, target_language=target_lang) return result['translatedText'] except SSLError: return google(text, source_lang, target_lang) except: print("The text unable to translate is", text) return text # -> Directly Usable GOOGLE api for translation def google(text, source_lang, target_lang): try: result = translate_client.translate( text, source_language=source_lang, target_language=target_lang) return result['translatedText'] except SSLError: return google(text, source_lang, target_lang) except: print("The text unable to translate is", text) return text # -> Directly Usable AWS api for translation def aws(text, source_lang, target_lang): result = translate_aws.translate_text( Text=text, SourceLanguageCode=source_lang, TargetLanguageCode=target_lang) return result.get('TranslatedText') # -> Directly Usable AZURE api for translation def azure(text, target_lang): params = {'api-version': '3.0', 'to': target_lang} body = [{'text': text}] request = requests.post( constructed_url, params=params, headers=headers, json=body) response = request.json() translated_text = response[0]['translations'][0]['text'] return translated_text # -> Directly Usable LINGVANEX api for translation # def lingvanex(text, source_lang, target_lang): # source_lang = source_lang+"_"+source_lang.upper() # target_lang = target_lang+"_"+target_lang.upper() # di = { # "from": '', # "to": target_lang, # "data": text, # "platform": "api"} # # api_response = api_instance.translate_post(request=di) # # translated_text = api_response.to_dict()['result'] # return translated_text # -> Directly Usable YANDEX api for translation def yandex(text, source_lang, target_lang): model_id = source_lang+"-"+target_lang data = {"key": "trnsl.1.1.20210129T100550Z.952c8e23466372bc.951c193191010d638334db2f2f095040b6e92843", "text": text, "lang": model_id} response = requests.post(url=url, data=data) translated_text = json.loads(response.text)['text'][0] return translated_text # -> Testing all Translation Resources if they all are working or not def checker(text, src_lang, tar_lang): # try: # print("ibm", ibm_watson(text, src_lang, tar_lang)) # except: # print("IBM TRANSLATION IS NOT WORKING!!!!!!!!!!!! PLEASE CHECK IN TRANSLATION_RESOURCES.PY IN CONVERSION") # pass start_time = time.time() try: print("google", google(text, src_lang, tar_lang)) except: print("GOOGLE TRANSLATION IS NOT WORKING!!!!!!!!!!!! PLEASE CHECK IN TRANSLATION_RESOURCES.PY IN CONVERSION") pass end_time = time.time() print("Time took is ->", end_time-start_time) start_time = time.time() try: print("aws", aws(text, src_lang, tar_lang)) except: print("AWS TRANSLATION IS NOT WORKING!!!!!!!!!!!! PLEASE CHECK IN TRANSLATION_RESOURCES.PY IN CONVERSION") pass end_time = time.time() print("Time took is ->", end_time - start_time) start_time = time.time() try: print("azure", azure(text, tar_lang)) except Exception as e: print("AZURE TRANSLATION IS NOT WORKING!!!!!!!!!!!! PLEASE CHECK IN TRANSLATION_RESOURCES.PY IN CONVERSION") print("Reason is ->", e) pass end_time = time.time() print("Time took is ->", end_time - start_time) start_time = time.time() # try: # print("lingvanex", lingvanex(text, src_lang, tar_lang)) # except: # print("LINGVANEX TRANSLATION IS NOT WORKING!!!!!!!!!!!! PLEASE CHECK IN TRANSLATION_RESOURCES.PY IN CONVERSION") # pass # end_time = time.time() # print("Time took is ->", end_time - start_time) # start_time = time.time() # try: # print("yandex", yandex(text, src_lang, tar_lang)) # except: # print("YANDEX TRANSLATION IS NOT WORKING!!!!!!!!!!!! PLEASE CHECK IN TRANSLATION_RESOURCES.PY IN CONVERSION") # pass # end_time = time.time() # print("Time took is ->", end_time - start_time) # print("Prince was Here!!!") # text = "Amazing to see everything alright." # checker(text, 'en', 'el')