File size: 14,305 Bytes
f74654d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const micBtn = document.getElementById('micBtn');
const statusText = document.getElementById('statusText');
const visualizer = document.getElementById('visualizer');
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');

// Result Modal Elements
const resultModal = document.getElementById('resultModal');
const closeModal = document.getElementById('closeModal');
const resultEmoji = document.getElementById('resultEmoji');
const resultLabel = document.getElementById('resultLabel');
const resultConfidence = document.getElementById('resultConfidence');
const btnCorrect = document.getElementById('btnCorrect');
const btnIncorrect = document.getElementById('btnIncorrect');
const correctionArea = document.getElementById('correctionArea');
const submitCorrection = document.getElementById('submitCorrection');

let mediaRecorder;
let audioChunks = [];
let currentTempFilename = null;
let currentPrediction = null;

// Emotion to Emoji Map
const emotionEmojis = {
    'neutral': '😐',
    'calm': '😌',
    'happiness': 'πŸ˜„',
    'happy': 'πŸ˜„',
    'sadness': '😒',
    'sad': '😒',
    'anger': '😠',
    'angry': '😠',
    'fear': '😱',
    'disgust': '🀒',
    'surprise': '😲'
};

// --- Recording Logic ---
micBtn.addEventListener('mousedown', startRecording);
micBtn.addEventListener('mouseup', stopRecording);
micBtn.addEventListener('mouseleave', () => {
    if (mediaRecorder && mediaRecorder.state === 'recording') {
        stopRecording();
    }
});

// Training Logic
const trainingModal = document.getElementById('trainingModal');
const trainingLog = document.getElementById('trainingLog');
const closeTrainingModal = document.getElementById('closeTrainingModal');

closeTrainingModal.addEventListener('click', () => {
    trainingModal.classList.add('hidden');
});


// --- Training & Password Logic ---
const passwordModal = document.getElementById('passwordModal');
const closePasswordModal = document.getElementById('closePasswordModal');
const submitPasswordBtn = document.getElementById('submitPasswordBtn');
const adminPasswordInput = document.getElementById('adminPasswordInput');

// Open Password Modal
document.getElementById('trainBtn').addEventListener('click', () => {
    passwordModal.classList.remove('hidden');
    adminPasswordInput.value = '';
    adminPasswordInput.focus();
});

// Close Password Modal
closePasswordModal.addEventListener('click', () => {
    passwordModal.classList.add('hidden');
});

// Handle Password Submission
// Handle Password Submission
function submitPassword() {
    const password = adminPasswordInput.value;
    if (!password) {
        showToast("Please enter a password", "error");
        return;
    }

    passwordModal.classList.add('hidden');
    startTraining(password);
}

submitPasswordBtn.addEventListener('click', submitPassword);

// Allow Enter key to submit password and Esc to close modals
document.addEventListener('keydown', (e) => {
    // Enter Key in Password Input
    if (e.key === 'Enter' && document.activeElement === adminPasswordInput) {
        submitPassword();
    }
    // Escape Key Global
    if (e.key === 'Escape') {
        passwordModal.classList.add('hidden');
        resultModal.classList.add('hidden');
        trainingModal.classList.add('hidden');
    }
});

async function startTraining(password) {
    // Open Terminal
    trainingModal.classList.remove('hidden');
    trainingLog.innerHTML = '<span class="log-line">Authenticating...</span>';

    try {
        const response = await fetch('/train', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ password: password })
        });

        if (response.status === 401) {
            trainingLog.innerHTML += '<span class="log-line" style="color:red">Error: Unauthorized. Incorrect Password.</span>';
            showToast("Incorrect Admin Password", "error");
            return;
        }

        const data = await response.json();

        if (data.status === 'training_started') {
            trainingLog.innerHTML += '<span class="log-line">Access Granted. Starting training sequence...</span>';
            pollLogs();
        }
    } catch (e) {
        showToast("Failed to start training.", "error");
        trainingLog.innerHTML += `<span class="log-line" style="color:red">Error: ${e.message}</span>`;
    }
}

