Update web/analyzer.html

#19
by TheFrogGod - opened
Files changed (1) hide show
  1. web/analyzer.html +199 -322
web/analyzer.html CHANGED
@@ -1,4 +1,5 @@
1
- web/analyzer.html<!DOCTYPE html>
 
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8" />
@@ -6,14 +7,13 @@ web/analyzer.html<!DOCTYPE html>
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <link rel="stylesheet" href="style.css" />
9
-
10
  </head>
11
  <body class="bg-[var(--latte-cream)] font-sans text-gray-800 min-h-screen">
12
  <!-- NAVBAR -->
13
  <nav
14
  class="fixed top-0 left-0 w-full z-50 backdrop-blur-md bg-white/20 border-b border-white/30 shadow-md"
15
  >
16
- <div class="flex justify-between items-center w-full px-6 py-4">
17
  <!-- Logo -->
18
  <a
19
  href="index.html"
@@ -22,18 +22,22 @@ web/analyzer.html<!DOCTYPE html>
22
  CTRL + ALT + HEAL
23
  </a>
24
 
25
- <ul class="hidden md:flex space-x-6 font-medium text-gray-800">
26
- <li><a href="index.html" class="nav-link ">Home</a></li>
27
- <li><a href="profile.html" class="nav-link ">Profile</a></li>
 
 
28
  <li><a href="analyzer.html" class="nav-link">Analyzer</a></li>
29
- <li><a href="past_data.html" class="nav-link ">Past Reports</a></li>
30
- <li id="authNavItem"><a href="login.html" class="nav-link">Login</a></li>
 
 
31
  </ul>
32
 
33
- <!-- Hamburger Menu -->
34
  <button
35
  id="hamburger"
36
- class="md:hidden text-[var(--latte-cream)] text-2xl"
37
  >
38
 
39
  </button>
@@ -44,48 +48,15 @@ web/analyzer.html<!DOCTYPE html>
44
  id="mobile-menu"
45
  class="hidden flex-col space-y-4 bg-white/30 backdrop-blur-lg border border-white/20 rounded-xl shadow-lg mt-2 p-4 mx-6 md:hidden"
46
  >
47
- <li>
48
- <a
49
- href="index.html"
50
- class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
51
- >Home</a
52
- >
53
- </li>
54
- <li>
55
- <a
56
- href="analyzer.html"
57
- class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
58
- >Analyzer</a
59
- >
60
- </li>
61
- <li>
62
- <a
63
- href="profile.html"
64
- class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
65
- >Profile</a
66
- >
67
- </li>
68
- <li>
69
- <a
70
- href="login.html"
71
- class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
72
- >Login</a
73
- >
74
- </li>
75
- <li>
76
- <a
77
- href="about.html"
78
- class="block text-gray-800 hover:text-[var(--tropical-indigo)]"
79
- >About</a
80
- >
81
- </li>
82
-
83
- >
84
- </li>
85
  </ul>
86
  </nav>
87
 
88
- <!--Shared helpers (API base + query params) -->
89
  <script src="script.js"></script>
90
  <script>
91
  const hamburger = document.getElementById("hamburger");
@@ -95,24 +66,15 @@ web/analyzer.html<!DOCTYPE html>
95
  mobileMenu.classList.toggle("hidden");
96
  });
97
 
98
- // Underline the active page only
99
  const currentPath = window.location.pathname.split("/").pop();
100
-
101
  document.querySelectorAll(".nav-link").forEach((link) => {
102
- if (link.getAttribute("href") === currentPath) {
103
- link.classList.add("active-page"); // we'll style this in CSS
104
- }
105
- });
106
-
107
- document.querySelectorAll("#mobile-menu a").forEach((link) => {
108
  if (link.getAttribute("href") === currentPath) {
109
  link.classList.add("active-page");
110
  }
111
  });
112
  </script>
113
 
114
- <!-- CHANGE MARKER TO USE THIS -->
115
- <!-- secound change marker-->
116
  <main class="max-w-5xl mx-auto px-4 mb-16 pt-24">
