File size: 11,066 Bytes
ca28016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Real-Time Image Generation Progress</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #1a1a1a;
            color: #fff;
        }
        
        .progress-container {
            background: #2a2a2a;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
            border: 1px solid #444;
        }
        
        .progress-bar {
            width: 100%;
            height: 20px;
            background: #333;
            border-radius: 10px;
            overflow: hidden;
            margin: 10px 0;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #00ff88, #00cc66);
            width: 0%;
            transition: width 0.3s ease;
            border-radius: 10px;
        }
        
        .progress-text {
            display: flex;
            justify-content: space-between;
            margin: 10px 0;
            font-size: 14px;
        }
        
        .status {
            font-weight: bold;
            color: #00ff88;
        }
        
        .time-info {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 10px;
            margin: 15px 0;
        }
        
        .time-box {
            background: #333;
            padding: 10px;
            border-radius: 5px;
            text-align: center;
        }
        
        .time-label {
            font-size: 12px;
            color: #aaa;
            margin-bottom: 5px;
        }
        
        .time-value {
            font-size: 18px;
            font-weight: bold;
            color: #00ff88;
        }
        
        button {
            background: #00ff88;
            color: #000;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-weight: bold;
        }
        
        button:hover {
            background: #00cc66;
        }
        
        button:disabled {
            background: #666;
            cursor: not-allowed;
        }
        
        .log {
            background: #111;
            border: 1px solid #333;
            border-radius: 5px;
            padding: 10px;
            height: 200px;
            overflow-y: auto;
            font-family: monospace;
            font-size: 12px;
            margin-top: 20px;
        }
        
        .log-entry {
            margin: 2px 0;
            padding: 2px 0;
        }
        
        .log-info { color: #00ff88; }
        .log-warning { color: #ffaa00; }
        .log-error { color: #ff4444; }
    </style>
</head>
<body>
    <h1>🎨 Real-Time Image Generation Progress</h1>
    
    <div class="progress-container">
        <h3>Image Generation Status</h3>
        
        <div class="progress-bar">
            <div class="progress-fill" id="progressFill"></div>
        </div>
        
        <div class="progress-text">
            <span class="status" id="statusText">Ready to generate</span>
            <span id="percentText">0%</span>
        </div>
        
        <div class="time-info">
            <div class="time-box">
                <div class="time-label">Elapsed</div>
                <div class="time-value" id="elapsedTime">0.0s</div>
            </div>
            <div class="time-box">
                <div class="time-label">Remaining</div>
                <div class="time-value" id="remainingTime">--</div>
            </div>
            <div class="time-box">
                <div class="time-label">Total Est.</div>
                <div class="time-value" id="totalTime">--</div>
            </div>
        </div>
        
        <div style="margin-top: 20px;">
            <input type="text" id="promptInput" placeholder="Enter your image prompt..." 
                   style="width: 70%; padding: 10px; margin-right: 10px; background: #333; color: #fff; border: 1px solid #555; border-radius: 5px;">
            <button onclick="startGeneration()" id="generateBtn">Generate Image</button>
        </div>
    </div>
    
    <div class="log" id="logContainer">
        <div class="log-entry log-info">Ready to start image generation...</div>
    </div>

    <script>
        let currentRequestId = null;
        let eventSource = null;
        let pollingInterval = null;

        function log(message, type = 'info') {
            const logContainer = document.getElementById('logContainer');
            const entry = document.createElement('div');
            entry.className = `log-entry log-${type}`;
            entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
            logContainer.appendChild(entry);
            logContainer.scrollTop = logContainer.scrollHeight;
        }

        function updateProgress(data) {
            const progressFill = document.getElementById('progressFill');
            const statusText = document.getElementById('statusText');
            const percentText = document.getElementById('percentText');
            const elapsedTime = document.getElementById('elapsedTime');
            const remainingTime = document.getElementById('remainingTime');
            const totalTime = document.getElementById('totalTime');

            progressFill.style.width = `${data.percent || 0}%`;
            statusText.textContent = data.status || 'Unknown';
            percentText.textContent = `${data.percent || 0}%`;
            elapsedTime.textContent = data.elapsed_formatted || '0.0s';
            remainingTime.textContent = data.remaining_formatted || '--';
            totalTime.textContent = data.total_estimated || '--';

            log(`Progress: ${data.percent || 0}% - ${data.status || 'Unknown'} - Step ${data.step || 0}/${data.total || 100}`);
        }

        function startGeneration() {
            const prompt = document.getElementById('promptInput').value.trim();
            if (!prompt) {
                alert('Please enter a prompt!');
                return;
            }

            const generateBtn = document.getElementById('generateBtn');
            generateBtn.disabled = true;
            generateBtn.textContent = 'Generating...';

            currentRequestId = 'img_' + Date.now();
            log(`Starting image generation for: "${prompt}"`, 'info');

            // Start image generation
            fetch('/image/generate', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    prompt: prompt,
                    provider: 'qwen_local',
                    request_id: currentRequestId,
                    width: 512,
                    height: 512,
                    steps: 20
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    log('Image generation completed successfully!', 'info');
                    updateProgress({
                        percent: 100,
                        status: 'completed',
                        elapsed_formatted: data.generation_time ? `${data.generation_time.toFixed(1)}s` : 'N/A'
                    });
                } else {
                    log(`Generation failed: ${data.error}`, 'error');
                    updateProgress({
                        percent: 0,
                        status: 'failed'
                    });
                }
                generateBtn.disabled = false;
                generateBtn.textContent = 'Generate Image';
                stopProgressTracking();
            })
            .catch(error => {
                log(`Network error: ${error.message}`, 'error');
                generateBtn.disabled = false;
                generateBtn.textContent = 'Generate Image';
                stopProgressTracking();
            });

            // Start progress tracking using Server-Sent Events (preferred)
            startProgressTracking();
        }

        function startProgressTracking() {
            if (!currentRequestId) return;

            log('Starting real-time progress tracking...', 'info');

            // Try Server-Sent Events first (more efficient)
            try {
                eventSource = new EventSource(`/image/progress/stream?request_id=${currentRequestId}`);
                
                eventSource.onmessage = function(event) {
                    try {
                        const data = JSON.parse(event.data);
                        updateProgress(data);
                        
                        if (data.status === 'completed' || data.status === 'failed') {
                            stopProgressTracking();
                        }
                    } catch (e) {
                        log(`Progress parsing error: ${e.message}`, 'warning');
                    }
                };

                eventSource.onerror = function(event) {
                    log('SSE connection error, falling back to polling...', 'warning');
                    eventSource.close();
                    eventSource = null;
                    startPolling();
                };

            } catch (e) {
                log('SSE not supported, using polling...', 'warning');
                startPolling();
            }
        }

        function startPolling() {
            if (!currentRequestId) return;

            pollingInterval = setInterval(() => {
                fetch(`/image/progress?request_id=${currentRequestId}`)
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            updateProgress(data);
                            
                            if (data.status === 'completed' || data.status === 'failed') {
                                stopProgressTracking();
                            }
                        }
                    })
                    .catch(error => {
                        log(`Polling error: ${error.message}`, 'warning');
                    });
            }, 1000); // Poll every second
        }

        function stopProgressTracking() {
            if (eventSource) {
                eventSource.close();
                eventSource = null;
                log('Stopped SSE progress tracking', 'info');
            }
            
            if (pollingInterval) {
                clearInterval(pollingInterval);
                pollingInterval = null;
                log('Stopped polling progress tracking', 'info');
            }
        }

        // Clean up on page unload
        window.addEventListener('beforeunload', stopProgressTracking);
    </script>
</body>
</html>