forked from AI4Bharat/setu-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.py
151 lines (120 loc) · 3.24 KB
/
decode.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
import os
import torch
from IndicTransTokenizer import IndicProcessor, IndicTransTokenizer
from datasets import load_dataset
import glob
from functools import partial
import json
import argparse
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def parse_args():
parser = argparse.ArgumentParser(description="Perform decoding")
parser.add_argument(
"--data_files",
type=str,
)
parser.add_argument(
"--format",
type=str,
default="arrow",
required=False
)
parser.add_argument(
"--cache_dir",
type=str,
)
parser.add_argument(
"--decode_dir",
type=str,
)
parser.add_argument(
"--batch_size",
type=int,
)
parser.add_argument(
"--total_procs",
type=int
)
parser.add_argument(
"--save_strs",
type=str2bool,
required=False,
default=True
)
parser.add_argument(
"--src_lang",
type=str,
)
parser.add_argument(
"--tgt_lang",
type=str,
)
args = parser.parse_args()
return args
def decode(
batch,
src_lang="eng_Latn",
tgt_lang="hin_Deva"
):
ip = IndicProcessor(inference=True)
tokenizer = IndicTransTokenizer(direction="en-indic")
p_batch = dict()
input_ids = batch.pop("translated_input_ids")
placeholder_entity_maps = list(map(lambda ple_map: json.loads(ple_map), batch["placeholder_entity_map"]))
outputs = tokenizer.batch_decode(input_ids, src=False)
p_batch["translated"] = ip.postprocess_batch(outputs, lang=tgt_lang, placeholder_entity_maps=placeholder_entity_maps)
return p_batch | {
"translated_input_ids": input_ids,
}
def save_to_str_lvl(batch):
written_file = []
for i in range(len(batch["sid"])):
try:
file_dir = os.path.dirname(batch["tlt_file_loc"][i])
os.makedirs(file_dir, exist_ok=True)
with open(batch["tlt_file_loc"][i], "w") as str_f:
str_f.write(batch["translated"][i])
written_file += [True]
except Exception as e:
written_file += [False]
return batch | {
"written": written_file
}
if __name__ == "__main__":
args = parse_args()
ds = load_dataset(
args.format,
data_files=glob.glob(args.data_files),
num_proc=args.total_procs,
cache_dir=args.cache_dir,
split="train"
)
print("Loaded Dataset....")
decoded_ds = ds.map(
partial(
decode,
src_lang=args.src_lang,
tgt_lang=args.tgt_lang,
),
batched=True,
batch_size=args.batch_size,
num_proc=args.total_procs,
)
if args.save_strs:
decoded_ds = decoded_ds.map(
save_to_str_lvl,
batched=True,
batch_size=args.batch_size,
num_proc=args.total_procs,
)
os.makedirs(args.decode_dir, exist_ok=True)
decoded_ds.save_to_disk(
args.decode_dir,
num_proc=args.total_procs,
)