File size: 1,575 Bytes
ce9b735 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | from transformers import pipeline
import time
class TextSummarizer:
def __init__(self, model_name="facebook/bart-large-cnn"):
"""
Initialize summarization pipeline
Args:
model_name (str): Hugging Face model for summarization
"""
try:
self.summarizer = pipeline("summarization", model=model_name)
except Exception as e:
raise RuntimeError(f"Failed to load summarization model: {e}")
def generate_summary(self, text, max_length=400, min_length=100):
"""
Generate summary for given text
Args:
text (str): Input text to summarize
max_length (int): Maximum length of summary
min_length (int): Minimum length of summary
Returns:
str: Generated summary
"""
try:
# Validate input text
if not text or len(text.strip()) == 0:
return "No text provided for summarization."
# Ensure min_length is less than max_length
min_length = min(min_length, max_length)
# Generate summary
summary = self.summarizer(
text,
max_length=max_length,
min_length=min_length,
do_sample=False
)[0]['summary_text']
return summary
except Exception as e:
return f"Error during summarization: {e}"
|