File size: 13,263 Bytes
467b79d
1788598
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc1c3d9
 
467b79d
dc1c3d9
467b79d
 
 
 
 
 
 
 
 
 
 
 
 
 
063ac44
467b79d
 
 
 
 
 
 
 
 
063ac44
 
 
467b79d
063ac44
 
467b79d
 
 
b0ad6a5
063ac44
b0ad6a5
063ac44
 
467b79d
 
 
 
063ac44
 
 
b0ad6a5
063ac44
467b79d
 
7403b8d
063ac44
 
 
 
 
b0ad6a5
 
 
063ac44
b0ad6a5
 
063ac44
 
b0ad6a5
 
063ac44
 
7403b8d
 
063ac44
467b79d
063ac44
467b79d
063ac44
 
467b79d
 
 
 
063ac44
b0ad6a5
063ac44
 
467b79d
 
b0ad6a5
063ac44
 
 
 
467b79d
 
 
 
b0ad6a5
 
 
 
 
467b79d
 
063ac44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467b79d
 
 
063ac44
467b79d
 
 
063ac44
 
 
b0ad6a5
467b79d
b0ad6a5
 
063ac44
b0ad6a5
 
 
 
 
063ac44
 
 
467b79d
 
b0ad6a5
 
063ac44
467b79d
 
b0ad6a5
 
063ac44
467b79d
 
 
 
b0ad6a5
 
063ac44
 
467b79d
 
 
b0ad6a5
 
 
 
 
 
 
467b79d
 
 
063ac44
 
 
b0ad6a5
467b79d
 
 
 
 
 
 
 
063ac44
467b79d
 
 
063ac44
467b79d
 
063ac44
467b79d
 
063ac44
 
 
 
 
467b79d
 
 
 
 
b0ad6a5
 
19564f5
063ac44
 
467b79d
063ac44
467b79d
 
 
b0ad6a5
467b79d
 
 
 
063ac44
b0ad6a5
 
467b79d
 
063ac44
 
 
 
 
467b79d
 
b0ad6a5
467b79d
 
 
19a0594
467b79d
 
 
 
 
19a0594
467b79d
 
 
 
 
 
 
19564f5
467b79d
19564f5
467b79d
19564f5
467b79d
19564f5
 
063ac44
b0ad6a5
19564f5
b0ad6a5
19564f5
 
 
19a0594
19564f5
 
 
b0ad6a5
063ac44
19564f5
b0ad6a5
19564f5
7403b8d
19564f5
 
 
b0ad6a5
 
19564f5
b0ad6a5
19564f5
 
467b79d
 
 
a6d731a
467b79d
19564f5
 
 
 
 
 
467b79d
 
a6d731a
467b79d
 
 
 
 
 
 
a6d731a
467b79d
 
a6d731a
467b79d
7403b8d
467b79d
b0ad6a5
467b79d
 
 
a6d731a
467b79d
 
 
 
 
 
a6d731a
 
 
 
 
 
 
 
 
 
 
7403b8d
a6d731a
 
7403b8d
 
a6d731a
7403b8d
467b79d
a6d731a
7403b8d
b0ad6a5
 
467b79d
 
 
 
 
 
 
 
 
 
a6d731a
467b79d
 
063ac44
467b79d
 
 
 
a6d731a
467b79d
 
 
a6d731a
467b79d
 
 
 
 
 
 
a6d731a
7403b8d
19564f5
467b79d
e387d27
dc1c3d9
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
<!-- <!doctype html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width" />
		<title>My static Space</title>
		<link rel="stylesheet" href="style.css" />
	</head>
	<body>
		<div class="card">
			<h1>Welcome to your static Space!</h1>
			<p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
			<p>
				Also don't forget to check the
				<a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
			</p>
		</div>
	</body>