117
  <!-- Upload Section -->
118
  <div class="bg-white border border-gray-200 rounded-lg p-6 shadow mb-6">
@@ -134,45 +96,80 @@ web/analyzer.html<!DOCTYPE html>
134
  </p>
135
  </div>
136
 
137
- <!-- AI Findings -->
138
  <div
139
  class="bg-[var(--latte-cream)] border border-[var(--wisteria)] rounded-lg p-6 mb-8"
140
  >
141
- <h3 class="text-lg font-semibold mb-3">AI Findings</h3>
142
  <div
143
- id="recommendations-output"
144
- class="bg-[#F9FAFB] text-sm border border-[var(--wisteria)] rounded p-4 space-y-4"
145
  >
146
- Findings and Recommendations will appear here.
147
  </div>
148
  </div>
149
 
150
- <!-- Chatbot -->
151
  <div
152
  class="bg-[var(--latte-cream)] border border-[var(--wisteria)] rounded-lg p-6 mb-8"
153
  >
154
- <h3 class="text-lg font-semibold mb-3">Ask Chatbot</h3>
155
  <div
156
- id="chat-output"
157
- class="space-y-2 h-48 overflow-auto text-sm rounded p-4"
158
  >
159
- <p><strong>Chatbot:</strong> Ask me something about your report</p>
160
- </div>
161
- <div class="flex mt-4 gap-2">
162
- <input
163
- type="text"
164
- id="user-question"
165
- placeholder="Ask a question..."
166
- class="flex-1 rounded px-3 py-2 focus:outline-none"
167
- />
168
- <button id="ask-btn" class="btn-primary px-4 py-2 rounded">
169
- Ask
170
- </button>
171
  </div>
172
  </div>
173
  </main>
174
 
175
- <!-- Firebase -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  <script type="module">
177
  import {
178
  getFirestore,
@@ -189,12 +186,12 @@ web/analyzer.html<!DOCTYPE html>
189
  } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-auth.js";
190
 
191
  const firebaseConfig = {
192
- apiKey: "AIzaSyAKWstCc7HqMgV8DmH2Sy6QwUP3JvZxu9g",
193
- authDomain: "appchallenge-c8fe3.firebaseapp.com",
194
- projectId: "appchallenge-c8fe3",
195
- storageBucket: "appchallenge-c8fe3.firebasestorage.app",
196
- messagingSenderId: "943791692455",
197
- appId: "1:943791692455:web:ae04d06bf09669b6bdd982",
198
  };
199
 
200
  const app = initializeApp(firebaseConfig);
@@ -205,47 +202,39 @@ web/analyzer.html<!DOCTYPE html>
205
  window.firestoreDb = db;
206
  window.firestoreHelpers = { collection, doc, addDoc, serverTimestamp };
207
 
