Update app.py
Browse files
app.py
CHANGED
@@ -98,20 +98,35 @@ def write_to_file_english(source):
|
|
98 |
return segmented_contents
|
99 |
|
100 |
|
101 |
-
def call_model_transformer(
|
102 |
if direction_trans == "English to Myanmar":
|
103 |
ct_model_path = "enmy_ctranslate2/"
|
104 |
sp_source_model_path = "enmy_ctranslate2/source.model"
|
105 |
sp_target_model_path = "enmy_ctranslate2/target.model"
|
106 |
-
if
|
107 |
gr.Warning("Please Enter English Text")
|
108 |
else:
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
# Subword the source sentences
|
113 |
-
|
114 |
-
|
115 |
# Translate the source sentences
|
116 |
translator = ctranslate2.Translator(ct_model_path, device="cpu") # or "cuda" for GPU
|
117 |
translations = translator.translate_batch(source_sents_subworded, batch_type="tokens", max_batch_size=4096)
|
@@ -123,6 +138,17 @@ def call_model_transformer(sources, direction_trans):
|
|
123 |
# Desubword the target sentences
|
124 |
translations_desubword = sp.decode(translations)
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
elif direction_trans == "Myanmar to English":
|
128 |
ct_model_path = "myen_ctranslate2/"
|
@@ -131,12 +157,26 @@ def call_model_transformer(sources, direction_trans):
|
|
131 |
if sources == "" :
|
132 |
gr.Warning("Please Enter Myanmar Text")
|
133 |
else:
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
#
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
# Translate the source sentences
|
142 |
translator = ctranslate2.Translator(ct_model_path, device="cpu") # or "cuda" for GPU
|
@@ -147,12 +187,24 @@ def call_model_transformer(sources, direction_trans):
|
|
147 |
sp.load(sp_target_model_path)
|
148 |
|
149 |
# Desubword the target sentences
|
150 |
-
translations_desubword = sp.decode(translations)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
else: gr.Warning("Please Select Language Direction")
|
153 |
|
154 |
|
155 |
-
return
|
156 |
|
157 |
|
158 |
def translate_trans_myen(source, translator, sp_source_model, sp_target_model):
|
|
|
98 |
return segmented_contents
|
99 |
|
100 |
|
101 |
+
def call_model_transformer(source, direction_trans):
|
102 |
if direction_trans == "English to Myanmar":
|
103 |
ct_model_path = "enmy_ctranslate2/"
|
104 |
sp_source_model_path = "enmy_ctranslate2/source.model"
|
105 |
sp_target_model_path = "enmy_ctranslate2/target.model"
|
106 |
+
if source == "" :
|
107 |
gr.Warning("Please Enter English Text")
|
108 |
else:
|
109 |
+
#Set file paths
|
110 |
+
source_file_path = "write-input.txt"
|
111 |
+
target_file_path = "read-output.txt"
|
112 |
+
|
113 |
+
# Load the source SentecePiece model
|
114 |
+
sp = spm.SentencePieceProcessor()
|
115 |
+
sp.load(sp_source_model_path)
|
116 |
+
|
117 |
+
# write source to file
|
118 |
+
with open(source_file_path, "w", encoding="utf-8") as file:
|
119 |
+
file.write(source)
|
120 |
+
|
121 |
+
# Open the source file
|
122 |
+
with open(source_file_path, "r") as source:
|
123 |
+
lines = source.readlines()
|
124 |
+
|
125 |
+
source_sents = [line.strip() for line in lines]
|
126 |
+
|
127 |
# Subword the source sentences
|
128 |
+
source_sents_subworded = sp.encode_as_pieces(source_sents)
|
129 |
+
|
130 |
# Translate the source sentences
|
131 |
translator = ctranslate2.Translator(ct_model_path, device="cpu") # or "cuda" for GPU
|
132 |
translations = translator.translate_batch(source_sents_subworded, batch_type="tokens", max_batch_size=4096)
|
|
|
138 |
# Desubword the target sentences
|
139 |
translations_desubword = sp.decode(translations)
|
140 |
|
141 |
+
|
142 |
+
# Save the translations to the a file
|
143 |
+
with open(target_file_path, "w+", encoding="utf-8") as target:
|
144 |
+
for line in translations_desubword:
|
145 |
+
target.write(line.strip() + "\n")
|
146 |
+
|
147 |
+
#print("Done")
|
148 |
+
|
149 |
+
with open(target_file_path, "r", encoding="utf-8") as file:
|
150 |
+
segmented_contents = file.read()
|
151 |
+
|
152 |
|
153 |
elif direction_trans == "Myanmar to English":
|
154 |
ct_model_path = "myen_ctranslate2/"
|
|
|
157 |
if sources == "" :
|
158 |
gr.Warning("Please Enter Myanmar Text")
|
159 |
else:
|
160 |
+
#Set file paths
|
161 |
+
source_file_path = "write-input.txt"
|
162 |
+
target_file_path = "read-output.txt"
|
163 |
+
|
164 |
+
# Load the source SentecePiece model
|
165 |
+
sp = spm.SentencePieceProcessor()
|
166 |
+
sp.load(sp_source_model_path)
|
167 |
+
|
168 |
+
# write source to file
|
169 |
+
with open(source_file_path, "w", encoding="utf-8") as file:
|
170 |
+
file.write(source)
|
171 |
+
|
172 |
+
# Open the source file
|
173 |
+
with open(source_file_path, "r") as source:
|
174 |
+
lines = source.readlines()
|
175 |
+
|
176 |
+
source_sents = [line.strip() for line in lines]
|
177 |
+
|
178 |
+
# Subword the source sentences
|
179 |
+
source_sents_subworded = sp.encode_as_pieces(source_sents)
|
180 |
|
181 |
# Translate the source sentences
|
182 |
translator = ctranslate2.Translator(ct_model_path, device="cpu") # or "cuda" for GPU
|
|
|
187 |
sp.load(sp_target_model_path)
|
188 |
|
189 |
# Desubword the target sentences
|
190 |
+
translations_desubword = sp.decode(translations)
|
191 |
+
|
192 |
+
|
193 |
+
# Save the translations to the a file
|
194 |
+
with open(target_file_path, "w+", encoding="utf-8") as target:
|
195 |
+
for line in translations_desubword:
|
196 |
+
target.write(line.strip() + "\n")
|
197 |
+
|
198 |
+
#print("Done")
|
199 |
+
|
200 |
+
with open(target_file_path, "r", encoding="utf-8") as file:
|
201 |
+
segmented_contents = file.read()
|
202 |
+
|
203 |
|
204 |
else: gr.Warning("Please Select Language Direction")
|
205 |
|
206 |
|
207 |
+
return segmented_contents
|
208 |
|
209 |
|
210 |
def translate_trans_myen(source, translator, sp_source_model, sp_target_model):
|