Spaces:
Running
Running
Create static/script.js
Browse files- static/script.js +62 -0
static/script.js
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function sendMessage() {
|
2 |
+
const inputField = document.getElementById('userInput');
|
3 |
+
const message = inputField.value.trim();
|
4 |
+
const imageUpload = document.getElementById('imageUpload').files[0];
|
5 |
+
const complexity = document.getElementById('complexity').value;
|
6 |
+
const model = document.getElementById('model').value; // Get the selected model
|
7 |
+
const chatbox = document.getElementById('chatbox');
|
8 |
+
|
9 |
+
if (message === "" && !imageUpload) return;
|
10 |
+
|
11 |
+
// Append user message
|
12 |
+
const userMessageDiv = document.createElement('div');
|
13 |
+
userMessageDiv.classList.add('user-message');
|
14 |
+
const userText = document.createElement('span');
|
15 |
+
userText.textContent = message;
|
16 |
+
userMessageDiv.appendChild(userText);
|
17 |
+
|
18 |
+
if (imageUpload) {
|
19 |
+
const imagePreview = document.createElement('img');
|
20 |
+
imagePreview.src = URL.createObjectURL(imageUpload);
|
21 |
+
imagePreview.style.maxWidth = '100px';
|
22 |
+
imagePreview.style.maxHeight = '100px';
|
23 |
+
imagePreview.style.marginTop = '10px';
|
24 |
+
userMessageDiv.appendChild(imagePreview);
|
25 |
+
}
|
26 |
+
|
27 |
+
chatbox.appendChild(userMessageDiv);
|
28 |
+
|
29 |
+
// Clear input field
|
30 |
+
inputField.value = '';
|
31 |
+
document.getElementById('imageUpload').value = '';
|
32 |
+
|
33 |
+
// Scroll chatbox to the bottom
|
34 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
35 |
+
|
36 |
+
// Send request to Flask backend
|
37 |
+
const formData = new FormData();
|
38 |
+
formData.append('image', imageUpload);
|
39 |
+
formData.append('question', message);
|
40 |
+
formData.append('complexity', complexity);
|
41 |
+
formData.append('model', model); // Append selected model
|
42 |
+
|
43 |
+
fetch('/ask', {
|
44 |
+
method: 'POST',
|
45 |
+
body: formData
|
46 |
+
})
|
47 |
+
.then(response => response.json())
|
48 |
+
.then(data => {
|
49 |
+
const botMessageDiv = document.createElement('div');
|
50 |
+
botMessageDiv.classList.add('bot-message');
|
51 |
+
const botText = document.createElement('span');
|
52 |
+
botText.textContent = data.answer;
|
53 |
+
botMessageDiv.appendChild(botText);
|
54 |
+
chatbox.appendChild(botMessageDiv);
|
55 |
+
|
56 |
+
// Scroll chatbox to the bottom
|
57 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
58 |
+
})
|
59 |
+
.catch(error => {
|
60 |
+
console.error('Error:', error);
|
61 |
+
});
|
62 |
+
}
|