208
- onAuthStateChanged(auth, (user) => {
209
- const authNavItem = document.getElementById("authNavItem");
210
-
211
- // Desktop links
212
- const homeNavDesktop = document.querySelector('ul.md\\:flex li a[href="index.html"]')?.parentElement;
213
- const profileNavDesktop = document.querySelector('ul.md\\:flex li a[href="profile.html"]')?.parentElement;
214
- const pastNavDesktop = document.querySelector('ul.md\\:flex li a[href="past_data.html"]')?.parentElement;
215
-
216
- // Mobile links
217
- const profileNavMobile = document.querySelector('#mobile-menu a[href="profile.html"]')?.parentElement;
218
- const pastNavMobile = document.querySelector('#mobile-menu a[href="past_data.html"]')?.parentElement;
219
- const homeNavMobile = document.querySelector('#mobile-menu a[href="index.html"]')?.parentElement;
220
- if (authNavItem) {
221
- if (user) {
222
- authNavItem.innerHTML =
223
- '<button onclick="logout()" class="hover:text-[#6B9080] text-red-600">Logout</button>';
224
-
225
- // Hide Home
226
- if (homeNavDesktop) homeNavDesktop.style.display = "none";
227
- if (homeNavMobile) homeNavMobile.style.display = "none";
228
- // Show Profile and Past Reports
229
-
230
- if (profileNavDesktop) profileNavDesktop.style.display = "block";
231
- if (pastNavDesktop) pastNavDesktop.style.display = "block";
232
- if (profileNavMobile) profileNavMobile.style.display = "block";
233
- if (pastNavMobile) pastNavMobile.style.display = "block";
234
- } else {
235
- authNavItem.innerHTML =
236
- '<a href="login.html" class="hover:text-[#6B9080]">Login</a>';
237
-
238
- // Show Home
239
- if (homeNavDesktop) homeNavDesktop.style.display = "block";
240
- if (homeNavMobile) homeNavMobile.style.display = "block";
241
- // Hide Profile and Past Reports
242
- if (profileNavDesktop) profileNavDesktop.style.display = "none";
243
- if (pastNavDesktop) pastNavDesktop.style.display = "none";
244
- if (profileNavMobile) profileNavMobile.style.display = "none";
245
- if (pastNavMobile) pastNavMobile.style.display = "none";
246
- }
247
- }
248
- });
249
 
