Spaces:
Running
Running
Commit
·
a61644d
1
Parent(s):
3c2c150
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,22 +1,34 @@
|
|
| 1 |
async function sendQuestion() {
|
| 2 |
const questionInput = document.getElementById("questionInput");
|
|
|
|
|
|
|
| 3 |
const audioResponse = document.getElementById("audioResponse");
|
| 4 |
|
| 5 |
-
const apiEndpoint =
|
| 6 |
-
"https://h8v918qrvg.execute-api.eu-central-1.amazonaws.com/Prod/audio/question";
|
| 7 |
-
|
| 8 |
if (questionInput.value) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try {
|
| 10 |
-
const response = await fetch(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
if (response.ok) {
|
| 17 |
-
const audioBase64 = await response.text();
|
| 18 |
const audioSrc = `data:audio/mpeg;base64,${audioBase64}`;
|
| 19 |
audioResponse.src = audioSrc;
|
|
|
|
| 20 |
audioResponse.play();
|
| 21 |
} else {
|
| 22 |
console.error("Server error:", response.status);
|
|
@@ -24,5 +36,18 @@ async function sendQuestion() {
|
|
| 24 |
} catch (error) {
|
| 25 |
console.error("Network error:", error);
|
| 26 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
}
|
|
|
|
| 1 |
async function sendQuestion() {
|
| 2 |
const questionInput = document.getElementById("questionInput");
|
| 3 |
+
const sendButton = document.querySelector("button");
|
| 4 |
+
const buttonContent = sendButton.querySelector(".button-content");
|
| 5 |
const audioResponse = document.getElementById("audioResponse");
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
if (questionInput.value) {
|
| 8 |
+
// Onemogućujemo samo dugme
|
| 9 |
+
sendButton.disabled = true;
|
| 10 |
+
buttonContent.innerHTML =
|
| 11 |
+
'Sending... <span class="loading-spinner"></span>';
|
| 12 |
+
|
| 13 |
+
const question = questionInput.value;
|
| 14 |
+
// Brisanje teksta iz polja za unos
|
| 15 |
+
questionInput.value = "";
|
| 16 |
+
|
| 17 |
try {
|
| 18 |
+
const response = await fetch(
|
| 19 |
+
"https://h8v918qrvg.execute-api.eu-central-1.amazonaws.com/Prod/audio/question",
|
| 20 |
+
{
|
| 21 |
+
method: "POST",
|
| 22 |
+
body: JSON.stringify({ transcript: question }),
|
| 23 |
+
headers: { "Content-Type": "application/json" },
|
| 24 |
+
}
|
| 25 |
+
);
|
| 26 |
|
| 27 |
if (response.ok) {
|
| 28 |
+
const audioBase64 = await response.text();
|
| 29 |
const audioSrc = `data:audio/mpeg;base64,${audioBase64}`;
|
| 30 |
audioResponse.src = audioSrc;
|
| 31 |
+
audioResponse.style.display = "block";
|
| 32 |
audioResponse.play();
|
| 33 |
} else {
|
| 34 |
console.error("Server error:", response.status);
|
|
|
|
| 36 |
} catch (error) {
|
| 37 |
console.error("Network error:", error);
|
| 38 |
}
|
| 39 |
+
|
| 40 |
+
// Dodavanje 'ended' događaja na audio element
|
| 41 |
+
audioResponse.onended = () => {
|
| 42 |
+
sendButton.disabled = false;
|
| 43 |
+
sendButton.innerHTML = '<span class="button-content">Send</span>';
|
| 44 |
+
};
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
function handleKeyPress(event) {
|
| 49 |
+
const sendButton = document.querySelector("button");
|
| 50 |
+
if (event.key === "Enter" && !sendButton.disabled) {
|
| 51 |
+
sendQuestion();
|
| 52 |
}
|
| 53 |
}
|