IlyaGusev commited on
Commit
07b334f
1 Parent(s): efb09f2

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +129 -0
README.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - ru
4
+ - ru-RU
5
+ tags:
6
+ - summarization
7
+ - t5
8
+ datasets:
9
+ - IlyaGusev/gazeta
10
+ license: apache-2.0
11
+ ---
12
+
13
+ # RuT5SumGazeta
14
+
15
+ ## Model description
16
+
17
+ This is the model for abstractive summarization for Russian based on [rut5-base](https://huggingface.co/cointegrated/rut5-base).
18
+
19
+
20
+ ## Intended uses & limitations
21
+
22
+ #### How to use
23
+
24
+ ```python
25
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
26
+
27
+ article_text = "..."
28
+
29
+ model_name = "IlyaGusev/rut5-base-sum-gazeta"
30
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
31
+ model = T5ForConditionalGeneration.from_pretrained(model_name)
32
+
33
+ input_ids = tokenizer(
34
+ [article_text],
35
+ add_special_tokens=True,
36
+ padding="max_length",
37
+ truncation=True,
38
+ max_length=400,
39
+ return_tensors="pt"
40
+ )["input_ids"]
41
+
42
+ output_ids = model.generate(
43
+ input_ids=input_ids,
44
+ max_length=200,
45
+ no_repeat_ngram_size=3,
46
+ num_beams=5,
47
+ early_stopping=True
48
+ )[0]
49
+
50
+ summary = tokenizer.decode(output_ids, skip_special_tokens=True)
51
+ print(summary)
52
+ ```
53
+
54
+ ## Training data
55
+
56
+ - Dataset: https://github.com/IlyaGusev/gazeta
57
+
58
+ ## Training procedure
59
+
60
+ - Training script: [TBA]
61
+
62
+ ## Eval results
63
+
64
+ | Model | R-1-f | R-2-f | R-L-f | chrF | BLEU |
65
+ |:--------------------------|:------|:------|:------|:-----|:-----|
66
+ | rut5-base-sum-gazeta | 32.3 | 14.5 | 27.9 | 39.6 | 11.5 |
67
+
68
+ Predicting all summaries:
69
+ ```python
70
+ import json
71
+ import torch
72
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
73
+ from datasets import load_dataset
74
+
75
+
76
+ def gen_batch(inputs, batch_size):
77
+ batch_start = 0
78
+ while batch_start < len(inputs):
79
+ yield inputs[batch_start: batch_start + batch_size]
80
+ batch_start += batch_size
81
+
82
+
83
+ def predict(
84
+ model_name,
85
+ input_records,
86
+ output_file,
87
+ max_source_tokens_count=400,
88
+ max_target_tokens_count=200,
89
+ batch_size=16
90
+ ):
91
+ device = "cuda" if torch.cuda.is_available() else "cpu"
92
+
93
+ tokenizer = MBartTokenizer.from_pretrained(model_name)
94
+ model = MBartForConditionalGeneration.from_pretrained(model_name).to(device)
95
+
96
+ predictions = []
97
+ for batch in gen_batch(input_records, batch_size):
98
+ texts = [r["text"] for r in batch]
99
+ input_ids = tokenizer(
100
+ texts,
101
+ add_special_tokens=True,
102
+ max_length=max_source_tokens_count,
103
+ padding="max_length",
104
+ truncation=True,
105
+ return_tensors="pt"
106
+ )["input_ids"].to(device)
107
+
108
+ output_ids = model.generate(
109
+ input_ids=input_ids,
110
+ max_length=max_target_tokens_count,
111
+ no_repeat_ngram_size=3,
112
+ num_beams=5,
113
+ early_stopping=True
114
+ )
115
+ summaries = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
116
+ for s in summaries:
117
+ print(s)
118
+ predictions.extend(summaries)
119
+ with open(output_file, "w") as w:
120
+ for p in predictions:
121
+ w.write(p.strip().replace("\n", " ") + "\n")
122
+
123
+ gazeta_test = load_dataset('IlyaGusev/gazeta', script_version="v1.0")["test"]
124
+ predict("IlyaGusev/mbart_ru_sum_gazeta", gazeta_test["test"], "t5_predictions.txt")
125
+ ```
126
+
127
+ Evaluation: https://github.com/IlyaGusev/summarus/blob/master/evaluate.py
128
+
129
+ Flags: --language ru --tokenize-after --lower