run_summarization.py 9.96 KB
Newer Older
Rémi Louf's avatar
Rémi Louf committed
1
#! /usr/bin/python3
Rémi Louf's avatar
Rémi Louf committed
2
3
4
5
import argparse
import logging
import os
import sys
Aymeric Augustin's avatar
Aymeric Augustin committed
6
from collections import namedtuple
Rémi Louf's avatar
Rémi Louf committed
7
8

import torch
9
from modeling_bertabs import BertAbs, build_predictor
Rémi Louf's avatar
Rémi Louf committed
10
11
12
from torch.utils.data import DataLoader, SequentialSampler
from tqdm import tqdm

Aymeric Augustin's avatar
Aymeric Augustin committed
13
from transformers import BertTokenizer
14
15
16

from .utils_summarization import (
    CNNDMDataset,
Rémi Louf's avatar
Rémi Louf committed
17
18
    build_mask,
    compute_token_type_ids,
Aymeric Augustin's avatar
Aymeric Augustin committed
19
    encode_for_summarization,
20
    truncate_or_pad,
Rémi Louf's avatar
Rémi Louf committed
21
22
)

Aymeric Augustin's avatar
Aymeric Augustin committed
23

Rémi Louf's avatar
Rémi Louf committed
24
25
26
27
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)


28
Batch = namedtuple("Batch", ["document_names", "batch_size", "src", "segs", "mask_src", "tgt_str"])
Rémi Louf's avatar
Rémi Louf committed
29
30
31
32


def evaluate(args):
    tokenizer = BertTokenizer.from_pretrained("bert-base-uncased", do_lower_case=True)
33
    model = BertAbs.from_pretrained("remi/bertabs-finetuned-extractive-abstractive-summarization")
34
35
    model.to(args.device)
    model.eval()
Rémi Louf's avatar
Rémi Louf committed
36
37
38
39
40
41
42

    symbols = {
        "BOS": tokenizer.vocab["[unused0]"],
        "EOS": tokenizer.vocab["[unused1]"],
        "PAD": tokenizer.vocab["[PAD]"],
    }

Rémi Louf's avatar
Rémi Louf committed
43
44
45
46
47
    if args.compute_rouge:
        reference_summaries = []
        generated_summaries = []

        import nltk
48
49
        import rouge

50
        nltk.download("punkt")
Rémi Louf's avatar
Rémi Louf committed
51
        rouge_evaluator = rouge.Rouge(
52
            metrics=["rouge-n", "rouge-l"],
Rémi Louf's avatar
Rémi Louf committed
53
54
55
            max_n=2,
            limit_length=True,
            length_limit=args.beam_size,
56
            length_limit_type="words",
Rémi Louf's avatar
Rémi Louf committed
57
58
59
60
61
62
63
            apply_avg=True,
            apply_best=False,
            alpha=0.5,  # Default F1_score
            weight_factor=1.2,
            stemming=True,
        )

Rémi Louf's avatar
Rémi Louf committed
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
    # these (unused) arguments are defined to keep the compatibility
    # with the legacy code and will be deleted in a next iteration.
    args.result_path = ""
    args.temp_dir = ""

    data_iterator = build_data_iterator(args, tokenizer)
    predictor = build_predictor(args, tokenizer, symbols, model)

    logger.info("***** Running evaluation *****")
    logger.info("  Number examples = %d", len(data_iterator.dataset))
    logger.info("  Batch size = %d", args.batch_size)
    logger.info("")
    logger.info("***** Beam Search parameters *****")
    logger.info("  Beam size = %d", args.beam_size)
    logger.info("  Minimum length = %d", args.min_length)
    logger.info("  Maximum length = %d", args.max_length)
    logger.info("  Alpha (length penalty) = %.2f", args.alpha)
    logger.info("  Trigrams %s be blocked", ("will" if args.block_trigram else "will NOT"))

    for batch in tqdm(data_iterator):
        batch_data = predictor.translate_batch(batch)
        translations = predictor.from_batch(batch_data)
        summaries = [format_summary(t) for t in translations]
        save_summaries(summaries, args.summaries_output_dir, batch.document_names)

Rémi Louf's avatar
Rémi Louf committed
89
90
91
92
93
94
95
96
97
98
        if args.compute_rouge:
            reference_summaries += batch.tgt_str
            generated_summaries += summaries

    if args.compute_rouge:
        scores = rouge_evaluator.get_scores(generated_summaries, reference_summaries)
        str_scores = format_rouge_scores(scores)
        save_rouge_scores(str_scores)
        print(str_scores)

Rémi Louf's avatar
Rémi Louf committed
99

