Spaces:
Sleeping
Sleeping
dyxohjl666
commited on
Commit
•
44257a9
1
Parent(s):
ee6c452
Add precomputing acl data
Browse files- controlled_summarization.py +87 -16
controlled_summarization.py
CHANGED
@@ -3,9 +3,64 @@ import torch
|
|
3 |
from SciAssist import Summarization
|
4 |
import os
|
5 |
import requests
|
|
|
|
|
|
|
6 |
device = "gpu" if torch.cuda.is_available() else "cpu"
|
7 |
|
8 |
-
ctrlsum_pipeline = Summarization(os_name="nt",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
def download_pdf(url, dest_folder):
|
@@ -30,16 +85,15 @@ def download_pdf(url, dest_folder):
|
|
30 |
return filename
|
31 |
|
32 |
|
33 |
-
def ctrlsum_for_str(input,length=None, keywords=None) -> List[Tuple[str, str]]:
|
34 |
-
|
35 |
if keywords is not None:
|
36 |
keywords = keywords.strip().split(",")
|
37 |
if keywords[0] == "":
|
38 |
keywords = None
|
39 |
-
if length==0 or length is None:
|
40 |
length = None
|
41 |
results = ctrlsum_pipeline.predict(input, type="str",
|
42 |
-
|
43 |
|
44 |
output = []
|
45 |
for res in results["summary"]:
|
@@ -49,31 +103,49 @@ def ctrlsum_for_str(input,length=None, keywords=None) -> List[Tuple[str, str]]:
|
|
49 |
|
50 |
def ctrlsum_for_file(input=None, length=None, keywords="", text="", url="") -> List[Tuple[str, str, str]]:
|
51 |
if input == None and url == "":
|
52 |
-
if text=="":
|
53 |
-
return None,"Input cannot be left blank.",None
|
54 |
else:
|
55 |
-
return ctrlsum_for_str(text,length,keywords),text, None
|
56 |
else:
|
57 |
-
filename=""
|
|
|
58 |
if url != "":
|
59 |
-
if len(url) > 4:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
filename = download_pdf(url, './cache/')
|
|
|
|
|
61 |
else:
|
62 |
filename = input.name
|
63 |
if keywords != "":
|
64 |
keywords = keywords.strip().split(",")
|
65 |
if keywords[0] == "":
|
66 |
keywords = None
|
67 |
-
if length==0:
|
68 |
length = None
|
69 |
# Identify the format of input and parse reference strings
|
70 |
if filename[-4:] == ".txt":
|
71 |
results = ctrlsum_pipeline.predict(filename, type="txt",
|
72 |
-
|
73 |
-
|
74 |
elif filename[-4:] == ".pdf":
|
75 |
results = ctrlsum_pipeline.predict(filename,
|
76 |
-
|
77 |
else:
|
78 |
return "File Format Error !", None, filename
|
79 |
|
@@ -83,5 +155,4 @@ def ctrlsum_for_file(input=None, length=None, keywords="", text="", url="") -> L
|
|
83 |
return "".join(output), results["raw_text"], filename
|
84 |
|
85 |
|
86 |
-
|
87 |
-
ctrlsum_str_example = "Language model pre-training has been shown to be effective for improving many natural language processing tasks ( Dai and Le , 2015 ; Peters et al. , 2018a ; Radford et al. , 2018 ; Howard and Ruder , 2018 ) . These include sentence-level tasks such as natural language inference ( Bowman et al. , 2015 ; Williams et al. , 2018 ) and paraphrasing ( Dolan and Brockett , 2005 ) , which aim to predict the relationships between sentences by analyzing them holistically , as well as token-level tasks such as named entity recognition and question answering , where models are required to produce fine-grained output at the token level ( Tjong Kim Sang and De Meulder , 2003 ; Rajpurkar et al. , 2016 ) . There are two existing strategies for applying pre-trained language representations to downstream tasks : feature-based and fine-tuning . The feature-based approach , such as ELMo ( Peters et al. , 2018a ) , uses task-specific architectures that include the pre-trained representations as additional features . The fine-tuning approach , such as the Generative Pre-trained Transformer ( OpenAI GPT ) ( Radford et al. , 2018 ) , introduces minimal task-specific parameters , and is trained on the downstream tasks by simply fine-tuning all pretrained parameters . The two approaches share the same objective function during pre-training , where they use unidirectional language models to learn general language representations . We argue that current techniques restrict the power of the pre-trained representations , especially for the fine-tuning approaches . The major limitation is that standard language models are unidirectional , and this limits the choice of architectures that can be used during pre-training . For example , in OpenAI GPT , the authors use a left-toright architecture , where every token can only attend to previous tokens in the self-attention layers of the Transformer ( Vaswani et al. , 2017 ) . Such restrictions are sub-optimal for sentence-level tasks , and could be very harmful when applying finetuning based approaches to token-level tasks such as question answering , where it is crucial to incorporate context from both directions . In this paper , we improve the fine-tuning based approaches by proposing BERT : Bidirectional Encoder Representations from Transformers . BERT alleviates the previously mentioned unidirectionality constraint by using a `` masked language model '' ( MLM ) pre-training objective , inspired by the Cloze task ( Taylor , 1953 ) . The masked language model randomly masks some of the tokens from the input , and the objective is to predict the original vocabulary id of the masked arXiv:1810.04805v2 [ cs.CL ] 24 May 2019 word based only on its context . Unlike left-toright language model pre-training , the MLM objective enables the representation to fuse the left and the right context , which allows us to pretrain a deep bidirectional Transformer . In addition to the masked language model , we also use a `` next sentence prediction '' task that jointly pretrains text-pair representations . The contributions of our paper are as follows : • We demonstrate the importance of bidirectional pre-training for language representations . Unlike Radford et al . ( 2018 ) , which uses unidirectional language models for pre-training , BERT uses masked language models to enable pretrained deep bidirectional representations . This is also in contrast to Peters et al . ( 2018a ) , which uses a shallow concatenation of independently trained left-to-right and right-to-left LMs . • We show that pre-trained representations reduce the need for many heavily-engineered taskspecific architectures . BERT is the first finetuning based representation model that achieves state-of-the-art performance on a large suite of sentence-level and token-level tasks , outperforming many task-specific architectures . • BERT advances the state of the art for eleven NLP tasks . The code and pre-trained models are available at https : //github.com/ google-research/bert . "
|
|
|
3 |
from SciAssist import Summarization
|
4 |
import os
|
5 |
import requests
|
6 |
+
from datasets import load_dataset
|
7 |
+
|
8 |
+
acl_data = load_dataset("dyxohjl666/CocoScisum_ACL", revision="refs/convert/parquet")
|
9 |
device = "gpu" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
+
ctrlsum_pipeline = Summarization(os_name="nt",device=device)
|
12 |
+
|
13 |
+
acl_dict = {}
|
14 |
+
recommended_kw = {}
|
15 |
+
|
16 |
+
|
17 |
+
def convert_to_dict(data):
|
18 |
+
""" Dict:
|
19 |
+
{ url:
|
20 |
+
{length:
|
21 |
+
{keywords: summary};
|
22 |
+
raw_text:
|
23 |
+
str;
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
"""
|
28 |
+
url = data["url"]
|
29 |
+
text = data["text"]
|
30 |
+
keywords = data["keywords"]
|
31 |
+
length = data["length"]
|
32 |
+
summary = data["summary"]
|
33 |
+
for u, t, k, l, s in zip(url, text, keywords, length, summary):
|
34 |
+
if len(u) < 5:
|
35 |
+
continue
|
36 |
+
u = u + ".pdf"
|
37 |
+
if k == None:
|
38 |
+
k = ""
|
39 |
+
if l == None:
|
40 |
+
l = ""
|
41 |
+
k = str(k).strip()
|
42 |
+
l = str(l).strip()
|
43 |
+
if u in acl_dict.keys():
|
44 |
+
if k in acl_dict[u][l].keys():
|
45 |
+
continue
|
46 |
+
else:
|
47 |
+
acl_dict[u][l][k] = s
|
48 |
+
else:
|
49 |
+
acl_dict[u] = {"": {}, "50": {}, "100": {}, "200": {}, "raw_text": t}
|
50 |
+
|
51 |
+
# kws
|
52 |
+
if u in recommended_kw.keys():
|
53 |
+
if k == "" or k in recommended_kw[u]:
|
54 |
+
continue
|
55 |
+
else:
|
56 |
+
recommended_kw[u].append(k)
|
57 |
+
else:
|
58 |
+
recommended_kw[u] = []
|
59 |
+
return 1
|
60 |
+
|
61 |
+
|
62 |
+
for i in acl_data.keys():
|
63 |
+
signal = convert_to_dict(acl_data[i])
|
64 |
|
65 |
|
66 |
def download_pdf(url, dest_folder):
|
|
|
85 |
return filename
|
86 |
|
87 |
|
88 |
+
def ctrlsum_for_str(input, length=None, keywords=None) -> List[Tuple[str, str]]:
|
|
|
89 |
if keywords is not None:
|
90 |
keywords = keywords.strip().split(",")
|
91 |
if keywords[0] == "":
|
92 |
keywords = None
|
93 |
+
if length == 0 or length is None:
|
94 |
length = None
|
95 |
results = ctrlsum_pipeline.predict(input, type="str",
|
96 |
+
length=length, keywords=keywords, num_beams=1)
|
97 |
|
98 |
output = []
|
99 |
for res in results["summary"]:
|
|
|
103 |
|
104 |
def ctrlsum_for_file(input=None, length=None, keywords="", text="", url="") -> List[Tuple[str, str, str]]:
|
105 |
if input == None and url == "":
|
106 |
+
if text == "":
|
107 |
+
return None, "Input cannot be left blank.", None
|
108 |
else:
|
109 |
+
return ctrlsum_for_str(text, length, keywords), text, None
|
110 |
else:
|
111 |
+
filename = ""
|
112 |
+
url = url.strip()
|
113 |
if url != "":
|
114 |
+
if len(url) > 4 and url[-3:] == "pdf":
|
115 |
+
if url.strip() in acl_dict.keys():
|
116 |
+
raw_text = acl_dict[url]["raw_text"]
|
117 |
+
l = str(length)
|
118 |
+
if length == 0:
|
119 |
+
l = ""
|
120 |
+
if l in acl_dict[url].keys():
|
121 |
+
if keywords.strip() in acl_dict[url][l].keys():
|
122 |
+
summary = acl_dict[url][l][keywords]
|
123 |
+
return summary, raw_text, None
|
124 |
+
if keywords.strip() == "":
|
125 |
+
keywords = None
|
126 |
+
if l == "":
|
127 |
+
l = None
|
128 |
+
return ctrlsum_for_str(raw_text, l, keywords), raw_text, None
|
129 |
+
|
130 |
filename = download_pdf(url, './cache/')
|
131 |
+
else:
|
132 |
+
"Invalid url(Not PDF)!", None, None
|
133 |
else:
|
134 |
filename = input.name
|
135 |
if keywords != "":
|
136 |
keywords = keywords.strip().split(",")
|
137 |
if keywords[0] == "":
|
138 |
keywords = None
|
139 |
+
if length == 0:
|
140 |
length = None
|
141 |
# Identify the format of input and parse reference strings
|
142 |
if filename[-4:] == ".txt":
|
143 |
results = ctrlsum_pipeline.predict(filename, type="txt",
|
144 |
+
save_results=False,
|
145 |
+
length=length, keywords=keywords, num_beams=1)
|
146 |
elif filename[-4:] == ".pdf":
|
147 |
results = ctrlsum_pipeline.predict(filename,
|
148 |
+
save_results=False, length=length, keywords=keywords, num_beams=1)
|
149 |
else:
|
150 |
return "File Format Error !", None, filename
|
151 |
|
|
|
155 |
return "".join(output), results["raw_text"], filename
|
156 |
|
157 |
|
158 |
+
ctrlsum_str_example = "Language model pre-training has been shown to be effective for improving many natural language processing tasks ( Dai and Le , 2015 ; Peters et al. , 2018a ; Radford et al. , 2018 ; Howard and Ruder , 2018 ) . These include sentence-level tasks such as natural language inference ( Bowman et al. , 2015 ; Williams et al. , 2018 ) and paraphrasing ( Dolan and Brockett , 2005 ) , which aim to predict the relationships between sentences by analyzing them holistically , as well as token-level tasks such as named entity recognition and question answering , where models are required to produce fine-grained output at the token level ( Tjong Kim Sang and De Meulder , 2003 ; Rajpurkar et al. , 2016 ) . There are two existing strategies for applying pre-trained language representations to downstream tasks : feature-based and fine-tuning . The feature-based approach , such as ELMo ( Peters et al. , 2018a ) , uses task-specific architectures that include the pre-trained representations as additional features . The fine-tuning approach , such as the Generative Pre-trained Transformer ( OpenAI GPT ) ( Radford et al. , 2018 ) , introduces minimal task-specific parameters , and is trained on the downstream tasks by simply fine-tuning all pretrained parameters . The two approaches share the same objective function during pre-training , where they use unidirectional language models to learn general language representations . We argue that current techniques restrict the power of the pre-trained representations , especially for the fine-tuning approaches . The major limitation is that standard language models are unidirectional , and this limits the choice of architectures that can be used during pre-training . For example , in OpenAI GPT , the authors use a left-toright architecture , where every token can only attend to previous tokens in the self-attention layers of the Transformer ( Vaswani et al. , 2017 ) . Such restrictions are sub-optimal for sentence-level tasks , and could be very harmful when applying finetuning based approaches to token-level tasks such as question answering , where it is crucial to incorporate context from both directions . In this paper , we improve the fine-tuning based approaches by proposing BERT : Bidirectional Encoder Representations from Transformers . BERT alleviates the previously mentioned unidirectionality constraint by using a `` masked language model '' ( MLM ) pre-training objective , inspired by the Cloze task ( Taylor , 1953 ) . The masked language model randomly masks some of the tokens from the input , and the objective is to predict the original vocabulary id of the masked arXiv:1810.04805v2 [ cs.CL ] 24 May 2019 word based only on its context . Unlike left-toright language model pre-training , the MLM objective enables the representation to fuse the left and the right context , which allows us to pretrain a deep bidirectional Transformer . In addition to the masked language model , we also use a `` next sentence prediction '' task that jointly pretrains text-pair representations . The contributions of our paper are as follows : • We demonstrate the importance of bidirectional pre-training for language representations . Unlike Radford et al . ( 2018 ) , which uses unidirectional language models for pre-training , BERT uses masked language models to enable pretrained deep bidirectional representations . This is also in contrast to Peters et al . ( 2018a ) , which uses a shallow concatenation of independently trained left-to-right and right-to-left LMs . • We show that pre-trained representations reduce the need for many heavily-engineered taskspecific architectures . BERT is the first finetuning based representation model that achieves state-of-the-art performance on a large suite of sentence-level and token-level tasks , outperforming many task-specific architectures . • BERT advances the state of the art for eleven NLP tasks . The code and pre-trained models are available at https : //github.com/ google-research/bert . "
|
|