File size: 2,646 Bytes
ac4bd9a | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | from transformers import pipeline
import torch
import logging
class TextSummarizer:
def __init__(self, model_name="facebook/bart-large-cnn"):
"""
Initialize summarization pipeline
Args:
model_name (str): Hugging Face model for summarization
"""
try:
# Configure device
device = 0 if torch.cuda.is_available() else -1
logging.info(f"Using device: {'cuda' if device == 0 else 'cpu'}")
# Initialize pipeline with explicit device mapping and lower precision
self.summarizer = pipeline(
"summarization",
model=model_name,
device=device,
torch_dtype=torch.float32
)
logging.info("Summarization pipeline initialized successfully")
except Exception as e:
logging.error(f"Failed to load summarization model: {str(e)}")
raise RuntimeError(f"Failed to load summarization model: {str(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 with chunking for long texts
max_chunk_length = 1024 # BART's max input length
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
summaries = []
for chunk in chunks:
if chunk.strip():
summary = self.summarizer(
chunk,
max_length=max_length // len(chunks), # Distribute length across chunks
min_length=min_length // len(chunks),
do_sample=False
)[0]['summary_text']
summaries.append(summary)
return " ".join(summaries)
except Exception as e:
logging.error(f"Error during summarization: {str(e)}")
return f"Error during summarization: {str(e)}" |