Rémi Louf's avatar
Rémi Louf committed
100
def save_summaries(summaries, path, original_document_name):
Lysandre's avatar
Lysandre committed
101
    """Write the summaries in fies that are prefixed by the original
Rémi Louf's avatar
Rémi Louf committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    files' name with the `_summary` appended.

    Attributes:
        original_document_names: List[string]
            Name of the document that was summarized.
        path: string
            Path were the summaries will be written
        summaries: List[string]
            The summaries that we produced.
    """
    for summary, document_name in zip(summaries, original_document_name):
        # Prepare the summary file's name
        if "." in document_name:
            bare_document_name = ".".join(document_name.split(".")[:-1])
            extension = document_name.split(".")[-1]
            name = bare_document_name + "_summary." + extension
        else:
            name = document_name + "_summary"

        file_path = os.path.join(path, name)
        with open(file_path, "w") as output:
            output.write(summary)


Rémi Louf's avatar
Rémi Louf committed
126
def format_summary(translation):
Lysandre's avatar
Lysandre committed
127
    """Transforms the output of the `from_batch` function
Rémi Louf's avatar
Rémi Louf committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    into nicely formatted summaries.
    """
    raw_summary, _, _ = translation
    summary = (
        raw_summary.replace("[unused0]", "")
        .replace("[unused3]", "")
        .replace("[PAD]", "")
        .replace("[unused1]", "")
        .replace(r" +", " ")
        .replace(" [unused2] ", ". ")
        .replace("[unused2]", "")
        .strip()
    )

    return summary


Rémi Louf's avatar
Rémi Louf committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def format_rouge_scores(scores):
    return """\n
****** ROUGE SCORES ******

** ROUGE 1
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}

** ROUGE 2
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}

** ROUGE L
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}""".format(
163
164
165
166
167
168
169
170
171
        scores["rouge-1"]["f"],
        scores["rouge-1"]["p"],
        scores["rouge-1"]["r"],
        scores["rouge-2"]["f"],
        scores["rouge-2"]["p"],
        scores["rouge-2"]["r"],
        scores["rouge-l"]["f"],
        scores["rouge-l"]["p"],
        scores["rouge-l"]["r"],
Rémi Louf's avatar
Rémi Louf committed
172
173
174
175
176
177
178
179
    )


def save_rouge_scores(str_scores):
    with open("rouge_scores.txt", "w") as output:
        output.write(str_scores)


Rémi Louf's avatar
Rémi Louf committed
180
181
182
183
184
185
186
187
#
# LOAD the dataset
#


def build_data_iterator(args, tokenizer):
    dataset = load_and_cache_examples(args, tokenizer)
    sampler = SequentialSampler(dataset)
188
189
190
191

    def collate_fn(data):
        return collate(data, tokenizer, block_size=512, device=args.device)

Lysandre's avatar
Lysandre committed
192
193
194
195
196
197
    iterator = DataLoader(
        dataset,
        sampler=sampler,
        batch_size=args.batch_size,
        collate_fn=collate_fn,
    )
Rémi Louf's avatar
Rémi Louf committed
198
199
200
201
202

    return iterator


def load_and_cache_examples(args, tokenizer):
203
    dataset = CNNDMDataset(args.documents_dir)
Rémi Louf's avatar
Rémi Louf committed
204
205
206
    return dataset


Rémi Louf's avatar
Rémi Louf committed
207
def collate(data, tokenizer, block_size, device):
Lysandre's avatar
Lysandre committed
208
    """Collate formats the data passed to the data loader.
Rémi Louf's avatar
Rémi Louf committed
209
210
211
212
213
214
215

    In particular we tokenize the data batch after batch to avoid keeping them
    all in memory. We output the data as a namedtuple to fit the original BertAbs's
    API.
    """
    data = [x for x in data if not len(x[1]) == 0]  # remove empty_files
    names = [name for name, _, _ in data]
Rémi Louf's avatar
Rémi Louf committed
216
    summaries = [" ".join(summary_list) for _, _, summary_list in data]
Rémi Louf's avatar
Rémi Louf committed
217

218
    encoded_text = [encode_for_summarization(story, summary, tokenizer) for _, story, summary in data]
Rémi Louf's avatar
Rémi Louf committed
219
    encoded_stories = torch.tensor(
220
        [truncate_or_pad(story, block_size, tokenizer.pad_token_id) for story, _ in encoded_text]
Rémi Louf's avatar
Rémi Louf committed
221
    )
Rémi Louf's avatar
Rémi Louf committed
222
223
    encoder_token_type_ids = compute_token_type_ids(encoded_stories, tokenizer.cls_token_id)
    encoder_mask = build_mask(encoded_stories, tokenizer.pad_token_id)
