Conversion_Kitchen_Code/kitchen_counter/conversion/subtitling/translation_resources.py

208 lines
6.6 KiB
Python
Raw Permalink Normal View History

2024-04-27 09:33:09 +00:00
from pprint import pprint
# from swagger_client.rest import ApiException
# 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
# import logging
# from logger import get_module_logger
# log = get_module_logger(__name__)
# google
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/mnfidea/project/MNF/conversion/subtitling/gifted-mountain-318504-0a5f94cda0c8.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):
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,
"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
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)
# print(response)
# print("hello")
translated_text = json.loads(response.text)['text'][0]
return translated_text
def checker(text, src_lang, tar_lang):
try:
print("google", google(text, src_lang, tar_lang))
except:
pass
try:
print("azure", azure(text, tar_lang))
except:
pass
try:
print("aws", aws(text, src_lang, 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
try:
print("ibm", ibm_watson(text, src_lang, tar_lang))
except:
pass
# log.info('Finished')
#text = "Jestem najbogatszym człowiekiem w tym mieście"
text = "Do saal se hum log humare call letter ka wait kar rahe."
text = "saal"
text = "teen"
#text = "दो साल से हम लोग हमारे कॉल लेटर का वेट कर रहे"
#text = "Durante dos años hemos estado esperando nuestra carta de llamada."
# checker(text,'pl','en')
# t = google(text, 'es', 'hi')
# print("this is t", t)
# print(google(text, 'en', 'hi'))
# print("azure", azure(text, 'en')) #work both
# print("aws", aws(text, 'en', 'hi')) # work both
# print("lingvanex", lingvanex(text, 'mr', 'gu'))
# print("lingvanex", lingvanex(text, 'ja', 'gu')) # work both but gives different result
# print("yandex", yandex(text, 'bg', 'gu')) #work both
# print("ibm_watson", ibm_watson(text, 'hi', 'gu')) #hi-gu does not work and single works
#print("ibm_watson", ibm_watson(text, 'en', 'gu'))
#text = "I am lokesh."
#print("google", google(text, 'mr', 'gu'))
# print("azure", azure(text, 'en'))
# print("aws", aws(text, 'en', 'hi'))
#print("lingvanex", lingvanex(text, 'mr', 'gu'))
#print("lingvanex", lingvanex(text, 'en', 'gu'))
#print("yandex", yandex(text, 'en', 'hi'))
# print("ibm_watson", ibm_watson(text, 'hi', 'gu'))
# print("ibm_watson", ibm_watson(text, 'en', 'gu'))
#print(aws(text, 'en', 'gu'))
# text = google(text, 'en', 'hi')
#text = "ഞാൻ കഴിക്കുന്നു."
#print("google", google(text, 'ml', 'en'))
# #text = aws(text, 'en', 'hi')
# #print(text)
# print("aws", aws(text, 'hi', 'en'))
# print("azure", azure(text, 'en'))
# print("lingvanex", lingvanex(text, 'hi', 'en'))
# print("ibm_watson", ibm_watson(text, 'hi', 'en'))
# print("yandex", yandex(text, 'hi', 'en'))