ikraamkb commited on
Commit
be9050c
·
verified ·
1 Parent(s): 2c0fbc1

Update static/appS.js

Browse files
Files changed (1) hide show
  1. static/appS.js +58 -3
static/appS.js CHANGED
@@ -5,10 +5,41 @@ document.addEventListener('DOMContentLoaded', () => {
5
  const fileBtn = document.getElementById('file-btn');
6
  const imageBtn = document.getElementById('image-btn');
7
  const sendButtons = document.querySelectorAll('.sendingQA');
 
 
8
 
9
  let selectedFile = null;
10
  let filePreviewBubble = null;
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  // File upload handlers
13
  fileBtn.addEventListener('click', () => fileUpload.click());
14
  imageBtn.addEventListener('click', () => imageUpload.click());
@@ -100,7 +131,27 @@ document.addEventListener('DOMContentLoaded', () => {
100
  }
101
 
102
  const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
 
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  // User message
105
  createMessageBubble(
106
  isSummarizeMode ? "Please summarize this document" : "Please caption this image",
@@ -122,13 +173,17 @@ document.addEventListener('DOMContentLoaded', () => {
122
  formData.append('length', length);
123
  }
124
 
125
- const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
126
  const response = await fetch(endpoint, {
127
  method: 'POST',
128
  body: formData
129
  });
130
 
 
 
 
 
131
  const result = await response.json();
 
132
 
133
  // Replace thinking message with actual result
134
  thinkingBubble.remove();
@@ -148,12 +203,12 @@ document.addEventListener('DOMContentLoaded', () => {
148
  );
149
  }
150
  } catch (error) {
 
151
  thinkingBubble.remove();
152
  createMessageBubble(
153
- "⚠️ Sorry, I encountered an error processing your request",
154
  "Aidan"
155
  );
156
- console.error(error);
157
  } finally {
158
  selectedFile = null;
159
  fileUpload.value = '';
 
5
  const fileBtn = document.getElementById('file-btn');
6
  const imageBtn = document.getElementById('image-btn');
7
  const sendButtons = document.querySelectorAll('.sendingQA');
8
+ const SummarizeInput = document.querySelector(".SummarizeInput");
9
+ const CaptionInput = document.querySelector(".CaptionInput");
10
 
11
  let selectedFile = null;
12
  let filePreviewBubble = null;
13
 
14
+ // Mode switching handler
15
+ document.querySelectorAll('.select-options input[type="radio"]').forEach(radio => {
16
+ radio.addEventListener('change', (e) => {
17
+ if (e.target.checked) {
18
+ const selectedValue = e.target.value;
19
+ if (selectedValue == "Summarize") {
20
+ SummarizeInput.classList.add("active");
21
+ SummarizeInput.classList.remove("innactive");
22
+ CaptionInput.classList.remove("active");
23
+ CaptionInput.classList.add("innactive");
24
+ } else {
25
+ SummarizeInput.classList.add("innactive");
26
+ SummarizeInput.classList.remove("active");
27
+ CaptionInput.classList.remove("innactive");
28
+ CaptionInput.classList.add("active");
29
+ }
30
+
31
+ // Clear current selection when switching modes
32
+ selectedFile = null;
33
+ fileUpload.value = '';
34
+ imageUpload.value = '';
35
+ if (filePreviewBubble) {
36
+ filePreviewBubble.remove();
37
+ filePreviewBubble = null;
38
+ }
39
+ }
40
+ });
41
+ });
42
+
43
  // File upload handlers
44
  fileBtn.addEventListener('click', () => fileUpload.click());
45
  imageBtn.addEventListener('click', () => imageUpload.click());
 
131
  }
132
 
133
  const isSummarizeMode = document.querySelector('input[name="option"]:checked').value === 'Summarize';
134
+ const endpoint = isSummarizeMode ? '/summarize/' : '/imagecaption/';
135
 
136
+ // Validate file type based on mode
137
+ if (isSummarizeMode) {
138
+ const validDocTypes = ['application/pdf',
139
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
140
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
141
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
142
+
143
+ if (!validDocTypes.includes(selectedFile.type)) {
144
+ alert('Please select a valid document (PDF, DOCX, PPTX, or XLSX)');
145
+ return;
146
+ }
147
+ } else {
148
+ const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
149
+ if (!validImageTypes.includes(selectedFile.type)) {
150
+ alert('Please select a valid image (JPEG, PNG, GIF, or WEBP)');
151
+ return;
152
+ }
153
+ }
154
+
155
  // User message
156
  createMessageBubble(
157
  isSummarizeMode ? "Please summarize this document" : "Please caption this image",
 
173
  formData.append('length', length);
174
  }
175
 
 
176
  const response = await fetch(endpoint, {
177
  method: 'POST',
178
  body: formData
179
  });
180
 
181
+ if (!response.ok) {
182
+ throw new Error(`HTTP error! status: ${response.status}`);
183
+ }
184
+
185
  const result = await response.json();
186
+ console.log("API Response:", result);
187
 
188
  // Replace thinking message with actual result
189
  thinkingBubble.remove();
 
203
  );
204
  }
205
  } catch (error) {
206
+ console.error("Error:", error);
207
  thinkingBubble.remove();
208
  createMessageBubble(
209
+ `⚠️ Error: ${error.message}`,
210
  "Aidan"
211
  );
 
212
  } finally {
213
  selectedFile = null;
214
  fileUpload.value = '';