-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
237 lines (217 loc) Β· 10 KB
/
script.js
File metadata and controls
237 lines (217 loc) Β· 10 KB
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
// frontend/script.js
// --- Configuration ---
const BACKEND_URL = "http://localhost:8000"; // Adjust if needed
// --- DOM Elements ---
// Upload Section
const pdfFileInput = document.getElementById('pdf-file');
const submitButton = document.getElementById('submit-button');
const loadingSpinner = document.getElementById('loading-spinner');
const uploadError = document.getElementById('upload-error');
const uploadStatus = document.getElementById('upload-status');
// Analysis Section
const analysisSection = document.getElementById('analysis-section');
const detailedAnalysisP = document.getElementById('detailed-analysis');
// Removed resultsTableBody and resultsTableEmpty as the section is gone
const potentialRisksUl = document.getElementById('potential-risks');
const potentialRisksEmpty = document.getElementById('potential-risks-empty');
// Get the single list element for recommendations
const recommendationsListUl = document.getElementById('recommendations-list');
const recommendationsListEmpty = document.getElementById('recommendations-list-empty');
// Removed rawLLMResponseContainer and rawLLMResponsePre
// Chat Section
const chatSection = document.getElementById('chat-section');
const chatHistoryDiv = document.getElementById('chat-history');
const chatInput = document.getElementById('chat-input');
const sendChatButton = document.getElementById('send-chat-button');
const chatLoadingSpinner = document.getElementById('chat-loading-spinner');
const chatError = document.getElementById('chat-error');
// --- Event Listeners ---
submitButton.addEventListener('click', handleAnalyzeRequest);
sendChatButton.addEventListener('click', handleChatRequest);
chatInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
handleChatRequest();
}
});
// --- Functions ---
/**
* Handles the PDF upload and analysis request.
*/
async function handleAnalyzeRequest() {
// (Keep this function the same as in med_report_analyzer_frontend_js_v2)
const file = pdfFileInput.files[0];
if (!file) {
showUploadError("Please select a PDF file first.");
return;
}
if (file.type !== "application/pdf") {
showUploadError("Invalid file type. Only PDF files are allowed.");
return;
}
hideUploadError();
hideAnalysis();
hideChat();
showLoading();
setUploadStatus("Uploading and analyzing... This may take a minute.");
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch(`${BACKEND_URL}/analyze`, {
method: 'POST',
body: formData,
});
if (!response.ok) {
let errorDetail = `Analysis failed (Status: ${response.status})`;
try {
const errorData = await response.json();
errorDetail = errorData.detail || `Error ${response.status}: ${response.statusText}`;
} catch (e) {
errorDetail = `Error ${response.status}: ${response.statusText}`;
}
throw new Error(errorDetail);
}
const data = await response.json();
displayAnalysis(data); // Call the updated displayAnalysis
showAnalysis();
showChat();
setUploadStatus("Analysis complete.");
} catch (error) {
console.error("Analysis error:", error);
showUploadError(`Analysis failed: ${error.message}`);
setUploadStatus("");
} finally {
hideLoading();
}
}
/**
* Handles sending a chat message and displaying the response.
*/
async function handleChatRequest() {
// (Keep this function the same as in med_report_analyzer_frontend_js_v2)
const message = chatInput.value.trim();
if (!message) { return; }
addChatMessage(message, 'user');
chatInput.value = '';
showChatLoading();
hideChatError();
try {
const response = await fetch(`${BACKEND_URL}/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', },
body: JSON.stringify({ message: message }),
});
if (!response.ok) {
let errorDetail = `Chat request failed (Status: ${response.status})`;
try {
const errorData = await response.json();
errorDetail = errorData.detail || `Error ${response.status}: ${response.statusText}`;
} catch (e) {
errorDetail = `Error ${response.status}: ${response.statusText}`;
}
throw new Error(errorDetail);
}
const data = await response.json();
addChatMessage(data.response, 'assistant');
} catch (error) {
console.error("Chat error:", error);
showChatError(`Chat error: ${error.message}`);
addChatMessage(`Sorry, I encountered an error: ${error.message}`, 'assistant', true);
} finally {
hideChatLoading();
}
}
/**
* Helper function to populate a list (UL) element with icons.
* @param {HTMLElement} listElement - The UL element to populate.
* @param {string[]} items - Array of strings to add as list items.
* @param {HTMLElement} emptyMessageElement - The element to show if the items array is empty or null.
* @param {string} emptyText - The text to display in the emptyMessageElement.
* @param {string} [iconClass='fa-solid fa-check text-sky-600'] - Font Awesome classes for the list item icon.
*/
function populateList(listElement, items, emptyMessageElement, emptyText, iconClass = 'fa-solid fa-check text-sky-600') {
// (Keep this function the same as in med_report_analyzer_frontend_js_v2)
listElement.innerHTML = '';
if (items && items.length > 0) {
emptyMessageElement.classList.add('hidden');
items.forEach(item => {
const li = document.createElement('li');
li.classList.add('flex', 'items-start', 'gap-2', 'text-slate-600');
li.innerHTML = `<i class="${iconClass} icon mt-1 flex-shrink-0"></i><span>${item}</span>`;
listElement.appendChild(li);
});
} else {
emptyMessageElement.textContent = emptyText;
emptyMessageElement.classList.remove('hidden');
}
}
/**
* Displays the analysis results received from the backend in the UI.
* (Updated for simplified response structure)
* @param {object} data - The analysis data object from the backend.
*/
function displayAnalysis(data) {
// Detailed Analysis Section
const detailedAnalysisText = data.detailed_analysis || 'No overall summary provided.';
detailedAnalysisP.textContent = detailedAnalysisText;
detailedAnalysisP.classList.toggle('italic', detailedAnalysisText === 'No overall summary provided.');
detailedAnalysisP.parentElement.classList.toggle('hidden', !data.detailed_analysis);
// Results Overview Section - REMOVED
// No code needed here anymore
// Potential Health Risks Section
populateList(
potentialRisksUl,
data.potential_risks,
potentialRisksEmpty,
'No potential risks identified.',
'fa-solid fa-triangle-exclamation text-orange-500' // Specific icon
);
// Recommendations Section (Simplified)
// Now populates the single list using data.recommendations directly
populateList(
recommendationsListUl, // Target the single UL element
data.recommendations, // Use the direct list from the updated API response
recommendationsListEmpty,
'No specific recommendations provided.',
'fa-solid fa-check text-sky-600' // Use a consistent icon
);
// Raw LLM Response Section - REMOVED
// No code needed here anymore
}
/**
* Adds a message (user or assistant) to the chat history UI.
* @param {string} message - The chat message text.
* @param {'user' | 'assistant'} sender - Indicates who sent the message.
* @param {boolean} [isError=false] - Optional flag to style assistant messages as errors.
*/
function addChatMessage(message, sender, isError = false) {
// (Keep this function the same as in med_report_analyzer_frontend_js_v2)
const placeholder = chatHistoryDiv.querySelector('p.text-slate-500');
if (placeholder) { placeholder.remove(); }
const messageDiv = document.createElement('div');
messageDiv.classList.add('chat-bubble');
messageDiv.classList.add(sender === 'user' ? 'chat-bubble-user' : 'chat-bubble-assistant');
if (isError && sender === 'assistant') {
messageDiv.classList.add('chat-bubble-error');
}
messageDiv.textContent = message;
chatHistoryDiv.appendChild(messageDiv);
chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight;
}
// --- UI State Management Functions ---
// (Keep all these functions the same as in med_report_analyzer_frontend_js_v2)
function showLoading() { loadingSpinner.classList.remove('hidden'); submitButton.disabled = true; }
function hideLoading() { loadingSpinner.classList.add('hidden'); submitButton.disabled = false; }
function showChatLoading() { chatLoadingSpinner.classList.remove('hidden'); sendChatButton.disabled = true; chatInput.disabled = true; }
function hideChatLoading() { chatLoadingSpinner.classList.add('hidden'); sendChatButton.disabled = false; chatInput.disabled = false; }
function showUploadError(message) { uploadError.textContent = message; uploadError.classList.remove('hidden'); }
function hideUploadError() { uploadError.classList.add('hidden'); }
function showChatError(message) { chatError.textContent = message; chatError.classList.remove('hidden'); }
function hideChatError() { chatError.classList.add('hidden'); }
function setUploadStatus(message) { if (message) { uploadStatus.textContent = message; uploadStatus.classList.remove('hidden'); } else { uploadStatus.classList.add('hidden'); } }
function showAnalysis() { analysisSection.classList.remove('hidden'); }
function hideAnalysis() { analysisSection.classList.add('hidden'); }
function showChat() { chatSection.classList.remove('hidden'); chatHistoryDiv.innerHTML = '<p class="text-slate-500 text-sm text-center italic">Chat history will appear here.</p>'; }
function hideChat() { chatSection.classList.add('hidden'); }
// --- Initial Page Load State ---
hideAnalysis();
hideChat();