File size: 9,252 Bytes
8d22bb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let currentSession = {
    topic: null,
    currentQuestion: null,
    questionIndex: null,
    totalScore: 0,
    questionCount: 0
};

// Voice recognition setup
let recognition = null;
if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    recognition = new SpeechRecognition();
    recognition.continuous = false;
    recognition.interimResults = false;
    recognition.lang = 'en-US';

    recognition.onresult = (event) => {
        const transcript = event.results[0][0].transcript;
        document.getElementById('studentAnswer').value = transcript;
    };

    recognition.onerror = (event) => {
        console.error('Speech recognition error:', event.error);
        stopVoiceInput();
    };
}

async function startSession(topic) {
    try {
        const response = await fetch('/api/start_session', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ topic: topic })
        });

        const data = await response.json();

        if (data.status === 'started') {
            currentSession.topic = topic;
            currentSession.totalScore = 0;
            currentSession.questionCount = 0;

            displayQuestion(data.first_question);

            document.getElementById('sessionTopic').textContent = `Topic: ${getTopicName(topic)}`;
            document.getElementById('topicSelection').classList.add('hidden');
            document.getElementById('vivaSession').classList.remove('hidden');

            updateProgress();
        }

    } catch (error) {
        console.error('Error starting session:', error);
        alert('Failed to start session. Please try again.');
    }
}

function getTopicName(topicKey) {
    const topicNames = {
        'upper_limb': 'Upper Limb Anatomy',
        'lower_limb': 'Lower Limb Anatomy',
        'cardiology': 'Cardiac Anatomy',
        'neuroanatomy': 'Neuroanatomy'
    };
    return topicNames[topicKey] || topicKey;
}

function displayQuestion(questionData) {
    if (questionData.completed) {
        endSession();
        return;
    }

    currentSession.currentQuestion = questionData.question;
    currentSession.questionIndex = questionData.question_index;

    document.getElementById('questionText').textContent = questionData.question;
    document.getElementById('studentAnswer').value = '';
    document.getElementById('feedbackArea').classList.add('hidden');

    updateProgress();
}

async function speakCurrentQuestion() {
    const questionText = currentSession.currentQuestion;
    if (!questionText) return;

    const listeningIndicator = document.getElementById('listeningIndicator');
    listeningIndicator.classList.remove('hidden');

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

        const data = await response.json();

        if (data.audio_data) {
            await playAudioData(data.audio_data);
        }
    } catch (error) {
        console.error('TTS failed:', error);
        // Fallback to browser TTS
        speakWithBrowserTTS(questionText);
    } finally {
        listeningIndicator.classList.add('hidden');
    }
}

async function playAudioData(base64Audio) {
    return new Promise((resolve) => {
        const audioBlob = base64ToBlob(base64Audio, 'audio/wav');
        const audioUrl = URL.createObjectURL(audioBlob);
        const audio = new Audio(audioUrl);

        audio.onended = () => {
            URL.revokeObjectURL(audioUrl);
            resolve();
        };

        audio.onerror = () => resolve();

        audio.play().catch(error => {
            console.error('Audio play failed:', error);
            resolve();
        });
    });
}

function speakWithBrowserTTS(text) {
    if ('speechSynthesis' in window) {
        const utterance = new SpeechSynthesisUtterance(text);
        utterance.rate = 0.8;
        utterance.pitch = 1.0;

        const voices = speechSynthesis.getVoices();
        const englishVoice = voices.find(voice => voice.lang.startsWith('en-'));
        if (englishVoice) {
            utterance.voice = englishVoice;
        }

        speechSynthesis.speak(utterance);
    }
}

function toggleVoiceInput() {
    const voiceBtn = document.getElementById('voiceBtn');

    if (!recognition) {
        alert('Speech recognition not supported in this browser. Please use Chrome or Edge.');
        return;
    }

    if (voiceBtn.textContent.includes('Start')) {
        startVoiceInput();
    } else {
        stopVoiceInput();
    }
}

