Spaces:
Running
Running
Download script.js from LabSoft/text-to-speech-demo: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/spaces/LabSoft/text-to-speech-demo/resolve/main/script.js
- Command line
-
hf download hf://spaces/LabSoft/text-to-speech-demo/script.js
-
curl -L -o script.js https://huggingface.co/spaces/LabSoft/text-to-speech-demo/resolve/main/script.js
1.68 kB
| async function sendQuestion() { | |
| const questionInput = document.getElementById("questionInput"); | |
| const sendButton = document.querySelector("button"); | |
| const buttonContent = sendButton.querySelector(".button-content"); | |
| const audioResponse = document.getElementById("audioResponse"); | |
| if (questionInput.value) { | |
| // Onemogućujemo samo dugme | |
| sendButton.disabled = true; | |
| buttonContent.innerHTML = | |
| 'Sending... <span class="loading-spinner"></span>'; | |
| const question = questionInput.value; | |
| // Brisanje teksta iz polja za unos | |
| questionInput.value = ""; | |
| try { | |
| const response = await fetch( | |
| "https://wnuy6xzt3e.execute-api.eu-central-1.amazonaws.com/Prod/audio/question", | |
| { | |
| method: "POST", | |
| body: JSON.stringify({ transcript: question }), | |
| headers: { "Content-Type": "application/json" }, | |
| } | |
| ); | |
| if (response.ok) { | |
| const audioBase64 = await response.text(); | |
| const audioSrc = `data:audio/mpeg;base64,${audioBase64}`; | |
| audioResponse.src = audioSrc; | |
| audioResponse.style.display = "block"; | |
| audioResponse.play(); | |
| } else { | |
| console.error("Server error:", response.status); | |
| } | |
| } catch (error) { | |
| console.error("Network error:", error); | |
| } | |
| // Dodavanje 'ended' događaja na audio element | |
| audioResponse.onended = () => { | |
| sendButton.disabled = false; | |
| sendButton.innerHTML = '<span class="button-content">Send</span>'; | |
| }; | |
| } | |
| } | |
| function handleKeyPress(event) { | |
| const sendButton = document.querySelector("button"); | |
| if (event.key === "Enter" && !sendButton.disabled) { | |
| sendQuestion(); | |
| } | |
| } | |