250
  window.logout = async () => {
251
  try {
@@ -258,22 +247,23 @@ const profileNavDesktop = document.querySelector('ul.md\\:flex li a[href="profil
258
  };
259
  </script>
260
 
 
261
  <script type="module">
262
- import {
263
- pipeline,
264
- env,
265
- } from "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0";
266
-
267
  const loadingEl = document.getElementById("loading");
268
- //const textOutput = document.getElementById("text-output");
269
  const recsOutput = document.getElementById("recommendations-output");
270
- const findingsOutput = document.getElementById(
271
- "detected-measurement-results"
272
- );
273
  const authStatus = document.getElementById("auth-status");
274
  let extractedText = "";
275
  let currentUser = null;
276
 
 
 
 
 
 
 
 
277
  document
278
  .getElementById("pdf-upload")
279
  .addEventListener("change", function () {
@@ -282,76 +272,6 @@ const profileNavDesktop = document.querySelector('ul.md\\:flex li a[href="profil
282
  : "No file uploaded yet.";
283
  });
284
 
285
- const saveAnalysis = async (uid, payload) => {
286
- try {
287
- const docRef = await firebase
288
- .firestore()
289
- .collection("users")
290
- .doc(uid)
291
- .collection("analyses")
292
- .add({
293
- ...payload,
294
- createdAt: serverTimestamp(),
295
- });
296
- return docRef.id;
297
- } catch (e) {
298
- console.error("Failed to save analysis:", e);
299
- }
300
- };
301
-
302
- const renderRecCard = (rec, idx) => {
303
- const sev = (rec.severity || "").toLowerCase();
304
- const sevClass = sev.includes("severe")
305
- ? "badge-high"
306
- : sev.includes("moderate")
307
- ? "badge-medium"
308
- : "badge-low";
309
- return `
310
- <div class="rec-card">
311
- <div class="flex items-center justify-between">
312
- <h4 class="rec-title">Finding ${idx + 1}: ${
313
- rec.findings || "N/A"
314
- }</h4>
315
- <span class="rec-badge ${sevClass}">${rec.severity || "—"}</span>
316
- </div>
317
- <ul class="rec-content space-y-1">
318
- <li><em>Recommendations:</em>
319
- <ul class="list-disc list-inside ml-6">
320
- ${(rec.recommendations || [])
321
- .map((r) => `<li>${r}</li>`)
322
- .join("")}
323
- </ul>
324
- </li>
325
- <li><em>Treatment:</em> ${
326
- rec.treatment_suggestions || "Not available"
327
- }</li>
328
- <li><em>Home Care:</em>
329
- <ul class="list-disc list-inside ml-6">
330
- ${(rec.home_care_guidance || [])
331
- .map((r) => `<li>${r}</li>`)
332
- .join("")}
333
- </ul>
334
- </li>
335
- ${
336
- rec.info_link
337
- ? `<li><a href="${rec.info_link}" target="_blank" class="rec-link">Learn more</a></li>`
338
- : ""
339
- }
340
- </ul>
341
- </div>
342
- `;
343
- };
344
-
345
- window.onAuthStateChanged(window.firebaseAuth, (user) => {
346
- currentUser = user;
347
- if (user) {
348
- authStatus.textContent = `Signed in as ${user.email || user.uid}`;
349
- } else {
350
- authStatus.textContent =
351
- "Not signed in. Sign in to save and view past analyses.";
352
- }
353
- });
354
-
355
  document
356
  .getElementById("analyze-btn")
357
  .addEventListener("click", async () => {
@@ -362,141 +282,98 @@ const profileNavDesktop = document.querySelector('ul.md\\:flex li a[href="profil
362
  }
363
 
364
  loadingEl.textContent = "Processing with AI...";
365
- //textOutput.textContent = "";
366
  recsOutput.textContent = "";
367
 
368
  const formData = new FormData();
369
  formData.append("file", file);
370
  formData.append("model", "bert");
371
 
372
- let data;
373
  try {
374
  const res = await fetch(api("analyze/"), {
375
  method: "POST",
376
  body: formData,
377
  });
378
  if (!res.ok) throw new Error(await res.text());
379
- data = await res.json();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
  } catch (err) {
381
  console.error(err);
382
  loadingEl.textContent = "Error during analysis: " + err.message;
383
- return;
384
- }
385
-
386
- extractedText = data.ocr_text || "";
387
- //textOutput.textContent = extractedText;
388
-
389
- const recs = Array.isArray(data.Detected_Anomolies)
390
- ? data.Detected_Anomolies
391
- : data.Detected_Anomolies
392
- ? [data.Detected_Anomolies]
393
- : [];
394
- if (data.Detected_Anomolies) {
395
- recsOutput.innerHTML = recs
396
- .map((rec, i) => renderRecCard(rec, i))
397
- .join("");
398
- } else {
399
- recsOutput.textContent = "No recommendations found.";
400
  }
 
401
 
402
- const findings = Array.isArray(data.Detected_Measurement_Values)
403
- ? data.Detected_Measurement_Values
404
- : data.Detected_Measurement_Values
405
- ? [data.Detected_Measurement_Values]
406
- : [];
407
- if (data.Detected_Measurement_Values) {
408
- findingsOutput.innerHTML = findings
409
- .map((finding, i) => renderRecCard(finding, i))
410
- .join("");
411
- }
412
-
413
- if (currentUser) {
414
- /*await saveAnalysis(currentUser.uid, {
415
- reportDate: date,
416
- ocr_text: extractedText,
417
- resolutions: recs,
418
- measurements: findings,
419
- });*/
420
- await postReportToBackend({
421
- user_id: currentUser.email,
422
- report_date: new Date(),
423
- ocr_text: extractedText,
424
- anomalies: JSON.stringify(recs),
425
- measurements: JSON.stringify(findings),
426
- });
427
- }
428
 
429
- loadingEl.textContent = "Analysis complete.";
430
- });
 
431
 
432
- async function postReportToBackend(report) {
433
- try {
434
- const response = await fetch(api('save_report/'), {
435
- method: 'POST',
436
- headers: {
437
- 'Content-Type': 'application/json',
438
- },
439
- body: JSON.stringify(report),
440
- });
441
- if (!response.ok) {
442
- throw new Error(`HTTP error! status: ${response.status}`);
443
- }
444
- const data = await response.json();
445
- console.log('Report successfully sent to backend:', data);
446
- } catch (error) {
447
- console.error('Error sending report to backend:', error);
448
- }
 
 
 
 
449
  }
450
 
451
- document.getElementById("ask-btn").onclick = async () => {
452
- const q = document.getElementById("user-question").value.trim();
453
- if (!q) return;
454
-
455
- if (!extractedText) {
456
- alert("Please analyze a document first before asking questions.");
457
- return;
458
- }
459
-
460
- const chat = document.getElementById("chat-output");
461
- chat.innerHTML += `<p><strong>You:</strong> ${q}</p>`;
462
- chat.scrollTop = chat.scrollHeight;
463
-
464
- chat.innerHTML += `<p><strong>Chatbot:</strong> <em>Thinking...</em></p>`;
465
- chat.scrollTop = chat.scrollHeight;
466
-
467
- try {
468
- const response = await fetch(api('chat/'), {
469
- method: "POST",
470
- headers: {
471
- "Content-Type": "application/json",
472
- },
473
- body: JSON.stringify({
474
- question: q,
475
- user_id: currentUser ? currentUser.uid : "anonymous"
476
- }),
477
- });
478
-
479
- if (!response.ok) {
480
- throw new Error(`HTTP error! status: ${response.status}`);
481
- }
482
-
483
- const data = await response.json();
484
-
485
- //now addign acctual reposnse
486
- const messages = chat.querySelectorAll('p');
487
- const lastMessage = messages[messages.length - 1];
488
- lastMessage.innerHTML = `<strong>Chatbot:</strong> ${data.answer}`;
489
-
490
- } catch (error) {
491
- console.error("Error:", error);
492
- const messages = chat.querySelectorAll('p');
493
- const lastMessage = messages[messages.length - 1];
494
- lastMessage.innerHTML = `<strong>Chatbot:</strong> Sorry, I encountered an error: ${error.message}`;
495
- }
496
-
497
- document.getElementById("user-question").value = "";
498
- chat.scrollTop = chat.scrollHeight;
499
- };
500
  </script>
 
501
  </body>
502
- </html>
 
1
+ <!DOCTYPE html>
2
+
3
  <html lang="en">
4
  <head>
5
  <meta charset="UTF-8" />
 
7
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
8
  <script src="https://cdn.tailwindcss.com"></script>
9
  <link rel="stylesheet" href="style.css" />
 
10
  </head>
11
  <body class="bg-[var(--latte-cream)] font-sans text-gray-800 min-h-screen">
12
  <!-- NAVBAR -->
13
  <nav
14
  class="fixed top-0 left-0 w-full z-50 backdrop-blur-md bg-white/20 border-b border-white/30 shadow-md"
15
  >
16
+ <div class="flex items-center px-6 py-4 max-w-7xl mx-auto">
17
  <!-- Logo -->
18
  <a
19
  href="index.html"
 
22
  CTRL + ALT + HEAL
23
  </a>
24
 
25
+ ```
26
+ <!-- Desktop Links (right aligned) -->
27
+ <ul class="hidden md:flex space-x-6 font-medium text-gray-800 ml-auto">
28
+ <li><a href="index.html" class="nav-link">Home</a></li>
29
+ <li><a href="profile.html" class="nav-link">Profile</a></li>
30
  <li><a href="analyzer.html" class="nav-link">Analyzer</a></li>
31
+ <li><a href="past_data.html" class="nav-link">Past Reports</a></li>
32
+ <li id="authNavItem">
33
+ <a href="login.html" class="nav-link">Login</a>
34
+ </li>
35
  </ul>
36
 
37
+ <!-- Hamburger Menu (mobile only) -->
38
  <button
39
  id="hamburger"
40
+ class="md:hidden text-[var(--latte-cream)] text-2xl ml-auto"
41
  >
42
 
43
  </button>
 
48
  id="mobile-menu"
49
  class="hidden flex-col space-y-4 bg-white/30 backdrop-blur-lg border border-white/20 rounded-xl shadow-lg mt-2 p-4 mx-6 md:hidden"
50
  >
51
+ <li><a href="index.html" class="block nav-link">Home</a></li>
52
+ <li><a href="analyzer.html" class="block nav-link">Analyzer</a></li>
53
+ <li><a href="profile.html" class="block nav-link">Profile</a></li>
54
+ <li><a href="login.html" class="block nav-link">Login</a></li>
55
+ <li><a href="about.html" class="block nav-link">About</a></li>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  </ul>
57
  </nav>
58
 
59
+ <!-- Shared helpers -->
60
  <script src="script.js"></script>
61
  <script>
62
  const hamburger = document.getElementById("hamburger");
 
66
  mobileMenu.classList.toggle("hidden");
67
  });
68
 
69
+ // Active page underline
70
  const currentPath = window.location.pathname.split("/").pop();
 
71
  document.querySelectorAll(".nav-link").forEach((link) => {
 
 
 
 
 
 
72
  if (link.getAttribute("href") === currentPath) {
73
  link.classList.add("active-page");
74
  }
75
  });
76
  </script>
77
 
 
 
78
  <main class="max-w-5xl mx-auto px-4 mb-16 pt-24">
79
  <!-- Upload Section -->
80
  <div class="bg-white border border-gray-200 rounded-lg p-6 shadow mb-6">
 
96
  </p>
97
  </div>
98
 
99
+ <!-- Extracted Text -->
100
  <div
101
  class="bg-[var(--latte-cream)] border border-[var(--wisteria)] rounded-lg p-6 mb-8"
102
  >
103
+ <h3 class="text-lg font-semibold mb-3">Extracted Text</h3>
104
  <div
105
+ id="text-output"
106
+ class="whitespace-pre-wrap h-60 overflow-auto bg-[#FAFBFC] text-sm border border-[var(--wisteria)] rounded p-4"
107
  >
108
+ OCR results will appear here.
109
  </div>
110
  </div>
111
 
112
+ <!-- AI Findings -->
113
  <div
114
  class="bg-[var(--latte-cream)] border border-[var(--wisteria)] rounded-lg p-6 mb-8"
115
  >
116
+ <h3 class="text-lg font-semibold mb-3">AI Findings</h3>
117
  <div
118
+ id="recommendations-output"
119
+ class="bg-[#F9FAFB] text-sm border border-[var(--wisteria)] rounded p-4 space-y-4"
120
  >
121
+ Findings and Recommendations will appear here.
 
 
 
 
 
 
 
 
 
 
 
122
  </div>
123
  </div>
124
  </main>
125
 
126
+ <!-- Floating Chat Button -->
127
+ <button
128
+ id="chat-toggle"
129
+ class="fixed bottom-6 right-6 bg-[var(--tropical-indigo)] text-white px-5 py-3 rounded-full shadow-lg hover:scale-105 transition"
130
+ >
131
+ 💬 Chat
132
+ </button>
133
+
134
+ <!-- Chatbot Drawer (below navbar) -->
135
+ <div
136
+ id="chat-drawer"
137
+ class="fixed top-16 right-0 w-96 max-w-full h-[calc(100%-4rem)] bg-white shadow-lg border-l border-gray-200 transform translate-x-full transition-transform duration-300 ease-in-out z-40 flex flex-col"
138
+ >
139
+ <div class="flex justify-between items-center p-4 border-b">
140
+ <h3 class="text-lg font-semibold">Ask Chatbot</h3>
141
+ <button id="chat-close" class="text-gray-600 hover:text-black text-xl">
142
+
143
+ </button>
144
+ </div>
145
+ <div id="chat-output" class="flex-1 overflow-auto p-4 space-y-2 text-sm">
146
+ <p><strong>Chatbot:</strong> Ask me something about your report</p>
147
+ </div>
148
+ <div class="flex gap-2 p-4 border-t">
149
+ <input
150
+ type="text"
151
+ id="user-question"
152
+ placeholder="Ask a question..."
153
+ class="flex-1 rounded px-3 py-2 focus:outline-none border"
154
+ />
155
+ <button id="ask-btn" class="btn-primary px-4 py-2 rounded">Ask</button>
156
+ </div>
157
+ </div>
158
+
159
+ <script>
160
+ const chatDrawer = document.getElementById("chat-drawer");
161
+ const chatToggle = document.getElementById("chat-toggle");
162
+ const chatClose = document.getElementById("chat-close");
163
+
164
+ chatToggle.addEventListener("click", () => {
165
+ chatDrawer.classList.toggle("translate-x-full");
166
+ });
167
+ chatClose.addEventListener("click", () => {
168
+ chatDrawer.classList.add("translate-x-full");
169
+ });
170
+ </script>
171
+
172
+ <!-- Firebase Auth + Firestore -->
173
  <script type="module">
174
  import {
175
  getFirestore,
 
186
  } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-auth.js";
187
 
188
  const firebaseConfig = {
189
+ apiKey: "AIzaSyAPhM_Ee7cLzyKHs5zyFy8g5ZOk9-pubRI",
190
+ authDomain: "login-tutorial-7a9e1.firebaseapp.com",
191
+ projectId: "login-tutorial-7a9e1",
192
+ storageBucket: "login-tutorial-7a9e1.firebasestorage.app",
193
+ messagingSenderId: "491093197824",
194
+ appId: "1:491093197824:web:9f866...",
195
  };
196
 
197
  const app = initializeApp(firebaseConfig);
 
202
  window.firestoreDb = db;
203
  window.firestoreHelpers = { collection, doc, addDoc, serverTimestamp };
204
 
205
+ onAuthStateChanged(auth, (user) => {
206
+ const authNavItem = document.getElementById("authNavItem");
207
+ const profileNavDesktop = document.querySelector(
208
+ 'ul.md\\:flex li a[href="profile.html"]'
209
+ )?.parentElement;
210
+ const pastNavDesktop = document.querySelector(
211
+ 'ul.md\\:flex li a[href="past_data.html"]'
212
+ )?.parentElement;
213
+ const profileNavMobile = document.querySelector(
214
+ '#mobile-menu a[href="profile.html"]'
215
+ )?.parentElement;
216
+ const pastNavMobile = document.querySelector(
217
+ '#mobile-menu a[href="past_data.html"]'
218
+ )?.parentElement;
219
+
220
+ if (authNavItem) {
221
+ if (user) {
222
+ authNavItem.innerHTML =
223
+ '<button onclick="logout()" class="hover:text-[#6B9080] text-red-600">Logout</button>';
224
+ if (profileNavDesktop) profileNavDesktop.style.display = "block";
225
+ if (pastNavDesktop) pastNavDesktop.style.display = "block";
226
+ if (profileNavMobile) profileNavMobile.style.display = "block";
227
+ if (pastNavMobile) pastNavMobile.style.display = "block";
228
+ } else {
229
+ authNavItem.innerHTML =
230
+ '<a href="login.html" class="hover:text-[#6B9080]">Login</a>';
231
+ if (profileNavDesktop) profileNavDesktop.style.display = "none";
232
+ if (pastNavDesktop) pastNavDesktop.style.display = "none";
233
+ if (profileNavMobile) profileNavMobile.style.display = "none";
234
+ if (pastNavMobile) pastNavMobile.style.display = "none";
235
+ }
236
+ }
237
+ });
 
 
 
 
 
 
 
 
238
 
239
  window.logout = async () => {
240
  try {
 
247
  };
248
  </script>
249
 
250
+ <!-- Analyzer + Chatbot API calls -->
251
  <script type="module">
 
 
 
 
 
252
  const loadingEl = document.getElementById("loading");
253
+ const textOutput = document.getElementById("text-output");
254
  const recsOutput = document.getElementById("recommendations-output");
255
+ const chat = document.getElementById("chat-output");
 
 
256
  const authStatus = document.getElementById("auth-status");
257
  let extractedText = "";
258
  let currentUser = null;
259
 
260
+ window.onAuthStateChanged(window.firebaseAuth, (user) => {
261
+ currentUser = user;
262
+ authStatus.textContent = user
263
+ ? `Signed in as ${user.email || user.uid}`
264
+ : "Not signed in. Sign in to save and view past analyses.";
265
+ });
266
+
267
  document
268
  .getElementById("pdf-upload")
269
  .addEventListener("change", function () {
 
272
  : "No file uploaded yet.";
273
  });
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  document
276
  .getElementById("analyze-btn")
277
  .addEventListener("click", async () => {
 
282
  }
283
 
284
  loadingEl.textContent = "Processing with AI...";
285
+ textOutput.textContent = "";
286
  recsOutput.textContent = "";
287
 
288
  const formData = new FormData();
289
  formData.append("file", file);
290
  formData.append("model", "bert");
291
 
 
292
  try {
293
  const res = await fetch(api("analyze/"), {
294
  method: "POST",
295
  body: formData,
296
  });
297
  if (!res.ok) throw new Error(await res.text());
298
+ const data = await res.json();
299
+
300
+ extractedText = data.ocr_text || "";
301
+ textOutput.textContent = extractedText;
302
+
303
+ const recs = Array.isArray(data.Detected_Anomolies)
304
+ ? data.Detected_Anomolies
305
+ : data.Detected_Anomolies
306
+ ? [data.Detected_Anomolies]
307
+ : [];
308
+ recsOutput.innerHTML = recs.length
309
+ ? recs
310
+ .map(
311
+ (rec, i) =>
312
+ `<div class="rec-card"><h4>Finding ${i + 1}: ${
313
+ rec.findings
314
+ }</h4></div>`
315
+ )
316
+ .join("")
317
+ : "No recommendations found.";
318
+
319
+ if (currentUser) {
320
+ await fetch(api("save_report/"), {
321
+ method: "POST",
322
+ headers: { "Content-Type": "application/json" },
323
+ body: JSON.stringify({
324
+ user_id: currentUser.email,
325
+ report_date: new Date(),
326
+ ocr_text: extractedText,
327
+ anomalies: JSON.stringify(recs),
328
+ }),
329
+ });
330
+ }
331
+ loadingEl.textContent = "Analysis complete.";
332
  } catch (err) {
333
  console.error(err);
334
  loadingEl.textContent = "Error during analysis: " + err.message;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  }
336
+ });
337
 
338
+ document.getElementById("ask-btn").onclick = async () => {
339
+ const q = document.getElementById("user-question").value.trim();
340
+ if (!q) return;
341
+ if (!extractedText) {
342
+ alert("Please analyze a document first before asking questions.");
343
+ return;
344
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
+ chat.innerHTML += `<p><strong>You:</strong> ${q}</p>`;
347
+ chat.innerHTML += `<p><strong>Chatbot:</strong> <em>Thinking...</em></p>`;
348
+ chat.scrollTop = chat.scrollHeight;
349
 
350
+ try {
351
+ const res = await fetch(api("chat/"), {
352
+ method: "POST",
353
+ headers: { "Content-Type": "application/json" },
354
+ body: JSON.stringify({
355
+ question: q,
356
+ user_id: currentUser ? currentUser.uid : "anonymous",
357
+ }),
358
+ });
359
+ if (!res.ok) throw new Error(await res.text());
360
+ const data = await res.json();
361
+ const last = chat.querySelectorAll("p");
362
+ last[
363
+ last.length - 1
364
+ ].innerHTML = `<strong>Chatbot:</strong> ${data.answer}`;
365
+ } catch (err) {
366
+ console.error("Error:", err);
367
+ const last = chat.querySelectorAll("p");
368
+ last[
369
+ last.length - 1
370
+ ].innerHTML = `<strong>Chatbot:</strong> Sorry, error: ${err.message}`;
371
  }
372
 
373
+ document.getElementById("user-question").value = "";
374
+ chat.scrollTop = chat.scrollHeight;
375
+ };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  </script>
377
+ ```
378
  </body>
379
+ </html>