Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +62 -19
static/script.js
CHANGED
@@ -1,24 +1,67 @@
|
|
1 |
-
document.
|
2 |
-
const
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
method: 'POST',
|
14 |
body: formData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
});
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
-
});
|
|
|
1 |
+
document.addEventListener("DOMContentLoaded", function() {
|
2 |
+
const recordBtn = document.getElementById("record-btn");
|
3 |
+
const audioPlayer = document.getElementById("audio-player");
|
4 |
+
const chatContainer = document.getElementById("chat-container");
|
5 |
+
|
6 |
+
recordBtn.addEventListener("click", function() {
|
7 |
+
// Implement audio recording logic here
|
8 |
+
// You can use the MediaRecorder API or a library like RecordRTC
|
9 |
|
10 |
+
// Once the audio is recorded, send it to the server for transcription
|
11 |
+
const formData = new FormData();
|
12 |
+
formData.append("audio", audioBlob);
|
13 |
|
14 |
+
fetch("/transcribe", {
|
15 |
+
method: "POST",
|
|
|
16 |
body: formData
|
17 |
+
})
|
18 |
+
.then(response => response.json())
|
19 |
+
.then(data => {
|
20 |
+
const transcribedText = data.text;
|
21 |
+
displayUserMessage(transcribedText);
|
22 |
+
|
23 |
+
// Send the transcribed text to the server for generating a response
|
24 |
+
const responseFormData = new FormData();
|
25 |
+
responseFormData.append("text", transcribedText);
|
26 |
+
|
27 |
+
fetch("/generate_response", {
|
28 |
+
method: "POST",
|
29 |
+
body: responseFormData
|
30 |
+
})
|
31 |
+
.then(response => response.json())
|
32 |
+
.then(data => {
|
33 |
+
const responseText = data.response;
|
34 |
+
displayAssistantMessage(responseText);
|
35 |
+
|
36 |
+
// Send the response text to the server for audio inference
|
37 |
+
const inferenceFormData = new FormData();
|
38 |
+
inferenceFormData.append("text", responseText);
|
39 |
+
|
40 |
+
fetch("/inference", {
|
41 |
+
method: "POST",
|
42 |
+
body: inferenceFormData
|
43 |
+
})
|
44 |
+
.then(response => response.json())
|
45 |
+
.then(data => {
|
46 |
+
const audioUrl = data.audio_url;
|
47 |
+
audioPlayer.src = audioUrl;
|
48 |
+
audioPlayer.style.display = "block";
|
49 |
+
});
|
50 |
+
});
|
51 |
});
|
52 |
+
});
|
53 |
+
|
54 |
+
function displayUserMessage(text) {
|
55 |
+
const messageElement = document.createElement("div");
|
56 |
+
messageElement.classList.add("user-message");
|
57 |
+
messageElement.textContent = text;
|
58 |
+
chatContainer.appendChild(messageElement);
|
59 |
+
}
|
60 |
+
|
61 |
+
function displayAssistantMessage(text) {
|
62 |
+
const messageElement = document.createElement("div");
|
63 |
+
messageElement.classList.add("assistant-message");
|
64 |
+
messageElement.textContent = text;
|
65 |
+
chatContainer.appendChild(messageElement);
|
66 |
}
|
67 |
+
});
|