Rémi Louf's avatar
Rémi Louf committed
224
225
226

    batch = Batch(
        document_names=names,
Rémi Louf's avatar
Rémi Louf committed
227
        batch_size=len(encoded_stories),
Rémi Louf's avatar
Rémi Louf committed
228
229
230
        src=encoded_stories.to(device),
        segs=encoder_token_type_ids.to(device),
        mask_src=encoder_mask.to(device),
Rémi Louf's avatar
Rémi Louf committed
231
        tgt_str=summaries,
Rémi Louf's avatar
Rémi Louf committed
232
233
234
235
236
237
    )

    return batch


def decode_summary(summary_tokens, tokenizer):
Lysandre's avatar
Lysandre committed
238
    """Decode the summary and return it in a format
Rémi Louf's avatar
Rémi Louf committed
239
240
241
242
243
244
245
246
247
248
    suitable for evaluation.
    """
    summary_tokens = summary_tokens.to("cpu").numpy()
    summary = tokenizer.decode(summary_tokens)
    sentences = summary.split(".")
    sentences = [s + "." for s in sentences]
    return sentences


def main():
Lysandre's avatar
Lysandre committed
249
    """The main function defines the interface with the users."""
Rémi Louf's avatar
Rémi Louf committed
250
251
252
253
254
255
256
257
258
259
260
261
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--documents_dir",
        default=None,
        type=str,
        required=True,
        help="The folder where the documents to summarize are located.",
    )
    parser.add_argument(
        "--summaries_output_dir",
        default=None,
        type=str,
262
263
        required=False,
        help="The folder in wich the summaries should be written. Defaults to the folder where the documents are",
Rémi Louf's avatar
Rémi Louf committed
264
    )
Rémi Louf's avatar
Rémi Louf committed
265
266
267
268
269
270
271
    parser.add_argument(
        "--compute_rouge",
        default=False,
        type=bool,
        required=False,
        help="Compute the ROUGE metrics during evaluation. Only available for the CNN/DailyMail dataset.",
    )
Rémi Louf's avatar
Rémi Louf committed
272
273
    # EVALUATION options
    parser.add_argument(
Lysandre's avatar
Lysandre committed
274
275
276
277
        "--no_cuda",
        default=False,
        type=bool,
        help="Whether to force the execution on CPU.",
Rémi Louf's avatar
Rémi Louf committed
278
279
    )
    parser.add_argument(
Lysandre's avatar
Lysandre committed
280
281
282
283
        "--batch_size",
        default=4,
        type=int,
        help="Batch size per GPU/CPU for training.",
Rémi Louf's avatar
Rémi Louf committed
284
285
286
    )
    # BEAM SEARCH arguments
    parser.add_argument(
Lysandre's avatar
Lysandre committed
287
288
289
290
        "--min_length",
        default=50,
        type=int,
        help="Minimum number of tokens for the summaries.",
Rémi Louf's avatar
Rémi Louf committed
291
292
    )
    parser.add_argument(
Lysandre's avatar
Lysandre committed
293
294
295
296
        "--max_length",
        default=200,
        type=int,
        help="Maixmum number of tokens for the summaries.",
Rémi Louf's avatar
Rémi Louf committed
297
298
    )
    parser.add_argument(
Lysandre's avatar
Lysandre committed
299
300
301
302
        "--beam_size",
        default=5,
        type=int,
        help="The number of beams to start with for each example.",
Rémi Louf's avatar
Rémi Louf committed
303
304
    )
    parser.add_argument(
Lysandre's avatar
Lysandre committed
305
306
307
308
        "--alpha",
        default=0.95,
        type=float,
        help="The value of alpha for the length penalty in the beam search.",
Rémi Louf's avatar
Rémi Louf committed
309
310
311
312
313
314
315
316
317
    )
    parser.add_argument(
        "--block_trigram",
        default=True,
        type=bool,
        help="Whether to block the existence of repeating trigrams in the text generated by beam search.",
    )
    args = parser.parse_args()

Rémi Louf's avatar
Rémi Louf committed
318
    # Select device (distibuted not available)
319
    args.device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
Rémi Louf's avatar
Rémi Louf committed
320
321

    # Check the existence of directories
322
323
324
    if not args.summaries_output_dir:
        args.summaries_output_dir = args.documents_dir

Rémi Louf's avatar
Rémi Louf committed
325
326
    if not documents_dir_is_valid(args.documents_dir):
        raise FileNotFoundError(
Sylvain Gugger's avatar
Sylvain Gugger committed
327
328
            "We could not find the directory you specified for the documents to summarize, or it was empty. Please"
            " specify a valid path."
Rémi Louf's avatar
Rémi Louf committed
329
        )
Rémi Louf's avatar
Rémi Louf committed
330
    os.makedirs(args.summaries_output_dir, exist_ok=True)
Rémi Louf's avatar
Rémi Louf committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

    evaluate(args)


def documents_dir_is_valid(path):
    if not os.path.exists(path):
        return False

    file_list = os.listdir(path)
    if len(file_list) == 0:
        return False

    return True


if __name__ == "__main__":
    main()