add translation script
Browse files- trans_ct2.py +24 -0
trans_ct2.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ctranslate2
|
2 |
+
import sys
|
3 |
+
|
4 |
+
|
5 |
+
model = sys.argv[1]
|
6 |
+
file_name = sys.argv[2]
|
7 |
+
|
8 |
+
file = open(file_name, 'r')
|
9 |
+
|
10 |
+
translator = ctranslate2.Translator(model, device="cuda")
|
11 |
+
#translator = ctranslate2.Translator(model, device="cpu")
|
12 |
+
|
13 |
+
for line in file:
|
14 |
+
line = line.strip()
|
15 |
+
#line = line.split()
|
16 |
+
# r = translator.translate_batch([line], beam_size=5, batch_type='examples')
|
17 |
+
r = translator.translate_batch(
|
18 |
+
[line.split()], replace_unknowns=True, beam_size=5, batch_type='examples'
|
19 |
+
)
|
20 |
+
results =' '.join(r[0].hypotheses[0])
|
21 |
+
# results =' '.join(r[0][0]['tokens'])
|
22 |
+
|
23 |
+
# print(results[0].hypotheses[0])
|
24 |
+
print(results)
|