File size: 12,387 Bytes
a66e866
8ab789a
07ef252
8ab789a
07ef252
 
 
 
 
 
 
 
 
8ab789a
07ef252
a66e866
 
07ef252
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
0eb7e14
07ef252
 
 
a66e866
07ef252
a66e866
 
 
 
 
 
 
07ef252
 
a66e866
 
 
07ef252
a66e866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07ef252
 
 
 
 
 
0eb7e14
8ab789a
07ef252
8ab789a
07ef252
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
8ab789a
 
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a66e866
 
 
 
07ef252
 
 
a66e866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07ef252
a66e866
 
 
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a66e866
07ef252
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
8ab789a
07ef252
 
 
 
 
 
 
 
 
 
 
 
 
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
import { WhisperForConditionalGeneration, WhisperProcessor } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]';

// Get DOM elements
const status = document.getElementById('status');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const clearBtn = document.getElementById('clearBtn');
const transcriptionContainer = document.getElementById('transcriptionContainer');
const chunkLengthSelect = document.getElementById('chunkLength');
const useWebGPUCheckbox = document.getElementById('useWebGPU');
const chunkCountDisplay = document.getElementById('chunkCount');
const recordingTimeDisplay = document.getElementById('recordingTime');
const visualizerBars = document.querySelectorAll('.bar');

// State
let model = null;
let processor = null;
let mediaStream = null;
let audioContext = null;
let mediaRecorder = null;
let recordedChunks = [];
let isRecording = false;
let chunkCount = 0;
let recordingStartTime = null;
let recordingInterval = null;
let analyser = null;
let animationId = null;

// Initialize the ATOM model
async function initModel() {
    try {
        status.textContent = 'Loading ATOM model with custom tokenizer... This may take a minute.';
        status.className = 'loading';
        
        const device = useWebGPUCheckbox.checked ? 'webgpu' : 'wasm';
        const dtype = useWebGPUCheckbox.checked ? 'fp32' : 'fp32';
        
        // Load processor (includes the custom Armenian tokenizer)
        status.textContent = 'Loading custom Armenian processor/tokenizer...';
        processor = await WhisperProcessor.from_pretrained('Chillarmo/ATOM', {
            progress_callback: (progress) => {
                if (progress.status === 'downloading') {
                    const percent = Math.round((progress.loaded / progress.total) * 100);
                    status.textContent = `Downloading ${progress.file}: ${percent}%`;
                }
            }
        });
        
        console.log('βœ“ ATOM Processor loaded (includes custom tokenizer)');
        
        // Load model
        status.textContent = 'Loading ATOM model...';
        model = await WhisperForConditionalGeneration.from_pretrained('Chillarmo/ATOM', {
            device: device,
            dtype: dtype,
            progress_callback: (progress) => {
                if (progress.status === 'downloading') {
                    const percent = Math.round((progress.loaded / progress.total) * 100);
                    status.textContent = `Downloading model ${progress.file}: ${percent}%`;
                } else if (progress.status === 'loading') {
                    status.textContent = `Loading ${progress.file}...`;
                }
            }
        });
        
        console.log('βœ“ ATOM Model loaded');
        console.log('Model config:', model.config);
        console.log('Processor:', processor);
        
        status.textContent = 'ATOM ready! Model + custom tokenizer loaded successfully.';
        status.className = 'ready';
        startBtn.disabled = false;
    } catch (error) {
        console.error('Model loading error:', error);
        status.textContent = `Error loading model: ${error.message}`;
        status.className = 'error';
        console.error('Full error details:', error);
    }
}

// Format time as MM:SS
function formatTime(seconds) {
    const mins = Math.floor(seconds / 60);
    const secs = Math.floor(seconds % 60);
    return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}

// Update recording time
function updateRecordingTime() {
    if (recordingStartTime) {
        const elapsed = (Date.now() - recordingStartTime) / 1000;
        recordingTimeDisplay.textContent = formatTime(elapsed);
    }
}

// Visualize audio
function visualizeAudio() {
    if (!analyser || !isRecording) return;
    
    const dataArray = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(dataArray);
    
    // Sample the data for visualization
    const barCount = visualizerBars.length;
    const step = Math.floor(dataArray.length / barCount);
    
    visualizerBars.forEach((bar, index) => {
        const value = dataArray[index * step];
        const height = (value / 255) * 70 + 4; // 4px minimum, 74px maximum
        bar.style.height = `${height}px`;
    });
    
    animationId = requestAnimationFrame(visualizeAudio);
}

// Start recording
async function startRecording() {
    try {
        // Request microphone access
        mediaStream = await navigator.mediaDevices.getUserMedia({ 
            audio: {
                channelCount: 1,
                sampleRate: 16000,
            } 
        });
        
        // Set up audio context for visualization
        audioContext = new AudioContext({ sampleRate: 16000 });
        const source = audioContext.createMediaStreamSource(mediaStream);
        analyser = audioContext.createAnalyser();
        analyser.fftSize = 256;
        source.connect(analyser);
        
        // Set up MediaRecorder
        mediaRecorder = new MediaRecorder(mediaStream);
        recordedChunks = [];
        
        mediaRecorder.ondataavailable = (event) => {
            if (event.data.size > 0) {
                recordedChunks.push(event.data);
            }
        };
        
        mediaRecorder.onstop = async () => {
            if (recordedChunks.length > 0) {
                await processAudioChunk(recordedChunks);
                recordedChunks = [];
            }
        };
        
        // Start recording
        const chunkDuration = parseInt(chunkLengthSelect.value) * 1000;
        mediaRecorder.start();
        
        // Schedule automatic chunk processing
        const chunkInterval = setInterval(() => {
            if (!isRecording) {
                clearInterval(chunkInterval);
                return;
            }
            
            mediaRecorder.stop();
            mediaRecorder.start();
        }, chunkDuration);
        
        isRecording = true;
        recordingStartTime = Date.now();
        recordingInterval = setInterval(updateRecordingTime, 100);
        
        status.textContent = 'Recording... Speak in Armenian';
        status.className = 'recording';
        startBtn.disabled = true;
        stopBtn.disabled = false;
        
        // Start visualization
        visualizeAudio();
        
    } catch (error) {
        console.error('Error starting recording:', error);
        status.textContent = `Error: ${error.message}`;
        status.className = 'error';
    }
}

