lyimo commited on
Commit
6003a18
·
verified ·
1 Parent(s): def7c9c

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +62 -19
static/script.js CHANGED
@@ -1,24 +1,67 @@
1
- document.getElementById('submit-audio').addEventListener('click', async () => {
2
- const audioInput = document.getElementById('audio-input');
3
- if (audioInput.files.length === 0) {
4
- alert('Please select an audio file.');
5
- return;
6
- }
 
 
7
 
8
- const formData = new FormData();
9
- formData.append('audio', audioInput.files[0]);
 
10
 
11
- try {
12
- const response = await fetch('/process_audio', {
13
- method: 'POST',
14
  body: formData
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  });
16
- const data = await response.json();
17
- document.querySelector('.response-text').textContent = data.response_text;
18
- const audioElement = document.querySelector('.response-audio');
19
- audioElement.src = data.response_audio_url;
20
- audioElement.style.display = 'block';
21
- } catch (error) {
22
- console.error('Error:', error);
 
 
 
 
 
 
 
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
+ });