Spaces:
Sleeping
Sleeping
File size: 3,495 Bytes
6003a18 293d776 6003a18 293d776 c6b6d05 293d776 6003a18 293d776 c6b6d05 6003a18 e2d284e 6003a18 e2d284e 293d776 6003a18 e2d284e 6003a18 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
document.addEventListener("DOMContentLoaded", function() {
const recordBtn = document.getElementById("record-btn");
const audioPlayer = document.getElementById("audio-player");
const chatContainer = document.getElementById("chat-container");
let mediaRecorder;
let chunks = [];
recordBtn.addEventListener("click", function() {
if (mediaRecorder && mediaRecorder.state === "recording") {
mediaRecorder.stop();
recordBtn.textContent = "Bonyeza kitufe cha kurekodi na uliza swali lako";
} else {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function(stream) {
mediaRecorder = new MediaRecorder(stream);
chunks = [];
mediaRecorder.addEventListener("dataavailable", function(e) {
chunks.push(e.data);
});
mediaRecorder.addEventListener("stop", function() {
const audioBlob = new Blob(chunks, { type: "audio/wav" });
sendAudioToServer(audioBlob);
});
mediaRecorder.start();
recordBtn.textContent = "Acha kurekodi";
})
.catch(function(err) {
console.error("Error accessing microphone:", err);
});
}
});
function sendAudioToServer(audioBlob) {
const formData = new FormData();
formData.append("audio", audioBlob, "recording.wav");
fetch("/transcribe", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
const transcribedText = data.text;
displayUserMessage(transcribedText);
// Send the transcribed text to the server for generating a response
const responseFormData = new FormData();
responseFormData.append("text", transcribedText);
fetch("/generate_response", {
method: "POST",
body: responseFormData
})
.then(response => response.json())
.then(data => {
const responseText = data.response;
displayAssistantMessage(responseText);
// Send the response text to the server for audio inference
const inferenceFormData = new FormData();
inferenceFormData.append("text", responseText);
fetch("/inference", {
method: "POST",
body: inferenceFormData
})
.then(response => response.json())
.then(data => {
const audioUrl = data.audio_url;
audioPlayer.src = audioUrl;
audioPlayer.style.display = "block";
});
});
});
}
function displayUserMessage(text) {
const messageElement = document.createElement("div");
messageElement.classList.add("user-message");
messageElement.textContent = text;
chatContainer.appendChild(messageElement);
}
function displayAssistantMessage(text) {
const messageElement = document.createElement("div");
messageElement.classList.add("assistant-message");
messageElement.textContent = text;
chatContainer.appendChild(messageElement);
}
}); |