ikraamkb commited on
Commit
75ed7dd
Β·
verified Β·
1 Parent(s): d4d9fc1

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +13 -64
static/appS.js CHANGED
@@ -12,11 +12,9 @@ document.addEventListener('DOMContentLoaded', () => {
12
 
13
  let selectedFile = null;
14
 
15
- // Default mode
16
  const summarizeRadio = document.getElementById('summarize-radio');
17
  if (summarizeRadio) summarizeRadio.checked = true;
18
 
19
- // Mode switching
20
  document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
21
  radio.addEventListener('change', (e) => {
22
  if (e.target.checked) {
@@ -36,7 +34,6 @@ document.addEventListener('DOMContentLoaded', () => {
36
  });
37
  });
38
 
39
- // File upload handlers
40
  fileBtn.addEventListener('click', () => fileUpload.click());
41
  imageBtn.addEventListener('click', () => imageUpload.click());
42
 
@@ -176,7 +173,7 @@ document.addEventListener('DOMContentLoaded', () => {
176
  return bubble;
177
  }
178
 
179
- /* async function handleSubmit() {
180
  if (!selectedFile) {
181
  alert("Please upload a file first");
182
  return;
@@ -184,13 +181,16 @@ document.addEventListener('DOMContentLoaded', () => {
184
 
185
  const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
186
 
187
- // ✨ KEY UPDATE HERE
188
- const endpoint = isSummarizeMode ? '/Summarization/summarize/' : '/Summarization/imagecaption/';
189
-
190
- const thinkingText = isSummarizeMode ? 'Processing document πŸ“„... <div class="loader"></div>' : "Generating caption πŸ–ΌοΈ ... <div class='loader'></div>";
191
- const senderName = "Aidan";
192
 
193
- const thinkingBubble = createMessageBubble(thinkingText, senderName);
 
 
 
 
194
 
195
  const formData = new FormData();
196
  formData.append('file', selectedFile);
@@ -202,20 +202,10 @@ document.addEventListener('DOMContentLoaded', () => {
202
  body: formData
203
  });
204
 
205
- /* if (!response.ok) {
206
- let errorMessage = 'Request failed';
207
- try {
208
- const error = await response.json();
209
- errorMessage = error.detail || error.error || errorMessage;
210
- } catch (e) {}
211
- throw new Error(errorMessage);
212
- } */
213
  if (!response.ok) {
214
- if (response.status === 404) {
215
- throw new Error("Service endpoint not found. Please check the AI service deployment.");
216
- }
217
- throw new Error("Request failed");
218
- }
219
 
220
  const result = await response.json();
221
  thinkingBubble.remove();
@@ -241,46 +231,6 @@ document.addEventListener('DOMContentLoaded', () => {
241
  } finally {
242
  selectedFile = null;
243
  }
244
- } */
245
- async function handleSubmit() {
246
- if (!selectedFile) {
247
- alert("Please upload a file first");
248
- return;
249
- }
250
-
251
- const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
252
-
253
- // βœ… Dynamic path handling for Hugging Face Spaces
254
- const BASE_PATH = window.location.pathname.split('/').slice(0, 3).join('/');
255
- const endpoint = isSummarizeMode
256
- ? `${BASE_PATH}/summarization/summarize/`
257
- : `${BASE_PATH}/summarization/imagecaption/`;
258
-
259
- const thinkingText = isSummarizeMode
260
- ? 'Processing document πŸ“„... <div class="loader"></div>'
261
- : "Generating caption πŸ–ΌοΈ... <div class='loader'></div>";
262
-
263
- const thinkingBubble = createMessageBubble(thinkingText, "Aidan");
264
-
265
- try {
266
- const response = await fetch(endpoint, {
267
- method: 'POST',
268
- body: formData
269
- });
270
-
271
- if (!response.ok) {
272
- const error = await response.json().catch(() => null);
273
- throw new Error(error?.detail || error?.error || "Request failed");
274
- }
275
-
276
- const result = await response.json();
277
- // ... (rest of your success handling)
278
- } catch (error) {
279
- console.error("API Error:", error);
280
- createMessageBubble(`⚠️ Error: ${error.message}`, "Aidan");
281
- } finally {
282
- thinkingBubble.remove();
283
- }
284
  }
285
 
286
  const style = document.createElement('style');
@@ -301,7 +251,6 @@ document.addEventListener('DOMContentLoaded', () => {
301
  `;
302
  document.head.appendChild(style);
303
 
304
- // βœ… Back button
305
  var backarrow = document.querySelector(".fa-arrow-left");
306
  backarrow.addEventListener('click', function () {
307
  window.location.href = '/';
 
12
 
13
  let selectedFile = null;
14
 
 
15
  const summarizeRadio = document.getElementById('summarize-radio');
16
  if (summarizeRadio) summarizeRadio.checked = true;
17
 
 
18
  document.querySelectorAll('.select-options input[name="mode"]').forEach(radio => {
19
  radio.addEventListener('change', (e) => {
20
  if (e.target.checked) {
 
34
  });
35
  });
36
 
 
37
  fileBtn.addEventListener('click', () => fileUpload.click());
38
  imageBtn.addEventListener('click', () => imageUpload.click());
39
 
 
173
  return bubble;
174
  }
175
 
176
+ async function handleSubmit() {
177
  if (!selectedFile) {
178
  alert("Please upload a file first");
179
  return;
 
181
 
182
  const isSummarizeMode = document.querySelector('input[name="mode"]:checked').value === 'Summarize';
183
 
184
+ const BASE_PATH = window.location.pathname.split('/').slice(0, 2).join('/');
185
+ const endpoint = isSummarizeMode
186
+ ? `${BASE_PATH}/Summarization/summarize/`
187
+ : `${BASE_PATH}/Summarization/imagecaption/`;
 
188
 
189
+ const thinkingText = isSummarizeMode
190
+ ? 'Processing document πŸ“„... <div class="loader"></div>'
191
+ : "Generating caption πŸ–ΌοΈ... <div class='loader'></div>";
192
+
193
+ const thinkingBubble = createMessageBubble(thinkingText, "Aidan");
194
 
195
  const formData = new FormData();
196
  formData.append('file', selectedFile);
 
202
  body: formData
203
  });
204
 
 
 
 
 
 
 
 
 
205
  if (!response.ok) {
206
+ const error = await response.json().catch(() => null);
207
+ throw new Error(error?.detail || error?.error || "Request failed");
208
+ }
 
 
209
 
210
  const result = await response.json();
211
  thinkingBubble.remove();
 
231
  } finally {
232
  selectedFile = null;
233
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  }
235
 
236
  const style = document.createElement('style');
 
251
  `;
252
  document.head.appendChild(style);
253
 
 
254
  var backarrow = document.querySelector(".fa-arrow-left");
255
  backarrow.addEventListener('click', function () {
256
  window.location.href = '/';