File size: 13,595 Bytes
3db77d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', () => {
    // DOM Elements
    const chatBox = document.getElementById('chat-box');
    const textInput = document.getElementById('text-input');
    const sendBtn = document.getElementById('send-btn');
    const micBtn = document.getElementById('mic-btn');
    const loadingIndicator = document.getElementById('loading-indicator');
    const statusIndicator = document.getElementById('status-indicator');
    
    // Mode Buttons
    const modeTextBtn = document.getElementById('mode-text');
    const modeVoiceBtn = document.getElementById('mode-voice');
    const modeVideoBtn = document.getElementById('mode-video');

    // Voice Controls
    const voiceControls = document.getElementById('voice-controls');
    const continuousToggle = document.getElementById('continuous-toggle');
    const rateSlider = document.getElementById('rate');
    const pitchSlider = document.getElementById('pitch');

    // Video Elements
    const videoFeed = document.getElementById('video-feed');
    const canvas = document.getElementById('canvas');
    const imageModal = document.getElementById('image-capture-modal');
    const closeModalBtn = document.getElementById('close-modal-btn');

    // State Variables
    let sessionId = null; // Will be set before each new query
    let currentMode = 'text'; // 'text', 'voice', 'video'
    let isListening = false;
    let isContinuousMode = false;
    let videoStream = null;

    const API_BASE_URL = 'https://nitinbot001-medbot-backend.hf.space';

    // Speech Recognition (STT) Setup
    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    let recognition;
    if (SpeechRecognition) {
        recognition = new SpeechRecognition();
        recognition.continuous = false;
        recognition.interimResults = false;
        recognition.lang = 'en-US'; // You can change this
    } else {
        micBtn.disabled = true;
        addMessageToUI('error', 'Speech Recognition is not supported in this browser.');
    }

    // Speech Synthesis (TTS) Setup
    const synth = window.speechSynthesis;

    // --- INITIALIZATION ---
    function initializeApp() {
        loadHistory();
        setupEventListeners();
        statusIndicator.textContent = '● Ready';
        statusIndicator.style.color = '#cccccc';
        addMessageToUI('ai', 'Hello! I am your MediBot Assistant. How can I help you today?');
    }

    async function startNewSession() {
        statusIndicator.textContent = '● Connecting...';
        statusIndicator.style.color = 'orange';
        try {
            const response = await fetch(`${API_BASE_URL}/start_session`, { method: 'POST' });
            if (!response.ok) throw new Error('Failed to start session');
            const data = await response.json();
            sessionId = data.session_id; // Set the session ID for the current transaction
            statusIndicator.textContent = '● Connected';
            statusIndicator.style.color = '#76ff03';
            console.log('New transaction session started:', sessionId);
            return true; // Indicate success
        } catch (error) {
            console.error('Session start error:', error);
            sessionId = null; // Ensure session ID is null on failure
            statusIndicator.textContent = '● Connection Failed';
            statusIndicator.style.color = '#ff4d4d';
            addMessageToUI('error', `Could not connect to the server. ${error.message}`);
            return false; // Indicate failure
        }
    }

    // --- EVENT LISTENERS ---
    function setupEventListeners() {
        sendBtn.addEventListener('click', handleTextInput);
        textInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter') handleTextInput();
        });

        // Mode Switching
        modeTextBtn.addEventListener('click', () => switchMode('text'));
        modeVoiceBtn.addEventListener('click', () => switchMode('voice'));
        modeVideoBtn.addEventListener('click', () => switchMode('video'));

        // Voice Controls
        micBtn.addEventListener('click', toggleListening);
        continuousToggle.addEventListener('change', (e) => {
            isContinuousMode = e.target.checked;
            if (recognition) {
                recognition.continuous = isContinuousMode;
            }
        });

        // STT Events
        if (recognition) {
            recognition.onstart = () => {
                isListening = true;
                micBtn.classList.add('listening');
                micBtn.innerHTML = '<i class="fas fa-stop"></i>';
            };
            recognition.onend = () => {
                isListening = false;
                micBtn.classList.remove('listening');
                micBtn.innerHTML = '<i class="fas fa-microphone"></i>';
                if (isContinuousMode && currentMode !== 'text') {
                    recognition.start(); // Keep listening in continuous mode
                }
            };
            recognition.onresult = (event) => {
                const transcript = event.results[event.results.length - 1][0].transcript.trim();
                textInput.value = transcript;
                processUserQuery(transcript);
            };
            recognition.onerror = (event) => {
                console.error('Speech recognition error:', event.error);
                addMessageToUI('error', `Speech recognition error: ${event.error}`);
            };
        }

        // Image Capture Modal
        closeModalBtn.addEventListener('click', () => {
            imageModal.classList.add('hidden');
            addMessageToUI('ai', 'Capturing image in 1 second...');
            setTimeout(captureAndSendImage, 1000);
        });
    }

    // --- CORE LOGIC ---
    function handleTextInput() {
        const query = textInput.value.trim();
        if (query) {
            processUserQuery(query);
            textInput.value = '';
        }
    }

    async function processUserQuery(query) {
        addMessageToUI('user', query);
        showLoading(true);

        // *** CHANGED LOGIC: Start a new session for every query ***
        const sessionStarted = await startNewSession();
        if (!sessionStarted) {
            showLoading(false);
            return; // Stop if session could not be created
        }

        try {
            const response = await fetch(`${API_BASE_URL}/process_query`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ session_id: sessionId, query: query })
            });

            const data = await response.json();

            if (!response.ok) {
                throw new Error(data.error || 'API request failed');
            }

            if (data.status === 'image_required') {
                handleImageRequest(data.message);
            } else {
                const message = data.response?.response || data.data;
                handleApiResponse(message);
            }

        } catch (error) {
            console.error('Error processing query:', error);
            handleApiResponse(`Sorry, I encountered an error: ${error.message}`, true);
        } finally {
            showLoading(false);
        }
    }

    function handleApiResponse(message, isError = false) {
        const type = isError ? 'error' : 'ai';
        addMessageToUI(type, message);
        if (!isError && (currentMode === 'voice' || currentMode === 'video')) {
            speak(message);
        }
    }

    // --- UI & STATE MANAGEMENT ---
    function switchMode(newMode) {
        if (currentMode === newMode) return;
        
        if (currentMode === 'video') stopCamera();
        if (isListening && recognition) recognition.stop();

        currentMode = newMode;
        
        document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
        document.getElementById(`mode-${newMode}`).classList.add('active');

        if (newMode === 'text') {
            document.body.classList.remove('body-video-mode');
            voiceControls.classList.add('hidden');
            micBtn.classList.add('hidden');
            sendBtn.classList.remove('hidden');
            textInput.classList.remove('hidden');
            videoFeed.style.display = 'none';
        } else if (newMode === 'voice') {
            document.body.classList.remove('body-video-mode');
            voiceControls.classList.remove('hidden');
            micBtn.classList.remove('hidden');
            sendBtn.classList.add('hidden');
            textInput.classList.add('hidden');
            videoFeed.style.display = 'none';
        } else if (newMode === 'video') {
            document.body.classList.add('body-video-mode');
            voiceControls.classList.remove('hidden');
            micBtn.classList.remove('hidden');
            sendBtn.classList.add('hidden');
            textInput.classList.add('hidden');
            videoFeed.style.display = 'block';
            startCamera();
        }
    }

    function addMessageToUI(sender, text) {
        const messageDiv = document.createElement('div');
        messageDiv.classList.add('message', `${sender}-message`);
        messageDiv.textContent = text;
        chatBox.appendChild(messageDiv);
        chatBox.scrollTop = chatBox.scrollHeight;
        saveHistory();
    }
    
    function showLoading(show) {
        loadingIndicator.style.display = show ? 'flex' : 'none';
    }

    // --- VOICE & VIDEO ---
    function toggleListening() {
        if (!recognition) return;
        if (isListening) {
            recognition.stop();
        } else {
            recognition.start();
        }
    }

    function speak(text) {
        if (synth.speaking) {
            console.error('SpeechSynthesis is already speaking.');
            return;
        }
        if (text !== '') {
            const utterance = new SpeechSynthesisUtterance(text);
            const femaleVoice = synth.getVoices().find(voice => voice.name.includes('Female') || voice.gender === 'female');
            if(femaleVoice) utterance.voice = femaleVoice;
            utterance.pitch = pitchSlider.value;
            utterance.rate = rateSlider.value;
            synth.speak(utterance);
        }
    }

    async function startCamera() {
        try {
            videoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
            videoFeed.srcObject = videoStream;
        } catch (err) {
            console.error("Error accessing camera: ", err);
            addMessageToUI('error', 'Could not access the camera. Please grant permission.');
            switchMode('voice');
        }
    }

    function stopCamera() {
        if (videoStream) {
            videoStream.getTracks().forEach(track => track.stop());
            videoFeed.srcObject = null;
            videoStream = null;
        }
    }
    
    function handleImageRequest(message) {
        addMessageToUI('ai', message);
        if (currentMode !== 'video') {
            addMessageToUI('ai', "Please switch to Video mode to provide an image.");
        } else {
            imageModal.classList.remove('hidden');
        }
    }

    async function captureAndSendImage() {
        if (!videoStream || !sessionId) {
            addMessageToUI('error', 'Cannot capture image. Video stream or session is not active.');
            return;
        };
        
        const videoTrack = videoStream.getVideoTracks()[0];
        const settings = videoTrack.getSettings();
        canvas.width = settings.width;
        canvas.height = settings.height;
        
        const context = canvas.getContext('2d');
        context.drawImage(videoFeed, 0, 0, canvas.width, canvas.height);

        canvas.toBlob(async (blob) => {
            const formData = new FormData();
            formData.append('session_id', sessionId);
            formData.append('photo', blob, 'capture.jpg');
            
            showLoading(true);
            try {
                const response = await fetch(`${API_BASE_URL}/process_with_image`, {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                if (!response.ok) throw new Error(data.message || 'Image processing failed');

                const message = data.response?.response || data.data;
                handleApiResponse(message);

            } catch (error) {
                console.error('Error sending image:', error);
                handleApiResponse(`Sorry, I couldn't process the image: ${error.message}`, true);
            } finally {
                showLoading(false);
            }
        }, 'image/jpeg');
    }

    // --- LOCAL STORAGE ---
    function saveHistory() {
        localStorage.setItem('medibotChatHistory', chatBox.innerHTML);
    }
    function loadHistory() {
        const history = localStorage.getItem('medibotChatHistory');
        if (history) {
            chatBox.innerHTML = history;
            chatBox.scrollTop = chatBox.scrollHeight;
        }
    }
    
    // --- START THE APP ---
    initializeApp();
});