Spaces:
Sleeping
Sleeping
Update static/scripts.js
Browse files- static/scripts.js +353 -239
static/scripts.js
CHANGED
|
@@ -1,239 +1,353 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
<
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
<
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
</
|
| 174 |
-
|
| 175 |
-
<
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
<
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```javascript
|
| 2 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
+
// Starry Background Animation
|
| 4 |
+
const stars = document.createElement('canvas');
|
| 5 |
+
stars.className = 'stars';
|
| 6 |
+
document.body.appendChild(stars);
|
| 7 |
+
const ctx = stars.getContext('2d');
|
| 8 |
+
stars.width = window.innerWidth;
|
| 9 |
+
stars.height = window.innerHeight;
|
| 10 |
+
const starArray = [];
|
| 11 |
+
for (let i = 0; i < 100; i++) {
|
| 12 |
+
starArray.push({
|
| 13 |
+
x: Math.random() * stars.width,
|
| 14 |
+
y: Math.random() * stars.height,
|
| 15 |
+
radius: Math.random() * 2,
|
| 16 |
+
opacity: Math.random()
|
| 17 |
+
});
|
| 18 |
+
}
|
| 19 |
+
function animateStars() {
|
| 20 |
+
ctx.clearRect(0, 0, stars.width, stars.height);
|
| 21 |
+
starArray.forEach(star => {
|
| 22 |
+
ctx.beginPath();
|
| 23 |
+
ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2);
|
| 24 |
+
ctx.fillStyle = `rgba(255, 255, 255, ${star.opacity})`;
|
| 25 |
+
ctx.fill();
|
| 26 |
+
star.opacity += (Math.random() - 0.5) * 0.05;
|
| 27 |
+
if (star.opacity > 1) star.opacity = 1;
|
| 28 |
+
if (star.opacity < 0) star.opacity = 0;
|
| 29 |
+
});
|
| 30 |
+
requestAnimationFrame(animateStars);
|
| 31 |
+
}
|
| 32 |
+
animateStars();
|
| 33 |
+
|
| 34 |
+
// Card Hover Effects
|
| 35 |
+
const cards = document.querySelectorAll(".card");
|
| 36 |
+
cards.forEach(card => {
|
| 37 |
+
card.addEventListener("mouseenter", () => {
|
| 38 |
+
if (!card.classList.contains('shadowed')) {
|
| 39 |
+
card.style.transition = "transform 0.4s ease-in-out";
|
| 40 |
+
card.style.transform = "scale(1.05) rotate(0.5deg)";
|
| 41 |
+
}
|
| 42 |
+
});
|
| 43 |
+
card.addEventListener("mouseleave", () => {
|
| 44 |
+
if (!card.classList.contains('shadowed')) {
|
| 45 |
+
card.style.transform = "scale(1) rotate(0deg)";
|
| 46 |
+
}
|
| 47 |
+
});
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
// Window Resize Handler for Canvas
|
| 51 |
+
window.addEventListener('resize', () => {
|
| 52 |
+
stars.width = window.innerWidth;
|
| 53 |
+
stars.height = window.innerHeight;
|
| 54 |
+
starArray.length = 0;
|
| 55 |
+
for (let i = 0; i < 100; i++) {
|
| 56 |
+
starArray.push({
|
| 57 |
+
x: Math.random() * stars.width,
|
| 58 |
+
y: Math.random() * stars.height,
|
| 59 |
+
radius: Math.random() * 2,
|
| 60 |
+
opacity: Math.random()
|
| 61 |
+
});
|
| 62 |
+
}
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
// Box Click to Open Modal
|
| 66 |
+
document.querySelectorAll('.function-box').forEach(box => {
|
| 67 |
+
box.addEventListener('click', () => {
|
| 68 |
+
const modalId = box.dataset.modal;
|
| 69 |
+
document.getElementById(modalId).classList.remove('hidden');
|
| 70 |
+
// Dim other function-box elements
|
| 71 |
+
document.querySelectorAll('.function-box').forEach(otherBox => {
|
| 72 |
+
if (otherBox !== box) {
|
| 73 |
+
otherBox.classList.add('shadowed');
|
| 74 |
+
}
|
| 75 |
+
});
|
| 76 |
+
});
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
// Chatbot Icon Click
|
| 80 |
+
document.querySelector('.chatbot-icon').addEventListener('click', () => {
|
| 81 |
+
document.getElementById('chatbot-modal').classList.remove('hidden');
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
// Close Modal
|
| 85 |
+
document.querySelectorAll('.close-modal').forEach(closeBtn => {
|
| 86 |
+
closeBtn.addEventListener('click', () => {
|
| 87 |
+
closeBtn.closest('.modal').classList.add('hidden');
|
| 88 |
+
// Reset forms and responses
|
| 89 |
+
const forms = closeBtn.closest('.modal').querySelectorAll('form');
|
| 90 |
+
forms.forEach(form => {
|
| 91 |
+
form.reset();
|
| 92 |
+
// Restaurer l'état des boutons
|
| 93 |
+
const submitButton = form.querySelector('button[type="submit"]');
|
| 94 |
+
if (submitButton) {
|
| 95 |
+
submitButton.classList.remove('loading');
|
| 96 |
+
submitButton.innerHTML = 'Send';
|
| 97 |
+
submitButton.disabled = false;
|
| 98 |
+
}
|
| 99 |
+
});
|
| 100 |
+
const responseCards = closeBtn.closest('.modal').querySelectorAll('.response-card');
|
| 101 |
+
responseCards.forEach(card => {
|
| 102 |
+
card.classList.add('hidden');
|
| 103 |
+
card.innerHTML = '';
|
| 104 |
+
});
|
| 105 |
+
const dropAreas = closeBtn.closest('.modal').querySelectorAll('.drop-area p');
|
| 106 |
+
dropAreas.forEach(p => p.textContent = 'Drop File Here or Click to Choose');
|
| 107 |
+
// Clear chatbot conversation
|
| 108 |
+
if (closeBtn.closest('.modal').id === 'chatbot-modal') {
|
| 109 |
+
document.getElementById('chatbot-conversation').innerHTML = '';
|
| 110 |
+
}
|
| 111 |
+
// Remove dimming from all function-box elements
|
| 112 |
+
document.querySelectorAll('.function-box').forEach(box => {
|
| 113 |
+
box.classList.remove('shadowed');
|
| 114 |
+
});
|
| 115 |
+
});
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
// Tab Switching for Summarize and Translate
|
| 119 |
+
document.querySelectorAll('.tab-btn').forEach(btn => {
|
| 120 |
+
btn.addEventListener('click', () => {
|
| 121 |
+
const tab = btn.dataset.tab;
|
| 122 |
+
btn.closest('.modal').querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
|
| 123 |
+
btn.classList.add('active');
|
| 124 |
+
btn.closest('.modal').querySelectorAll('.tab-content').forEach(content => {
|
| 125 |
+
content.classList.add('hidden');
|
| 126 |
+
if (content.dataset.tab === tab) content.classList.remove('hidden');
|
| 127 |
+
});
|
| 128 |
+
});
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
// File Drop Handler
|
| 132 |
+
window.handleDrop = function(event, dropArea) {
|
| 133 |
+
event.preventDefault();
|
| 134 |
+
const fileInput = dropArea.querySelector('input[type="file"]');
|
| 135 |
+
const files = event.dataTransfer.files;
|
| 136 |
+
if (files.length > 0) {
|
| 137 |
+
fileInput.files = files;
|
| 138 |
+
dropArea.querySelector('p').textContent = `Selected: ${files[0].name}`;
|
| 139 |
+
}
|
| 140 |
+
};
|
| 141 |
+
|
| 142 |
+
// Chatbot Form Submission
|
| 143 |
+
document.getElementById('chatbot-form').addEventListener('submit', async (e) => {
|
| 144 |
+
e.preventDefault();
|
| 145 |
+
const input = e.target.querySelector('input');
|
| 146 |
+
const message = input.value.trim();
|
| 147 |
+
const submitButton = e.target.querySelector('button[type="submit"]');
|
| 148 |
+
if (!message) return;
|
| 149 |
+
|
| 150 |
+
// Mettre le bouton en mode chargement
|
| 151 |
+
submitButton.classList.add('loading');
|
| 152 |
+
submitButton.innerHTML = '<span class="spinner"></span>Loading...';
|
| 153 |
+
submitButton.disabled = true;
|
| 154 |
+
|
| 155 |
+
const conversation = document.getElementById('chatbot-conversation');
|
| 156 |
+
conversation.innerHTML += `<p class="user-message">You: ${message}</p>`;
|
| 157 |
+
conversation.scrollTop = conversation.scrollHeight;
|
| 158 |
+
input.value = '';
|
| 159 |
+
|
| 160 |
+
try {
|
| 161 |
+
const response = await fetch('/chat', {
|
| 162 |
+
method: 'POST',
|
| 163 |
+
headers: { 'Content-Type': 'application/json' },
|
| 164 |
+
body: JSON.stringify({ message })
|
| 165 |
+
});
|
| 166 |
+
const data = await response.json();
|
| 167 |
+
// Restaurer le bouton
|
| 168 |
+
submitButton.classList.remove('loading');
|
| 169 |
+
submitButton.innerHTML = 'Send';
|
| 170 |
+
submitButton.disabled = false;
|
| 171 |
+
|
| 172 |
+
if (response.ok) {
|
| 173 |
+
conversation.innerHTML += `<p class="bot-message">Gemini: ${data.response}</p>`;
|
| 174 |
+
} else {
|
| 175 |
+
conversation.innerHTML += `<p class="bot-message">Error: ${data.detail}</p>`;
|
| 176 |
+
}
|
| 177 |
+
conversation.scrollTop = conversation.scrollHeight;
|
| 178 |
+
} catch (error) {
|
| 179 |
+
// Restaurer le bouton en cas d'erreur
|
| 180 |
+
submitButton.classList.remove('loading');
|
| 181 |
+
submitButton.innerHTML = 'Send';
|
| 182 |
+
submitButton.disabled = false;
|
| 183 |
+
conversation.innerHTML += `<p class="bot-message">Error: ${error.message}</p>`;
|
| 184 |
+
conversation.scrollTop = conversation.scrollHeight;
|
| 185 |
+
}
|
| 186 |
+
});
|
| 187 |
+
|
| 188 |
+
// Form Submissions for Other Functionalities
|
| 189 |
+
const forms = {
|
| 190 |
+
'summarize-form': { endpoint: '/process', intent: 'summarize' },
|
| 191 |
+
'summarize-file-form': { endpoint: '/process', intent: 'summarize' },
|
| 192 |
+
'translate-text-form': { endpoint: '/process', intent: 'translate' },
|
| 193 |
+
'translate-file-form': { endpoint: '/process', intent: 'file-translate' },
|
| 194 |
+
'file-qa-form': { endpoint: '/process', intent: 'file-qa' },
|
| 195 |
+
'image-caption-form': { endpoint: '/process', intent: 'image-to-text' },
|
| 196 |
+
'visual-qa-form': { endpoint: '/process', intent: 'visual-qa' },
|
| 197 |
+
'visualize-form': { endpoint: '/process', intent: 'visualize' }
|
| 198 |
+
};
|
| 199 |
+
|
| 200 |
+
Object.keys(forms).forEach(formId => {
|
| 201 |
+
const form = document.getElementById(formId);
|
| 202 |
+
form.addEventListener('submit', async (e) => {
|
| 203 |
+
e.preventDefault();
|
| 204 |
+
const formData = new FormData();
|
| 205 |
+
const responseCard = form.nextElementSibling;
|
| 206 |
+
const submitButton = form.querySelector('button[type="submit"]');
|
| 207 |
+
responseCard.classList.add('hidden');
|
| 208 |
+
responseCard.innerHTML = '';
|
| 209 |
+
|
| 210 |
+
// Mettre le bouton en mode chargement
|
| 211 |
+
submitButton.classList.add('loading');
|
| 212 |
+
submitButton.innerHTML = '<span class="spinner"></span>Loading...';
|
| 213 |
+
submitButton.disabled = true;
|
| 214 |
+
|
| 215 |
+
try {
|
| 216 |
+
// Préparer les données du formulaire
|
| 217 |
+
if (formId === 'summarize-form') {
|
| 218 |
+
formData.append('text', form.querySelector('input[type="text"]').value);
|
| 219 |
+
} else if (formId === 'summarize-file-form') {
|
| 220 |
+
const file = form.querySelector('input[type="file"]').files[0];
|
| 221 |
+
if (!file) {
|
| 222 |
+
responseCard.innerHTML = `<p class="text-red-500">Please select a file.</p>`;
|
| 223 |
+
responseCard.classList.remove('hidden');
|
| 224 |
+
submitButton.classList.remove('loading');
|
| 225 |
+
submitButton.innerHTML = 'Send';
|
| 226 |
+
submitButton.disabled = false;
|
| 227 |
+
return;
|
| 228 |
+
}
|
| 229 |
+
formData.append('file', file);
|
| 230 |
+
} else if (formId === 'translate-text-form') {
|
| 231 |
+
const text = form.querySelector('input[type="text"]').value;
|
| 232 |
+
const lang = form.querySelector('select[name="language"]').value;
|
| 233 |
+
formData.append('text', `Translate to ${lang}: ${text}`);
|
| 234 |
+
} else if (formId === 'translate-file-form') {
|
| 235 |
+
const file = form.querySelector('input[type="file"]').files[0];
|
| 236 |
+
const lang = form.querySelector('select[name="language"]').value;
|
| 237 |
+
if (!file) {
|
| 238 |
+
responseCard.innerHTML = `<p class="text-red-500">Please select a file.</p>`;
|
| 239 |
+
responseCard.classList.remove('hidden');
|
| 240 |
+
submitButton.classList.remove('loading');
|
| 241 |
+
submitButton.innerHTML = 'Send';
|
| 242 |
+
submitButton.disabled = false;
|
| 243 |
+
return;
|
| 244 |
+
}
|
| 245 |
+
formData.append('file', file);
|
| 246 |
+
formData.append('text', `Translate to ${lang}`);
|
| 247 |
+
} else if (formId === 'file-qa-form') {
|
| 248 |
+
const file = form.querySelector('input[type="file"]').files[0];
|
| 249 |
+
const text = form.querySelector('input[type="text"]').value;
|
| 250 |
+
if (!file) {
|
| 251 |
+
responseCard.innerHTML = `<p class="text-red-500">Please select a file.</p>`;
|
| 252 |
+
responseCard.classList.remove('hidden');
|
| 253 |
+
submitButton.classList.remove('loading');
|
| 254 |
+
submitButton.innerHTML = 'Send';
|
| 255 |
+
submitButton.disabled = false;
|
| 256 |
+
return;
|
| 257 |
+
}
|
| 258 |
+
formData.append('file', file);
|
| 259 |
+
formData.append('text', text);
|
| 260 |
+
} else if (formId === 'image-caption-form') {
|
| 261 |
+
const file = form.querySelector('input[type="file"]').files[0];
|
| 262 |
+
if (!file) {
|
| 263 |
+
responseCard.innerHTML = `<p class="text-red-500">Please select an image.</p>`;
|
| 264 |
+
responseCard.classList.remove('hidden');
|
| 265 |
+
submitButton.classList.remove('loading');
|
| 266 |
+
submitButton.innerHTML = 'Send';
|
| 267 |
+
submitButton.disabled = false;
|
| 268 |
+
return;
|
| 269 |
+
}
|
| 270 |
+
formData.append('file', file);
|
| 271 |
+
} else if (formId === 'visual-qa-form') {
|
| 272 |
+
const file = form.querySelector('input[type="file"]').files[0];
|
| 273 |
+
const text = form.querySelector('input[type="text"]').value;
|
| 274 |
+
if (!file) {
|
| 275 |
+
responseCard.innerHTML = `<p class="text-red-500">Please select an image.</p>`;
|
| 276 |
+
responseCard.classList.remove('hidden');
|
| 277 |
+
submitButton.classList.remove('loading');
|
| 278 |
+
submitButton.innerHTML = 'Send';
|
| 279 |
+
submitButton.disabled = false;
|
| 280 |
+
return;
|
| 281 |
+
}
|
| 282 |
+
formData.append('file', file);
|
| 283 |
+
formData.append('text', text);
|
| 284 |
+
} else if (formId === 'visualize-form') {
|
| 285 |
+
const file = form.querySelector('input[type="file"]').files[0];
|
| 286 |
+
const visualizationType = form.querySelector('select[name="visualization-type"]').value;
|
| 287 |
+
if (!file) {
|
| 288 |
+
responseCard.innerHTML = `<p class="text-red-500">Please select a file.</p>`;
|
| 289 |
+
responseCard.classList.remove('hidden');
|
| 290 |
+
submitButton.classList.remove('loading');
|
| 291 |
+
submitButton.innerHTML = 'Send';
|
| 292 |
+
submitButton.disabled = false;
|
| 293 |
+
return;
|
| 294 |
+
}
|
| 295 |
+
formData.append('file', file);
|
| 296 |
+
formData.append('text', visualizationType);
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
// Envoyer la requête
|
| 300 |
+
const response = await fetch(forms[formId].endpoint, {
|
| 301 |
+
method: 'POST',
|
| 302 |
+
body: formData
|
| 303 |
+
});
|
| 304 |
+
const data = await response.json();
|
| 305 |
+
|
| 306 |
+
// Restaurer le bouton
|
| 307 |
+
submitButton.classList.remove('loading');
|
| 308 |
+
submitButton.innerHTML = 'Send';
|
| 309 |
+
submitButton.disabled = false;
|
| 310 |
+
|
| 311 |
+
// Afficher la réponse
|
| 312 |
+
if (response.ok) {
|
| 313 |
+
// Cas spécial pour image-caption et visual-qa
|
| 314 |
+
if (formId === 'image-caption-form' && data.additional_data && data.additional_data.image_url) {
|
| 315 |
+
responseCard.innerHTML = `
|
| 316 |
+
<img src="${data.additional_data.image_url}" alt="Uploaded Image" class="response-image">
|
| 317 |
+
<p><strong>Caption:</strong> ${data.response}</p>
|
| 318 |
+
`;
|
| 319 |
+
} else if (formId === 'visual-qa-form' && data.additional_data && data.additional_data.image_url && data.additional_data.question) {
|
| 320 |
+
responseCard.innerHTML = `
|
| 321 |
+
<img src="${data.additional_data.image_url}" alt="Uploaded Image" class="response-image">
|
| 322 |
+
<p><strong>Question:</strong> ${data.additional_data.question}</p>
|
| 323 |
+
<p><strong>Answer:</strong> ${data.response}</p>
|
| 324 |
+
`;
|
| 325 |
+
} else {
|
| 326 |
+
// Cas général pour les autres formulaires
|
| 327 |
+
responseCard.innerHTML = `<p>${data.response}</p>`;
|
| 328 |
+
if (data.additional_data) {
|
| 329 |
+
Object.entries(data.additional_data).forEach(([key, value]) => {
|
| 330 |
+
// Ignorer image_url pour les autres formulaires
|
| 331 |
+
if (key !== 'image_url') {
|
| 332 |
+
responseCard.innerHTML += `<p><strong>${key.replace('_', ' ').replace(/\b\w/g, char => char.toUpperCase())}:</strong> ${value}</p>`;
|
| 333 |
+
}
|
| 334 |
+
});
|
| 335 |
+
}
|
| 336 |
+
}
|
| 337 |
+
responseCard.classList.remove('hidden');
|
| 338 |
+
} else {
|
| 339 |
+
responseCard.innerHTML = `<p class="text-red-500">Error: ${data.detail}</p>`;
|
| 340 |
+
responseCard.classList.remove('hidden');
|
| 341 |
+
}
|
| 342 |
+
} catch (error) {
|
| 343 |
+
// Restaurer le bouton en cas d'erreur
|
| 344 |
+
submitButton.classList.remove('loading');
|
| 345 |
+
submitButton.innerHTML = 'Send';
|
| 346 |
+
submitButton.disabled = false;
|
| 347 |
+
responseCard.innerHTML = `<p class="text-red-500">Error: ${error.message}</p>`;
|
| 348 |
+
responseCard.classList.remove('hidden');
|
| 349 |
+
}
|
| 350 |
+
});
|
| 351 |
+
});
|
| 352 |
+
});
|
| 353 |
+
```
|