async function pollLogs(startIndex = 0) {
    try {
        const response = await fetch(`/logs?after=${startIndex}`);
        const data = await response.json();

        if (data.logs && data.logs.length > 0) {
            data.logs.forEach(log => {
                const line = document.createElement('span');
                line.className = 'log-line';
                line.innerText = log;
                if (log.includes("CRITICAL") || log.includes("Error")) line.style.color = '#ff5555';
                if (log.includes("Success") || log.includes("complete")) line.style.color = '#55ff55';
                trainingLog.appendChild(line);
            });
            // Auto scroll
            trainingLog.scrollTop = trainingLog.scrollHeight;
        }

        // Continue polling if not complete (simple check: if logs stop or specific message?)
        // Better: The backend just keeps logs coming. We'll poll until we see "Training complete"
        const lastLog = data.logs.length > 0 ? data.logs[data.logs.length - 1] : "";

        if (lastLog.includes("Training complete")) {
            trainingLog.innerHTML += '<span class="log-line">>> Process finished. You may close this window.</span>';
            return;
        }

        setTimeout(() => pollLogs(data.next_index), 500); // Poll every 500ms

    } catch (e) {
        console.error("Polling error", e);
        setTimeout(() => pollLogs(startIndex), 2000); // Retry slower on error
    }
}

// Touch support for mobile
micBtn.addEventListener('touchstart', (e) => { e.preventDefault(); startRecording(); });
micBtn.addEventListener('touchend', (e) => { e.preventDefault(); stopRecording(); });


function startRecording() {
    statusText.innerText = "Recording...";
    micBtn.classList.add('recording');
    visualizer.classList.remove('hidden');
    audioChunks = [];

    navigator.mediaDevices.getUserMedia({ audio: true })
        .then(stream => {
            mediaRecorder = new MediaRecorder(stream);
            mediaRecorder.start();

            mediaRecorder.addEventListener("dataavailable", event => {
                audioChunks.push(event.data);
            });

            mediaRecorder.addEventListener("stop", () => {
                const audioBlob = new Blob(audioChunks, { type: 'audio/wav' }); // Default typically webm, assumes backend handles it or we send as file
                // Usually comes as webm/ogg from browser. We'll verify mimetype.
                uploadAudio(audioBlob, "recording.wav"); // Naming it .wav but content might be webm, backend pydub handles it.
            });
        })
        .catch(err => {
            console.error("Error accessing mic:", err);
            statusText.innerText = "Error Accessing Mic";
        });
}

function stopRecording() {
    if (mediaRecorder && mediaRecorder.state !== 'inactive') {
        mediaRecorder.stop();
        statusText.innerText = "Processing...";
        micBtn.classList.remove('recording');
        visualizer.classList.add('hidden');
    }
}

// --- File Upload Logic ---
fileInput.addEventListener('change', (e) => {
    if (e.target.files.length > 0) {
        handleFile(e.target.files[0]);
    }
});

dropZone.addEventListener('dragover', (e) => {
    e.preventDefault();
    dropZone.classList.add('dragover');
});

dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));

dropZone.addEventListener('drop', (e) => {
    e.preventDefault();
    dropZone.classList.remove('dragover');
    if (e.dataTransfer.files.length > 0) {
        handleFile(e.dataTransfer.files[0]);
    }
});

function handleFile(file) {
    statusText.innerText = `Uploading ${file.name}...`;
    uploadAudio(file, file.name);
}

// --- Toast Notifications ---
function showToast(message, type = 'info') {
    const container = document.getElementById('toast-container');
    const toast = document.createElement('div');
    toast.className = `toast ${type}`;

    // Icon selection
    let icon = 'fa-info-circle';
    if (type === 'success') icon = 'fa-check-circle';
    if (type === 'error') icon = 'fa-exclamation-circle';

    toast.innerHTML = `

        <i class="fa-solid ${icon}"></i>

        <span>${message}</span>

    `;

    container.appendChild(toast);

    // Auto remove
    setTimeout(() => {
        toast.classList.add('hide');
        toast.addEventListener('animationend', () => toast.remove());
    }, 3000);
}

