ikraamkb commited on
Commit
b061157
ยท
verified ยท
1 Parent(s): 9e28f68

Update static/application.js

Browse files
Files changed (1) hide show
  1. static/application.js +64 -129
static/application.js CHANGED
@@ -1,138 +1,73 @@
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 filePreviewDiv = null;
10
- let autoScroll = true;
11
-
12
- convo.addEventListener("scroll", () => {
13
- const threshold = 150;
14
- autoScroll = convo.scrollHeight - convo.scrollTop - convo.clientHeight < threshold;
15
- });
16
-
17
- function scrollToBottom() {
18
- if (autoScroll) {
19
- convo.scrollTo({ top: convo.scrollHeight, behavior: "smooth" });
20
- }
 
 
 
 
21
  }
22
 
23
- function createMessageBubble(text, sender, audioUrl = null, fileName = 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 textDiv = document.createElement("div");
32
- textDiv.className = "text";
33
- textDiv.innerText = text;
34
-
35
- bubble.appendChild(label);
36
- bubble.appendChild(textDiv);
37
-
38
- if (fileName) {
39
- const fileLine = document.createElement("div");
40
- fileLine.style.fontSize = "0.85rem";
41
- fileLine.style.color = "#555";
42
- fileLine.innerHTML = `๐Ÿ“Ž <strong>File:</strong> ${fileName}`;
43
- bubble.appendChild(fileLine);
44
- }
45
-
46
- if (audioUrl) {
47
- const audio = document.createElement("audio");
48
- audio.controls = true;
49
- audio.autoplay = true;
50
- audio.src = audioUrl;
51
- audio.style.marginTop = "10px";
52
- bubble.appendChild(audio);
53
- }
54
-
55
- convo.appendChild(bubble);
56
- scrollToBottom();
57
- return bubble;
58
  }
59
 
60
- function showFilePreview(filename, icon = "๐Ÿ“Ž") {
61
- if (filePreviewDiv) filePreviewDiv.remove();
62
-
63
- filePreviewDiv = document.createElement("div");
64
- filePreviewDiv.className = "file-preview-bubble";
65
- filePreviewDiv.innerHTML = `${icon} <strong>Selected:</strong> ${filename}`;
66
-
67
- convo.appendChild(filePreviewDiv);
68
- scrollToBottom();
69
- }
70
 
71
- sendBtn.addEventListener("click", async () => {
72
- const question = userInput.value.trim();
73
- if (!question) return;
74
 
75
- if (!selectedFile) {
76
- alert("Please upload a file before asking a question.");
77
- return;
78
- }
79
-
80
- if (filePreviewDiv) filePreviewDiv.remove(); // Remove preview bubble
81
-
82
- createMessageBubble(question, "You", null, selectedFile.name);
83
-
84
- const thinkingBubble = createMessageBubble("Waiat let me think ๐Ÿค”", "Chris");
85
-
86
- const formData = new FormData();
87
- formData.append("question", question);
88
- formData.append("file", selectedFile);
89
-
90
- try {
91
- const response = await fetch("/predict", {
92
- method: "POST",
93
- body: formData
94
- });
95
-
96
- const result = await response.json();
97
 
98
- thinkingBubble.querySelector(".text").innerText = result.answer || "No response received.";
99
-
100
- if (result.audio) {
101
- const audio = document.createElement("audio");
102
- audio.controls = true;
103
- audio.autoplay = true;
104
- audio.src = result.audio;
105
- audio.style.marginTop = "10px";
106
- thinkingBubble.appendChild(audio);
107
- }
108
-
109
- } catch (err) {
110
- thinkingBubble.querySelector(".text").innerText = "โš ๏ธ Chris had trouble connecting.";
 
 
 
 
 
 
 
 
 
 
 
111
  }
112
-
113
- userInput.value = "";
114
- selectedFile = null;
115
- });
116
-
117
- imageIcon.addEventListener("click", () => {
118
- const input = document.createElement("input");
119
- input.type = "file";
120
- input.accept = "image/*";
121
- input.onchange = () => {
122
- selectedFile = input.files[0];
123
- if (selectedFile) showFilePreview(selectedFile.name, "๐Ÿ–ผ๏ธ");
124
- };
125
- input.click();
126
- });
127
-
128
- fileIcon.addEventListener("click", () => {
129
- const input = document.createElement("input");
130
- input.type = "file";
131
- input.accept = ".pdf,.docx,.pptx,.xlsx";
132
- input.onchange = () => {
133
- selectedFile = input.files[0];
134
- if (selectedFile) showFilePreview(selectedFile.name, "๐Ÿ“„");
135
- };
136
- input.click();
137
- });
138
  });
 
1
+ document.addEventListener("DOMContentLoaded", () => {
2
+ const convo = document.querySelector(".convo");
3
+ const sendBtn = document.querySelector(".sendingQA");
4
+ const questionInput = document.querySelector(".qt");
5
+ const fileInput = document.querySelector("#file-upload");
6
+ const imageInput = document.querySelector("#image-upload");
7
+
8
+ // Handle text or file submission
9
+ function sendQuestion() {
10
+ const question = questionInput.value.trim();
11
+ if (!question && !fileInput.files.length && !imageInput.files.length) return;
12
+
13
+ const userBubble = document.createElement("div");
14
+ userBubble.className = "bubble right";
15
+ userBubble.textContent = question || "Sent a file/image";
16
+ convo.appendChild(userBubble);
17
+
18
+ // Preview file name (if any)
19
+ const file = fileInput.files[0];
20
+ if (file) {
21
+ const filePreview = document.createElement("div");
22
+ filePreview.className = "file-preview-bubble bubble right";
23
+ filePreview.textContent = `Selected: ${file.name}`;
24
+ convo.appendChild(filePreview);
25
  }
26
 
27
+ // Preview image name (if any)
28
+ const image = imageInput.files[0];
29
+ if (image) {
30
+ const imagePreview = document.createElement("div");
31
+ imagePreview.className = "file-preview-bubble bubble right";
32
+ imagePreview.textContent = `Selected: ${image.name}`;
33
+ convo.appendChild(imagePreview);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  }
35
 
36
+ convo.scrollTop = convo.scrollHeight;
37
+ questionInput.value = "";
 
 
 
 
 
 
 
 
38
 
39
+ // TODO: Send data to backend here
40
+ }
 
41
 
42
+ // Handle send button click
43
+ sendBtn.addEventListener("click", sendQuestion);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ // Handle Enter key in input
46
+ questionInput.addEventListener("keydown", (e) => {
47
+ if (e.key === "Enter") {
48
+ e.preventDefault();
49
+ sendQuestion();
50
+ }
51
+ });
52
+
53
+ // Toggle audio mute/unmute
54
+ convo.addEventListener("click", (e) => {
55
+ if (e.target.classList.contains("audio-toggle")) {
56
+ const audioIcon = e.target;
57
+ const answerBlock = audioIcon.closest(".bubble");
58
+ const audioElement = answerBlock.querySelector("audio");
59
+
60
+ if (audioElement) {
61
+ if (audioElement.muted) {
62
+ audioElement.muted = false;
63
+ audioIcon.classList.replace("fa-volume-xmark", "fa-volume-high");
64
+ audioIcon.title = "Click to mute";
65
+ } else {
66
+ audioElement.muted = true;
67
+ audioIcon.classList.replace("fa-volume-high", "fa-volume-xmark");
68
+ audioIcon.title = "Click to unmute";
69
  }
70
+ }
71
+ }
72
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  });