Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +80 -127
static/appS.js
CHANGED
@@ -1,85 +1,3 @@
|
|
1 |
-
|
2 |
-
/*
|
3 |
-
document.addEventListener('DOMContentLoaded', () => {
|
4 |
-
var GotitB = document.querySelector(".explainChoix button")
|
5 |
-
var explain = document.querySelector(".explainChoix")
|
6 |
-
var SummarizeInput = document.querySelector(".SummarizeInput")
|
7 |
-
var CaptionInput = document.querySelector(".CaptionInput")
|
8 |
-
GotitB.addEventListener("click",()=>{
|
9 |
-
explain.style.opacity="0"
|
10 |
-
})
|
11 |
-
|
12 |
-
document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
|
13 |
-
radio.addEventListener('change', (e) => {
|
14 |
-
if (e.target.checked) {
|
15 |
-
const selectedValue = e.target.value;
|
16 |
-
if(selectedValue=="Summarize"){
|
17 |
-
SummarizeInput.classList.add("active")
|
18 |
-
SummarizeInput.classList.remove("innactive")
|
19 |
-
CaptionInput.classList.remove("active")
|
20 |
-
CaptionInput.classList.add("innactive")
|
21 |
-
}
|
22 |
-
else{
|
23 |
-
SummarizeInput.classList.add("innactive")
|
24 |
-
SummarizeInput.classList.remove("active")
|
25 |
-
CaptionInput.classList.remove("innactive")
|
26 |
-
CaptionInput.classList.add("active")
|
27 |
-
}
|
28 |
-
}
|
29 |
-
});
|
30 |
-
});
|
31 |
-
const fileUpload = document.getElementById('file-upload');
|
32 |
-
const imageUpload = document.getElementById('image-upload');
|
33 |
-
|
34 |
-
// Get the icon buttons
|
35 |
-
const fileBtn = document.getElementById('file-btn');
|
36 |
-
const imageBtn = document.getElementById('image-btn');
|
37 |
-
|
38 |
-
// Set up file input for documents (PDF, DOCX, PPTX, XLSX)
|
39 |
-
fileBtn.addEventListener('click', () => {
|
40 |
-
fileUpload.click();
|
41 |
-
});
|
42 |
-
|
43 |
-
fileUpload.addEventListener('change', (e) => {
|
44 |
-
if (e.target.files.length > 0) {
|
45 |
-
const file = e.target.files[0];
|
46 |
-
const validDocTypes = [
|
47 |
-
'application/pdf',
|
48 |
-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
49 |
-
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
50 |
-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
51 |
-
];
|
52 |
-
|
53 |
-
if (validDocTypes.includes(file.type)) {
|
54 |
-
console.log('Valid document selected:', file.name);
|
55 |
-
// Handle the document file here
|
56 |
-
} else {
|
57 |
-
alert('Please select a valid document (PDF, DOCX, PPTX, or XLSX)');
|
58 |
-
fileUpload.value = ''; // Reset the input
|
59 |
-
}
|
60 |
-
}
|
61 |
-
});
|
62 |
-
|
63 |
-
// Set up file input for images
|
64 |
-
imageBtn.addEventListener('click', () => {
|
65 |
-
imageUpload.click();
|
66 |
-
});
|
67 |
-
|
68 |
-
imageUpload.addEventListener('change', (e) => {
|
69 |
-
if (e.target.files.length > 0) {
|
70 |
-
const file = e.target.files[0];
|
71 |
-
const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
72 |
-
|
73 |
-
if (validImageTypes.includes(file.type)) {
|
74 |
-
console.log('Valid image selected:', file.name);
|
75 |
-
// Handle the image file here
|
76 |
-
} else {
|
77 |
-
alert('Please select a valid image (JPEG, PNG, GIF, or WEBP)');
|
78 |
-
imageUpload.value = ''; // Reset the input
|
79 |
-
}
|
80 |
-
}
|
81 |
-
});
|
82 |
-
});*/
|
83 |
document.addEventListener('DOMContentLoaded', () => {
|
84 |
// UI Elements
|
85 |
const GotitB = document.querySelector(".explainChoix button");
|
@@ -134,34 +52,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
134 |
});
|
135 |
});
|
136 |
|
137 |
-
// Drag and drop functionality
|
138 |
-
convo.addEventListener('dragover', (e) => {
|
139 |
-
e.preventDefault();
|
140 |
-
convo.classList.add('drag-over');
|
141 |
-
});
|
142 |
-
|
143 |
-
convo.addEventListener('dragleave', () => {
|
144 |
-
convo.classList.remove('drag-over');
|
145 |
-
});
|
146 |
-
|
147 |
-
convo.addEventListener('drop', async (e) => {
|
148 |
-
e.preventDefault();
|
149 |
-
convo.classList.remove('drag-over');
|
150 |
-
|
151 |
-
if (e.dataTransfer.files.length) {
|
152 |
-
const file = e.dataTransfer.files[0];
|
153 |
-
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
154 |
-
|
155 |
-
if (isSummarizeMode) {
|
156 |
-
fileUpload.files = e.dataTransfer.files;
|
157 |
-
await handleSummarize();
|
158 |
-
} else {
|
159 |
-
imageUpload.files = e.dataTransfer.files;
|
160 |
-
await handleCaption();
|
161 |
-
}
|
162 |
-
}
|
163 |
-
});
|
164 |
-
|
165 |
// Handle document summarization
|
166 |
async function handleSummarize() {
|
167 |
const file = fileUpload.files[0];
|
@@ -170,7 +60,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
170 |
return;
|
171 |
}
|
172 |
|
173 |
-
const length = document.querySelector('input[name="optionS"]:checked')
|
174 |
|
175 |
try {
|
176 |
// Show loading state
|
@@ -182,7 +72,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
182 |
const result = await summarizeDocument(file, length);
|
183 |
|
184 |
// Display results
|
185 |
-
displaySummaryResult(file.name, result.summary, result.
|
186 |
} catch (error) {
|
187 |
displayError(error.message || 'Failed to summarize document');
|
188 |
}
|
@@ -203,10 +93,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
203 |
displayThinkingMessage();
|
204 |
|
205 |
// Call API
|
206 |
-
const
|
207 |
|
208 |
// Display results
|
209 |
-
displayCaptionResult(file.name,
|
210 |
} catch (error) {
|
211 |
displayError(error.message || 'Failed to generate caption');
|
212 |
}
|
@@ -218,14 +108,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
218 |
formData.append('file', file);
|
219 |
formData.append('length', length);
|
220 |
|
221 |
-
const response = await fetch('/summarize', {
|
222 |
method: 'POST',
|
223 |
body: formData
|
224 |
});
|
225 |
|
226 |
if (!response.ok) {
|
227 |
const error = await response.json();
|
228 |
-
throw new Error(error.
|
229 |
}
|
230 |
|
231 |
return await response.json();
|
@@ -235,18 +125,17 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
235 |
const formData = new FormData();
|
236 |
formData.append('file', file);
|
237 |
|
238 |
-
const response = await fetch('/
|
239 |
method: 'POST',
|
240 |
body: formData
|
241 |
});
|
242 |
|
243 |
if (!response.ok) {
|
244 |
const error = await response.json();
|
245 |
-
throw new Error(error.
|
246 |
}
|
247 |
|
248 |
-
|
249 |
-
return result.caption;
|
250 |
}
|
251 |
|
252 |
// UI Helper Functions
|
@@ -287,15 +176,24 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
287 |
<div class="text">
|
288 |
<strong>Summary:</strong><br><br>
|
289 |
${summary.replace(/\n/g, '<br>')}
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
</div>
|
293 |
`;
|
294 |
convo.appendChild(bubble);
|
|
|
295 |
convo.scrollTop = convo.scrollHeight;
|
296 |
}
|
297 |
|
298 |
-
function displayCaptionResult(filename, caption) {
|
299 |
// Remove thinking message
|
300 |
convo.removeChild(convo.lastChild);
|
301 |
|
@@ -306,12 +204,39 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
306 |
<div class="text">
|
307 |
<strong>Caption:</strong><br><br>
|
308 |
${caption}
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
</div>
|
310 |
`;
|
311 |
convo.appendChild(bubble);
|
|
|
312 |
convo.scrollTop = convo.scrollHeight;
|
313 |
}
|
314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
function displayError(message) {
|
316 |
const bubble = document.createElement('div');
|
317 |
bubble.className = 'bubble left';
|
@@ -340,18 +265,46 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
340 |
0% { transform: rotate(0deg); }
|
341 |
100% { transform: rotate(360deg); }
|
342 |
}
|
343 |
-
.download-
|
344 |
-
display: inline-
|
345 |
-
|
|
|
346 |
padding: 5px 10px;
|
347 |
background: #3b82f6;
|
348 |
color: white;
|
|
|
349 |
border-radius: 5px;
|
|
|
350 |
text-decoration: none;
|
|
|
351 |
}
|
352 |
-
.download-
|
353 |
background: #2563eb;
|
354 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
`;
|
356 |
document.head.appendChild(style);
|
357 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
document.addEventListener('DOMContentLoaded', () => {
|
2 |
// UI Elements
|
3 |
const GotitB = document.querySelector(".explainChoix button");
|
|
|
52 |
});
|
53 |
});
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
// Handle document summarization
|
56 |
async function handleSummarize() {
|
57 |
const file = fileUpload.files[0];
|
|
|
60 |
return;
|
61 |
}
|
62 |
|
63 |
+
const length = document.querySelector('input[name="optionS"]:checked')?.value || 'medium';
|
64 |
|
65 |
try {
|
66 |
// Show loading state
|
|
|
72 |
const result = await summarizeDocument(file, length);
|
73 |
|
74 |
// Display results
|
75 |
+
displaySummaryResult(file.name, result.summary, result.audio_url, result.pdf_url);
|
76 |
} catch (error) {
|
77 |
displayError(error.message || 'Failed to summarize document');
|
78 |
}
|
|
|
93 |
displayThinkingMessage();
|
94 |
|
95 |
// Call API
|
96 |
+
const result = await captionImage(file);
|
97 |
|
98 |
// Display results
|
99 |
+
displayCaptionResult(file.name, result.answer, result.audio);
|
100 |
} catch (error) {
|
101 |
displayError(error.message || 'Failed to generate caption');
|
102 |
}
|
|
|
108 |
formData.append('file', file);
|
109 |
formData.append('length', length);
|
110 |
|
111 |
+
const response = await fetch('/summarize/', {
|
112 |
method: 'POST',
|
113 |
body: formData
|
114 |
});
|
115 |
|
116 |
if (!response.ok) {
|
117 |
const error = await response.json();
|
118 |
+
throw new Error(error.error || 'Summarization failed');
|
119 |
}
|
120 |
|
121 |
return await response.json();
|
|
|
125 |
const formData = new FormData();
|
126 |
formData.append('file', file);
|
127 |
|
128 |
+
const response = await fetch('/imagecaption/', {
|
129 |
method: 'POST',
|
130 |
body: formData
|
131 |
});
|
132 |
|
133 |
if (!response.ok) {
|
134 |
const error = await response.json();
|
135 |
+
throw new Error(error.error || 'Captioning failed');
|
136 |
}
|
137 |
|
138 |
+
return await response.json();
|
|
|
139 |
}
|
140 |
|
141 |
// UI Helper Functions
|
|
|
176 |
<div class="text">
|
177 |
<strong>Summary:</strong><br><br>
|
178 |
${summary.replace(/\n/g, '<br>')}
|
179 |
+
<div class="action-buttons" style="margin-top: 10px; display: flex; gap: 10px;">
|
180 |
+
${audioUrl ? `
|
181 |
+
<button class="audio-control" data-audio="${audioUrl}">
|
182 |
+
<i class="fa-solid fa-volume-high"></i> Audio
|
183 |
+
</button>` : ''}
|
184 |
+
${pdfUrl ? `
|
185 |
+
<a href="${pdfUrl}" download="${filename.split('.')[0]}_summary.pdf" class="download-btn">
|
186 |
+
<i class="fa-solid fa-download"></i> PDF
|
187 |
+
</a>` : ''}
|
188 |
+
</div>
|
189 |
</div>
|
190 |
`;
|
191 |
convo.appendChild(bubble);
|
192 |
+
setupAudioControls();
|
193 |
convo.scrollTop = convo.scrollHeight;
|
194 |
}
|
195 |
|
196 |
+
function displayCaptionResult(filename, caption, audioUrl) {
|
197 |
// Remove thinking message
|
198 |
convo.removeChild(convo.lastChild);
|
199 |
|
|
|
204 |
<div class="text">
|
205 |
<strong>Caption:</strong><br><br>
|
206 |
${caption}
|
207 |
+
${audioUrl ? `
|
208 |
+
<div class="action-buttons" style="margin-top: 10px;">
|
209 |
+
<button class="audio-control" data-audio="${audioUrl}">
|
210 |
+
<i class="fa-solid fa-volume-high"></i> Audio
|
211 |
+
</button>
|
212 |
+
</div>` : ''}
|
213 |
</div>
|
214 |
`;
|
215 |
convo.appendChild(bubble);
|
216 |
+
setupAudioControls();
|
217 |
convo.scrollTop = convo.scrollHeight;
|
218 |
}
|
219 |
|
220 |
+
function setupAudioControls() {
|
221 |
+
document.querySelectorAll('.audio-control').forEach(button => {
|
222 |
+
const audioUrl = button.getAttribute('data-audio');
|
223 |
+
const audio = new Audio(audioUrl);
|
224 |
+
const icon = button.querySelector('i');
|
225 |
+
|
226 |
+
button.addEventListener('click', () => {
|
227 |
+
if (audio.paused) {
|
228 |
+
audio.play();
|
229 |
+
icon.classList.remove('fa-volume-xmark');
|
230 |
+
icon.classList.add('fa-volume-high');
|
231 |
+
} else {
|
232 |
+
audio.pause();
|
233 |
+
icon.classList.remove('fa-volume-high');
|
234 |
+
icon.classList.add('fa-volume-xmark');
|
235 |
+
}
|
236 |
+
});
|
237 |
+
});
|
238 |
+
}
|
239 |
+
|
240 |
function displayError(message) {
|
241 |
const bubble = document.createElement('div');
|
242 |
bubble.className = 'bubble left';
|
|
|
265 |
0% { transform: rotate(0deg); }
|
266 |
100% { transform: rotate(360deg); }
|
267 |
}
|
268 |
+
.download-btn, .audio-control {
|
269 |
+
display: inline-flex;
|
270 |
+
align-items: center;
|
271 |
+
gap: 5px;
|
272 |
padding: 5px 10px;
|
273 |
background: #3b82f6;
|
274 |
color: white;
|
275 |
+
border: none;
|
276 |
border-radius: 5px;
|
277 |
+
cursor: pointer;
|
278 |
text-decoration: none;
|
279 |
+
font-size: 14px;
|
280 |
}
|
281 |
+
.download-btn:hover, .audio-control:hover {
|
282 |
background: #2563eb;
|
283 |
}
|
284 |
+
.bubble {
|
285 |
+
max-width: 80%;
|
286 |
+
padding: 12px 16px;
|
287 |
+
margin: 8px 0;
|
288 |
+
border-radius: 18px;
|
289 |
+
position: relative;
|
290 |
+
clear: both;
|
291 |
+
}
|
292 |
+
.bubble.left {
|
293 |
+
background: #f1f1f1;
|
294 |
+
float: left;
|
295 |
+
margin-right: auto;
|
296 |
+
}
|
297 |
+
.bubble.right {
|
298 |
+
background: #3b82f6;
|
299 |
+
color: white;
|
300 |
+
float: right;
|
301 |
+
margin-left: auto;
|
302 |
+
}
|
303 |
+
.label {
|
304 |
+
font-weight: bold;
|
305 |
+
font-size: 12px;
|
306 |
+
margin-bottom: 4px;
|
307 |
+
}
|
308 |
`;
|
309 |
document.head.appendChild(style);
|
310 |
});
|