File size: 5,644 Bytes
f9d7028 |
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 157 158 159 160 |
import os
import re
import sys
from tqdm import tqdm
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# dictionary mapping flores codes to mBART supported codes
langs_supported = {
"eng_Latn": "en_XX",
"guj_Gujr": "gu_IN",
"hin_Deva": "hi_IN",
"npi_Deva": "ne_NP",
"ben_Beng": "bn_IN",
"mal_Mlym": "ml_IN",
"mar_Deva": "mr_IN",
"tam_Taml": "ta_IN",
"tel_Telu": "te_IN",
"urd_Arab": "ur_PK",
}
def predict(batch, tokenizer, model, bos_token_id):
encoded_batch = tokenizer(batch, padding=True, return_tensors="pt").to(model.device)
generated_tokens = model.generate(
**encoded_batch,
num_beams=5,
max_length=256,
min_length=0,
forced_bos_token_id=bos_token_id,
)
hypothesis = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
return hypothesis
def main(devtest_data_dir, batch_size):
# load the pre-trained mBART tokenizers and models for English-XX and XX-English translation
enxx_model_name = "facebook/mbart-large-50-one-to-many-mmt"
xxen_model_name = "facebook/mbart-large-50-many-to-one-mmt"
tokenizers = {
"enxx": AutoTokenizer.from_pretrained(enxx_model_name),
"xxen": AutoTokenizer.from_pretrained(xxen_model_name),
}
models = {
"enxx": AutoModelForSeq2SeqLM.from_pretrained(enxx_model_name).cuda(),
"xxen": AutoModelForSeq2SeqLM.from_pretrained(xxen_model_name).cuda(),
}
# set the models to evaluation mode
for model_name in models:
models[model_name].eval()
# iterate over a list of language pairs from `devtest_data_dir`
for pair in sorted(os.listdir(devtest_data_dir)):
if "-" not in pair:
continue
src_lang, tgt_lang = pair.split("-")
# check if the source and target languages are supported
if (
src_lang not in langs_supported.keys()
or tgt_lang not in langs_supported.keys()
):
print(f"Skipping {src_lang}-{tgt_lang} ...")
continue
# -------------------------------------------------------------------
# source to target evaluation
# -------------------------------------------------------------------
print(f"Evaluating {src_lang}-{tgt_lang} ...")
infname = os.path.join(devtest_data_dir, pair, f"test.{src_lang}")
outfname = os.path.join(devtest_data_dir, pair, f"test.{tgt_lang}.pred.mbart50")
with open(infname, "r") as f:
src_sents = f.read().split("\n")
add_new_line = False
if src_sents[-1] == "":
add_new_line = True
src_sents = src_sents[:-1]
# set the source language for tokenization
tokenizers["enxx"].src_lang = langs_supported[src_lang]
# process sentences in batches and generate predictions
hypothesis = []
for i in tqdm(range(0, len(src_sents), batch_size)):
start, end = i, int(min(len(src_sents), i + batch_size))
batch = src_sents[start:end]
bos_token_id = tokenizers["enxx"].lang_code_to_id[langs_supported[tgt_lang]]
hypothesis += predict(
batch, tokenizers["enxx"], models["enxx"], bos_token_id
)
assert len(hypothesis) == len(src_sents)
hypothesis = [
re.sub("\s+", " ", x.replace("\n", " ").replace("\t", " ")).strip()
for x in hypothesis
]
if add_new_line:
hypothesis = hypothesis
with open(outfname, "w") as f:
f.write("\n".join(hypothesis))
# -------------------------------------------------------------------
# target to source evaluation
# -------------------------------------------------------------------
infname = os.path.join(devtest_data_dir, pair, f"test.{tgt_lang}")
outfname = os.path.join(devtest_data_dir, pair, f"test.{src_lang}.pred.mbart50")
with open(infname, "r") as f:
src_sents = f.read().split("\n")
add_new_line = False
if src_sents[-1] == "":
add_new_line = True
src_sents = src_sents[:-1]
# set the source language for tokenization
tokenizers["xxen"].src_lang = langs_supported[tgt_lang]
# process sentences in batches and generate predictions
hypothesis = []
for i in tqdm(range(0, len(src_sents), batch_size)):
start, end = i, int(min(len(src_sents), i + batch_size))
batch = src_sents[start:end]
bos_token_id = tokenizers["xxen"].lang_code_to_id[langs_supported[src_lang]]
hypothesis += predict(
batch, tokenizers["xxen"], models["xxen"], bos_token_id
)
assert len(hypothesis) == len(src_sents)
hypothesis = [
re.sub("\s+", " ", x.replace("\n", " ").replace("\t", " ")).strip()
for x in hypothesis
]
if add_new_line:
hypothesis = hypothesis
with open(outfname, "w") as f:
f.write("\n".join(hypothesis))
if __name__ == "__main__":
# expects En-X subdirectories pairs within the devtest data directory
devtest_data_dir = sys.argv[1]
batch_size = int(sys.argv[2])
if not torch.cuda.is_available():
print("No GPU available")
sys.exit(1)
main(devtest_data_dir, batch_size)
|