// --- API Calls ---
async function uploadAudio(fileOrBlob, filename) {
    const formData = new FormData();
    formData.append("file", fileOrBlob, filename); // Append file

    try {
        const response = await fetch('/predict', {
            method: 'POST',
            body: formData
        });

        if (!response.ok) {
            const errData = await response.json();
            throw new Error(errData.detail || "Prediction failed");
        }

        const data = await response.json();
        showResult(data);
        statusText.innerText = "Click & Hold to Record";
        showToast("Analysis Complete", "success");
    } catch (error) {
        console.error(error);
        statusText.innerText = "Error: " + error.message;
        showToast("Error: " + error.message, "error");
    }
}

function showResult(data) {
    currentTempFilename = data.temp_filename;
    currentPrediction = data.prediction;

    resultEmoji.innerText = emotionEmojis[data.prediction.toLowerCase()] || '❓';
    resultLabel.innerText = data.prediction.charAt(0).toUpperCase() + data.prediction.slice(1);
    resultConfidence.innerText = `Confidence: ${(data.confidence * 100).toFixed(1)}%`;

    // Reset feedback UI
    correctionArea.classList.add('hidden');
    resultModal.classList.remove('hidden');

    if (data.is_fallback) {
        showToast("Model not trained. Please label this audio to build the dataset.", "info");
        correctionArea.classList.remove('hidden');
        resultLabel.innerText = "Label Required";
        resultEmoji.innerText = "🏷️";
        resultConfidence.innerText = "Help the AI learn!";
    }

    // --- NLP Analysis Display ---
    let nlpDiv = document.getElementById('nlp-results');
    if (!nlpDiv) {
        nlpDiv = document.createElement('div');
        nlpDiv.id = 'nlp-results';
        nlpDiv.className = 'nlp-container';

        // Insert before feedback section
        const feedbackSection = resultModal.querySelector('.feedback-section');
        resultModal.querySelector('.modal-content').insertBefore(nlpDiv, feedbackSection);
    }

    // Clear previous
    nlpDiv.innerHTML = '';

    if (data.nlp_analysis && data.nlp_analysis.transcription) {
        const textEmotion = data.nlp_analysis.text_emotion;
        const confidencePct = (textEmotion.score * 100).toFixed(1);

        // Show Hybrid Breakdown
        nlpDiv.innerHTML = `

            <div class="divider">Hybrid Analysis</div>

            <p class="transcription">"${data.nlp_analysis.transcription}"</p>

            

            <div class="breakdown-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; font-size: 0.9rem;">

                <div class="breakdown-item">

                    <div style="color: #94a3b8;">Audio Tone</div>

                    <div class="highlight">${data.audio_emotion.label}</div>

                    <div class="confidence-small">${(data.audio_emotion.confidence * 100).toFixed(1)}%</div>

                </div>

                <div class="breakdown-item">

                     <div style="color: #94a3b8;">Text Context</div>

                    <div class="highlight">${textEmotion.label}</div>

                    <div class="confidence-small">${confidencePct}%</div>

                </div>

            </div>

            <div style="margin-top: 10px; font-size: 0.8rem; color: #64748b;">

                Result fused from acoustic and semantic models.

            </div>

        `;
    } else {
        nlpDiv.innerHTML = `

            <div class="divider">Context Analysis</div>

            <p style="color: #64748b; font-style: italic;">No speech detected or analysis unavailable.</p>

        `;
    }
}

// --- Modal & Feedback ---
closeModal.addEventListener('click', () => resultModal.classList.add('hidden'));
window.onclick = (event) => {
    if (event.target == resultModal) resultModal.classList.add('hidden');
};

btnCorrect.addEventListener('click', () => {
    submitFeedback(currentPrediction);
});

btnIncorrect.addEventListener('click', () => {
    correctionArea.classList.remove('hidden');
});

submitCorrection.addEventListener('click', () => {
    const selected = document.getElementById('emotionSelect').value;
    if (selected) {
        submitFeedback(selected);
    }
});

async function submitFeedback(correctLabel) {
    try {
        const response = await fetch('/feedback', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                filename: currentTempFilename,
                original_emotion: currentPrediction,
                correct_emotion: correctLabel
            })
        });

        const res = await response.json();
        if (res.status === 'success') {
            showToast("Feedback saved successfully!", "success");
            resultModal.classList.add('hidden');
        }
    } catch (e) {
        showToast("Failed to save feedback.", "error");
    }
}