Limiting BART HuggingFace Model to complete sentences of maximum length

Viewed 589

I'm implementing BART on HuggingFace, see reference: https://huggingface.co/transformers/model_doc/bart.html

Here is the code from their documentation that works in creating a generated summary:

from transformers import BartModel, BartTokenizer, BartForConditionalGeneration

model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large')

def baseBart(ARTICLE_TO_SUMMARIZE):
  inputs = tokenizer([ARTICLE_TO_SUMMARIZE], max_length=1024, return_tensors='pt')
  # Generate Summary
  summary_ids = model.generate(inputs['input_ids'], num_beams=4, max_length=25, early_stopping=True)
  return [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids][0]

I need to impose conciseness with my summaries so I am setting max_length=25. In doing so though, I'm getting incomplete sentences such as these two examples:

EX1: The opacity at the left lung base appears stable from prior exam. There is elevation of the left hemidi

EX 2: There is normal mineralization and alignment. No fracture or osseous lesion is identified. The ankle mort

How do I make sure that the predicted summary is only coherent sentences with complete thoughts and remains concise. If possible, I'd prefer to not perform a regex on the summarized output and cut off any text after the last period, but actually have the BART model produce sentences within the the maximum length.

I tried setting truncation=True in the model but that didn't work.

0 Answers
Related