Spaces:
Sleeping
Sleeping
File size: 13,728 Bytes
8a693e2 | 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 | // ===== GLOBAL STATE =====
let currentResults = null;
let explanations = null;
// ===== INITIALIZATION =====
document.addEventListener('DOMContentLoaded', () => {
// Navigation is now handled via onclick="showSection", but we init other listeners
initFileUpload();
initScrollAnimations();
initEnterKey();
});
// ===== NAVIGATION (Compatible with Tailwind HTML) =====
function showSection(sectionId) {
// Hide all sections
document.querySelectorAll('.section').forEach(sec => {
sec.classList.remove('active');
sec.style.display = 'none';
});
// Show target section
const target = document.getElementById(sectionId);
if (target) {
target.style.display = 'block';
// Small timeout to allow display:block to apply before adding active class for animation
setTimeout(() => {
target.classList.add('active');
}, 10);
// Scroll to top
window.scrollTo({ top: 0, behavior: 'smooth' });
}
}
// ===== SCROLL ANIMATIONS =====
function initScrollAnimations() {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.remove('opacity-0', 'translate-y-4');
entry.target.classList.add('opacity-100', 'translate-y-0');
}
});
}, { threshold: 0.1 });
// Add animation classes to cards
document.querySelectorAll('.feature-card, .upload-card').forEach(card => {
card.classList.add('transition-all', 'duration-700', 'opacity-0', 'translate-y-4');
observer.observe(card);
});
}
// ===== FILE UPLOAD =====
function initFileUpload() {
const fileInput = document.getElementById('fileInput');
const uploadArea = document.getElementById('uploadArea');
if (!fileInput || !uploadArea) return;
// Click to upload (handled by onclick in HTML, but keeping listener just in case)
fileInput.addEventListener('change', handleFileSelect);
// Drag and drop Visuals
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
// Add Tailwind classes for drag state
uploadArea.classList.add('border-secondary', 'bg-blue-50', 'scale-[1.02]');
});
uploadArea.addEventListener('dragleave', () => {
// Remove Tailwind classes
uploadArea.classList.remove('border-secondary', 'bg-blue-50', 'scale-[1.02]');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('border-secondary', 'bg-blue-50', 'scale-[1.02]');
const file = e.dataTransfer.files[0];
if (file && file.type === 'application/pdf') {
uploadFile(file);
} else {
showToast('Please upload a PDF file', 'error');
}
});
}
function handleFileSelect(e) {
const file = e.target.files[0];
if (file) {
uploadFile(file);
}
}
async function uploadFile(file) {
const formData = new FormData();
formData.append('file', file);
// Show progress
document.getElementById('uploadArea').classList.add('hidden');
document.getElementById('uploadProgress').classList.remove('hidden');
try {
const response = await fetch('/api/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || 'Upload failed');
}
currentResults = data.results;
showToast(`✓ Found ${data.count} lab results!`, 'success');
// Generate explanations
await generateExplanations();
// Display results
displayResults();
// Switch to results tab automatically
showSection('results-section');
} catch (error) {
showToast(error.message, 'error');
resetUploadArea();
}
}
async function generateExplanations() {
// Optional: show a mini toast or loading indicator
try {
const response = await fetch('/api/explain', { method: 'POST' });
const data = await response.json();
if (!response.ok) throw new Error(data.error);
explanations = data.explanations;
} catch (error) {
console.warn('Auto-explanation generation failed:', error);
// We continue anyway, results will just say "Loading..." or show basic info
}
}
function displayResults() {
const container = document.getElementById('resultsContainer');
if (!currentResults || currentResults.length === 0) {
container.innerHTML = '<div class="text-center text-gray-500 py-10">No results found</div>';
return;
}
// Update summary stats in the Summary tab
updateSummaryStats();
// Clear container
container.innerHTML = '';
// Create result cards with Tailwind Styling
currentResults.forEach((result, index) => {
const card = createResultCard(result, index);
container.appendChild(card);
});
// Reset upload area for next time
resetUploadArea();
}
function updateSummaryStats() {
const stats = { normal: 0, high: 0, low: 0 };
currentResults.forEach(result => {
// Normalize status string
const status = result.status ? result.status.toLowerCase() : 'normal';
if (status.includes('high')) stats.high++;
else if (status.includes('low')) stats.low++;
else stats.normal++;
});
// Update the DOM elements in the Summary Section
document.getElementById('normalCount').textContent = stats.normal;
document.getElementById('highCount').textContent = stats.high;
document.getElementById('lowCount').textContent = stats.low;
// Reveal the stats container
const statsContainer = document.getElementById('summaryStats');
if(statsContainer) statsContainer.classList.remove('hidden');
const generateBtn = document.getElementById('generateSummaryBtn');
if(generateBtn) generateBtn.classList.remove('hidden');
}
function createResultCard(result, index) {
const card = document.createElement('div');
// TAILWIND STYLING: Card container
card.className = 'bg-white rounded-xl shadow-sm border border-slate-100 p-5 mb-4 hover:shadow-md transition-shadow';
// Status colors
let statusColors = 'bg-green-100 text-green-700'; // Default normal
let borderClass = 'border-l-4 border-green-500';
const statusLower = result.status ? result.status.toLowerCase() : '';
if (statusLower.includes('high')) {
statusColors = 'bg-red-100 text-red-700';
borderClass = 'border-l-4 border-red-500';
} else if (statusLower.includes('low')) {
statusColors = 'bg-yellow-100 text-yellow-800';
borderClass = 'border-l-4 border-yellow-500';
}
const explanation = explanations && explanations[result.test_name]
? explanations[result.test_name]
: 'Analysis available in Chat or Summary.';
// HTML Structure using Tailwind
card.innerHTML = `
<div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4 ${borderClass} pl-4">
<div class="flex-grow">
<h3 class="text-lg font-bold text-slate-800">${escapeHtml(result.test_name)}</h3>
<p class="text-sm text-slate-500">Ref Range: ${escapeHtml(result.reference_range || 'N/A')}</p>
</div>
<div class="flex items-center gap-3 w-full md:w-auto justify-between md:justify-end">
<span class="text-xl font-bold text-slate-700">${escapeHtml(result.value)} <span class="text-sm font-normal text-slate-500">${escapeHtml(result.unit)}</span></span>
<span class="px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wide ${statusColors}">
${escapeHtml(result.status)}
</span>
</div>
</div>
<div class="mt-4 pt-3 border-t border-slate-50">
<p class="text-sm text-slate-600 leading-relaxed">
<span class="font-semibold text-primary">Insight:</span> ${escapeHtml(explanation)}
</p>
</div>
`;
return card;
}
function resetUploadArea() {
document.getElementById('uploadArea').classList.remove('hidden');
document.getElementById('uploadProgress').classList.add('hidden');
document.getElementById('fileInput').value = '';
}
// ===== SUMMARY GENERATION =====
async function generateSummary() {
if (!currentResults) {
showToast('Please upload a lab report first', 'error');
return;
}
const contentDiv = document.getElementById('summaryContent');
contentDiv.innerHTML = '<div class="flex justify-center p-8"><div class="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div></div>';
try {
const response = await fetch('/api/summary');
const data = await response.json();
if (!response.ok) throw new Error(data.error);
// Render summary with Markdown-like paragraphs
contentDiv.innerHTML = `
<div class="prose prose-slate max-w-none">
<h3 class="text-xl font-semibold mb-4 text-primary">Analysis Report</h3>
<div class="text-slate-700 leading-relaxed whitespace-pre-line">
${escapeHtml(data.summary).replace(/\n/g, '<br>')}
</div>
</div>
`;
} catch (error) {
showToast('Error: ' + error.message, 'error');
contentDiv.innerHTML = '<p class="text-red-500 text-center">Failed to generate summary.</p>';
}
}
// ===== CHAT FUNCTIONALITY =====
function initEnterKey() {
const input = document.getElementById('chatInput');
if (input) {
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') askQuestion();
});
}
}
async function askQuestion() {
if (!currentResults) {
showToast('Please upload a lab report first', 'error');
return;
}
const input = document.getElementById('chatInput');
const question = input.value.trim();
if (!question) return;
// Clear input
input.value = '';
// Add user message
addChatMessage(question, 'user');
// Show loading
const loadingId = addChatMessage('Analyzing...', 'assistant', true);
try {
const response = await fetch('/api/ask', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question })
});
const data = await response.json();
if (!response.ok) throw new Error(data.error);
// Remove loading
const loadingEl = document.getElementById(loadingId);
if(loadingEl) loadingEl.remove();
// Add response
addChatMessage(data.answer, 'assistant');
} catch (error) {
const loadingEl = document.getElementById(loadingId);
if(loadingEl) loadingEl.remove();
showToast(error.message, 'error');
}
}
function addChatMessage(text, sender, isLoading = false) {
const container = document.getElementById('chatMessages');
const wrapper = document.createElement('div');
// Flex alignment based on sender
wrapper.className = `flex w-full mb-4 ${sender === 'user' ? 'justify-end' : 'justify-start'}`;
const bubble = document.createElement('div');
// Bubble Styling
const baseStyle = "max-w-[85%] rounded-2xl px-5 py-3 shadow-sm text-sm leading-relaxed";
const userStyle = "bg-secondary text-white rounded-br-none"; // Blue bubble
const aiStyle = "bg-slate-100 text-slate-800 rounded-tl-none border border-slate-200"; // Grey bubble
bubble.className = `${baseStyle} ${sender === 'user' ? userStyle : aiStyle} ${isLoading ? 'animate-pulse' : ''}`;
bubble.innerHTML = escapeHtml(text).replace(/\n/g, '<br>');
if (isLoading) bubble.id = `loading-${Date.now()}`;
wrapper.appendChild(bubble);
container.appendChild(wrapper);
// Scroll to bottom
container.scrollTop = container.scrollHeight;
return bubble.id;
}
// ===== TOAST NOTIFICATIONS =====
function showToast(message, type = 'info') {
const toast = document.getElementById('toast');
toast.textContent = message;
// Tailwind classes for toast
toast.className = `fixed bottom-5 right-5 px-6 py-3 rounded-lg shadow-2xl transform transition-all duration-300 z-50 text-white font-medium translate-y-0 opacity-100`;
if (type === 'error') toast.classList.add('bg-red-600');
else if (type === 'success') toast.classList.add('bg-green-600');
else toast.classList.add('bg-slate-800');
setTimeout(() => {
toast.classList.remove('translate-y-0', 'opacity-100');
toast.classList.add('translate-y-20', 'opacity-0');
}, 4000);
}
// ===== UTILS =====
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
} |