Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +51 -79
static/appS.js
CHANGED
@@ -66,7 +66,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
66 |
const file = e.dataTransfer.files[0];
|
67 |
const dataTransfer = new DataTransfer();
|
68 |
dataTransfer.items.add(file);
|
69 |
-
|
70 |
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
71 |
if (isSummarizeMode) {
|
72 |
fileUpload.files = dataTransfer.files;
|
@@ -80,19 +80,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
80 |
|
81 |
// Send button handlers
|
82 |
sendButtons.forEach(button => {
|
83 |
-
button.addEventListener('click',
|
84 |
-
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
85 |
-
if (isSummarizeMode) {
|
86 |
-
await handleSummarize();
|
87 |
-
} else {
|
88 |
-
await handleCaption();
|
89 |
-
}
|
90 |
-
});
|
91 |
});
|
92 |
|
93 |
function displayFilePreview(file) {
|
94 |
if (filePreviewBubble) filePreviewBubble.remove();
|
95 |
-
|
96 |
filePreviewBubble = createMessageBubble(
|
97 |
`📎 Selected ${file.type.startsWith('image/') ? 'image' : 'document'}: ${file.name}`,
|
98 |
"You"
|
@@ -109,24 +102,24 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
109 |
|
110 |
const message = document.createElement('div');
|
111 |
message.className = "text";
|
112 |
-
|
113 |
if (sender === "You") {
|
114 |
message.textContent = text;
|
115 |
} else {
|
116 |
message.innerHTML = typeof text === 'string' ? text : 'Processing...';
|
117 |
-
|
118 |
if (audioSrc) {
|
119 |
const audioContainer = document.createElement('div');
|
120 |
audioContainer.style.display = 'flex';
|
121 |
audioContainer.style.alignItems = 'center';
|
122 |
audioContainer.style.gap = '10px';
|
123 |
audioContainer.style.marginTop = '8px';
|
124 |
-
|
125 |
const audio = new Audio(audioSrc);
|
126 |
const audioIcon = document.createElement('i');
|
127 |
audioIcon.className = 'fa-solid fa-volume-high audio-toggle';
|
128 |
audioIcon.style.cursor = 'pointer';
|
129 |
-
|
130 |
audioIcon.addEventListener('click', () => {
|
131 |
if (audio.paused) {
|
132 |
audio.play();
|
@@ -138,7 +131,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
138 |
audioIcon.title = "Click to unmute";
|
139 |
}
|
140 |
});
|
141 |
-
|
142 |
audioContainer.appendChild(audioIcon);
|
143 |
audioContainer.appendChild(document.createTextNode('Listen'));
|
144 |
message.appendChild(audioContainer);
|
@@ -152,87 +145,66 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
152 |
return bubble;
|
153 |
}
|
154 |
|
155 |
-
|
156 |
-
if (!selectedFile) return;
|
157 |
-
|
158 |
-
const formData = new FormData();
|
159 |
-
formData.append('file', selectedFile);
|
160 |
-
formData.append('length', 'medium'); // or short/long if you allow user choice
|
161 |
-
|
162 |
-
const bubble = createMessageBubble('Processing document...', 'Assistant');
|
163 |
-
|
164 |
-
try {
|
165 |
-
const response = await fetch('/summarize/', {
|
166 |
-
method: 'POST',
|
167 |
-
body: formData,
|
168 |
-
});
|
169 |
-
|
170 |
-
const data = await response.json();
|
171 |
-
if (response.ok) {
|
172 |
-
let summary = data.summary || "No summary generated.";
|
173 |
-
createMessageBubble(summary, "Assistant");
|
174 |
-
|
175 |
-
if (data.audioUrl) {
|
176 |
-
const audio = document.createElement('audio');
|
177 |
-
audio.src = data.audioUrl;
|
178 |
-
audio.controls = true;
|
179 |
-
bubble.appendChild(audio);
|
180 |
-
}
|
181 |
-
|
182 |
-
if (data.pdfUrl) {
|
183 |
-
const link = document.createElement('a');
|
184 |
-
link.href = data.pdfUrl;
|
185 |
-
link.textContent = 'Download Summary PDF';
|
186 |
-
link.target = "_blank";
|
187 |
-
bubble.appendChild(link);
|
188 |
-
}
|
189 |
-
} else {
|
190 |
-
createMessageBubble(`Error: ${data.detail || "Failed to summarize"}`, "Assistant");
|
191 |
-
}
|
192 |
-
} catch (error) {
|
193 |
-
createMessageBubble(`Error: ${error.message}`, "Assistant");
|
194 |
-
}
|
195 |
-
}
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
async function handleCaption() {
|
200 |
if (!selectedFile) {
|
201 |
-
alert("Please upload
|
202 |
return;
|
203 |
}
|
204 |
|
205 |
-
const
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
|
210 |
-
|
211 |
-
const formData = new FormData();
|
212 |
-
formData.append('file', selectedFile);
|
213 |
|
214 |
-
|
|
|
|
|
|
|
|
|
|
|
215 |
method: 'POST',
|
216 |
body: formData
|
217 |
});
|
218 |
|
219 |
if (!response.ok) {
|
220 |
const error = await response.json();
|
221 |
-
throw new Error(error.detail || '
|
222 |
}
|
223 |
|
224 |
const result = await response.json();
|
225 |
thinkingBubble.remove();
|
226 |
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
} catch (error) {
|
234 |
thinkingBubble.remove();
|
235 |
-
createMessageBubble(`⚠️ Error: ${error.message}`,
|
236 |
} finally {
|
237 |
resetFileInputs();
|
238 |
}
|
@@ -248,7 +220,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
248 |
}
|
249 |
}
|
250 |
|
251 |
-
//
|
252 |
const style = document.createElement('style');
|
253 |
style.textContent = `
|
254 |
.loader {
|
@@ -282,4 +254,4 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
282 |
}
|
283 |
`;
|
284 |
document.head.appendChild(style);
|
285 |
-
});
|
|
|
66 |
const file = e.dataTransfer.files[0];
|
67 |
const dataTransfer = new DataTransfer();
|
68 |
dataTransfer.items.add(file);
|
69 |
+
|
70 |
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
71 |
if (isSummarizeMode) {
|
72 |
fileUpload.files = dataTransfer.files;
|
|
|
80 |
|
81 |
// Send button handlers
|
82 |
sendButtons.forEach(button => {
|
83 |
+
button.addEventListener('click', handleSubmit);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
});
|
85 |
|
86 |
function displayFilePreview(file) {
|
87 |
if (filePreviewBubble) filePreviewBubble.remove();
|
88 |
+
|
89 |
filePreviewBubble = createMessageBubble(
|
90 |
`📎 Selected ${file.type.startsWith('image/') ? 'image' : 'document'}: ${file.name}`,
|
91 |
"You"
|
|
|
102 |
|
103 |
const message = document.createElement('div');
|
104 |
message.className = "text";
|
105 |
+
|
106 |
if (sender === "You") {
|
107 |
message.textContent = text;
|
108 |
} else {
|
109 |
message.innerHTML = typeof text === 'string' ? text : 'Processing...';
|
110 |
+
|
111 |
if (audioSrc) {
|
112 |
const audioContainer = document.createElement('div');
|
113 |
audioContainer.style.display = 'flex';
|
114 |
audioContainer.style.alignItems = 'center';
|
115 |
audioContainer.style.gap = '10px';
|
116 |
audioContainer.style.marginTop = '8px';
|
117 |
+
|
118 |
const audio = new Audio(audioSrc);
|
119 |
const audioIcon = document.createElement('i');
|
120 |
audioIcon.className = 'fa-solid fa-volume-high audio-toggle';
|
121 |
audioIcon.style.cursor = 'pointer';
|
122 |
+
|
123 |
audioIcon.addEventListener('click', () => {
|
124 |
if (audio.paused) {
|
125 |
audio.play();
|
|
|
131 |
audioIcon.title = "Click to unmute";
|
132 |
}
|
133 |
});
|
134 |
+
|
135 |
audioContainer.appendChild(audioIcon);
|
136 |
audioContainer.appendChild(document.createTextNode('Listen'));
|
137 |
message.appendChild(audioContainer);
|
|
|
145 |
return bubble;
|
146 |
}
|
147 |
|
148 |
+
async function handleSubmit() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
if (!selectedFile) {
|
150 |
+
alert("Please upload a file first");
|
151 |
return;
|
152 |
}
|
153 |
|
154 |
+
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
155 |
+
const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
|
156 |
+
const thinkingText = isSummarizeMode ? 'Processing document...' : "Generating caption... <div class='loader'></div>";
|
157 |
+
const senderName = isSummarizeMode ? 'Assistant' : 'Aidan';
|
158 |
|
159 |
+
const thinkingBubble = createMessageBubble(thinkingText, senderName);
|
|
|
|
|
160 |
|
161 |
+
const formData = new FormData();
|
162 |
+
formData.append('file', selectedFile);
|
163 |
+
if (isSummarizeMode) formData.append('length', 'medium');
|
164 |
+
|
165 |
+
try {
|
166 |
+
const response = await fetch(endpoint, {
|
167 |
method: 'POST',
|
168 |
body: formData
|
169 |
});
|
170 |
|
171 |
if (!response.ok) {
|
172 |
const error = await response.json();
|
173 |
+
throw new Error(error.detail || 'Request failed');
|
174 |
}
|
175 |
|
176 |
const result = await response.json();
|
177 |
thinkingBubble.remove();
|
178 |
|
179 |
+
if (isSummarizeMode) {
|
180 |
+
createMessageBubble(result.summary || "No summary generated.", "Assistant");
|
181 |
+
|
182 |
+
if (result.audioUrl) {
|
183 |
+
const audio = document.createElement('audio');
|
184 |
+
audio.src = result.audioUrl;
|
185 |
+
audio.controls = true;
|
186 |
+
convo.appendChild(audio);
|
187 |
+
}
|
188 |
+
|
189 |
+
if (result.pdfUrl) {
|
190 |
+
const link = document.createElement('a');
|
191 |
+
link.href = result.pdfUrl;
|
192 |
+
link.textContent = 'Download Summary PDF';
|
193 |
+
link.className = 'download-link';
|
194 |
+
link.target = "_blank";
|
195 |
+
convo.appendChild(link);
|
196 |
+
}
|
197 |
+
} else {
|
198 |
+
createMessageBubble(
|
199 |
+
`<strong>Caption:</strong><br><br>${result.answer || result.caption}
|
200 |
+
${result.audio ? `<br><audio controls src="${result.audio}"></audio>` : ''}`,
|
201 |
+
"Aidan",
|
202 |
+
result.audio
|
203 |
+
);
|
204 |
+
}
|
205 |
} catch (error) {
|
206 |
thinkingBubble.remove();
|
207 |
+
createMessageBubble(`⚠️ Error: ${error.message}`, senderName);
|
208 |
} finally {
|
209 |
resetFileInputs();
|
210 |
}
|
|
|
220 |
}
|
221 |
}
|
222 |
|
223 |
+
// Loader CSS
|
224 |
const style = document.createElement('style');
|
225 |
style.textContent = `
|
226 |
.loader {
|
|
|
254 |
}
|
255 |
`;
|
256 |
document.head.appendChild(style);
|
257 |
+
});
|