-
Notifications
You must be signed in to change notification settings - Fork 1
/
call_onemt.py
156 lines (131 loc) · 6.74 KB
/
call_onemt.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from load_onemt import Translator
import sentencepiece as spm
from oneconfig import *
import random
print (SUBWORD_MODEL_PATH)
print (TRANSLATION_MODEL_FOLDER)
print (TRANSLATION_MODEL_PATH)
language_mapping = {
"asm": "asm_Beng",
"awa": "awa_Deva",
"ben": "ben_Beng",
"ban": "ben_Beng",
"bho": "bho_Deva",
"bra": "bra_Deva",
"brx": "brx_Deva",
"doi": "doi_Deva",
"gom": "gom_Deva",
"gon": "gon_Deva",
"guj": "guj_Gujr",
"hin": "hin_Deva",
"hingh": "hingh_Deva",
"hoc": "hoc_Wara",
"kan": "kan_Knda",
"kas_arab": "kas_Arab",
"kas": "kas_Arab",
"kas_deva": "kas_Deva",
"kha": "kha_Latn",
"lus": "lus_Latn",
"mai": "mai_Deva",
"mag": "mag_Deva",
"mal": "mal_Mlym",
"mar": "mar_Deva",
"mni_beng": "mni_Beng",
"mni": "mni_Beng",
"mni_mtei": "mni_Mtei",
"npi": "npi_Deva",
"ory": "ory_Orya",
"ori": "ory_Orya",
"odi": "ory_Orya",
"pan": "pan_Guru",
"pun": "pan_Guru",
"san": "san_Deva",
"sat": "sat_Olck",
"sin": "sin_Sinh",
"snd_arab": "snd_Arab",
"snd_deva": "snd_Deva",
"snd": "snd_Arab",
"tam": "tam_Taml",
"tcy": "tcy_Knda",
"tel": "tel_Telu",
"urd_arab": "urd_Arab",
"urd": "urd_Arab",
"eng": "eng_Latn",
"xnr": "xnr_Deva"
}
familymap = {"asm_Beng":"Magadhi","awa_Deva":"CentralIndic","ben_Beng":"Magadhi","bho_Deva":"Magadhi","bra_Deva":"CentralIndic","brx_Deva":"TibetoBurman","doi_Deva":"WesternIndic","eng_Latn":"WestGermanic","gom_Deva":"Maharashtri","gon_Deva":"Dravidian","guj_Gujr":"WesternIndic","hin_Deva":"CentralIndic","hingh_Deva":"CentralIndic","hoc_Wara":"AustroAsiatic","kan_Knda":"Dravidian","kas_Arab":"WesternIndic","kas_Deva":"WesternIndic","kha_Latn":"AustroAsiatic","lus_Latn":"TibetoBurman","mag_Deva":"Magadhi","mai_Deva":"Magadhi","mal_Mlym":"Dravidian","mar_Deva":"Maharashtri","mni_Beng":"TibetoBurman","mni_Mtei":"TibetoBurman","npi_Deva":"CentralIndic","ory_Orya":"Magadhi","pan_Guru":"WesternIndic","san_Deva":"Vedic","sat_Olck":"AustroAsiatic","sin_Sinh":"Maharashtri","snd_Arab":"WesternIndic","snd_Deva":"WesternIndic","tam_Taml":"Dravidian","tcy_Knda":"Dravidian","tel_Telu":"Dravidian","urd_Arab":"CentralIndic","xnr_Deva":"CentralIndic"}
def add_family(lang):
return familymap[lang]+'+'+lang
s = spm.SentencePieceProcessor(model_file=SUBWORD_MODEL_PATH)
translator = Translator(data_dir=TRANSLATION_MODEL_FOLDER, checkpoint_path=TRANSLATION_MODEL_PATH, batch_size=100)
def split_into_parts(text, num_words=100):
"""Splits text into parts of a given number of words.
Args:
text: The text to split.
num_words: The number of words per part.
Returns:
A list of strings, where each string is a part of the text.
"""
words = text.split()
parts = []
current_part = []
for word in words:
current_part.append(word)
if len(current_part) == num_words:
parts.append(' '.join(current_part))
current_part = []
if current_part:
parts.append(' '.join(current_part))
return parts
def translate_onemt(task, domain, text, ttext, sl, tl):
nsl = add_family(language_mapping[sl])
ntl = add_family(language_mapping[tl])
text = text.replace('।','.')
ttext = ttext.replace('।','.')
if task=='Translation':
nsl = add_family(language_mapping[sl])
ntl = add_family(language_mapping[tl])
text = {'task': task+'$'+nsl+'#'+ntl, 'domain': domain, 'input': {nsl: text }}
elif task=='Translation quality estimation':
#nsl = add_family(language_mapping['eng'])
#ntl = add_family(language_mapping['hin'])
text = {'task': task+'$'+nsl+'#'+ntl, 'domain': domain, 'input': {nsl: text , ntl: ttext}}
elif task=='Translation post editing':
text = {'task': task+'$'+nsl+'#'+ntl, 'domain': domain, 'input': {nsl: text , ntl: ttext}}
else:
text = {'task': task+'$'+nsl+'#'+ntl, 'domain': domain, 'input': {nsl: text , ntl: ttext}}
print ('1--->',text)
soutput = s.encode(str(text), out_type=str)
print ('2--->',soutput)
out = translator.translate([" ".join(soutput)])
print (out[0],'<----------OOO')
if task=='Translation':
output = eval(out[0].replace(' ','').replace('▁',' '))['output'][ntl]
elif task=='Translation quality estimation':
#print (eval(out[0].replace(' ','').replace('▁',' ')))
#output = 'quality estimation :'+ str(eval(out[0].replace(' ','').replace('▁',' '))['output']['quality estimation score out of 100']*100,2)
output = str(eval(out[0].replace(' ','').replace('▁',' '))['output']).replace(' out of 100','')
elif task=='Translation post editing':
output = str(eval(out[0].replace(' ','').replace('▁',' '))['output']['post edited '+ntl])
else:
output = str(eval(out[0].replace(' ','').replace('▁',' '))['output'])
'''
if len(text.split())>200:
otext = ''
for p in split_into_parts(text):
soutput = s.encode('###'+nsl+'-to-'+ntl+'### '+p, out_type=str)
out = translator.translate([" ".join(soutput)])
output = out[0].replace(' ','').replace('▁',' ')
otext = otext + ' '+output
return otext.strip()
else:
soutput = s.encode('###'+nsl+'-to-'+ntl+'### '+text, out_type=str)
out = translator.translate([" ".join(soutput)])
output = out[0].replace(' ','').replace('▁',' ')
'''
output = output.replace('।','.')
print (output, 'Before')
return output.strip()
#_inp = "{'task': 'Translation$WesternIndic+guj_Gujr#WestGermanic+eng_Latn', 'domain': 'general', 'output': {'CentralIndic+hin_Deva': 'नई दिल्ली-केंद्र सरकार ने महंगाई भत्ते में वृद्धि की घोषणा की है। यह वृद्धि उन केंद्रीय कर्मचारियों और स्वायत्त संस्थानों के कर्मचारियों पर लागू होगी, जिन्हें 5वें और 6वें वेतन आयोग के अनुसार वेतन मिल रहा है। वित्त मंत्रालय में सार्वजनिक उद्यम विभाग ने इस संबंध में 7 नवंबर 2024 को एक कार्यालय ज्ञापन जारी किया है। }}}"
#_inp = '{"task": "Translation quality estimation$CentralIndic+hin_Deva#WestGermanic+eng_Latn", "domain": "general", "input": {"CentralIndic+hin_Deva": "exe फ़ाइल चलाएँ और निर्देशों का पालन करें", "WestGermanic+eng_Latn": "Run the .exe file and follow the instructions."}}'
#print (translate_onemt(_inp, "eng","hin"))