Views
No views yet
summarization: https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.SummarizationPipeline1from transformers import AutoTokenizer, T5ForConditionalGeneration
2
3model_name = "IlyaGusev/rut5_base_sum_gazeta"
4tokenizer = AutoTokenizer.from_pretrained(model_name)
5model = T5ForConditionalGeneration.from_pretrained(model_name)
6
7article_text = "..."
8
9input_ids = tokenizer(
10 [article_text],
11 max_length=600,
12 add_special_tokens=True,
13 padding="max_length",
14 truncation=True,
15 return_tensors="pt"
16)["input_ids"]
17
18output_ids = model.generate(
19 input_ids=input_ids,
20 no_repeat_ngram_size=4
21)[0]
22
23summary = tokenizer.decode(output_ids, skip_special_tokens=True)
24print(summary)| Model | R-1-f | R-2-f | R-L-f | chrF | METEOR | BLEU | Avg char length |
|---|---|---|---|---|---|---|---|
| mbart_ru_sum_gazeta | 32.4 | 14.3 | 28.0 | 39.7 | 26.4 | 12.1 | 371 |
| rut5_base_sum_gazeta | 32.2 | 14.4 | 28.1 | 39.8 | 25.7 | 12.3 | 330 |
| rugpt3medium_sum_gazeta | 26.2 | 7.7 | 21.7 | 33.8 | 18.2 | 4.3 | 244 |
| Model | R-1-f | R-2-f | R-L-f | chrF | METEOR | BLEU | Avg char length |
|---|---|---|---|---|---|---|---|
| mbart_ru_sum_gazeta | 28.7 | 11.1 | 24.4 | 37.3 | 22.7 | 9.4 | 373 |
| rut5_base_sum_gazeta | 28.6 | 11.1 | 24.5 | 37.2 | 22.0 | 9.4 | 331 |
| rugpt3medium_sum_gazeta | 24.1 | 6.5 | 19.8 | 32.1 | 16.3 | 3.6 | 242 |
1import json
2import torch
3from transformers import AutoTokenizer, T5ForConditionalGeneration
4from datasets import load_dataset
5
6
7def gen_batch(inputs, batch_size):
8 batch_start = 0
9 while batch_start < len(inputs):
10 yield inputs[batch_start: batch_start + batch_size]
11 batch_start += batch_size
12
13
14def predict(
15 model_name,
16 input_records,
17 output_file,
18 max_source_tokens_count=600,
19 batch_size=8
20):
21 device = "cuda" if torch.cuda.is_available() else "cpu"
22
23 tokenizer = AutoTokenizer.from_pretrained(model_name)
24 model = T5ForConditionalGeneration.from_pretrained(model_name).to(device)
25
26 predictions = []
27 for batch in gen_batch(input_records, batch_size):
28 texts = [r["text"] for r in batch]
29 input_ids = tokenizer(
30 texts,
31 add_special_tokens=True,
32 max_length=max_source_tokens_count,
33 padding="max_length",
34 truncation=True,
35 return_tensors="pt"
36 )["input_ids"].to(device)
37
38 output_ids = model.generate(
39 input_ids=input_ids,
40 no_repeat_ngram_size=4
41 )
42 summaries = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
43 for s in summaries:
44 print(s)
45 predictions.extend(summaries)
46 with open(output_file, "w") as w:
47 for p in predictions:
48 w.write(p.strip().replace("\n", " ") + "\n")
49
50gazeta_test = load_dataset('IlyaGusev/gazeta', script_version="v1.0")["test"]
51predict("IlyaGusev/rut5_base_sum_gazeta", list(gazeta_test), "t5_predictions.txt")