Spaces:
Paused
Paused
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,66 +1,69 @@
|
|
| 1 |
-
const form = document.
|
| 2 |
-
const input =
|
| 3 |
-
const chatbox = document.getElementById(
|
| 4 |
-
const avatar = document.getElementById(
|
|
|
|
| 5 |
|
| 6 |
-
function
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
}
|
| 13 |
|
| 14 |
-
function streamText(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
chatbox.appendChild(aiBubble);
|
| 39 |
-
chatbox.scrollTop = chatbox.scrollHeight;
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
const res = await fetch("/api/chat", {
|
| 44 |
-
method: "POST",
|
| 45 |
-
headers: { "Content-Type": "application/json" },
|
| 46 |
-
body: JSON.stringify({ message })
|
| 47 |
-
});
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
// Audio playback if available
|
| 55 |
-
if (data.audio) {
|
| 56 |
-
const audio = new Audio(data.audio);
|
| 57 |
-
audio.play();
|
| 58 |
-
}
|
| 59 |
-
} else {
|
| 60 |
-
aiBubble.textContent = "⚠️ SHODAN is silent. Something went wrong.";
|
| 61 |
-
}
|
| 62 |
-
} catch (err) {
|
| 63 |
-
aiBubble.textContent = "❌ ERROR contacting SHODAN backend.";
|
| 64 |
-
console.error(err);
|
| 65 |
-
}
|
| 66 |
-
});
|
|
|
|
| 1 |
+
const form = document.getElementById('chat-form');
|
| 2 |
+
const input = form.querySelector('input');
|
| 3 |
+
const chatbox = document.getElementById('chatbox');
|
| 4 |
+
const avatar = document.getElementById('avatar');
|
| 5 |
+
const overlay = document.getElementById('overlay');
|
| 6 |
|
| 7 |
+
function addBubble(text, sender = 'user') {
|
| 8 |
+
const bubble = document.createElement('div');
|
| 9 |
+
bubble.className = 'bubble ' + sender;
|
| 10 |
+
bubble.innerText = text;
|
| 11 |
+
chatbox.appendChild(bubble);
|
| 12 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
| 13 |
}
|
| 14 |
|
| 15 |
+
function streamText(text, onDone) {
|
| 16 |
+
const bubble = document.createElement('div');
|
| 17 |
+
bubble.className = 'bubble ai';
|
| 18 |
+
chatbox.appendChild(bubble);
|
| 19 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
| 20 |
+
|
| 21 |
+
let i = 0;
|
| 22 |
+
function stream() {
|
| 23 |
+
if (i < text.length) {
|
| 24 |
+
bubble.innerText += text[i++];
|
| 25 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
| 26 |
+
setTimeout(stream, 20);
|
| 27 |
+
} else {
|
| 28 |
+
onDone && onDone();
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
stream();
|
| 32 |
}
|
| 33 |
|
| 34 |
+
function playAudio(url) {
|
| 35 |
+
avatar.classList.add('speaking');
|
| 36 |
+
const audio = new Audio(url);
|
| 37 |
+
audio.play();
|
| 38 |
|
| 39 |
+
audio.onended = () => {
|
| 40 |
+
avatar.classList.remove('speaking');
|
| 41 |
+
fetch(`/delete_audio?path=${encodeURIComponent(url)}`);
|
| 42 |
+
};
|
| 43 |
+
}
|
| 44 |
|
| 45 |
+
form.onsubmit = async (e) => {
|
| 46 |
+
e.preventDefault();
|
| 47 |
+
const msg = input.value.trim();
|
| 48 |
+
if (!msg) return;
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
addBubble(msg, 'user');
|
| 51 |
+
input.value = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
try {
|
| 54 |
+
const response = await fetch('/ask', {
|
| 55 |
+
method: 'POST',
|
| 56 |
+
headers: { 'Content-Type': 'application/json' },
|
| 57 |
+
body: JSON.stringify({ message: msg })
|
| 58 |
+
});
|
| 59 |
+
const { text, audio_url } = await response.json();
|
| 60 |
|
| 61 |
+
streamText(text, () => {
|
| 62 |
+
if (audio_url) playAudio(audio_url);
|
| 63 |
+
});
|
| 64 |
+
} catch (err) {
|
| 65 |
+
streamText("❌ SHODAN encountered an error.", null);
|
| 66 |
+
console.error(err);
|
| 67 |
+
}
|
| 68 |
+
};
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|