-
Notifications
You must be signed in to change notification settings - Fork 0
/
Summarizer.py
137 lines (96 loc) · 3.95 KB
/
Summarizer.py
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
import pandas as pd
import numpy as np
import re
import spacy
import transformers
# Summarize one text
def summarize(text):
# Removing the first common part
splitted_text = text.split('Numac')
splitted_text = splitted_text[1]
# removing symbols
no_symbols = splitted_text.replace('\n',' ')
no_symbols = no_symbols.replace('/','')
no_symbols = no_symbols.replace('§','')
# Removing 'begin, eerste, laatste,...' from the end of the text and creating our main text.
index = no_symbols.rfind('begin')
corpus = no_symbols[:index]
# Starting the summarizer
import transformers
undisputed_best_model = transformers.MBartForConditionalGeneration.from_pretrained(
"ml6team/mbart-large-cc25-cnn-dailymail-nl-finetune")
tokenizer = transformers.MBartTokenizer.from_pretrained("facebook/mbart-large-cc25")
summarization_pipeline = transformers.pipeline(
task="summarization",
model=undisputed_best_model,
tokenizer=tokenizer,
)
summarization_pipeline.model.config.decoder_start_token_id = tokenizer.lang_code_to_id[
"nl_XX"
]
article = corpus
summary = summarization_pipeline(
article,
do_sample=True,
top_p=0.75,
top_k=50,
# num_beams=4,
min_length=50,
early_stopping=True,
truncation=True,
)[0]["summary_text"]
return summary
'''--------------------------------------------------------------------------------------------------------'''
# Looping through a .csv and creating summaries.
def summarize_csv(filepath):
df = pd.read_csv(filepath)
# Remove the German Translations
df = df[df["Text"].str.contains("Duitse vertaling")==False]
# Remove empty text rows if any are left.
df.dropna(axis = 0, how ='any', inplace = True)
# Create a summary column
df['Summary'] = ''
# Loop over the columns
for idx, row in df.iterrows():
text = df.loc[idx,'Text']
# Removing the first common part
splitted_text = text.split('Numac')
splitted_text = splitted_text[1]
# Removing symbols.
no_symbols = splitted_text.replace('\n',' ')
no_symbols = no_symbols.replace('/','')
no_symbols = no_symbols.replace('§','')
# Removing 'begin, eerste, laatste,...' from the end of the text and creating our main text.
index = no_symbols.rfind('begin')
corpus = no_symbols[:index]
substring = 'Erratum'
if substring in corpus:
df.loc[idx,'Summary'] = corpus
else:
print('check')
# loading mBart finetune Model
undisputed_best_model = transformers.MBartForConditionalGeneration.from_pretrained(
"ml6team/mbart-large-cc25-cnn-dailymail-nl-finetune"
)
tokenizer = transformers.MBartTokenizer.from_pretrained("facebook/mbart-large-cc25")
summarization_pipeline = transformers.pipeline(
task="summarization",
model=undisputed_best_model,
tokenizer=tokenizer,
)
summarization_pipeline.model.config.decoder_start_token_id = tokenizer.lang_code_to_id[
"nl_XX"
]
article = corpus
df.loc[idx,'Summary'] = summarization_pipeline(
article,
do_sample=True,
top_p=0.75,
top_k=50,
# num_beams=4,
min_length=50,
early_stopping=True,
truncation=True,
)[0]["summary_text"]
df = df.reset_index(drop=True)
df.to_csv("KPMG_summarized.csv", index=False, encoding="utf-8-sig")