File size: 1,654 Bytes
1cf25f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import partial

import torch

from datasets import load_dataset
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer


model_name = "facebook/nllb-200-3.3B"  # "facebook/nllb-200-distilled-600M"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, use_auth_token=True, torch_dtype=torch.float32)
model.to(device, torch.float32, True)
tokenizer = AutoTokenizer.from_pretrained(
    model_name, use_auth_token=True, src_lang="eng_Latn"
)


def to_lang_code(text, lang_code):
    inputs = tokenizer(text, return_tensors="pt").to(device)
    translated_tokens = model.generate(
        **inputs,
        forced_bos_token_id=tokenizer.lang_code_to_id[lang_code],
        max_length=int(len(inputs.tokens()) * 1.5)  # 50% more tokens for the translation just in case
    )
    return tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]


if __name__ == "__main__":
    languages = (("nb", "nob_Latn"), ("nn", "nno_Latn"))
    ds = load_dataset("paws-x", "en")
    dss = {}
    for lang, translate_code in languages:
        translate = partial(to_lang_code, lang_code=translate_code)
        dss[lang] = ds.map(lambda example: {
            "sentence1": translate(example["sentence1"]),
            "sentence2": translate(example["sentence2"]),
        }, desc=f"Translating to {lang}")
        for split in ("test", "validation", "train"):
            json_lines = dss[lang][split].to_pandas().to_json(orient='records', lines=True)
            with open(f"{lang}_{split}.json", "w") as json_file:
                json_file.write(json_lines)