// Stop recording
function stopRecording() {
    isRecording = false;
    
    if (mediaRecorder && mediaRecorder.state !== 'inactive') {
        mediaRecorder.stop();
    }
    
    if (mediaStream) {
        mediaStream.getTracks().forEach(track => track.stop());
    }
    
    if (audioContext) {
        audioContext.close();
    }
    
    if (recordingInterval) {
        clearInterval(recordingInterval);
    }
    
    if (animationId) {
        cancelAnimationFrame(animationId);
    }
    
    // Reset visualizer
    visualizerBars.forEach(bar => {
        bar.style.height = '4px';
    });
    
    status.textContent = 'Recording stopped. Ready for next recording.';
    status.className = 'ready';
    startBtn.disabled = false;
    stopBtn.disabled = true;
}

// Process audio chunk
async function processAudioChunk(chunks) {
    try {
        status.textContent = 'Processing audio...';
        status.className = 'processing';
        
        // Create audio blob
        const audioBlob = new Blob(chunks, { type: 'audio/webm' });
        
        // Convert to array buffer
        const arrayBuffer = await audioBlob.arrayBuffer();
        
        // Decode audio
        const tempAudioContext = new (window.AudioContext || window.webkitAudioContext)();
        const audioBuffer = await tempAudioContext.decodeAudioData(arrayBuffer);
        
        // Get audio data as Float32Array
        const audioData = audioBuffer.getChannelData(0);
        
        console.log('Processing audio chunk:', audioData.length, 'samples at', audioBuffer.sampleRate, 'Hz');
        
        // Process audio with the processor (includes custom tokenizer)
        const inputs = await processor(audioData, {
            sampling_rate: audioBuffer.sampleRate,
        });
        
        console.log('Processor output:', inputs);
        
        // Generate with the model
        const outputs = await model.generate({
            ...inputs,
        });
        
        console.log('Model outputs:', outputs);
        
        // Decode the output tokens using the custom tokenizer
        const decoded = processor.batch_decode(outputs, {
            skip_special_tokens: true,
        });
        
        console.log('Decoded text:', decoded);
        
        // Add to transcription
        const text = decoded[0].trim();
        if (text) {
            addTranscription(text);
            chunkCount++;
            chunkCountDisplay.textContent = chunkCount;
        }
        
        if (isRecording) {
            status.textContent = 'Recording... Speak in Armenian';
            status.className = 'recording';
        } else {
            status.textContent = 'Ready for next recording.';
            status.className = 'ready';
        }
        
        tempAudioContext.close();
        
    } catch (error) {
        console.error('Error processing audio:', error);
        status.textContent = `Processing error: ${error.message}`;
        status.className = 'error';
        console.error('Full processing error:', error);
        
        // Restore recording status if still recording
        setTimeout(() => {
            if (isRecording) {
                status.textContent = 'Recording... Speak in Armenian';
                status.className = 'recording';
            }
        }, 2000);
    }
}

// Add transcription to UI
function addTranscription(text) {
    // Remove empty state if present
    const emptyState = transcriptionContainer.querySelector('.empty-state');
    if (emptyState) {
        emptyState.remove();
    }
    
    // Create transcription item
    const item = document.createElement('div');
    item.className = 'transcription-item';
    
    const timestamp = document.createElement('div');
    timestamp.className = 'timestamp';
    timestamp.textContent = new Date().toLocaleTimeString();
    
    const textDiv = document.createElement('div');
    textDiv.className = 'text';
    textDiv.textContent = text;
    
    item.appendChild(timestamp);
    item.appendChild(textDiv);
    
    transcriptionContainer.appendChild(item);
    
    // Auto-scroll to bottom
    transcriptionContainer.scrollTop = transcriptionContainer.scrollHeight;
}

// Clear transcriptions
function clearTranscriptions() {
    transcriptionContainer.innerHTML = `
        <div class="empty-state">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z" />
            </svg>
            <p>Click "Start Recording" to begin transcribing Armenian speech</p>
        </div>
    `;
    chunkCount = 0;
    chunkCountDisplay.textContent = '0';
    recordingTimeDisplay.textContent = '00:00';
}

// Event listeners
startBtn.addEventListener('click', startRecording);
stopBtn.addEventListener('click', stopRecording);
clearBtn.addEventListener('click', clearTranscriptions);

// Check WebGPU support
if (useWebGPUCheckbox.checked && !navigator.gpu) {
    status.textContent = 'WebGPU not supported, falling back to WASM';
    status.className = 'error';
    useWebGPUCheckbox.checked = false;
    setTimeout(() => initModel(), 2000);
} else {
    // Initialize model on load
    initModel();
}

// Re-initialize if WebGPU setting changes
useWebGPUCheckbox.addEventListener('change', () => {
    if (isRecording) {
        alert('Please stop recording before changing acceleration settings');
        useWebGPUCheckbox.checked = !useWebGPUCheckbox.checked;
        return;
    }
    status.textContent = 'Reinitializing model...';
    status.className = 'loading';
    startBtn.disabled = true;
    initModel();
});