Spaces:
Sleeping
Sleeping
File size: 24,496 Bytes
ca2c89c |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import nltk
from collections import Counter
import networkx as nx
from nltk.corpus import stopwords
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem import WordNetLemmatizer
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from matplotlib_venn import venn2
from utils.model_loader import load_summarizer
from utils.helpers import fig_to_html, df_to_html_table
def summarization_handler(text_input, min_length=30, max_length=300, use_sampling=False):
"""Show text summarization capabilities."""
output_html = []
# Add result area container
output_html.append('<div class="result-area">')
output_html.append('<h2 class="task-header">Text Summarization</h2>')
output_html.append("""
<div class="alert alert-info">
<i class="fas fa-info-circle"></i>
Text summarization condenses text to capture its main points, enabling quicker comprehension of large volumes of information.
</div>
""")
# Model info
output_html.append("""
<div class="alert alert-info">
<h4><i class="fas fa-tools"></i> Models & Techniques Used:</h4>
<ul>
<li><b>Extractive Summarization</b> - Selects important sentences from the original text</li>
<li><b>Abstractive Summarization</b> - BART model fine-tuned on CNN/DM dataset to generate new summary text</li>
<li><b>Performance</b> - ROUGE scores of approximately 40-45 on CNN/DM benchmark</li>
</ul>
</div>
""")
try:
# Check if text is long enough for summarization
sentences = nltk.sent_tokenize(text_input)
word_count = len(text_input.split())
if len(sentences) < 3 or word_count < 40:
output_html.append(f"""
<div class="alert alert-warning">
<h3>Text Too Short for Summarization</h3>
<p>The provided text contains only {len(sentences)} sentences and {word_count} words.
For effective summarization, please provide a longer text (at least 3 sentences and 40 words).</p>
</div>
""")
else:
# Original Text Section
output_html.append('<h3 class="task-subheader">Original Text</h3>')
output_html.append(f"""
<div class="card">
<div class="card-body">
<div class="text-content" style="word-wrap: break-word; word-break: break-word; overflow-wrap: break-word; max-height: 500px; overflow-y: auto; padding: 15px; background-color: #f8f9fa; border-radius: 5px; border: 1px solid #e9ecef; line-height: 1.6;">{text_input}</div>
</div>
</div>
<p>Length: {word_count} words.</p>
""")
# Text Statistics
char_count = len(text_input)
avg_sentence_length = word_count / len(sentences)
avg_word_length = sum(len(word) for word in text_input.split()) / word_count
# Neural Summarization Section
output_html.append('<h3 class="task-subheader">Neural Abstractive Summarization</h3>')
output_html.append('<p>Using BART model to generate a human-like summary</p>')
# Parameter summary
output_html.append(f"""
<div class="alert alert-light">
<span><strong>Parameters:</strong> Min Length: {min_length} | Max Length: {max_length} | Sampling: {'Enabled' if use_sampling else 'Disabled'}</span>
</div>
""")
try:
# Load summarizer model
summarizer = load_summarizer()
if summarizer is None:
output_html.append("""
<div class="alert alert-danger">
<p>Failed to load the abstractive summarization model. This may be due to memory constraints or missing dependencies.</p>
</div>
""")
else:
# Check length limitations
max_token_limit = 1024 # BART typically has 1024 token limit
# If text is too long, warn user and truncate
if word_count > max_token_limit:
output_html.append(f"""
<div class="alert alert-warning">
<p><b>⚠️ Note:</b> Text exceeds model's length limit. Only the first ~{max_token_limit} tokens will be used for summarization.</p>
</div>
""")
# Generate summary using the specified min_length and max_length
abstractive_results = summarizer(
text_input,
max_length=max_length,
min_length=min_length,
do_sample=use_sampling,
temperature=0.7 if use_sampling else 1.0,
top_p=0.9 if use_sampling else 1.0,
length_penalty=2.0
)
abstractive_summary = abstractive_results[0]['summary_text']
# Calculate reduction statistics
abstractive_word_count = len(abstractive_summary.split())
abstractive_reduction = (1 - abstractive_word_count / word_count) * 100
# Summary Results
output_html.append(f"""
<div class="card">
<div class="card-header">
<h4 class="mb-0">Neural Summary</h4>
</div>
<div class="card-body">
<div style="line-height: 1.6;">
{abstractive_summary}
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-4">
<div class="card text-center">
<div class="card-body">
<h5 class="text-muted">Original Length</h5>
<h3 class="text-primary">{word_count} words</h3>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card text-center">
<div class="card-body">
<h5 class="text-muted">Summary Length</h5>
<h3 class="text-success">{abstractive_word_count} words</h3>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card text-center">
<div class="card-body">
<h5 class="text-muted">Compression</h5>
<h3 class="text-info">{abstractive_reduction:.1f}%</h3>
</div>
</div>
</div>
</div>
""")
# Key Terms & Topics Section
output_html.append('<h3 class="task-subheader">Key Topics & Terms</h3>')
# Extract key terms with TF-IDF
key_terms = extract_key_terms(text_input, n=10)
# Create layout stacked vertically: table first, then chart
output_html.append('<div class="row">')
# Row 1: Key terms table (full width)
output_html.append('<div class="col-12">')
output_html.append('<h4>Key Terms</h4>')
# Create key terms table
terms_df = pd.DataFrame({
'#': range(1, len(key_terms) + 1),
'Keyword': [term[0] for term in key_terms],
'TF-IDF Score': [f"{term[1]:.4f}" for term in key_terms]
})
output_html.append(df_to_html_table(terms_df))
output_html.append('</div>') # Close row 1 column
output_html.append('</div>') # Close row 1
# Row 2: Term importance chart (full width)
output_html.append('<div class="row mt-3">')
output_html.append('<div class="col-12">')
output_html.append('<h4>Term Importance</h4>')
# Create horizontal bar chart of key terms
fig = plt.figure(figsize=(10, 8))
# Reverse the order for bottom-to-top display
terms = [term[0] for term in key_terms]
scores = [term[1] for term in key_terms]
# Sort by score for better visualization
sorted_data = sorted(zip(terms, scores), key=lambda x: x[1])
terms = [x[0] for x in sorted_data]
scores = [x[1] for x in sorted_data]
# Create horizontal bar chart
plt.barh(terms, scores, color='#1976D2')
plt.xlabel('TF-IDF Score')
plt.ylabel('Keyword')
plt.title('Key Terms by TF-IDF Score')
plt.tight_layout()
output_html.append(fig_to_html(fig))
output_html.append('</div>') # Close row 2 column
output_html.append('</div>') # Close row 2
except Exception as e:
output_html.append(f"""
<div class="alert alert-danger">
<h4>Abstractive Summarization Error</h4>
<p>Failed to perform abstractive summarization: {str(e)}</p>
</div>
""")
# Extractive Summarization
output_html.append('<h3 class="task-subheader">Extractive Summarization</h3>')
output_html.append("""
<div class="alert alert-light">
<p class="mb-0">
Extractive summarization works by identifying important sentences in the text and extracting them to form a summary.
This implementation uses a variant of the TextRank algorithm, which is based on Google's PageRank.
</p>
</div>
""")
# Perform TextRank Summarization
extractive_summary = textrank_summarize(text_input, num_sentences=min(3, max(1, len(sentences) // 3)))
# Clean up the placeholder separator
extractive_summary = extractive_summary.replace("SENTBREAKOS.OS", " ")
# Calculate reduction statistics
extractive_word_count = len(extractive_summary.split())
extractive_reduction = (1 - extractive_word_count / word_count) * 100
output_html.append(f"""
<div class="alert alert-success">
<h4>Extractive Summary ({extractive_reduction:.1f}% reduction)</h4>
<div style="line-height: 1.6;">
{extractive_summary}
</div>
</div>
""")
# Sentence importance visualization
output_html.append('<h4>Sentence Importance</h4>')
output_html.append('<p>The graph below shows the relative importance of each sentence based on the TextRank algorithm:</p>')
# Get sentence scores from TextRank
sentence_scores = textrank_sentence_scores(text_input)
# Sort sentences by their original order
sentence_items = list(sentence_scores.items())
sentence_items.sort(key=lambda x: int(x[0].split('_')[1]))
# Create visualization
fig = plt.figure(figsize=(10, 6))
bars = plt.bar(
[f"Sent {item[0].split('_')[1]}" for item in sentence_items],
[item[1] for item in sentence_items],
color='#1976D2'
)
# Highlight selected sentences
selected_indices = [int(idx.split('_')[1]) for idx in sentence_scores.keys() if idx in extractive_summary.split('SENTBREAKOS.OS')]
for i, bar in enumerate(bars):
if i+1 in selected_indices:
bar.set_color('#4CAF50')
plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.02,
'Selected', ha='center', va='bottom', fontsize=8, rotation=90)
plt.xlabel('Sentence')
plt.ylabel('Importance Score')
plt.title('Sentence Importance Based on TextRank')
plt.xticks(rotation=45)
plt.tight_layout()
output_html.append(fig_to_html(fig))
# Compare the two approaches
output_html.append('<h3 class="task-subheader">Summary Comparison</h3>')
# Calculate overlap between summaries
extractive_words = set(re.findall(r'\b\w+\b', extractive_summary.lower()))
abstractive_words = set(re.findall(r'\b\w+\b', abstractive_summary.lower()))
common_words = extractive_words.intersection(abstractive_words)
if len(extractive_words) > 0 and len(abstractive_words) > 0:
overlap_percentage = len(common_words) / ((len(extractive_words) + len(abstractive_words)) / 2) * 100
else:
overlap_percentage = 0
# Create comparison table
comparison_data = {
'Metric': ['Word Count', 'Reduction %', 'Sentences', 'Words per Sentence', 'Unique Words'],
'Extractive': [
extractive_word_count,
f"{extractive_reduction:.1f}%",
len(nltk.sent_tokenize(extractive_summary)),
f"{extractive_word_count / max(1, len(nltk.sent_tokenize(extractive_summary))):.1f}",
len(extractive_words)
],
'Abstractive': [
abstractive_word_count,
f"{abstractive_reduction:.1f}%",
len(nltk.sent_tokenize(abstractive_summary)),
f"{abstractive_word_count / max(1, len(nltk.sent_tokenize(abstractive_summary))):.1f}",
len(abstractive_words)
]
}
comparison_df = pd.DataFrame(comparison_data)
output_html.append('<div class="row">')
# Column 1: Comparison table
output_html.append('<div class="col-md-6">')
output_html.append('<h4>Summary Statistics</h4>')
output_html.append(df_to_html_table(comparison_df))
output_html.append('</div>')
# Column 2: Venn diagram of word overlap
output_html.append('<div class="col-md-6">')
output_html.append('<h4>Word Overlap Visualization</h4>')
# Create Venn diagram
fig = plt.figure(figsize=(8, 6))
venn = venn2(
subsets=(
len(extractive_words - abstractive_words),
len(abstractive_words - extractive_words),
len(common_words)
),
set_labels=('Extractive', 'Abstractive')
)
# Set colors
venn.get_patch_by_id('10').set_color('#4CAF50')
venn.get_patch_by_id('01').set_color('#03A9F4')
venn.get_patch_by_id('11').set_color('#9C27B0')
plt.title('Word Overlap Between Summaries')
plt.text(0, -0.25, f"Overlap: {overlap_percentage:.1f}%", ha='center')
output_html.append(fig_to_html(fig))
# Show key shared and unique words
shared_words_list = list(common_words)
extractive_only = list(extractive_words - abstractive_words)
abstractive_only = list(abstractive_words - extractive_words)
# Limit the number of words shown
max_words = 10
output_html.append(f"""
<div class="mt-3">
<h5>Key Shared Words ({min(max_words, len(shared_words_list))} of {len(shared_words_list)})</h5>
<div class="d-flex flex-wrap gap-1 mb-2">
{' '.join([f'<span class="badge bg-primary">{word}</span>' for word in shared_words_list[:max_words]])}
</div>
<h5>Unique to Extractive ({min(max_words, len(extractive_only))} of {len(extractive_only)})</h5>
<div class="d-flex flex-wrap gap-1 mb-2">
{' '.join([f'<span class="badge bg-success">{word}</span>' for word in extractive_only[:max_words]])}
</div>
<h5>Unique to Abstractive ({min(max_words, len(abstractive_only))} of {len(abstractive_only)})</h5>
<div class="d-flex flex-wrap gap-1 mb-2">
{' '.join([f'<span class="badge bg-info">{word}</span>' for word in abstractive_only[:max_words]])}
</div>
</div>
""")
output_html.append('</div>') # Close column 2
output_html.append('</div>') # Close row
except Exception as e:
output_html.append(f"""
<div class="alert alert-danger">
<h3>Error</h3>
<p>Failed to summarize text: {str(e)}</p>
</div>
""")
# About Text Summarization section
output_html.append("""
<div class="card mt-4">
<div class="card-header">
<h4 class="mb-0">
<i class="fas fa-info-circle"></i>
About Text Summarization
</h4>
</div>
<div class="card-body">
<h5>What is Text Summarization?</h5>
<p>Text summarization is the process of creating a shorter version of a text while preserving its key information
and meaning. It helps users quickly grasp the main points without reading the entire document.</p>
<h5>Two Main Approaches:</h5>
<ul>
<li><b>Extractive Summarization:</b> Selects and extracts existing sentences from the source text based on their importance</li>
<li><b>Abstractive Summarization:</b> Generates new sentences that capture the meaning of the source text (similar to how humans write summaries)</li>
</ul>
<h5>Applications:</h5>
<ul>
<li><b>News digests</b> - Quick summaries of news articles</li>
<li><b>Research papers</b> - Condensing long academic papers</li>
<li><b>Legal documents</b> - Summarizing complex legal text</li>
<li><b>Meeting notes</b> - Extracting key points from discussions</li>
<li><b>Content curation</b> - Creating snippets for content recommendations</li>
</ul>
</div>
</div>
""")
output_html.append('</div>') # Close result-area div
return '\n'.join(output_html)
def extract_key_terms(text, n=10):
"""Extract key terms using TF-IDF"""
try:
# Tokenize and preprocess
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()
# Tokenize and clean text
words = word_tokenize(text.lower())
words = [lemmatizer.lemmatize(word) for word in words
if word.isalnum() and word not in stop_words and len(word) > 2]
# Create document for TF-IDF
document = [' '.join(words)]
# Create TF-IDF vectorizer
vectorizer = TfidfVectorizer(max_features=100)
tfidf_matrix = vectorizer.fit_transform(document)
# Get feature names and scores
feature_names = vectorizer.get_feature_names_out()
scores = tfidf_matrix.toarray()[0]
# Create term-score pairs and sort by score
term_scores = [(term, score) for term, score in zip(feature_names, scores)]
term_scores.sort(key=lambda x: x[1], reverse=True)
return term_scores[:n]
except Exception as e:
print(f"Error extracting key terms: {str(e)}")
return [("term", 0.0) for _ in range(n)] # Return empty placeholder
# TextRank extractive summarization algorithm
def textrank_summarize(text, num_sentences=3):
"""Generate an extractive summary using TextRank algorithm"""
# Tokenize text into sentences
sentences = sent_tokenize(text)
# If text is too short, return the original text
if len(sentences) <= num_sentences:
return text
# Build a graph of sentences with similarity edges
sentence_scores = textrank_sentence_scores(text)
# Sort sentences by score
ranked_sentences = sorted([(score, i, s) for i, (s, score) in enumerate(zip(sentences, sentence_scores.values()))], reverse=True)
# Select top sentences based on score
selected_sentences = sorted(ranked_sentences[:num_sentences], key=lambda x: x[1])
# Combine selected sentences
summary = "SENTBREAKOS.OS".join([s[2] for s in selected_sentences])
return summary
def textrank_sentence_scores(text):
"""Generate sentence scores using TextRank algorithm"""
# Tokenize text into sentences
sentences = sent_tokenize(text)
# Create sentence IDs
sentence_ids = [f"sentence_{i+1}" for i in range(len(sentences))]
# Create sentence graph
G = nx.Graph()
# Add nodes
for sentence_id in sentence_ids:
G.add_node(sentence_id)
# Remove stopwords and preprocess sentences
stop_words = set(stopwords.words('english'))
sentence_words = []
for sentence in sentences:
words = [word.lower() for word in word_tokenize(sentence) if word.lower() not in stop_words and word.isalnum()]
sentence_words.append(words)
# Add edges based on sentence similarity
for i in range(len(sentence_ids)):
for j in range(i+1, len(sentence_ids)):
similarity = sentence_similarity(sentence_words[i], sentence_words[j])
if similarity > 0:
G.add_edge(sentence_ids[i], sentence_ids[j], weight=similarity)
# Run PageRank
scores = nx.pagerank(G)
return scores
def sentence_similarity(words1, words2):
"""Calculate similarity between two sentences based on word overlap"""
if not words1 or not words2:
return 0
# Convert to sets for intersection
set1 = set(words1)
set2 = set(words2)
# Jaccard similarity
intersection = len(set1.intersection(set2))
union = len(set1.union(set2))
if union == 0:
return 0
return intersection / union
|