from pprint import pprint from conversion.translation.swagger_client.rest import ApiException from conversion.translation 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 from google.cloud import translate import os import requests import uuid import json from MNF.settings import BasePath basePath = BasePath() # import logging # from logger import get_module_logger # log = get_module_logger(__name__) # google os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = rf"{basePath}/conversion/My First Project-2573112d5326.json" translate_client = Translate.Client() client = translate.TranslationServiceClient() project_id = 'authentic-bongo-272808' location = "global" parent = f"projects/{project_id}/locations/{location}" # ibm authenticator = IAMAuthenticator( 'K7Qkaj3YtTSeBq1Z-LlH6MwDZIIKadvjHPHU28JynoIq') 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/f3ec0635-1e7d-4461-8c09-e13db386a564') # aws translate_aws = boto3.client( service_name='translate', region_name='us-east-2', use_ssl=True) # azure subscription_key = "83ce6233419541929f7ab0d3035fca58" 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 # configuration = swagger_client.Configuration() # configuration.api_key['Authorization'] = 'a_eRp1RPOP8hg3id8HYlYrlRCSwxxHR4c506NXiHtMcxIcM5GHdHfnhJn3Ea2kkSO87GTiC8qEts4ImUmN' # api_instance = swagger_client.TranslateApi( # swagger_client.ApiClient(configuration)) # yandex url = "https://translate.yandex.net/api/v1.5/tr.json/translate" #log.info('Logger started working') 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() # log.debug('Asad') return translation['translations'][0]['translation'] def google(text, source_lang, target_lang): print("in google") print("text", text) print("source_lang", source_lang) print("target_lang", target_lang) result = translate_client.translate( text, source_language=source_lang, target_language=target_lang) # log.debug('Lokesh') # print(result) return result['translatedText'] def aws(text, source_lang, target_lang): result = translate_aws.translate_text( Text=text, SourceLanguageCode=source_lang, TargetLanguageCode=target_lang) return result.get('TranslatedText') 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() # print(response) translated_text = response[0]['translations'][0]['text'] return translated_text # def lingvanex(text, source_lang, target_lang): # source_lang = source_lang+"_"+source_lang.upper() # target_lang = target_lang+"_"+target_lang.upper() # di = { # "from": source_lang, # "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 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 def checker(text, src_lang, tar_lang): try: print("ibm", ibm_watson(text, src_lang, tar_lang)) except: pass try: print("google", google(text, src_lang, tar_lang)) except: pass try: print("aws", aws(text, src_lang, tar_lang)) except: pass try: print("azure", azure(text, tar_lang)) except: pass try: print("lingvanex", lingvanex(text, src_lang, tar_lang)) except: pass try: print("yandex", yandex(text, src_lang, tar_lang)) except: pass # log.info('Finished') text = "Amazing to see everything alright." #checker(text, 'en', 'el') # checker(text,'or','pa') # checker(text,'or','zh') # checker(text,'or','fr') # checker(text,'or','ne') # checker(text,'or','es') # checker(text,'or','id') # print(google(text,'en','or')) #print(aws(text, 'or', 'en'))