Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- index.html +81 -0
- script.js +352 -0
- style.css +251 -0
index.html
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>MediBot Conversational AI</title>
|
| 7 |
+
<link rel="stylesheet" href="style.css">
|
| 8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<div class="main-container">
|
| 12 |
+
<!-- Video Element (for video mode) -->
|
| 13 |
+
<video id="video-feed" autoplay playsinline muted></video>
|
| 14 |
+
|
| 15 |
+
<div class="chat-app">
|
| 16 |
+
<div class="header">
|
| 17 |
+
<div class="app-title">
|
| 18 |
+
<i class="fas fa-robot"></i>
|
| 19 |
+
<span>MediBot Assistant</span>
|
| 20 |
+
</div>
|
| 21 |
+
<div class="status-indicator" id="status-indicator">● Connecting...</div>
|
| 22 |
+
</div>
|
| 23 |
+
|
| 24 |
+
<div class="chat-box" id="chat-box">
|
| 25 |
+
<!-- Messages will be appended here -->
|
| 26 |
+
</div>
|
| 27 |
+
|
| 28 |
+
<div class="loading-indicator" id="loading-indicator" style="display: none;">
|
| 29 |
+
<div class="spinner"></div>
|
| 30 |
+
<span>MediBot is thinking...</span>
|
| 31 |
+
</div>
|
| 32 |
+
|
| 33 |
+
<div class="input-area">
|
| 34 |
+
<input type="text" id="text-input" placeholder="Type your message...">
|
| 35 |
+
<button id="mic-btn" class="control-btn"><i class="fas fa-microphone"></i></button>
|
| 36 |
+
<button id="send-btn" class="control-btn"><i class="fas fa-paper-plane"></i></button>
|
| 37 |
+
</div>
|
| 38 |
+
</div>
|
| 39 |
+
|
| 40 |
+
<div class="controls-panel">
|
| 41 |
+
<h3>Controls</h3>
|
| 42 |
+
<div class="mode-switcher">
|
| 43 |
+
<button id="mode-text" class="mode-btn active">Text</button>
|
| 44 |
+
<button id="mode-voice" class="mode-btn">Voice</button>
|
| 45 |
+
<button id="mode-video" class="mode-btn">Video</button>
|
| 46 |
+
</div>
|
| 47 |
+
<div id="voice-controls" class="control-section hidden">
|
| 48 |
+
<h4>Voice Settings</h4>
|
| 49 |
+
<div class="toggle-switch">
|
| 50 |
+
<span>Push-to-Talk</span>
|
| 51 |
+
<label class="switch">
|
| 52 |
+
<input type="checkbox" id="continuous-toggle">
|
| 53 |
+
<span class="slider round"></span>
|
| 54 |
+
</label>
|
| 55 |
+
<span>Continuous</span>
|
| 56 |
+
</div>
|
| 57 |
+
<div class="tts-settings">
|
| 58 |
+
<label for="rate">Speed:</label>
|
| 59 |
+
<input type="range" id="rate" min="0.5" max="2" value="1" step="0.1">
|
| 60 |
+
<label for="pitch">Pitch:</label>
|
| 61 |
+
<input type="range" id="pitch" min="0" max="2" value="1" step="0.1">
|
| 62 |
+
</div>
|
| 63 |
+
</div>
|
| 64 |
+
</div>
|
| 65 |
+
</div>
|
| 66 |
+
|
| 67 |
+
<!-- Image Capture Popup/Modal -->
|
| 68 |
+
<div id="image-capture-modal" class="modal-overlay hidden">
|
| 69 |
+
<div class="modal-content">
|
| 70 |
+
<h3><i class="fas fa-camera"></i> Image Capture Required</h3>
|
| 71 |
+
<p>Don't shake the camera, I am analyzing the object.</p>
|
| 72 |
+
<button id="close-modal-btn">Okay, I'm Ready</button>
|
| 73 |
+
</div>
|
| 74 |
+
</div>
|
| 75 |
+
|
| 76 |
+
<!-- Canvas for capturing image -->
|
| 77 |
+
<canvas id="canvas" style="display: none;"></canvas>
|
| 78 |
+
|
| 79 |
+
<script src="script.js"></script>
|
| 80 |
+
</body>
|
| 81 |
+
</html>
|
script.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
// DOM Elements
|
| 3 |
+
const chatBox = document.getElementById('chat-box');
|
| 4 |
+
const textInput = document.getElementById('text-input');
|
| 5 |
+
const sendBtn = document.getElementById('send-btn');
|
| 6 |
+
const micBtn = document.getElementById('mic-btn');
|
| 7 |
+
const loadingIndicator = document.getElementById('loading-indicator');
|
| 8 |
+
const statusIndicator = document.getElementById('status-indicator');
|
| 9 |
+
|
| 10 |
+
// Mode Buttons
|
| 11 |
+
const modeTextBtn = document.getElementById('mode-text');
|
| 12 |
+
const modeVoiceBtn = document.getElementById('mode-voice');
|
| 13 |
+
const modeVideoBtn = document.getElementById('mode-video');
|
| 14 |
+
|
| 15 |
+
// Voice Controls
|
| 16 |
+
const voiceControls = document.getElementById('voice-controls');
|
| 17 |
+
const continuousToggle = document.getElementById('continuous-toggle');
|
| 18 |
+
const rateSlider = document.getElementById('rate');
|
| 19 |
+
const pitchSlider = document.getElementById('pitch');
|
| 20 |
+
|
| 21 |
+
// Video Elements
|
| 22 |
+
const videoFeed = document.getElementById('video-feed');
|
| 23 |
+
const canvas = document.getElementById('canvas');
|
| 24 |
+
const imageModal = document.getElementById('image-capture-modal');
|
| 25 |
+
const closeModalBtn = document.getElementById('close-modal-btn');
|
| 26 |
+
|
| 27 |
+
// State Variables
|
| 28 |
+
let sessionId = null; // Will be set before each new query
|
| 29 |
+
let currentMode = 'text'; // 'text', 'voice', 'video'
|
| 30 |
+
let isListening = false;
|
| 31 |
+
let isContinuousMode = false;
|
| 32 |
+
let videoStream = null;
|
| 33 |
+
|
| 34 |
+
const API_BASE_URL = 'https://nitinbot001-medbot-backend.hf.space';
|
| 35 |
+
|
| 36 |
+
// Speech Recognition (STT) Setup
|
| 37 |
+
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 38 |
+
let recognition;
|
| 39 |
+
if (SpeechRecognition) {
|
| 40 |
+
recognition = new SpeechRecognition();
|
| 41 |
+
recognition.continuous = false;
|
| 42 |
+
recognition.interimResults = false;
|
| 43 |
+
recognition.lang = 'en-US'; // You can change this
|
| 44 |
+
} else {
|
| 45 |
+
micBtn.disabled = true;
|
| 46 |
+
addMessageToUI('error', 'Speech Recognition is not supported in this browser.');
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Speech Synthesis (TTS) Setup
|
| 50 |
+
const synth = window.speechSynthesis;
|
| 51 |
+
|
| 52 |
+
// --- INITIALIZATION ---
|
| 53 |
+
function initializeApp() {
|
| 54 |
+
loadHistory();
|
| 55 |
+
setupEventListeners();
|
| 56 |
+
statusIndicator.textContent = '● Ready';
|
| 57 |
+
statusIndicator.style.color = '#cccccc';
|
| 58 |
+
addMessageToUI('ai', 'Hello! I am your MediBot Assistant. How can I help you today?');
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
async function startNewSession() {
|
| 62 |
+
statusIndicator.textContent = '● Connecting...';
|
| 63 |
+
statusIndicator.style.color = 'orange';
|
| 64 |
+
try {
|
| 65 |
+
const response = await fetch(`${API_BASE_URL}/start_session`, { method: 'POST' });
|
| 66 |
+
if (!response.ok) throw new Error('Failed to start session');
|
| 67 |
+
const data = await response.json();
|
| 68 |
+
sessionId = data.session_id; // Set the session ID for the current transaction
|
| 69 |
+
statusIndicator.textContent = '● Connected';
|
| 70 |
+
statusIndicator.style.color = '#76ff03';
|
| 71 |
+
console.log('New transaction session started:', sessionId);
|
| 72 |
+
return true; // Indicate success
|
| 73 |
+
} catch (error) {
|
| 74 |
+
console.error('Session start error:', error);
|
| 75 |
+
sessionId = null; // Ensure session ID is null on failure
|
| 76 |
+
statusIndicator.textContent = '● Connection Failed';
|
| 77 |
+
statusIndicator.style.color = '#ff4d4d';
|
| 78 |
+
addMessageToUI('error', `Could not connect to the server. ${error.message}`);
|
| 79 |
+
return false; // Indicate failure
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// --- EVENT LISTENERS ---
|
| 84 |
+
function setupEventListeners() {
|
| 85 |
+
sendBtn.addEventListener('click', handleTextInput);
|
| 86 |
+
textInput.addEventListener('keydown', (e) => {
|
| 87 |
+
if (e.key === 'Enter') handleTextInput();
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
// Mode Switching
|
| 91 |
+
modeTextBtn.addEventListener('click', () => switchMode('text'));
|
| 92 |
+
modeVoiceBtn.addEventListener('click', () => switchMode('voice'));
|
| 93 |
+
modeVideoBtn.addEventListener('click', () => switchMode('video'));
|
| 94 |
+
|
| 95 |
+
// Voice Controls
|
| 96 |
+
micBtn.addEventListener('click', toggleListening);
|
| 97 |
+
continuousToggle.addEventListener('change', (e) => {
|
| 98 |
+
isContinuousMode = e.target.checked;
|
| 99 |
+
if (recognition) {
|
| 100 |
+
recognition.continuous = isContinuousMode;
|
| 101 |
+
}
|
| 102 |
+
});
|
| 103 |
+
|
| 104 |
+
// STT Events
|
| 105 |
+
if (recognition) {
|
| 106 |
+
recognition.onstart = () => {
|
| 107 |
+
isListening = true;
|
| 108 |
+
micBtn.classList.add('listening');
|
| 109 |
+
micBtn.innerHTML = '<i class="fas fa-stop"></i>';
|
| 110 |
+
};
|
| 111 |
+
recognition.onend = () => {
|
| 112 |
+
isListening = false;
|
| 113 |
+
micBtn.classList.remove('listening');
|
| 114 |
+
micBtn.innerHTML = '<i class="fas fa-microphone"></i>';
|
| 115 |
+
if (isContinuousMode && currentMode !== 'text') {
|
| 116 |
+
recognition.start(); // Keep listening in continuous mode
|
| 117 |
+
}
|
| 118 |
+
};
|
| 119 |
+
recognition.onresult = (event) => {
|
| 120 |
+
const transcript = event.results[event.results.length - 1][0].transcript.trim();
|
| 121 |
+
textInput.value = transcript;
|
| 122 |
+
processUserQuery(transcript);
|
| 123 |
+
};
|
| 124 |
+
recognition.onerror = (event) => {
|
| 125 |
+
console.error('Speech recognition error:', event.error);
|
| 126 |
+
addMessageToUI('error', `Speech recognition error: ${event.error}`);
|
| 127 |
+
};
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// Image Capture Modal
|
| 131 |
+
closeModalBtn.addEventListener('click', () => {
|
| 132 |
+
imageModal.classList.add('hidden');
|
| 133 |
+
addMessageToUI('ai', 'Capturing image in 1 second...');
|
| 134 |
+
setTimeout(captureAndSendImage, 1000);
|
| 135 |
+
});
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// --- CORE LOGIC ---
|
| 139 |
+
function handleTextInput() {
|
| 140 |
+
const query = textInput.value.trim();
|
| 141 |
+
if (query) {
|
| 142 |
+
processUserQuery(query);
|
| 143 |
+
textInput.value = '';
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
async function processUserQuery(query) {
|
| 148 |
+
addMessageToUI('user', query);
|
| 149 |
+
showLoading(true);
|
| 150 |
+
|
| 151 |
+
// *** CHANGED LOGIC: Start a new session for every query ***
|
| 152 |
+
const sessionStarted = await startNewSession();
|
| 153 |
+
if (!sessionStarted) {
|
| 154 |
+
showLoading(false);
|
| 155 |
+
return; // Stop if session could not be created
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
try {
|
| 159 |
+
const response = await fetch(`${API_BASE_URL}/process_query`, {
|
| 160 |
+
method: 'POST',
|
| 161 |
+
headers: { 'Content-Type': 'application/json' },
|
| 162 |
+
body: JSON.stringify({ session_id: sessionId, query: query })
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
+
const data = await response.json();
|
| 166 |
+
|
| 167 |
+
if (!response.ok) {
|
| 168 |
+
throw new Error(data.error || 'API request failed');
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
if (data.status === 'image_required') {
|
| 172 |
+
handleImageRequest(data.message);
|
| 173 |
+
} else {
|
| 174 |
+
const message = data.response?.response || data.data;
|
| 175 |
+
handleApiResponse(message);
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
} catch (error) {
|
| 179 |
+
console.error('Error processing query:', error);
|
| 180 |
+
handleApiResponse(`Sorry, I encountered an error: ${error.message}`, true);
|
| 181 |
+
} finally {
|
| 182 |
+
showLoading(false);
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
function handleApiResponse(message, isError = false) {
|
| 187 |
+
const type = isError ? 'error' : 'ai';
|
| 188 |
+
addMessageToUI(type, message);
|
| 189 |
+
if (!isError && (currentMode === 'voice' || currentMode === 'video')) {
|
| 190 |
+
speak(message);
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
// --- UI & STATE MANAGEMENT ---
|
| 195 |
+
function switchMode(newMode) {
|
| 196 |
+
if (currentMode === newMode) return;
|
| 197 |
+
|
| 198 |
+
if (currentMode === 'video') stopCamera();
|
| 199 |
+
if (isListening && recognition) recognition.stop();
|
| 200 |
+
|
| 201 |
+
currentMode = newMode;
|
| 202 |
+
|
| 203 |
+
document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
|
| 204 |
+
document.getElementById(`mode-${newMode}`).classList.add('active');
|
| 205 |
+
|
| 206 |
+
if (newMode === 'text') {
|
| 207 |
+
document.body.classList.remove('body-video-mode');
|
| 208 |
+
voiceControls.classList.add('hidden');
|
| 209 |
+
micBtn.classList.add('hidden');
|
| 210 |
+
sendBtn.classList.remove('hidden');
|
| 211 |
+
textInput.classList.remove('hidden');
|
| 212 |
+
videoFeed.style.display = 'none';
|
| 213 |
+
} else if (newMode === 'voice') {
|
| 214 |
+
document.body.classList.remove('body-video-mode');
|
| 215 |
+
voiceControls.classList.remove('hidden');
|
| 216 |
+
micBtn.classList.remove('hidden');
|
| 217 |
+
sendBtn.classList.add('hidden');
|
| 218 |
+
textInput.classList.add('hidden');
|
| 219 |
+
videoFeed.style.display = 'none';
|
| 220 |
+
} else if (newMode === 'video') {
|
| 221 |
+
document.body.classList.add('body-video-mode');
|
| 222 |
+
voiceControls.classList.remove('hidden');
|
| 223 |
+
micBtn.classList.remove('hidden');
|
| 224 |
+
sendBtn.classList.add('hidden');
|
| 225 |
+
textInput.classList.add('hidden');
|
| 226 |
+
videoFeed.style.display = 'block';
|
| 227 |
+
startCamera();
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
function addMessageToUI(sender, text) {
|
| 232 |
+
const messageDiv = document.createElement('div');
|
| 233 |
+
messageDiv.classList.add('message', `${sender}-message`);
|
| 234 |
+
messageDiv.textContent = text;
|
| 235 |
+
chatBox.appendChild(messageDiv);
|
| 236 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 237 |
+
saveHistory();
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
function showLoading(show) {
|
| 241 |
+
loadingIndicator.style.display = show ? 'flex' : 'none';
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
// --- VOICE & VIDEO ---
|
| 245 |
+
function toggleListening() {
|
| 246 |
+
if (!recognition) return;
|
| 247 |
+
if (isListening) {
|
| 248 |
+
recognition.stop();
|
| 249 |
+
} else {
|
| 250 |
+
recognition.start();
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
function speak(text) {
|
| 255 |
+
if (synth.speaking) {
|
| 256 |
+
console.error('SpeechSynthesis is already speaking.');
|
| 257 |
+
return;
|
| 258 |
+
}
|
| 259 |
+
if (text !== '') {
|
| 260 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 261 |
+
const femaleVoice = synth.getVoices().find(voice => voice.name.includes('Female') || voice.gender === 'female');
|
| 262 |
+
if(femaleVoice) utterance.voice = femaleVoice;
|
| 263 |
+
utterance.pitch = pitchSlider.value;
|
| 264 |
+
utterance.rate = rateSlider.value;
|
| 265 |
+
synth.speak(utterance);
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
async function startCamera() {
|
| 270 |
+
try {
|
| 271 |
+
videoStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
|
| 272 |
+
videoFeed.srcObject = videoStream;
|
| 273 |
+
} catch (err) {
|
| 274 |
+
console.error("Error accessing camera: ", err);
|
| 275 |
+
addMessageToUI('error', 'Could not access the camera. Please grant permission.');
|
| 276 |
+
switchMode('voice');
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
function stopCamera() {
|
| 281 |
+
if (videoStream) {
|
| 282 |
+
videoStream.getTracks().forEach(track => track.stop());
|
| 283 |
+
videoFeed.srcObject = null;
|
| 284 |
+
videoStream = null;
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
function handleImageRequest(message) {
|
| 289 |
+
addMessageToUI('ai', message);
|
| 290 |
+
if (currentMode !== 'video') {
|
| 291 |
+
addMessageToUI('ai', "Please switch to Video mode to provide an image.");
|
| 292 |
+
} else {
|
| 293 |
+
imageModal.classList.remove('hidden');
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
async function captureAndSendImage() {
|
| 298 |
+
if (!videoStream || !sessionId) {
|
| 299 |
+
addMessageToUI('error', 'Cannot capture image. Video stream or session is not active.');
|
| 300 |
+
return;
|
| 301 |
+
};
|
| 302 |
+
|
| 303 |
+
const videoTrack = videoStream.getVideoTracks()[0];
|
| 304 |
+
const settings = videoTrack.getSettings();
|
| 305 |
+
canvas.width = settings.width;
|
| 306 |
+
canvas.height = settings.height;
|
| 307 |
+
|
| 308 |
+
const context = canvas.getContext('2d');
|
| 309 |
+
context.drawImage(videoFeed, 0, 0, canvas.width, canvas.height);
|
| 310 |
+
|
| 311 |
+
canvas.toBlob(async (blob) => {
|
| 312 |
+
const formData = new FormData();
|
| 313 |
+
formData.append('session_id', sessionId);
|
| 314 |
+
formData.append('photo', blob, 'capture.jpg');
|
| 315 |
+
|
| 316 |
+
showLoading(true);
|
| 317 |
+
try {
|
| 318 |
+
const response = await fetch(`${API_BASE_URL}/process_with_image`, {
|
| 319 |
+
method: 'POST',
|
| 320 |
+
body: formData
|
| 321 |
+
});
|
| 322 |
+
|
| 323 |
+
const data = await response.json();
|
| 324 |
+
if (!response.ok) throw new Error(data.message || 'Image processing failed');
|
| 325 |
+
|
| 326 |
+
const message = data.response?.response || data.data;
|
| 327 |
+
handleApiResponse(message);
|
| 328 |
+
|
| 329 |
+
} catch (error) {
|
| 330 |
+
console.error('Error sending image:', error);
|
| 331 |
+
handleApiResponse(`Sorry, I couldn't process the image: ${error.message}`, true);
|
| 332 |
+
} finally {
|
| 333 |
+
showLoading(false);
|
| 334 |
+
}
|
| 335 |
+
}, 'image/jpeg');
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
// --- LOCAL STORAGE ---
|
| 339 |
+
function saveHistory() {
|
| 340 |
+
localStorage.setItem('medibotChatHistory', chatBox.innerHTML);
|
| 341 |
+
}
|
| 342 |
+
function loadHistory() {
|
| 343 |
+
const history = localStorage.getItem('medibotChatHistory');
|
| 344 |
+
if (history) {
|
| 345 |
+
chatBox.innerHTML = history;
|
| 346 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 347 |
+
}
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
:root {
|
| 2 |
+
--primary-bg: #f0f2f5;
|
| 3 |
+
--secondary-bg: #ffffff;
|
| 4 |
+
--header-bg: #00a884;
|
| 5 |
+
--user-msg-bg: #d9fdd3;
|
| 6 |
+
--ai-msg-bg: #ffffff;
|
| 7 |
+
--text-dark: #111b21;
|
| 8 |
+
--text-light: #ffffff;
|
| 9 |
+
--border-color: #e9edef;
|
| 10 |
+
--accent-color: #005c4b;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
body {
|
| 14 |
+
font-family: Arial, sans-serif;
|
| 15 |
+
margin: 0;
|
| 16 |
+
background-color: var(--primary-bg);
|
| 17 |
+
display: flex;
|
| 18 |
+
justify-content: center;
|
| 19 |
+
align-items: center;
|
| 20 |
+
min-height: 100vh;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
.main-container {
|
| 24 |
+
display: flex;
|
| 25 |
+
gap: 20px;
|
| 26 |
+
width: 95%;
|
| 27 |
+
max-width: 1200px;
|
| 28 |
+
height: 90vh;
|
| 29 |
+
max-height: 800px;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
.chat-app {
|
| 33 |
+
flex: 2;
|
| 34 |
+
display: flex;
|
| 35 |
+
flex-direction: column;
|
| 36 |
+
background-color: var(--secondary-bg);
|
| 37 |
+
border-radius: 8px;
|
| 38 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 39 |
+
overflow: hidden;
|
| 40 |
+
position: relative;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
#video-feed {
|
| 44 |
+
position: absolute;
|
| 45 |
+
top: 0; left: 0;
|
| 46 |
+
width: 100%; height: 100%;
|
| 47 |
+
object-fit: cover;
|
| 48 |
+
z-index: -1;
|
| 49 |
+
display: none;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
.body-video-mode .chat-app {
|
| 53 |
+
background-color: rgba(0, 0, 0, 0.4); /* Make chat slightly transparent */
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
.header {
|
| 57 |
+
background-color: var(--header-bg);
|
| 58 |
+
color: var(--text-light);
|
| 59 |
+
padding: 15px 20px;
|
| 60 |
+
display: flex;
|
| 61 |
+
justify-content: space-between;
|
| 62 |
+
align-items: center;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.app-title {
|
| 66 |
+
font-size: 1.2rem;
|
| 67 |
+
font-weight: bold;
|
| 68 |
+
}
|
| 69 |
+
.app-title i { margin-right: 10px; }
|
| 70 |
+
|
| 71 |
+
.status-indicator {
|
| 72 |
+
font-size: 0.8rem;
|
| 73 |
+
opacity: 0.9;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.chat-box {
|
| 77 |
+
flex-grow: 1;
|
| 78 |
+
padding: 20px;
|
| 79 |
+
overflow-y: auto;
|
| 80 |
+
background-image: url('https://user-images.githubusercontent.com/15075759/28719144-86dc0f70-73b1-11e7-911d-60d70fcded21.png');
|
| 81 |
+
background-repeat: repeat;
|
| 82 |
+
display: flex;
|
| 83 |
+
flex-direction: column;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.message {
|
| 87 |
+
padding: 10px 15px;
|
| 88 |
+
border-radius: 18px;
|
| 89 |
+
margin-bottom: 10px;
|
| 90 |
+
max-width: 70%;
|
| 91 |
+
word-wrap: break-word;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
.user-message {
|
| 95 |
+
background-color: var(--user-msg-bg);
|
| 96 |
+
align-self: flex-end;
|
| 97 |
+
border-bottom-right-radius: 4px;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.ai-message {
|
| 101 |
+
background-color: var(--ai-msg-bg);
|
| 102 |
+
align-self: flex-start;
|
| 103 |
+
border: 1px solid var(--border-color);
|
| 104 |
+
border-bottom-left-radius: 4px;
|
| 105 |
+
}
|
| 106 |
+
.error-message {
|
| 107 |
+
background-color: #ffdddd;
|
| 108 |
+
color: #d8000c;
|
| 109 |
+
align-self: center;
|
| 110 |
+
max-width: 80%;
|
| 111 |
+
text-align: center;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
.input-area {
|
| 115 |
+
display: flex;
|
| 116 |
+
padding: 10px;
|
| 117 |
+
background-color: var(--primary-bg);
|
| 118 |
+
border-top: 1px solid var(--border-color);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
#text-input {
|
| 122 |
+
flex-grow: 1;
|
| 123 |
+
border: none;
|
| 124 |
+
padding: 12px 15px;
|
| 125 |
+
border-radius: 20px;
|
| 126 |
+
font-size: 1rem;
|
| 127 |
+
}
|
| 128 |
+
#text-input:focus { outline: none; }
|
| 129 |
+
|
| 130 |
+
.control-btn {
|
| 131 |
+
background: var(--header-bg);
|
| 132 |
+
color: white;
|
| 133 |
+
border: none;
|
| 134 |
+
border-radius: 50%;
|
| 135 |
+
width: 45px;
|
| 136 |
+
height: 45px;
|
| 137 |
+
margin-left: 10px;
|
| 138 |
+
cursor: pointer;
|
| 139 |
+
font-size: 1.2rem;
|
| 140 |
+
transition: background-color 0.2s;
|
| 141 |
+
}
|
| 142 |
+
.control-btn:hover { background-color: var(--accent-color); }
|
| 143 |
+
#mic-btn.listening { background-color: #ff4d4d; }
|
| 144 |
+
|
| 145 |
+
.controls-panel {
|
| 146 |
+
flex: 1;
|
| 147 |
+
background: var(--secondary-bg);
|
| 148 |
+
padding: 20px;
|
| 149 |
+
border-radius: 8px;
|
| 150 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
.controls-panel h3, .controls-panel h4 {
|
| 154 |
+
margin-top: 0;
|
| 155 |
+
color: var(--accent-color);
|
| 156 |
+
border-bottom: 2px solid var(--border-color);
|
| 157 |
+
padding-bottom: 10px;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
.mode-switcher {
|
| 161 |
+
display: flex;
|
| 162 |
+
gap: 10px;
|
| 163 |
+
margin-bottom: 20px;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.mode-btn {
|
| 167 |
+
flex: 1;
|
| 168 |
+
padding: 10px;
|
| 169 |
+
border: 1px solid var(--header-bg);
|
| 170 |
+
background: white;
|
| 171 |
+
color: var(--header-bg);
|
| 172 |
+
border-radius: 5px;
|
| 173 |
+
cursor: pointer;
|
| 174 |
+
transition: all 0.2s;
|
| 175 |
+
}
|
| 176 |
+
.mode-btn.active, .mode-btn:hover {
|
| 177 |
+
background: var(--header-bg);
|
| 178 |
+
color: white;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.control-section {
|
| 182 |
+
margin-top: 20px;
|
| 183 |
+
}
|
| 184 |
+
.hidden { display: none !important; }
|
| 185 |
+
|
| 186 |
+
/* Toggle Switch */
|
| 187 |
+
.toggle-switch { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
|
| 188 |
+
.switch { position: relative; display: inline-block; width: 60px; height: 34px; }
|
| 189 |
+
.switch input { opacity: 0; width: 0; height: 0; }
|
| 190 |
+
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; }
|
| 191 |
+
.slider.round { border-radius: 34px; }
|
| 192 |
+
.slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%; }
|
| 193 |
+
input:checked + .slider { background-color: var(--header-bg); }
|
| 194 |
+
input:checked + .slider:before { transform: translateX(26px); }
|
| 195 |
+
|
| 196 |
+
/* TTS Sliders */
|
| 197 |
+
.tts-settings { display: flex; flex-direction: column; gap: 10px; }
|
| 198 |
+
.tts-settings label { font-weight: bold; }
|
| 199 |
+
.tts-settings input[type="range"] { width: 100%; }
|
| 200 |
+
|
| 201 |
+
/* Loading Indicator */
|
| 202 |
+
.loading-indicator {
|
| 203 |
+
display: flex;
|
| 204 |
+
align-items: center;
|
| 205 |
+
justify-content: center;
|
| 206 |
+
gap: 10px;
|
| 207 |
+
padding: 10px;
|
| 208 |
+
background-color: rgba(255, 255, 255, 0.8);
|
| 209 |
+
}
|
| 210 |
+
.spinner {
|
| 211 |
+
border: 4px solid rgba(0,0,0,0.1);
|
| 212 |
+
width: 24px;
|
| 213 |
+
height: 24px;
|
| 214 |
+
border-radius: 50%;
|
| 215 |
+
border-left-color: var(--header-bg);
|
| 216 |
+
animation: spin 1s ease infinite;
|
| 217 |
+
}
|
| 218 |
+
@keyframes spin {
|
| 219 |
+
0% { transform: rotate(0deg); }
|
| 220 |
+
100% { transform: rotate(360deg); }
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
/* Modal */
|
| 224 |
+
.modal-overlay {
|
| 225 |
+
position: fixed;
|
| 226 |
+
top: 0; left: 0;
|
| 227 |
+
width: 100%; height: 100%;
|
| 228 |
+
background: rgba(0,0,0,0.6);
|
| 229 |
+
display: flex;
|
| 230 |
+
justify-content: center;
|
| 231 |
+
align-items: center;
|
| 232 |
+
z-index: 1000;
|
| 233 |
+
}
|
| 234 |
+
.modal-content {
|
| 235 |
+
background: white;
|
| 236 |
+
padding: 30px;
|
| 237 |
+
border-radius: 8px;
|
| 238 |
+
text-align: center;
|
| 239 |
+
max-width: 400px;
|
| 240 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
|
| 241 |
+
}
|
| 242 |
+
#close-modal-btn {
|
| 243 |
+
background: var(--header-bg);
|
| 244 |
+
color: white;
|
| 245 |
+
border: none;
|
| 246 |
+
padding: 10px 20px;
|
| 247 |
+
border-radius: 5px;
|
| 248 |
+
cursor: pointer;
|
| 249 |
+
margin-top: 15px;
|
| 250 |
+
font-size: 1rem;
|
| 251 |
+
}
|