Spaces:
Running
Running
from typing import List, Tuple | |
import torch | |
from SciAssist import Summarization | |
device = "gpu" if torch.cuda.is_available() else "cpu" | |
ctrlsum_pipeline = Summarization(os_name="nt",checkpoint="google/flan-t5-base") | |
def ctrlsum_for_str(input,length=None, keywords=None) -> List[Tuple[str, str]]: | |
if keywords is not None: | |
keywords = keywords.strip().split(",") | |
if keywords[0] == "": | |
keywords = None | |
if length==0 or length is None: | |
length = None | |
results = ctrlsum_pipeline.predict(input, type="str", | |
length=length, keywords=keywords) | |
output = [] | |
for res in results["summary"]: | |
output.append(f"{res}\n\n") | |
return "".join(output) | |
def ctrlsum_for_file(input, length=None, keywords=None, text="") -> List[Tuple[str, str]]: | |
if input == None: | |
if text=="": | |
return None | |
else: | |
return ctrlsum_for_str(text,length,keywords),text | |
else: | |
filename = input.name | |
if keywords is not None: | |
keywords = keywords.strip().split(",") | |
if keywords[0] == "": | |
keywords = None | |
if length==0: | |
length = None | |
# Identify the format of input and parse reference strings | |
if filename[-4:] == ".txt": | |
results = ctrlsum_pipeline.predict(filename, type="txt", | |
save_results=False, | |
length=length, keywords=keywords) | |
elif filename[-4:] == ".pdf": | |
results = ctrlsum_pipeline.predict(filename, | |
save_results=False, length=length, keywords=keywords) | |
else: | |
return [("File Format Error !", None)] | |
output = [] | |
for res in results["summary"]: | |
output.append(f"{res}\n\n") | |
return "".join(output), results["raw_text"] | |
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 . " |