forked from 0Xiaohei0/VoiceToJapanese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
89 lines (81 loc) · 3.44 KB
/
translator.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import traceback
from transformers import pipeline
import requests
import json
import pysbd
use_deepl = False
deepl_api_key = ''
fugu_translator = None
def initialize():
global fugu_translator
fugu_translator = pipeline(
'translation', model='./models--staka--fugumt-en-ja/snapshots/2d6da1c7352386e12ddd46ce3d0bbb2310200fcc')
def translate(text, from_code, to_code):
if (use_deepl):
DEEPL_TOKEN = deepl_api_key
# Send text to translation service
print(f"Calling deepl with apikey: {DEEPL_TOKEN}")
headers = {
'Authorization': f'DeepL-Auth-Key {DEEPL_TOKEN}',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = f'text={text}&target_lang={to_code.upper()}'
translationResponse = requests.post(
'https://api-free.deepl.com/v2/translate', headers=headers, data=data.encode('utf-8'))
# print(translationResponse.content)
try:
responseJSON = json.loads(
translationResponse.content.decode('utf-8'))
if "translations" in responseJSON:
text_output = responseJSON['translations'][0]['text']
return text_output
except:
print("Could not load json response from deepl.")
print(f'Response content: {translationResponse}')
print(f'Response content: {translationResponse.content}')
print(traceback.format_exc())
return ''
else:
if (from_code == 'en' and to_code == 'ja'):
global fugu_translator
if (fugu_translator == None):
fugu_translator = pipeline(
'translation', model='./models--staka--fugumt-en-ja/snapshots/2d6da1c7352386e12ddd46ce3d0bbb2310200fcc')
seg_en = pysbd.Segmenter(language="en", clean=False)
data = fugu_translator(seg_en.segment(text))
concatenated_text = ''.join(
item['translation_text'] for item in data)
return concatenated_text
else:
print(
f"no avaliable model to translate from{from_code} to {to_code}")
return ''
# DEEPL_TOKEN = os.environ.get("translation-service-api-token")
# # Send text to translation service
# headers = {
# 'Authorization': f'DeepL-Auth-Key {DEEPL_TOKEN}',
# 'Content-Type': 'application/x-www-form-urlencoded',
# }
# data = f'text={text}&target_lang={to_code.upper()}'
# translationResponse = requests.post(
# 'https://api-free.deepl.com/v2/translate', headers=headers, data=data.encode('utf-8'))
# # print(translationResponse.content)
# responseJSON = json.loads(
# translationResponse.content.decode('utf-8'))
# if "translations" in responseJSON:
# text_output = responseJSON['translations'][0]['text']
# return text_output
# # Download and install Argos Translate package
# argostranslate.package.update_package_index()
# available_packages = argostranslate.package.get_available_packages()
# package_to_install = next(
# filter(
# lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
# )
# )
# argostranslate.package.install_from_path(package_to_install.download())
# # print(f'{from_code}, {to_code}')
# # Translate
# translatedText = argostranslate.translate.translate(
# text, from_code, to_code)
# return translatedText