Spaces:
Running
Running
Update static/appS.js
Browse files- static/appS.js +243 -70
static/appS.js
CHANGED
@@ -1,80 +1,253 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
GotitB.addEventListener("click",()=>{
|
7 |
-
explain.style.opacity="0"
|
8 |
-
})
|
9 |
-
|
10 |
-
document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
|
11 |
-
radio.addEventListener('change', (e) => {
|
12 |
-
if (e.target.checked) {
|
13 |
-
const selectedValue = e.target.value;
|
14 |
-
if(selectedValue=="Summarize"){
|
15 |
-
SummarizeInput.classList.add("active")
|
16 |
-
SummarizeInput.classList.remove("innactive")
|
17 |
-
CaptionInput.classList.remove("active")
|
18 |
-
CaptionInput.classList.add("innactive")
|
19 |
-
}
|
20 |
-
else{
|
21 |
-
SummarizeInput.classList.add("innactive")
|
22 |
-
SummarizeInput.classList.remove("active")
|
23 |
-
CaptionInput.classList.remove("innactive")
|
24 |
-
CaptionInput.classList.add("active")
|
25 |
-
}
|
26 |
-
}
|
27 |
-
});
|
28 |
-
});
|
29 |
const fileUpload = document.getElementById('file-upload');
|
30 |
const imageUpload = document.getElementById('image-upload');
|
31 |
-
|
32 |
-
// Get the icon buttons
|
33 |
const fileBtn = document.getElementById('file-btn');
|
34 |
const imageBtn = document.getElementById('image-btn');
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
});
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
}
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
});
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
imageUpload.click();
|
64 |
});
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
70 |
|
71 |
-
if (
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
77 |
}
|
78 |
-
}
|
79 |
});
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
2 |
+
const GotitB = document.querySelector(".explainChoix button");
|
3 |
+
const explain = document.querySelector(".explainChoix");
|
4 |
+
const SummarizeInput = document.querySelector(".SummarizeInput");
|
5 |
+
const CaptionInput = document.querySelector(".CaptionInput");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
const fileUpload = document.getElementById('file-upload');
|
7 |
const imageUpload = document.getElementById('image-upload');
|
|
|
|
|
8 |
const fileBtn = document.getElementById('file-btn');
|
9 |
const imageBtn = document.getElementById('image-btn');
|
10 |
+
const convo = document.querySelector('.convo');
|
11 |
+
|
12 |
+
GotitB.addEventListener("click", () => {
|
13 |
+
explain.style.opacity = "0";
|
14 |
});
|
15 |
+
|
16 |
+
document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
|
17 |
+
radio.addEventListener('change', (e) => {
|
18 |
+
if (e.target.checked) {
|
19 |
+
const selectedValue = e.target.value;
|
20 |
+
if (selectedValue === "Summarize") {
|
21 |
+
SummarizeInput.classList.add("active");
|
22 |
+
SummarizeInput.classList.remove("innactive");
|
23 |
+
CaptionInput.classList.remove("active");
|
24 |
+
CaptionInput.classList.add("innactive");
|
25 |
+
} else {
|
26 |
+
SummarizeInput.classList.add("innactive");
|
27 |
+
SummarizeInput.classList.remove("active");
|
28 |
+
CaptionInput.classList.remove("innactive");
|
29 |
+
CaptionInput.classList.add("active");
|
30 |
+
}
|
31 |
+
}
|
32 |
+
});
|
33 |
+
});
|
34 |
+
|
35 |
+
fileBtn.addEventListener('click', () => fileUpload.click());
|
36 |
+
imageBtn.addEventListener('click', () => imageUpload.click());
|
37 |
+
|
38 |
+
document.querySelectorAll('.sendingQA').forEach(button => {
|
39 |
+
button.addEventListener('click', async () => {
|
40 |
+
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
41 |
+
if (isSummarizeMode) {
|
42 |
+
await handleSummarize();
|
43 |
+
} else {
|
44 |
+
await handleCaption();
|
45 |
+
}
|
46 |
+
});
|
47 |
+
});
|
48 |
+
|
49 |
+
convo.addEventListener('dragover', (e) => {
|
50 |
+
e.preventDefault();
|
51 |
+
convo.classList.add('drag-over');
|
52 |
});
|
53 |
+
|
54 |
+
convo.addEventListener('dragleave', () => {
|
55 |
+
convo.classList.remove('drag-over');
|
|
|
56 |
});
|
57 |
+
|
58 |
+
convo.addEventListener('drop', async (e) => {
|
59 |
+
e.preventDefault();
|
60 |
+
convo.classList.remove('drag-over');
|
|
|
61 |
|
62 |
+
if (e.dataTransfer.files.length) {
|
63 |
+
const file = e.dataTransfer.files[0];
|
64 |
+
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
65 |
+
|
66 |
+
if (isSummarizeMode) {
|
67 |
+
fileUpload.files = e.dataTransfer.files;
|
68 |
+
await handleSummarize();
|
69 |
+
} else {
|
70 |
+
imageUpload.files = e.dataTransfer.files;
|
71 |
+
await handleCaption();
|
72 |
+
}
|
73 |
}
|
|
|
74 |
});
|
75 |
+
|
76 |
+
async function handleSummarize() {
|
77 |
+
const file = fileUpload.files[0];
|
78 |
+
if (!file) {
|
79 |
+
displayError('Please upload a document first');
|
80 |
+
return;
|
81 |
+
}
|
82 |
+
const length = document.querySelector('input[name="optionS"]:checked')?.value || "medium";
|
83 |
+
|
84 |
+
try {
|
85 |
+
convo.innerHTML = '';
|
86 |
+
displayFileInfo(file.name, 'document');
|
87 |
+
displayThinkingMessage();
|
88 |
+
|
89 |
+
const result = await summarizeDocument(file, length);
|
90 |
+
|
91 |
+
displaySummaryResult(file.name, result.summary, result.audioUrl, result.pdfUrl);
|
92 |
+
} catch (error) {
|
93 |
+
displayError(error.message || 'Failed to summarize document');
|
94 |
+
}
|
95 |
+
}
|
96 |
+
|
97 |
+
async function handleCaption() {
|
98 |
+
const file = imageUpload.files[0];
|
99 |
+
if (!file) {
|
100 |
+
displayError('Please upload an image first');
|
101 |
+
return;
|
102 |
+
}
|
103 |
+
try {
|
104 |
+
convo.innerHTML = '';
|
105 |
+
displayFileInfo(file.name, 'image');
|
106 |
+
displayThinkingMessage();
|
107 |
+
|
108 |
+
const caption = await captionImage(file);
|
109 |
+
|
110 |
+
displayCaptionResult(file.name, caption);
|
111 |
+
} catch (error) {
|
112 |
+
displayError(error.message || 'Failed to generate caption');
|
113 |
+
}
|
114 |
+
}
|
115 |
+
|
116 |
+
async function summarizeDocument(file, length) {
|
117 |
+
const formData = new FormData();
|
118 |
+
formData.append('file', file);
|
119 |
+
formData.append('length', length);
|
120 |
+
|
121 |
+
const response = await fetch('/summarize/', {
|
122 |
+
method: 'POST',
|
123 |
+
body: formData
|
124 |
+
});
|
125 |
+
|
126 |
+
if (!response.ok) {
|
127 |
+
const error = await response.json();
|
128 |
+
throw new Error(error.detail || 'Summarization failed');
|
129 |
+
}
|
130 |
+
|
131 |
+
return await response.json();
|
132 |
+
}
|
133 |
+
|
134 |
+
async function captionImage(file) {
|
135 |
+
const formData = new FormData();
|
136 |
+
formData.append('file', file);
|
137 |
+
|
138 |
+
const response = await fetch('/imagecaption/', {
|
139 |
+
method: 'POST',
|
140 |
+
body: formData
|
141 |
+
});
|
142 |
+
|
143 |
+
if (!response.ok) {
|
144 |
+
const error = await response.json();
|
145 |
+
throw new Error(error.detail || 'Captioning failed');
|
146 |
+
}
|
147 |
+
|
148 |
+
const result = await response.json();
|
149 |
+
return result.caption;
|
150 |
+
}
|
151 |
+
|
152 |
+
function displayFileInfo(filename, type) {
|
153 |
+
const bubble = document.createElement('div');
|
154 |
+
bubble.className = 'bubble right';
|
155 |
+
bubble.innerHTML = `
|
156 |
+
<div class="label">You</div>
|
157 |
+
<div class="text">${type === 'document' ? '📄' : '🖼️'} ${filename}</div>
|
158 |
+
`;
|
159 |
+
convo.appendChild(bubble);
|
160 |
+
}
|
161 |
+
|
162 |
+
function displayThinkingMessage() {
|
163 |
+
const bubble = document.createElement('div');
|
164 |
+
bubble.className = 'bubble left';
|
165 |
+
bubble.innerHTML = `
|
166 |
+
<div class="label">Aidan</div>
|
167 |
+
<div class="text">
|
168 |
+
<div style="display:flex;align-items:center;gap:8px">
|
169 |
+
<span>Processing...</span>
|
170 |
+
<div class="loader"></div>
|
171 |
+
</div>
|
172 |
+
</div>
|
173 |
+
`;
|
174 |
+
convo.appendChild(bubble);
|
175 |
+
convo.scrollTop = convo.scrollHeight;
|
176 |
+
}
|
177 |
+
|
178 |
+
function displaySummaryResult(filename, summary, audioUrl, pdfUrl) {
|
179 |
+
convo.removeChild(convo.lastChild);
|
180 |
+
|
181 |
+
const bubble = document.createElement('div');
|
182 |
+
bubble.className = 'bubble left';
|
183 |
+
bubble.innerHTML = `
|
184 |
+
<div class="label">Aidan</div>
|
185 |
+
<div class="text">
|
186 |
+
<strong>Summary:</strong><br><br>
|
187 |
+
${summary.replace(/\n/g, '<br>')}
|
188 |
+
${audioUrl ? `<br><br><audio controls src="${audioUrl}" style="width: 100%;"></audio>` : ''}
|
189 |
+
${pdfUrl ? `<br><a href="${pdfUrl}" download class="download-link">📥 Download PDF Summary</a>` : ''}
|
190 |
+
</div>
|
191 |
+
`;
|
192 |
+
convo.appendChild(bubble);
|
193 |
+
convo.scrollTop = convo.scrollHeight;
|
194 |
+
}
|
195 |
+
|
196 |
+
function displayCaptionResult(filename, caption) {
|
197 |
+
convo.removeChild(convo.lastChild);
|
198 |
+
|
199 |
+
const bubble = document.createElement('div');
|
200 |
+
bubble.className = 'bubble left';
|
201 |
+
bubble.innerHTML = `
|
202 |
+
<div class="label">Aidan</div>
|
203 |
+
<div class="text">
|
204 |
+
<strong>Caption:</strong><br><br>
|
205 |
+
${caption}
|
206 |
+
</div>
|
207 |
+
`;
|
208 |
+
convo.appendChild(bubble);
|
209 |
+
convo.scrollTop = convo.scrollHeight;
|
210 |
+
}
|
211 |
+
|
212 |
+
function displayError(message) {
|
213 |
+
const bubble = document.createElement('div');
|
214 |
+
bubble.className = 'bubble left';
|
215 |
+
bubble.innerHTML = `
|
216 |
+
<div class="label">Aidan</div>
|
217 |
+
<div class="text" style="color: #ef4444;">
|
218 |
+
⚠️ ${message}
|
219 |
+
</div>
|
220 |
+
`;
|
221 |
+
convo.appendChild(bubble);
|
222 |
+
convo.scrollTop = convo.scrollHeight;
|
223 |
+
}
|
224 |
+
|
225 |
+
const style = document.createElement('style');
|
226 |
+
style.textContent = `
|
227 |
+
.loader {
|
228 |
+
border: 2px solid #f3f3f3;
|
229 |
+
border-top: 2px solid #3b82f6;
|
230 |
+
border-radius: 50%;
|
231 |
+
width: 16px;
|
232 |
+
height: 16px;
|
233 |
+
animation: spin 1s linear infinite;
|
234 |
+
}
|
235 |
+
@keyframes spin {
|
236 |
+
0% { transform: rotate(0deg); }
|
237 |
+
100% { transform: rotate(360deg); }
|
238 |
+
}
|
239 |
+
.download-link {
|
240 |
+
display: inline-block;
|
241 |
+
margin-top: 10px;
|
242 |
+
padding: 5px 10px;
|
243 |
+
background: #3b82f6;
|
244 |
+
color: white;
|
245 |
+
border-radius: 5px;
|
246 |
+
text-decoration: none;
|
247 |
+
}
|
248 |
+
.download-link:hover {
|
249 |
+
background: #2563eb;
|
250 |
+
}
|
251 |
+
`;
|
252 |
+
document.head.appendChild(style);
|
253 |
+
});
|