nlp-ultimate-tutor / templates /summarization.html
aradhyapavan's picture
nlp ultimate tutor
ca2c89c verified
{% extends "base.html" %}
{% block title %}Text Summarization - NLP Ultimate Tutorial{% endblock %}
{% block content %}
<div class="container">
<!-- Header Section -->
<div class="row mb-4">
<div class="col-12">
<div class="card">
<div class="card-header">
<h1 class="mb-0">
<i class="fas fa-compress-alt"></i>
Text Summarization
</h1>
</div>
<div class="card-body">
<p class="lead">Condense text to capture its main points using both extractive and abstractive summarization techniques.</p>
<div class="alert alert-info">
<i class="fas fa-info-circle"></i>
<strong>About:</strong> Text summarization condenses text to capture its main points, enabling quicker comprehension of large volumes of information.
</div>
</div>
</div>
</div>
</div>
{% include "_analysis_nav.html" %}
<!-- Text Input Section -->
<div class="row mb-4">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i class="fas fa-keyboard"></i>
Enter your text:
</h3>
</div>
<div class="card-body">
<div class="row mb-3">
<div class="col-md-8">
<textarea id="textInput" class="form-control" rows="8" placeholder="Enter or paste your text here... (minimum 3 sentences, 40 words)">Climate change is the long-term alteration of temperature and typical weather patterns in a place. Climate change is already affecting our planet in many ways - from rising sea levels to more frequent extreme weather events. The primary cause of current climate change is largely human activity, like burning fossil fuels, which adds heat-trapping gases to Earth's atmosphere. The consequences of changing climate are already being felt worldwide, affecting ecosystems, agriculture, and human settlements. Addressing climate change requires immediate action to reduce greenhouse gas emissions and adapt to the changes that are already occurring.</textarea>
</div>
<div class="col-md-4">
<label for="sampleSelect" class="form-label">Or choose a sample:</label>
<select id="sampleSelect" class="form-select">
<option value="Custom">Custom</option>
<option value="News Article">News Article</option>
<option value="Product Review">Product Review</option>
<option value="Scientific Text">Scientific Text</option>
<option value="Literary Text">Literary Text</option>
</select>
</div>
</div>
<div class="d-flex justify-content-between align-items-center">
<div>
<button id="processBtn" class="btn btn-primary btn-lg">
<i class="fas fa-compress-alt"></i>
Generate Summary
</button>
</div>
<div>
<button id="clearBtn" class="btn btn-outline-secondary">
<i class="fas fa-trash"></i>
Clear
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Summary Settings Section -->
<div class="row mb-4">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i class="fas fa-cog"></i>
Summary Settings
</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-4">
<label for="minLength" class="form-label">Minimum Length</label>
<input type="range" class="form-range" id="minLength" min="10" max="100" value="30" step="5">
<div class="d-flex justify-content-between">
<small>10</small>
<small id="minLengthValue">30</small>
<small>100</small>
</div>
</div>
<div class="col-md-4">
<label for="maxLength" class="form-range" class="form-label">Maximum Length</label>
<input type="range" class="form-range" id="maxLength" min="50" max="350" value="300" step="10">
<div class="d-flex justify-content-between">
<small>50</small>
<small id="maxLengthValue">300</small>
<small>350</small>
</div>
</div>
<div class="col-md-4">
<div class="form-check mt-4">
<input class="form-check-input" type="checkbox" id="useSampling">
<label class="form-check-label" for="useSampling">
Use sampling for diverse summaries
</label>
<div class="form-text">Enable to generate more creative summaries</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Summarization Methods Info -->
<div class="row mb-4">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i class="fas fa-info-circle"></i>
Summarization Methods
</h3>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="card h-100">
<div class="card-body text-center">
<i class="fas fa-cut fa-2x text-primary mb-2"></i>
<h5>Extractive Summarization</h5>
<p class="small">Selects important sentences from the original text using TextRank algorithm.</p>
<ul class="list-unstyled small text-start">
<li>• Preserves original wording</li>
<li>• Fast and reliable</li>
<li>• Based on sentence importance</li>
</ul>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card h-100">
<div class="card-body text-center">
<i class="fas fa-brain fa-2x text-success mb-2"></i>
<h5>Abstractive Summarization</h5>
<p class="small">Generates new sentences using BART model fine-tuned on CNN/DM dataset.</p>
<ul class="list-unstyled small text-start">
<li>• Creates human-like summaries</li>
<li>• More natural language</li>
<li>• Higher compression ratios</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Results Section -->
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i class="fas fa-chart-bar"></i>
Summarization Results
</h3>
</div>
<div class="card-body">
<div id="resultsContainer">
<div class="text-center text-muted py-5">
<i class="fas fa-arrow-up fa-2x mb-3"></i>
<p>Click "Generate Summary" to see summarization results</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block extra_scripts %}
<script>
// Initialize page
document.addEventListener('DOMContentLoaded', function() {
// Only carry over text when explicitly requested by quick-nav
const shouldCarry = sessionStorage.getItem('carryTextOnNextPage') === '1';
if (shouldCarry) {
const storedText = sessionStorage.getItem('analysisText');
if (storedText) document.getElementById('textInput').value = storedText;
sessionStorage.removeItem('carryTextOnNextPage');
} else {
// Fresh route: leave default sample text
}
// Update slider values
document.getElementById('minLength').addEventListener('input', function() {
document.getElementById('minLengthValue').textContent = this.value;
});
document.getElementById('maxLength').addEventListener('input', function() {
document.getElementById('maxLengthValue').textContent = this.value;
});
// Sample text dropdown handler with Custom restore
document.getElementById('sampleSelect').addEventListener('change', function() {
const sampleType = this.value;
const textInput = document.getElementById('textInput');
const routeKey = 'customTextBackup:' + (window.location.pathname || '/summarization');
if (sampleType === 'Custom') {
// Restore previously typed custom text if any
const backup = sessionStorage.getItem(routeKey);
if (backup !== null) {
textInput.value = backup;
}
return;
}
// Save current custom text before switching away
sessionStorage.setItem(routeKey, textInput.value);
// Get sample text from server
fetch('/api/sample-text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({sample_type: sampleType})
})
.then(response => response.json())
.then(data => {
textInput.value = data.text || '';
});
});
// While typing, keep a backup of custom text for this route
(function(){
const textInput = document.getElementById('textInput');
const routeKey = 'customTextBackup:' + (window.location.pathname || '/summarization');
textInput.addEventListener('input', function(){
sessionStorage.setItem(routeKey, textInput.value);
});
})();
// Process button handler
document.getElementById('processBtn').addEventListener('click', function() {
const text = document.getElementById('textInput').value.trim();
if (!text) {
alert('Please enter some text to summarize.');
return;
}
// Show loading state
this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Processing...';
this.disabled = true;
// Process text
processSummarization();
// Reset button after a delay
setTimeout(() => {
this.innerHTML = '<i class="fas fa-compress-alt"></i> Generate Summary';
this.disabled = false;
}, 2000);
});
// Clear button handler
document.getElementById('clearBtn').addEventListener('click', function() {
document.getElementById('textInput').value = '';
document.getElementById('resultsContainer').innerHTML = `
<div class="text-center text-muted py-5">
<i class="fas fa-arrow-up fa-2x mb-3"></i>
<p>Click "Generate Summary" to see summarization results</p>
</div>
`;
});
// Keyboard shortcuts
document.addEventListener('keydown', function(e) {
// Ctrl+Enter to process
if (e.ctrlKey && e.key === 'Enter') {
document.getElementById('processBtn').click();
}
// Ctrl+L to clear
if (e.ctrlKey && e.key === 'l') {
e.preventDefault();
document.getElementById('clearBtn').click();
}
});
});
// Process summarization
function processSummarization() {
const text = document.getElementById('textInput').value.trim();
if (!text) {
alert('Please enter some text to summarize.');
return;
}
const minLength = parseInt(document.getElementById('minLength').value);
const maxLength = parseInt(document.getElementById('maxLength').value);
const useSampling = document.getElementById('useSampling').checked;
showLoading('resultsContainer');
fetch('/api/summarization', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: text,
min_length: minLength,
max_length: maxLength,
use_sampling: useSampling
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
displayResults(data.result);
} else {
showError(data.error || 'An error occurred while processing the text');
}
})
.catch(error => {
showError('Failed to process text: ' + error.message);
})
.finally(() => {
hideLoading('resultsContainer');
});
}
// Show loading state
function showLoading(elementId) {
const element = document.getElementById(elementId);
if (element) {
element.innerHTML = `
<div class="text-center py-4">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-2">Generating summaries...</p>
</div>
`;
}
}
// Hide loading state
function hideLoading(elementId) {
const element = document.getElementById(elementId);
if (element && element.innerHTML.includes('spinner-border')) {
element.innerHTML = '';
}
}
// Show error message
function showError(message, elementId = 'resultsContainer') {
const element = document.getElementById(elementId);
if (element) {
element.innerHTML = `
<div class="alert alert-danger fade-in">
<i class="fas fa-exclamation-triangle"></i>
<strong>Error:</strong> ${message}
</div>
`;
}
}
// Display results
function displayResults(result) {
const container = document.getElementById('resultsContainer');
if (container) {
container.innerHTML = result;
container.classList.add('fade-in');
// Scroll to results
container.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
</script>
{% endblock %}