File size: 871 Bytes
a76ffcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from transformers import pipeline


class Summary:

    def __init__(self, text_to_summarise):
        """
        The Constructor for the Summary class.
        :param text_to_summarise: The text to summarise
        """
        if text_to_summarise is None or len(text_to_summarise.strip()) == 0:
            raise ValueError("Text to summarise cannot be empty")

        self.text_to_summarise = text_to_summarise

        summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

        self.source_text_length = len(text_to_summarise.split(' '))
        max = int(self.source_text_length * 0.8)
        min = int(self.source_text_length * 0.6)
        result = summarizer(text_to_summarise, max_length=max, min_length=min, do_sample=False)
        self.result = result[0]['summary_text']
        self.summary_text_length = len(self.result.split(' '))