vineelagampa commited on
Commit
5340d8c
·
verified ·
1 Parent(s): 0dc7e7e

upadate analyzer with chatbot changes

Browse files
Files changed (1) hide show
  1. web/analyzer.html +51 -18
web/analyzer.html CHANGED
@@ -129,10 +129,10 @@
129
 
130
  <ul class="hidden md:flex space-x-6 font-medium text-gray-800">
131
  <li><a href="index.html" class="nav-link">Home</a></li>
132
- <li><a href="profile.html" class="nav-link">Profile</a></li>
133
  <li><a href="analyzer.html" class="nav-link">Analyzer</a></li>
134
  <li><a href="past_data.html" class="nav-link">Past Reports</a></li>
135
- <li><a href="login.html" class="nav-link">Login</a></li>
 
136
  </ul>
137
 
138
  <!-- Hamburger Menu -->
@@ -542,22 +542,55 @@
542
  }
543
  }
544
 
545
- document.getElementById("ask-btn").onclick = async () => {
546
- const q = document.getElementById("user-question").value.trim();
547
- if (!q) return;
548
- const chat = document.getElementById("chat-output");
549
- chat.innerHTML += `<p><strong>You:</strong> ${q}</p>`;
550
- chat.scrollTop = chat.scrollHeight;
551
- env.allowLocalModels = false;
552
- const qa = await pipeline(
553
- "question-answering",
554
- "Xenova/distilbert-base-uncased-distilled-squad"
555
- );
556
- const out = await qa(q, extractedText);
557
- chat.innerHTML += `<p><strong>Chatbot:</strong> ${out.answer}</p>`;
558
- document.getElementById("user-question").value = "";
559
- chat.scrollTop = chat.scrollHeight;
560
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561
  </script>
562
  </body>
563
  </html>
 
129
 
130
  <ul class="hidden md:flex space-x-6 font-medium text-gray-800">
131
  <li><a href="index.html" class="nav-link">Home</a></li>
 
132
  <li><a href="analyzer.html" class="nav-link">Analyzer</a></li>
133
  <li><a href="past_data.html" class="nav-link">Past Reports</a></li>
134
+ <li><a href="profile.html" class="nav-link">Profile</a></li>
135
+ <li id="authNavItem"><a href="login.html" class="nav-link">Login</a></li>
136
  </ul>
137
 
138
  <!-- Hamburger Menu -->
 
542
  }
543
  }
544
 
545
+ document.getElementById("ask-btn").onclick = async () => {
546
+ const q = document.getElementById("user-question").value.trim();
547
+ if (!q) return;
548
+
549
+ if (!extractedText) {
550
+ alert("Please analyze a document first before asking questions.");
551
+ return;
552
+ }
553
+
554
+ const chat = document.getElementById("chat-output");
555
+ chat.innerHTML += `<p><strong>You:</strong> ${q}</p>`;
556
+ chat.scrollTop = chat.scrollHeight;
557
+
558
+ chat.innerHTML += `<p><strong>Chatbot:</strong> <em>Thinking...</em></p>`;
559
+ chat.scrollTop = chat.scrollHeight;
560
+
561
+ try {
562
+ const response = await fetch("http://localhost:9000/chat/", {
563
+ method: "POST",
564
+ headers: {
565
+ "Content-Type": "application/json",
566
+ },
567
+ body: JSON.stringify({
568
+ question: q,
569
+ user_id: currentUser ? currentUser.uid : "anonymous"
570
+ }),
571
+ });
572
+
573
+ if (!response.ok) {
574
+ throw new Error(`HTTP error! status: ${response.status}`);
575
+ }
576
+
577
+ const data = await response.json();
578
+
579
+ //now addign acctual reposnse
580
+ const messages = chat.querySelectorAll('p');
581
+ const lastMessage = messages[messages.length - 1];
582
+ lastMessage.innerHTML = `<strong>Chatbot:</strong> ${data.answer}`;
583
+
584
+ } catch (error) {
585
+ console.error("Error:", error);
586
+ const messages = chat.querySelectorAll('p');
587
+ const lastMessage = messages[messages.length - 1];
588
+ lastMessage.innerHTML = `<strong>Chatbot:</strong> Sorry, I encountered an error: ${error.message}`;
589
+ }
590
+
591
+ document.getElementById("user-question").value = "";
592
+ chat.scrollTop = chat.scrollHeight;
593
+ };
594
  </script>
595
  </body>
596
  </html>