File size: 7,065 Bytes
c076d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf41ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cee8760
c076d9b
 
 
 
 
7cf41ef
 
 
 
 
 
c076d9b
 
7cf41ef
 
c076d9b
 
 
 
cee8760
7cf41ef
c076d9b
7cf41ef
 
c076d9b
 
7cf41ef
 
cee8760
7cf41ef
c076d9b
 
 
 
 
 
7cf41ef
c076d9b
7cf41ef
c076d9b
 
 
 
cee8760
 
c076d9b
 
 
 
 
 
 
 
 
 
 
cee8760
 
 
 
 
c076d9b
7cf41ef
cee8760
 
 
 
 
 
 
 
7cf41ef
 
 
 
 
 
 
 
 
cee8760
7cf41ef
 
cee8760
c076d9b
 
 
 
 
 
 
 
7cf41ef
c076d9b
 
7cf41ef
c076d9b
 
 
cee8760
 
c076d9b
 
 
 
 
 
 
 
 
 
 
 
 
cee8760
 
c076d9b
 
 
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
161
162
163
164
165
166
167
168
169
import argparse
import re
from functools import partial
from pathlib import Path
from typing import Optional, Union

import nltk
import torch
from datasets import load_dataset
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")



def split_into_chunks(text, tokenizer, max_tokens=128):
    # Split tokenized text into sentences
    sentences = nltk.sent_tokenize(text)

    # Create chunks based on the maximum number of tokens
    chunks = []
    current_chunk = []
    tokens_count = 0

    for sentence in sentences:
        sentence_tokens = tokenizer.encode(sentence, add_special_tokens=False)
        sentence_token_count = len(sentence_tokens)

        if tokens_count + sentence_token_count > max_tokens:
            # If adding this sentence to the current chunk would exceed the maximum number of tokens, add the current chunk to the list of chunks
            if current_chunk:
                chunk_text = tokenizer.decode(current_chunk)
                chunks.append(chunk_text)
                current_chunk = []
                tokens_count = 0

        # Add the sentence to the current chunk
        current_chunk.extend(sentence_tokens)
        tokens_count += sentence_token_count

    # Add any remaining tokens as the last chunk
    if current_chunk:
        chunk_text = tokenizer.decode(current_chunk)
        chunks.append(chunk_text)

    return chunks


def to_lang_code(texts, lang_code, model, tokenizer, max_tokens=128, sentence_joiner=" "):
    is_string = isinstance(texts, str)
    if is_string:
        texts = [texts]
    batch_size = len(texts)
    to_translate = []
    lengths = []
    for text in texts:
        # Split in chunks of non-breaking sentences and keep lengths of chunks
        chunks = split_into_chunks(text, tokenizer=tokenizer, max_tokens=max_tokens)
        lengths.append(len(chunks))
        to_translate += chunks
    translated_texts = []
    # Split in batches for translation
    to_translate_batches = [to_translate[i:i + batch_size] for i in range(0, len(to_translate), batch_size)]
    for to_translate_batch in to_translate_batches:
        inputs = tokenizer(to_translate_batch, return_tensors="pt", padding=True, truncation=True).to(DEVICE)
        translated_tokens = model.generate(
            **inputs,
            forced_bos_token_id=tokenizer.lang_code_to_id[lang_code],
            max_length=512,  # max_new_tokens=512,
            # max_length=int(len(inputs.tokens()) * 1.25)  # 25% more tokens for the translation just in case
        )
        translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)
        translated_texts += translated_text
    # Merge outputs properly
    outputs = []
    start = 0
    for length in lengths:
        outputs.append(sentence_joiner.join(translated_texts[start:start + length]))
        start += length
    return outputs[0] if is_string else outputs


def main(
    dataset_name: str,
    dataset_columns: Union[list, tuple],
    model_name: Optional[str]="facebook/nllb-200-1.3B",  # "facebook/nllb-200-distilled-600M"
    model_revision: Optional[str]=None,
    dataset_splits: Union[list, tuple]=("train", "validation", "test"),
    dataset_config: Optional[str]=None,
    dataset_revision: Optional[str]=None,
    source_lang: Optional[str]="eng_Latn",
    target_langs: Optional[Union[list, tuple]]=("nob_Latn", "nno_Latn"),
    sentence_joiner: Optional[str]=" ",
    max_tokens_per_chunk: Optional[int]=128,
    batch_size: Optional[int]=24,
    output_dir: Optional[Path]=Path("./"),
) -> None:

    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, revision=model_revision, use_auth_token=True, src_lang=source_lang,
    )

    for lang_code in target_langs:
        lang_code_short = re.split(r"[-_ /]", lang_code)[0]
        if dataset_config:
            output_path = output_dir / dataset_config / lang_code_short
        else:
            output_path = output_dir / lang_code_short
        for split in dataset_splits:
            ds = load_dataset(dataset_name, name=dataset_config, revision=dataset_revision, split=split)
            translate = partial(
                to_lang_code, 
                lang_code=lang_code, 
                model=model, 
                tokenizer=tokenizer, 
                sentence_joiner=sentence_joiner,
                max_tokens=max_tokens_per_chunk,
            )
            ds = ds.map(
                lambda batch: {
                    column: translate(batch[column])
                    for column in dataset_columns
                },
                batched=True,
                batch_size=batch_size,
                desc=f"Translating to {lang_code} ({split})",
            )
            ds.save_to_disk(output_path / split, max_shard_size="1GB")
            json_filename = f"{lang_code_short}_{split}.json.gz".lower()
            ds.to_pandas().to_json(
                output_path / json_filename, orient='records', lines=True
            )



if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Translate datasets using Facebook's NLLB models")
    parser.add_argument('dataset_name')
    parser.add_argument('dataset_columns', help="Comma separated column names to translate")
    parser.add_argument('--dataset_splits', default="train,validation,test", help="Comma separated splits to translate")
    parser.add_argument('--dataset_config')
    parser.add_argument('--dataset_revision')
    parser.add_argument('--model_name', default="facebook/nllb-200-1.3B")
    parser.add_argument('--model_revision')
    parser.add_argument('--source_lang', default="eng_Latn")
    parser.add_argument('--target_langs', default="nob_Latn,nno_Latn", help="Comma separated target languages to translate to")
    parser.add_argument('--sentence_joiner', default=" ", help="String to join sentences split for translation")
    parser.add_argument('--max_tokens_per_chunk', default=128, type=int, help="Max number of tokens for each chunk for translation")
    parser.add_argument('--batch_size', '-bs', default=24, type=int, help='Number of inputs per batch for prediction')
    parser.add_argument('--output_dir', '-o', default="./", type=str)
    args = parser.parse_args()
    main(
        dataset_name=args.dataset_name,
        dataset_columns=args.dataset_columns.split(","),
        dataset_splits=args.dataset_splits.split(","),
        dataset_config=args.dataset_config,
        dataset_revision=args.dataset_revision,
        model_name=args.model_name,
        model_revision=args.model_revision,
        source_lang=args.source_lang,
        target_langs=args.target_langs.split(","),
        sentence_joiner=args.sentence_joiner,
        max_tokens_per_chunk=args.max_tokens_per_chunk,
        batch_size=args.batch_size,
        output_dir=Path(args.output_dir),
    )