</html> -->


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>AI Text Summarization</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .card {
            background: white;
            border-radius: 8px;
            padding: 32px 40px;
            max-width: 680px;
            width: 100%;
            box-shadow: 0 1px 3px rgba(0,0,0,0.08);
            border: 1px solid #e8e8e8;
        }
        
        h1 {
            color: #1a1a1a;
            font-size: 1.6rem;
            font-weight: 600;
            margin-bottom: 4px;
            letter-spacing: -0.3px;
        }
        
        .subtitle {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 24px;
            padding-bottom: 16px;
            border-bottom: 1px solid #eee;
            font-weight: 400;
        }
        
        .model-status {
            margin: -8px 0 20px 0;
            padding: 8px 0;
            font-size: 0.85rem;
            color: #2d7a3a;
            font-weight: 400;
        }
        
        .model-status.loading {
            color: #b45309;
        }
        
        .model-status.error {
            color: #b91c1c;
        }
        
        .input-group {
            margin-bottom: 16px;
        }
        
        .input-group label {
            display: block;
            font-weight: 500;
            color: #333;
            margin-bottom: 6px;
            font-size: 0.9rem;
        }
        
        textarea {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid #d0d0d0;
            border-radius: 4px;
            font-size: 14px;
            font-family: inherit;
            resize: vertical;
            transition: border-color 0.2s;
            min-height: 120px;
            background: white;
            color: #333;
            line-height: 1.5;
        }
        
        textarea:focus {
            outline: none;
            border-color: #4a6cf7;
        }
        
        textarea::placeholder {
            color: #999;
        }
        
        .output-group {
            margin-bottom: 16px;
        }
        
        .output-group label {
            display: block;
            font-weight: 500;
            color: #333;
            margin-bottom: 6px;
            font-size: 0.9rem;
        }
        
        #outputText {
            background: #fafafa;
            padding: 10px 12px;
            border-radius: 4px;
            min-height: 80px;
            border: 1px solid #e8e8e8;
            color: #333;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-size: 14px;
            line-height: 1.5;
        }
        
        #outputText .placeholder {
            color: #999;
        }
        
        #outputText.error {
            color: #b91c1c;
        }
        
        #outputText.loading {
            color: #4a6cf7;
        }
        
        .button-group {
            display: flex;
            gap: 10px;
            margin-top: 4px;
        }
        
        button {
            padding: 8px 24px;
            border-radius: 4px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            border: 1px solid transparent;
            background: transparent;
        }
        
        .submit-btn {
            background: #4a6cf7;
            color: white;
            border-color: #4a6cf7;
            order: 2;
            margin-left: auto;
        }
        
        .submit-btn:hover:not(:disabled) {
            background: #3b5de7;
            border-color: #3b5de7;
        }
        
        .submit-btn:disabled {
            background: #a0aec0;
            border-color: #a0aec0;
            cursor: not-allowed;
        }
        
        .clear-btn {
            color: #4a5568;
            border-color: #d0d0d0;
            background: white;
            order: 1;
        }
        
        .clear-btn:hover:not(:disabled) {
            background: #f7fafc;
            border-color: #a0aec0;
        }
        
        .clear-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        
        .token-info {
            margin-top: 8px;
            font-size: 0.8rem;
            color: #999;
            text-align: right;
        }
        
        @media (max-width: 600px) {
            .card {
                padding: 20px;
            }
            
            h1 {
                font-size: 1.3rem;
            }
            
            .button-group {
                flex-wrap: wrap;
            }
            
            .submit-btn {
                flex: 1;
            }
            
            .clear-btn {
                flex: 0.4;
                min-width: 80px;
            }
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>AI Text summarization</h1>
        <div class="subtitle">Powered by FalconsAI T5 Sequence-to-Sequence model.</div>
        
        <!-- Model status - styled to blend in, not stand out -->
        <div id="modelStatus" class="model-status loading">⏳ Loading model...</div>
        
        <div class="input-group">
            <label for="inputText">Input text to summarize:</label>
            <textarea 
                id="inputText" 
                rows="6" 
                placeholder="Paste document text here..."
            ></textarea>
        </div>
        
        <div class="output-group">
            <label for="outputText">Summarized text:</label>
            <div id="outputText"><span class="placeholder">Your summary will appear here...</span></div>
            <div id="tokenInfo" class="token-info"></div>
        </div>
        
        <div class="button-group">
            <button id="clearButton" class="clear-btn" disabled>Clear</button>
            <button id="summarizeButton" class="submit-btn" disabled>Submit</button>
        </div>
    </div>

    <!-- Use the official @huggingface/transformers import map -->
    <script type="importmap">
        {
            "imports": {
                "@huggingface/transformers": "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.0/dist/transformers.min.js"
            }
        }
    </script>
    
    <script type="module">
        import { pipeline } from '@huggingface/transformers';
        
        // DOM elements
        const inputText = document.getElementById('inputText');
        const outputText = document.getElementById('outputText');
        const tokenInfo = document.getElementById('tokenInfo');
        const summarizeBtn = document.getElementById('summarizeButton');
        const clearBtn = document.getElementById('clearButton');
        const modelStatus = document.getElementById('modelStatus');
        
        // Initialize the summarization pipeline
        let summarizer = null;
        let isModelReady = false;
        
        async function initModel() {
            try {
                modelStatus.textContent = '⏳ Loading model...';
                modelStatus.className = 'model-status loading';
                summarizeBtn.disabled = true;
                clearBtn.disabled = true;
                
                summarizer = await pipeline(
                    'summarization', 
                    'onnx-community/text_summarization-ONNX'
                );
                
                isModelReady = true;
                modelStatus.textContent = 'βœ… Model loaded successfully! Ready to summarize.';
                modelStatus.className = 'model-status';
                summarizeBtn.disabled = false;
                clearBtn.disabled = false;
                
                console.log('βœ… FalconsAI summarization model loaded successfully!');
                
            } catch (error) {
                console.error('Failed to load model:', error);
                modelStatus.textContent = '❌ Failed to load model. Please refresh the page.';
                modelStatus.className = 'model-status error';
                summarizeBtn.disabled = true;
                clearBtn.disabled = true;
                outputText.textContent = 'Failed to load the summarization model. Please refresh the page.';
                outputText.className = 'error';
            }
        }
        
        // Main summarize function - MATCHES PYTHON APP.PY DEFAULTS
        async function summarize() {
            if (!isModelReady || !summarizer) {
                outputText.textContent = 'Model is still loading. Please wait...';
                outputText.className = 'error';
                return;
            }
            
            const text = inputText.value.trim();
            
            // Check if input is empty or only whitespace
            if (!text) {
                outputText.textContent = 'Please enter document text to summarize.';
                outputText.className = 'error';
                tokenInfo.textContent = '';
                return;
            }
            
            // Approximate token count using words
            const inputTokens = text.split(/\s+/).length;
            
            // Check if word count is less than 5
            if (inputTokens < 5) {
                outputText.textContent = 'Insufficient text to summarize.';
                outputText.className = 'error';
                tokenInfo.textContent = `Input tokens: ${inputTokens}`;
                return;
            }
            
            // Show loading state
            outputText.textContent = 'Summarizing...';
            outputText.className = 'loading';
            summarizeBtn.disabled = true;
            tokenInfo.textContent = '';
            
            try {
                // Proceed with summarization - MATCH PYTHON DEFAULTS
                const result = await summarizer(text, { 
                    truncation: true,
                    max_length: 200,    // Python default: 200
                    min_length: 50,     // Python default: 50
                    do_sample: false,   // Python default: False
                    num_beams: 4,       // Python default: 4 beams
                    early_stopping: true
                });
                
                // Get summary text
                const summaryText = result[0].summary_text;
                
                // Calculate output tokens (approximate using word count)
                const outputTokens = summaryText.split(/\s+/).length;
                
                // Debug print statement (visible in browser console)
                console.log(`Input tokens: ${inputTokens}, Output tokens: ${outputTokens}`);
                
                // Update UI
                outputText.textContent = summaryText;
                outputText.className = '';
                tokenInfo.textContent = `Input tokens: ${inputTokens} | Output tokens: ${outputTokens}`;
                
            } catch (error) {
                console.error('Summarization error:', error);
                outputText.textContent = `An error occurred: ${error.message || 'Unknown error'}`;
                outputText.className = 'error';
            } finally {
                summarizeBtn.disabled = false;
            }
        }
        
        // Clear function
        function clearAll() {
            inputText.value = '';
            outputText.innerHTML = '<span class="placeholder">Your summary will appear here...</span>';
            outputText.className = '';
            tokenInfo.textContent = '';
        }
        
        // Event listeners
        summarizeBtn.addEventListener('click', summarize);
        clearBtn.addEventListener('click', clearAll);
        
        // Allow Ctrl+Enter to summarize (same as your Python version)
        inputText.addEventListener('keydown', (e) => {
            if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
                e.preventDefault();
                summarize();
            }
        });
        
        // Initialize the model when the page loads
        console.log('πŸš€ Loading FalconsAI summarization model...');
        initModel();
    </script>
</body>
</html>