ikraamkb commited on
Commit
0604e9c
·
verified ·
1 Parent(s): a8e3b62

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +45 -22
static/application.js CHANGED
@@ -1,12 +1,15 @@
1
  document.addEventListener("DOMContentLoaded", function () {
2
  const convo = document.querySelector(".convo");
3
- const sendBtn = document.getElementById("sendBtn");
4
- const userInput = document.getElementById("questionInput");
5
- const fileInput = document.getElementById("fileInput");
 
6
 
 
7
  let autoScroll = true;
8
 
9
- convo.addEventListener('scroll', () => {
 
10
  const threshold = 150;
11
  autoScroll = convo.scrollHeight - convo.scrollTop - convo.clientHeight < threshold;
12
  });
@@ -18,62 +21,82 @@ document.addEventListener("DOMContentLoaded", function () {
18
  }
19
 
20
  function createMessageBubble(text, sender, audioUrl = null) {
21
- const message = document.createElement("div");
22
- message.classList.add("message", sender === "You" ? "user" : "ai");
23
 
24
  const label = document.createElement("div");
25
  label.className = "label";
26
  label.innerText = sender;
27
- message.appendChild(label);
28
 
29
- const bubble = document.createElement("div");
30
- bubble.className = "bubble";
31
- bubble.innerText = text;
32
- message.appendChild(bubble);
 
 
33
 
34
  if (audioUrl) {
35
  const audio = document.createElement("audio");
36
- audio.src = audioUrl;
37
  audio.controls = true;
38
  audio.autoplay = true;
39
- audio.style.display = "block";
40
- audio.style.marginTop = "8px";
41
- message.appendChild(audio);
42
  }
43
 
44
- convo.appendChild(message);
45
  scrollToBottom();
46
  }
47
 
48
  sendBtn.addEventListener("click", async () => {
49
  const question = userInput.value.trim();
50
- const file = fileInput.files[0];
51
 
52
- if (!question || !file) return;
 
 
 
53
 
54
  createMessageBubble(question, "You");
55
 
56
  const formData = new FormData();
57
  formData.append("question", question);
58
- formData.append("file", file);
59
 
60
  try {
61
  const response = await fetch("/predict", {
62
  method: "POST",
63
  body: formData,
64
  });
65
-
66
  const result = await response.json();
 
67
  if (result.answer) {
68
  createMessageBubble(result.answer, "Chris", result.audio);
69
  } else {
70
- createMessageBubble("Sorry, I couldn't find an answer.", "Chris");
71
  }
72
  } catch (err) {
73
  createMessageBubble("Error connecting to server.", "Chris");
74
  }
75
 
76
  userInput.value = "";
77
- fileInput.value = "";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  });
79
  });
 
1
  document.addEventListener("DOMContentLoaded", function () {
2
  const convo = document.querySelector(".convo");
3
+ const sendBtn = document.querySelector(".sendingQA");
4
+ const userInput = document.querySelector(".qt input");
5
+ const imageIcon = document.querySelector(".fa-image");
6
+ const fileIcon = document.querySelector(".fa-file");
7
 
8
+ let selectedFile = null;
9
  let autoScroll = true;
10
 
11
+ // Enable auto-scroll unless user scrolls up
12
+ convo.addEventListener("scroll", () => {
13
  const threshold = 150;
14
  autoScroll = convo.scrollHeight - convo.scrollTop - convo.clientHeight < threshold;
15
  });
 
21
  }
22
 
23
  function createMessageBubble(text, sender, audioUrl = null) {
24
+ const bubble = document.createElement("div");
25
+ bubble.classList.add("bubble", sender === "You" ? "right" : "left");
26
 
27
  const label = document.createElement("div");
28
  label.className = "label";
29
  label.innerText = sender;
 
30
 
31
+ const messageText = document.createElement("div");
32
+ messageText.className = "text";
33
+ messageText.innerText = text;
34
+
35
+ bubble.appendChild(label);
36
+ bubble.appendChild(messageText);
37
 
38
  if (audioUrl) {
39
  const audio = document.createElement("audio");
 
40
  audio.controls = true;
41
  audio.autoplay = true;
42
+ audio.src = audioUrl;
43
+ audio.style.marginTop = "10px";
44
+ bubble.appendChild(audio);
45
  }
46
 
47
+ convo.appendChild(bubble);
48
  scrollToBottom();
49
  }
50
 
51
  sendBtn.addEventListener("click", async () => {
52
  const question = userInput.value.trim();
53
+ if (!question) return;
54
 
55
+ if (!selectedFile) {
56
+ alert("Please upload a file or image.");
57
+ return;
58
+ }
59
 
60
  createMessageBubble(question, "You");
61
 
62
  const formData = new FormData();
63
  formData.append("question", question);
64
+ formData.append("file", selectedFile);
65
 
66
  try {
67
  const response = await fetch("/predict", {
68
  method: "POST",
69
  body: formData,
70
  });
 
71
  const result = await response.json();
72
+
73
  if (result.answer) {
74
  createMessageBubble(result.answer, "Chris", result.audio);
75
  } else {
76
+ createMessageBubble("No response received.", "Chris");
77
  }
78
  } catch (err) {
79
  createMessageBubble("Error connecting to server.", "Chris");
80
  }
81
 
82
  userInput.value = "";
83
+ selectedFile = null;
84
+ });
85
+
86
+ // File picker handlers
87
+ imageIcon.addEventListener("click", () => {
88
+ const input = document.createElement("input");
89
+ input.type = "file";
90
+ input.accept = "image/*";
91
+ input.onchange = () => selectedFile = input.files[0];
92
+ input.click();
93
+ });
94
+
95
+ fileIcon.addEventListener("click", () => {
96
+ const input = document.createElement("input");
97
+ input.type = "file";
98
+ input.accept = ".pdf,.docx,.pptx,.xlsx";
99
+ input.onchange = () => selectedFile = input.files[0];
100
+ input.click();
101
  });
102
  });