Conversion_Kitchen_Code/kitchen_counter/conversion/translation/testing.py

132 lines
4.9 KiB
Python
Raw Normal View History

2024-04-27 09:33:09 +00:00
from google.cloud import translate_v2 as Translate
import os
import time
import sys
# from .MNF.settings import BasePath
basePath = "/home/user/mnf/project/MNF"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = rf"{basePath}/MNF/json_keys/authentication.json"
translate_client = Translate.Client()
#from translation_resources import google
def google(text, source_lang, target_lang):
try:
start = time.time()
for _ in range(10):
result = translate_client.translate(
text, source_language=source_lang, target_language=target_lang)
print("intermediate time:",time.time()-start)
end = time.time()
print("Time Elapsed:", end-start)
# log.debug('Lokesh')
# print(result)
return result['translatedText']
except Exception as e:
print("The text unable to translate is",e)
return text
text1 = "my name is dharmesh"
text2 = "Newspapers are the mirror of the world. It plays an important role in modern civilization. Newspapers are published in different languages in our country. It helps us to gain common sense. Provides reviews and puzzles on newspaper reports, speeches, business, movies, sports, etc. Newspapers are read for both profit and pleasure. It brings to the public valuable perspectives, acts of injustice, oppression and maladministration. It angered the people and criticized the authorities for their failures. It forms public opinion. Therefore freedom of the press should not be hampered.It forms public opinion. Therefore freedom of the press should not be hampered"
# print("->------------------",google("मेरा नाम धर्मेश है","hi","fr"))
# print(translate_client.get_languages())
# print(translate_client.get_languages(target_language="hi"))
import requests
import uuid
# azure api creds and setup
s2 = "dde2e184b0c34628b4856df73f44787c"
subscription_key = "83ce6233419541929f7ab0d3035fca58"
endpoint = "https://api.cognitive.microsofttranslator.com"
location = "eastus"
path = '/translate'
constructed_url = endpoint + path
headers = {
'Ocp-Apim-Subscription-Key': s2,
'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"
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)
print("AZure 1t")
print(request)
response = request.json()
print("AZure ouptpt")
print(response)
translated_text = response[0]['translations'][0]['text']
print(translated_text)
return translated_text
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson import LanguageTranslatorV3
import json
# 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')
# language_translator.set_disable_ssl_verification(True)
# languages = language_translator.list_languages().get_result()
# print(json.dumps(languages, indent=2))
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()
print(translation)
return translation['translations'][0]['translation']
# with open('f1.txt', 'w') as f:
# sys.stdout = f
# print("->------------------",google(text1,"en","hi"))
print("----------->",azure(text1, "hi"))
# print("----->",ibm_watson(text1, "en", "hi"))
# Transliteration
text = "API"
subscription_key = "881a5a21d1634ae3b54978d9f1bae27a"
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"
# print("source_script", source_script)
# print("dest_script", dest_script)
# print("source_lang", source_lang)
params = {'api-version': '3.0', 'language': "bn", 'fromScript': "Latn", 'toScript': "Beng"}
body = [{'text': text}]
request = requests.post(constructed_url1, params=params, headers=headers, json=body)
response = request.json()
print(response)
try:
out = response[0]['text']
print(out)
except Exception as e:
print("The error was ", e)
out = text
print(out)