Update src/summarizer.py
Browse files- src/summarizer.py +70 -47
src/summarizer.py
CHANGED
|
@@ -1,47 +1,70 @@
|
|
| 1 |
-
from transformers import pipeline
|
| 2 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import torch
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
class TextSummarizer:
|
| 6 |
+
def __init__(self, model_name="facebook/bart-large-cnn"):
|
| 7 |
+
"""
|
| 8 |
+
Initialize summarization pipeline
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
model_name (str): Hugging Face model for summarization
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Configure device
|
| 15 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 16 |
+
logging.info(f"Using device: {'cuda' if device == 0 else 'cpu'}")
|
| 17 |
+
|
| 18 |
+
# Initialize pipeline with explicit device mapping and lower precision
|
| 19 |
+
self.summarizer = pipeline(
|
| 20 |
+
"summarization",
|
| 21 |
+
model=model_name,
|
| 22 |
+
device=device,
|
| 23 |
+
torch_dtype=torch.float32
|
| 24 |
+
)
|
| 25 |
+
logging.info("Summarization pipeline initialized successfully")
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logging.error(f"Failed to load summarization model: {str(e)}")
|
| 29 |
+
raise RuntimeError(f"Failed to load summarization model: {str(e)}")
|
| 30 |
+
|
| 31 |
+
def generate_summary(self, text, max_length=400, min_length=100):
|
| 32 |
+
"""
|
| 33 |
+
Generate summary for given text
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
text (str): Input text to summarize
|
| 37 |
+
max_length (int): Maximum length of summary
|
| 38 |
+
min_length (int): Minimum length of summary
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
str: Generated summary
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
# Validate input text
|
| 45 |
+
if not text or len(text.strip()) == 0:
|
| 46 |
+
return "No text provided for summarization."
|
| 47 |
+
|
| 48 |
+
# Ensure min_length is less than max_length
|
| 49 |
+
min_length = min(min_length, max_length)
|
| 50 |
+
|
| 51 |
+
# Generate summary with chunking for long texts
|
| 52 |
+
max_chunk_length = 1024 # BART's max input length
|
| 53 |
+
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
| 54 |
+
summaries = []
|
| 55 |
+
|
| 56 |
+
for chunk in chunks:
|
| 57 |
+
if chunk.strip():
|
| 58 |
+
summary = self.summarizer(
|
| 59 |
+
chunk,
|
| 60 |
+
max_length=max_length // len(chunks), # Distribute length across chunks
|
| 61 |
+
min_length=min_length // len(chunks),
|
| 62 |
+
do_sample=False
|
| 63 |
+
)[0]['summary_text']
|
| 64 |
+
summaries.append(summary)
|
| 65 |
+
|
| 66 |
+
return " ".join(summaries)
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
logging.error(f"Error during summarization: {str(e)}")
|
| 70 |
+
return f"Error during summarization: {str(e)}"
|