Spaces:
Running
Running
File size: 2,178 Bytes
87b8c5c |
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 |
function sendMessage() {
const inputField = document.getElementById('userInput');
const message = inputField.value.trim();
const imageUpload = document.getElementById('imageUpload').files[0];
const complexity = document.getElementById('complexity').value;
const model = document.getElementById('model').value; // Get the selected model
const chatbox = document.getElementById('chatbox');
if (message === "" && !imageUpload) return;
// Append user message
const userMessageDiv = document.createElement('div');
userMessageDiv.classList.add('user-message');
const userText = document.createElement('span');
userText.textContent = message;
userMessageDiv.appendChild(userText);
if (imageUpload) {
const imagePreview = document.createElement('img');
imagePreview.src = URL.createObjectURL(imageUpload);
imagePreview.style.maxWidth = '100px';
imagePreview.style.maxHeight = '100px';
imagePreview.style.marginTop = '10px';
userMessageDiv.appendChild(imagePreview);
}
chatbox.appendChild(userMessageDiv);
// Clear input field
inputField.value = '';
document.getElementById('imageUpload').value = '';
// Scroll chatbox to the bottom
chatbox.scrollTop = chatbox.scrollHeight;
// Send request to Flask backend
const formData = new FormData();
formData.append('image', imageUpload);
formData.append('question', message);
formData.append('complexity', complexity);
formData.append('model', model); // Append selected model
fetch('/ask', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
const botMessageDiv = document.createElement('div');
botMessageDiv.classList.add('bot-message');
const botText = document.createElement('span');
botText.textContent = data.answer;
botMessageDiv.appendChild(botText);
chatbox.appendChild(botMessageDiv);
// Scroll chatbox to the bottom
chatbox.scrollTop = chatbox.scrollHeight;
})
.catch(error => {
console.error('Error:', error);
});
}
|