File size: 8,785 Bytes
634567d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Batch Processing Page

let batchQueue = [];
let batchResults = [];

// DOM Elements
const loadSamplesBtn = document.getElementById('loadSamplesBtn');
const runBatchBtn = document.getElementById('runBatchBtn');
const exportBtn = document.getElementById('exportBtn');
const tableBody = document.getElementById('batchTableBody');

// Sample texts for demo
const sampleTexts = [
    {
        text: "Artificial intelligence has revolutionized the way we interact with technology. Machine learning algorithms can now process vast amounts of data and identify patterns that humans might miss. Deep learning neural networks have enabled breakthroughs in computer vision, natural language processing, and speech recognition. These advances are transforming industries from healthcare to finance.",
        models: ['textrank', 'bart', 'pegasus']
    },
    {
        text: "Climate change poses one of the greatest challenges to humanity. Rising global temperatures are causing ice caps to melt and sea levels to rise. Extreme weather events are becoming more frequent and severe. Scientists warn that without immediate action, the consequences could be catastrophic for future generations.",
        models: ['textrank', 'bart']
    },
    {
        text: "The human brain is the most complex organ in the body, containing approximately 86 billion neurons. These neurons communicate through electrical and chemical signals, forming intricate networks that enable thought, memory, and consciousness. Neuroscientists continue to uncover the mysteries of how the brain processes information and generates our subjective experiences.",
        models: ['bart', 'pegasus']
    }
];

// Load sample documents
loadSamplesBtn.addEventListener('click', function() {
    batchQueue = [...sampleTexts];
    renderTable();
});

// Run batch processing
runBatchBtn.addEventListener('click', async function() {
    if (batchQueue.length === 0) {
        alert('No items in queue. Please load samples first.');
        return;
    }
    
    runBatchBtn.disabled = true;
    runBatchBtn.textContent = 'Processing...';
    
    for (let i = 0; i < batchQueue.length; i++) {
        const item = batchQueue[i];
        item.status = 'processing';
        renderTable();
        
        try {
            const results = {};
            
            for (const model of item.models) {
                const response = await fetch('/api/summarize', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        text: item.text,
                        model: model
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    results[model] = {
                        summary: data.summary,
                        metadata: data.metadata
                    };
                }
            }
            
            item.results = results;
            item.status = 'complete';
            batchResults.push(item);
            
        } catch (error) {
            item.status = 'error';
            item.error = error.message;
        }
        
        renderTable();
    }
    
    runBatchBtn.disabled = false;
    runBatchBtn.textContent = 'Run Batch';
});

// Export results to CSV
exportBtn.addEventListener('click', function() {
    if (batchResults.length === 0) {
        alert('No results to export. Please run batch processing first.');
        return;
    }
    
    let csv = 'Source Text,Model,Summary,Processing Time (s),Compression Ratio\n';
    
    batchResults.forEach(item => {
        if (item.results) {
            Object.keys(item.results).forEach(model => {
                const result = item.results[model];
                const sourceText = item.text.replace(/"/g, '""').substring(0, 100) + '...';
                const summary = result.summary.replace(/"/g, '""');
                const time = result.metadata.processing_time.toFixed(2);
                const compression = (result.metadata.compression_ratio * 100).toFixed(1) + '%';
                
                csv += `"${sourceText}","${model}","${summary}",${time},${compression}\n`;
            });
        }
    });
    
    // Download CSV
    const blob = new Blob([csv], { type: 'text/csv' });
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = 'batch_results_' + new Date().toISOString().split('T')[0] + '.csv';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
});

// Render table
function renderTable() {
    if (batchQueue.length === 0) {
        tableBody.innerHTML = `
            <tr class="empty-state">
                <td colspan="4">
                    <div class="empty-message">
                        No items in the queue. Load samples or upload a CSV to begin.
                    </div>
                </td>
            </tr>
        `;
        return;
    }
    
    tableBody.innerHTML = batchQueue.map((item, index) => {
        const preview = item.text.substring(0, 80) + '...';
        const modelBadges = item.models.map(m => 
            `<span class="model-badge">${m.toUpperCase()}</span>`
        ).join('');
        
        let statusBadge = '';
        if (!item.status || item.status === 'pending') {
            statusBadge = '<span class="status-badge status-pending">Pending</span>';
        } else if (item.status === 'processing') {
            statusBadge = '<span class="status-badge status-processing">Processing...</span>';
        } else if (item.status === 'complete') {
            statusBadge = '<span class="status-badge status-complete">Complete</span>';
        } else if (item.status === 'error') {
            statusBadge = '<span class="status-badge status-error">Error</span>';
        }
        
        return `
            <tr>
                <td><div class="source-preview">${preview}</div></td>
                <td><div class="model-badges">${modelBadges}</div></td>
                <td>${statusBadge}</td>
                <td>
                    <div class="action-buttons">
                        <button class="btn-icon" onclick="viewItem(${index})" ${item.status !== 'complete' ? 'disabled' : ''}>View</button>
                        <button class="btn-icon" onclick="removeItem(${index})">Remove</button>
                    </div>
                </td>
            </tr>
        `;
    }).join('');
}

// View item results
function viewItem(index) {
    const item = batchQueue[index];
    if (!item.results) return;
    
    let resultsHtml = '<div style="max-width: 800px; margin: 0 auto;">';
    resultsHtml += '<h3 style="margin-bottom: 1rem;">Batch Results</h3>';
    resultsHtml += `<p style="color: #6D8196; margin-bottom: 2rem;"><strong>Source:</strong> ${item.text.substring(0, 200)}...</p>`;
    
    Object.keys(item.results).forEach(model => {
        const result = item.results[model];
        resultsHtml += `
            <div style="margin-bottom: 2rem; padding: 1.5rem; background: #F5F0F6; border-radius: 8px;">
                <h4 style="margin-bottom: 0.5rem; color: #4A4A4A;">${model.toUpperCase()}</h4>
                <p style="line-height: 1.8; margin-bottom: 1rem;">${result.summary}</p>
                <div style="display: flex; gap: 2rem; font-size: 0.9rem; color: #6D8196;">
                    <span><strong>Time:</strong> ${result.metadata.processing_time.toFixed(2)}s</span>
                    <span><strong>Compression:</strong> ${(result.metadata.compression_ratio * 100).toFixed(1)}%</span>
                </div>
            </div>
        `;
    });
    
    resultsHtml += '</div>';
    
    // Create modal
    const modal = document.createElement('div');
    modal.style.cssText = 'position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 9999; padding: 2rem;';
    modal.innerHTML = `
        <div style="background: white; border-radius: 12px; padding: 2rem; max-height: 90vh; overflow-y: auto; position: relative;">
            <button onclick="this.parentElement.parentElement.remove()" style="position: absolute; top: 1rem; right: 1rem; background: none; border: none; font-size: 1.5rem; cursor: pointer; color: #4A4A4A;">×</button>
            ${resultsHtml}
        </div>
    `;
    document.body.appendChild(modal);
}

// Remove item from queue
function removeItem(index) {
    batchQueue.splice(index, 1);
    renderTable();
}

// Initial render
renderTable();