File size: 12,452 Bytes
7525da2
 
 
 
 
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
dd1b723
7525da2
 
dd1b723
7525da2
 
 
 
 
dd1b723
 
7525da2
 
 
 
 
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
 
 
dd1b723
7525da2
 
dd1b723
7525da2
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1b723
7525da2
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class GemmaChatbot {
    constructor() {
        this.generator = null;
        this.isInitialized = false;
        this.isGenerating = false;
        this.currentDevice = 'cpu';
        this.messages = [];
        this.maxTokens = 1000;
        
        this.initializeElements();
        this.attachEventListeners();
        this.initializeModel();
    }

    initializeElements() {
        // Input elements
        this.messageInput = document.getElementById('messageInput');
        this.sendButton = document.getElementById('sendButton');
        this.messagesContainer = document.getElementById('messagesContainer');
        this.charCount = document.getElementById('charCount');
        
        // UI elements
        this.loadingOverlay = document.getElementById('loadingOverlay');
        this.errorModal = document.getElementById('errorModal');
        this.errorMessage = document.getElementById('errorMessage');
        this.retryButton = document.getElementById('retryButton');
        this.deviceToggle = document.getElementById('deviceToggle');
        this.statusText = document.getElementById('statusText');
        this.progressFill = document.getElementById('progressFill');
    }

    attachEventListeners() {
        // Send message
        this.sendButton.addEventListener('click', () => this.sendMessage());
        
        // Enter key to send (Ctrl+Enter for new line)
        this.messageInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter' && !e.ctrlKey) {
                e.preventDefault();
                this.sendMessage();
            }
        });

        // Auto-resize textarea
        this.messageInput.addEventListener('input', () => {
            this.autoResizeTextarea();
            this.updateCharCount();
        });

        // Device toggle
        this.deviceToggle.addEventListener('click', () => this.toggleDevice());

        // Retry button
        this.retryButton.addEventListener('click', () => this.retryInitialization());

        // WebGPU support detection
        this.checkWebGPUSupport();
    }

    async checkWebGPUSupport() {
        if ('gpu' in navigator) {
            try {
                const adapter = await navigator.gpu.requestAdapter();
                if (adapter) {
                    this.statusText.textContent = 'Ready (GPU available)';
                    this.statusText.parentElement.classList.add('ready');
                }
            } catch (error) {
                console.log('WebGPU not available');
            }
        }
    }

    async initializeModel() {
        try {
            this.updateProgress(10, 'Initializing transformers.js...');
            
            // Wait for transformers to be available
            while (!window.transformers) {
                await new Promise(resolve => setTimeout(resolve, 100));
            }

            const { pipeline, TextStreamer } = window.transformers;
            this.updateProgress(30, 'Loading Gemma model...');

            // Create pipeline with error handling
            this.generator = await pipeline(
                'text-generation',
                'onnx-community/gemma-3-270m-it-ONNX',
                { 
                    dtype: 'fp32',
                    device: this.currentDevice 
                }
            );

            this.updateProgress(90, 'Model loaded successfully!');
            
            // Initialize with system message
            this.messages = [
                { role: 'system', content: 'You are a helpful assistant.' }
            ];

            this.isInitialized = true;
            this.updateProgress(100, 'Ready!');
            
            setTimeout(() => {
                this.hideLoadingOverlay();
                this.enableInput();
            }, 500);

        } catch (error) {
            console.error('Model initialization failed:', error);
            this.showError('Failed to initialize the AI model. Please check your internet connection and try again.');
        }
    }

    async toggleDevice() {
        if (this.isGenerating) return;

        const options = this.deviceToggle.querySelectorAll('.device-option');
        const statusCircle = this.statusText.parentElement.querySelector('i');

        if (this.currentDevice === 'cpu') {
            // Try to switch to GPU
            if ('gpu' in navigator) {
                try {
                    this.currentDevice = 'webgpu';
                    options[0].classList.remove('active');
                    options[1].classList.add('active');
                    this.statusText.textContent = 'Switching to GPU...';
                    statusCircle.style.color = '#ff9800';

                    // Reinitialize model with GPU
                    await this.reinitializeModel();

                } catch (error) {
                    console.log('GPU initialization failed, staying on CPU');
                    this.currentDevice = 'cpu';
                    statusCircle.style.color = '#4caf50';
                    this.statusText.textContent = 'Ready (CPU)';
                }
            } else {
                this.showToast('WebGPU not supported in this browser');
            }
        } else {
            // Switch to CPU
            this.currentDevice = 'cpu';
            options[1].classList.remove('active');
            options[0].classList.add('active');
            this.statusText.textContent = 'Switching to CPU...';

            await this.reinitializeModel();
        }
    }

    async reinitializeModel() {
        try {
            this.isInitialized = false;
            this.disableInput();

            const { pipeline, TextStreamer } = window.transformers;
            
            this.generator = await pipeline(
                'text-generation',
                'onnx-community/gemma-3-270m-it-ONNX',
                { 
                    dtype: 'fp32',
                    device: this.currentDevice 
                }
            );

            this.isInitialized = true;
            this.enableInput();
            
            const deviceName = this.currentDevice === 'gpu' ? 'GPU' : 'CPU';
            this.statusText.textContent = `Ready (${deviceName})`;
            this.showToast(`Switched to ${deviceName} execution`);

        } catch (error) {
            console.error('Model reinitialization failed:', error);
            this.showError('Failed to switch execution device. Please try again.');
        }
    }

    async sendMessage() {
        if (!this.isInitialized || this.isGenerating) return;

        const message = this.messageInput.value.trim();
        if (!message) return;

        // Add user message to UI
        this.addMessage('user', message);
        
        // Clear input
        this.messageInput.value = '';
        this.autoResizeTextarea();
        this.updateCharCount();

        // Add to conversation history
        this.messages.push({ role: 'user', content: message });

        // Generate response
        await this.generateResponse();
    }

    async generateResponse() {
        if (this.isGenerating) return;
        
        this.isGenerating = true;
        this.disableInput();

        try {
            // Add assistant message placeholder
            const assistantMessageEl = this.addMessage('assistant', '');
            
            // Create streaming response
            const { pipeline, TextStreamer } = window.transformers;
            const streamer = new TextStreamer(this.generator.tokenizer, {
                skip_prompt: true,
                skip_special_tokens: true,
                callback_function: (text) => {
                    this.updateStreamingMessage(assistantMessageEl, text);
                }
            });

            // Generate response with streaming
            const output = await this.generator(this.messages, {
                max_new_tokens: 512,
                do_sample: false,
                streamer: streamer
            });

            // Get final response
            const finalResponse = output[0].generated_text.at(-1).content;
            
            // Add final response to conversation
            this.messages.push({ role: 'assistant', content: finalResponse });

        } catch (error) {
            console.error('Generation failed:', error);
            this.updateStreamingMessage(assistantMessageEl, 'I apologize, but I encountered an error while generating a response. Please try again.');
            this.showToast('Generation failed. Please try again.');
        } finally {
            this.isGenerating = false;
            this.enableInput();
        }
    }

    addMessage(role, content) {
        const messageEl = document.createElement('div');
        messageEl.className = `message ${role}`;
        
        const avatar = role === 'user' ? 'fas fa-user' : 'fas fa-robot';
        const roleName = role === 'user' ? 'You' : 'Gemma AI';
        
        messageEl.innerHTML = `
            <div class="message-avatar">
                <i class="${avatar}"></i>
            </div>
            <div class="message-content">
                <div class="message-header">
                    <span class="message-role">${roleName}</span>
                </div>
                <div class="message-text">${this.escapeHtml(content)}</div>
            </div>
        `;

        this.messagesContainer.appendChild(messageEl);
        this.scrollToBottom();

        return messageEl;
    }

    updateStreamingMessage(messageEl, content) {
        const messageText = messageEl.querySelector('.message-text');
        messageText.innerHTML = this.escapeHtml(content);
        this.scrollToBottom();
    }

    autoResizeTextarea() {
        const textarea = this.messageInput;
        textarea.style.height = 'auto';
        textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px';
    }

    updateCharCount() {
        const count = this.messageInput.value.length;
        this.charCount.textContent = count;
        
        if (count > this.maxTokens * 0.9) {
            this.charCount.style.color = '#f44336';
        } else if (count > this.maxTokens * 0.7) {
            this.charCount.style.color = '#ff9800';
        } else {
            this.charCount.style.color = '';
        }
    }

    enableInput() {
        this.messageInput.disabled = false;
        this.sendButton.disabled = false;
        this.messageInput.placeholder = "Type your message here...";
    }

    disableInput() {
        this.messageInput.disabled = true;
        this.sendButton.disabled = true;
        this.messageInput.placeholder = "AI is generating a response...";
    }

    scrollToBottom() {
        this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
    }

    escapeHtml(text) {
        const div = document.createElement('div');
        div.textContent = text;
        return div.innerHTML;
    }

    updateProgress(percentage, status) {
        this.progressFill.style.width = `${percentage}%`;
        this.statusText.textContent = status;
    }

    hideLoadingOverlay() {
        this.loadingOverlay.style.display = 'none';
    }

    showError(message) {
        this.errorMessage.textContent = message;
        this.errorModal.style.display = 'flex';
        this.hideLoadingOverlay();
    }

    hideError() {
        this.errorModal.style.display = 'none';
    }

    retryInitialization() {
        this.hideError();
        this.showLoadingOverlay();
        this.initializeModel();
    }

    showLoadingOverlay() {
        this.loadingOverlay.style.display = 'flex';
        this.updateProgress(0, 'Initializing...');
    }

    showToast(message) {
        const toast = document.createElement('div');
        toast.className = 'toast';
        toast.textContent = message;
        document.body.appendChild(toast);

        setTimeout(() => toast.classList.add('show'), 100);
        setTimeout(() => {
            toast.classList.remove('show');
            setTimeout(() => document.body.removeChild(toast), 300);
        }, 3000);
    }
}

// Initialize the chatbot when the page loads
document.addEventListener('DOMContentLoaded', () => {
    new GemmaChatbot();
});

// Handle page visibility change
document.addEventListener('visibilitychange', () => {
    if (document.hidden) {
        // Page is hidden, can pause any ongoing operations
    } else {
        // Page is visible, resume operations
    }
});