function startVoiceInput() {
    const voiceBtn = document.getElementById('voiceBtn');
    voiceBtn.textContent = 'πŸ›‘ Stop Listening';
    voiceBtn.style.background = '#e74c3c';

    recognition.start();
}

function stopVoiceInput() {
    const voiceBtn = document.getElementById('voiceBtn');
    voiceBtn.textContent = '🎀 Voice Input';
    voiceBtn.style.background = '#9b59b6';

    if (recognition) {
        recognition.stop();
    }
}

async function submitAnswer() {
    const answer = document.getElementById('studentAnswer').value.trim();

    if (!answer) {
        alert('Please enter your answer');
        return;
    }

    try {
        const response = await fetch('/api/evaluate_answer', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                question_index: currentSession.questionIndex,
                answer: answer
            })
        });

        const data = await response.json();

        displayFeedback(data);

    } catch (error) {
        console.error('Error submitting answer:', error);
        alert('Failed to submit answer. Please try again.');
    }
}

function displayFeedback(data) {
    currentSession.totalScore += data.score;
    currentSession.questionCount++;

    document.getElementById('feedbackText').textContent = data.feedback;
    document.getElementById('scoreValue').textContent = data.score.toFixed(1);
    document.getElementById('totalScore').textContent = currentSession.totalScore.toFixed(1);
    document.getElementById('feedbackArea').classList.remove('hidden');

    updateProgress();
    addToHistory(data);
}

function addToHistory(data) {
    const historyContainer = document.getElementById('conversationHistory');
    const historyItem = document.createElement('div');
    historyItem.className = 'history-item';

    historyItem.innerHTML = `
        <strong>Q${currentSession.questionCount}:</strong> ${currentSession.currentQuestion}<br>
        <strong>Your Answer:</strong> ${data.transcribed_answer || document.getElementById('studentAnswer').value.substring(0, 100)}...<br>
        <strong>Score:</strong> ${data.score.toFixed(1)}/10
    `;

    historyContainer.appendChild(historyItem);
    historyContainer.scrollTop = historyContainer.scrollHeight;
}

function updateProgress() {
    const progressFill = document.querySelector('.progress-fill');
    if (currentSession.questionCount > 0) {
        const progress = (currentSession.questionCount / 5) * 100; // Assuming 5 questions per topic
        progressFill.style.width = `${Math.min(progress, 100)}%`;
    } else {
        progressFill.style.width = '0%';
    }
}

function nextQuestion() {
    // In a real implementation, this would get the next question from the server
    // For now, we'll simulate by ending the session after a few questions
    if (currentSession.questionCount >= 3) {
        endSession();
    } else {
        // Simulate getting next question
        const nextQuestion = {
            question: `Next question about ${currentSession.topic}...`,
            question_index: currentSession.questionIndex + 1
        };
        displayQuestion(nextQuestion);
    }
}

function endSession() {
    const averageScore = currentSession.totalScore / currentSession.questionCount;
    alert(`Session completed! Your average score: ${averageScore.toFixed(1)}/10\nWell done!`);
    resetSession();
}

function resetSession() {
    currentSession = {
        topic: null,
        currentQuestion: null,
        questionIndex: null,
        totalScore: 0,
        questionCount: 0
    };

    document.getElementById('vivaSession').classList.add('hidden');
    document.getElementById('topicSelection').classList.remove('hidden');
    document.getElementById('conversationHistory').innerHTML = '';
    document.getElementById('totalScore').textContent = '0';
}

// Utility function
function base64ToBlob(base64, mimeType) {
    const byteCharacters = atob(base64);
    const byteNumbers = new Array(byteCharacters.length);
    for (let i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    const byteArray = new Uint8Array(byteNumbers);
    return new Blob([byteArray], { type: mimeType });
}

// Initialize speech synthesis voices
if ('speechSynthesis' in window) {
    speechSynthesis.getVoices();
}