text_plain_output_1.png
from transformers import BartTokenizer, BartModel, BartForConditionalGeneration

from transformers import PegasusForConditionalGeneration, PegasusTokenizer

tokenizer_model_1 = PegasusTokenizer.from_pretrained('google/pegasus-large')
loaded_model_1 = PegasusForConditionalGeneration.from_pretrained('google/pegasus-large')

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

def generate_summary_google(text):
    pred = []
    r = len(text)
    for i in range(r):
        texts = text[i]
        tokens = tokenizer_model_1(texts, truncation=True, padding='longest', return_tensors='pt')
        summary = loaded_model_1.generate(**tokens)
        prediction = tokenizer_model_1.decode(summary[0])
        prediction = tokenizer_model_1.decode(summary[0]).replace('<pad>', '').replace('</s>', '')
        pred.append(prediction)
    return pred

def generate_summary_facebook(text):
    pred = []
    for i in range(len(text)):
        texts = text[i]
        tokens = tokenizer_model_2.batch_encode_plus([texts], return_tensors='pt')
        summary = model.generate(tokens['input_ids'], max_length=100, early_stopping=True)
        prediction = tokenizer_model_2.decode(summary[0], skip_special_tokens=True)
        pred.append(prediction)
    return pred

if __name__ == '__main__':
    user_input = input('Enter text to summarize: ')
    text = [user_input]
    summaries_google = generate_summary_google(text)
    summaries_facebook = generate_summary_facebook(text)

print('Google Pegasus Summary:')
print(summaries_google[0])