Spaces:
Sleeping
Sleeping
Update static/appS.js
Browse files- static/appS.js +69 -73
static/appS.js
CHANGED
@@ -133,88 +133,84 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
133 |
return bubble;
|
134 |
}
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
const thinkingBubble = createMessageBubble(
|
152 |
-
isSummarizeMode ? "Summarizing your document..." : "Generating caption for your image...",
|
153 |
-
"Aidan"
|
154 |
-
);
|
155 |
-
|
156 |
-
try {
|
157 |
-
const formData = new FormData();
|
158 |
-
formData.append('file', selectedFile);
|
159 |
-
|
160 |
-
let endpoint, result;
|
161 |
-
|
162 |
-
if (isSummarizeMode) {
|
163 |
-
const length = document.querySelector('input[name="optionS"]:checked').value;
|
164 |
-
formData.append('length', length);
|
165 |
-
endpoint = '/summarize/';
|
166 |
-
} else {
|
167 |
-
endpoint = '/imagecaption/';
|
168 |
-
}
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
thinkingBubble.remove();
|
185 |
-
|
186 |
-
if (isSummarizeMode) {
|
187 |
-
createMessageBubble(
|
188 |
-
`<strong>Summary:</strong><br><br>${result.summary.replace(/\n/g, '<br>')}` +
|
189 |
-
(result.pdf_url ? `<br><br><a href="${result.pdf_url}" download target="_blank">Download PDF</a>` : ''),
|
190 |
-
"Aidan",
|
191 |
-
result.audio_url
|
192 |
-
);
|
193 |
-
} else {
|
194 |
-
createMessageBubble(
|
195 |
-
`<strong>Caption:</strong><br><br>${result.answer}`,
|
196 |
-
"Aidan",
|
197 |
-
result.audio
|
198 |
-
);
|
199 |
-
}
|
200 |
-
} catch (error) {
|
201 |
-
console.error("Error:", error);
|
202 |
-
thinkingBubble.remove();
|
203 |
createMessageBubble(
|
204 |
-
|
205 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
);
|
207 |
-
}
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
}
|
216 |
}
|
217 |
-
|
218 |
// Attach send handlers
|
219 |
sendButtons.forEach(button => {
|
220 |
button.addEventListener('click', handleSend);
|
|
|
133 |
return bubble;
|
134 |
}
|
135 |
|
136 |
+
async function handleSend() {
|
137 |
+
if (!selectedFile) {
|
138 |
+
alert("Please upload a file first");
|
139 |
+
return;
|
140 |
+
}
|
141 |
|
142 |
+
const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
|
143 |
+
|
144 |
+
// User message
|
145 |
+
createMessageBubble(
|
146 |
+
isSummarizeMode ? "Please summarize this document" : "Please caption this image",
|
147 |
+
"You"
|
148 |
+
);
|
149 |
+
|
150 |
+
// Thinking message
|
151 |
+
const thinkingBubble = createMessageBubble(
|
152 |
+
isSummarizeMode ? "Summarizing your document..." : "Generating caption for your image...",
|
153 |
+
"Aidan"
|
154 |
+
);
|
155 |
+
|
156 |
+
try {
|
157 |
+
const formData = new FormData();
|
158 |
+
formData.append('file', selectedFile);
|
159 |
|
160 |
+
const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
|
161 |
+
|
162 |
+
// For summarize mode, add length parameter
|
163 |
+
if (isSummarizeMode) {
|
164 |
+
const length = document.querySelector('input[name="optionS"]:checked').value;
|
165 |
+
formData.append('length', length);
|
166 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
+
const response = await fetch(endpoint, {
|
169 |
+
method: 'POST',
|
170 |
+
body: formData
|
171 |
+
});
|
172 |
|
173 |
+
const result = await response.json();
|
174 |
+
|
175 |
+
if (!response.ok) {
|
176 |
+
throw new Error(result.detail || result.message || "Request failed");
|
177 |
+
}
|
178 |
|
179 |
+
// Replace thinking message with actual result
|
180 |
+
thinkingBubble.remove();
|
181 |
+
|
182 |
+
if (isSummarizeMode) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
createMessageBubble(
|
184 |
+
`<strong>Summary:</strong><br><br>${result.summary.replace(/\n/g, '<br>')}` +
|
185 |
+
(result.pdf_url ? `<br><br><a href="${result.pdf_url}" download target="_blank">Download PDF</a>` : ''),
|
186 |
+
"Aidan",
|
187 |
+
result.audio_url
|
188 |
+
);
|
189 |
+
} else {
|
190 |
+
createMessageBubble(
|
191 |
+
`<strong>Caption:</strong><br><br>${result.answer || result.caption}`,
|
192 |
+
"Aidan",
|
193 |
+
result.audio
|
194 |
);
|
195 |
+
}
|
196 |
+
} catch (error) {
|
197 |
+
console.error("Error:", error);
|
198 |
+
thinkingBubble.remove();
|
199 |
+
createMessageBubble(
|
200 |
+
`⚠️ Error: ${error.message}`,
|
201 |
+
"Aidan"
|
202 |
+
);
|
203 |
+
} finally {
|
204 |
+
// Reset file inputs
|
205 |
+
selectedFile = null;
|
206 |
+
fileUpload.value = '';
|
207 |
+
imageUpload.value = '';
|
208 |
+
if (filePreviewBubble) {
|
209 |
+
filePreviewBubble.remove();
|
210 |
+
filePreviewBubble = null;
|
211 |
}
|
212 |
}
|
213 |
+
}
|
214 |
// Attach send handlers
|
215 |
sendButtons.forEach(button => {
|
216 |
button.addEventListener('click', handleSend);
|