-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
153 lines (119 loc) · 4.08 KB
/
input.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// ------------------------------------------------------------------------------------------------------------------------
//
// Description: OpenAI Whisper API
// Author: PIGMIL
// Website: https://pigmil.com
//
// ------------------------------------------------------------------------------------------------------------------------
let chunks = [];
let mediaRecorder;
let startButton = document.getElementById("start");
let stopButton = document.getElementById("stop");
let audioForm = document.getElementById("audio_form");
// Initial state
stopButton.style.backgroundColor = "#2b2b3a"; // Gray color
stopButton.disabled = true;
function getRequest(){
let blob = new Blob(chunks, { type: "audio/webm" });
chunks = [];
let audioURL = URL.createObjectURL(blob);
document.getElementById("audio").src = audioURL;
// Create a new FormData object.
var formData = new FormData();
// Create a blob file object from the blob.
var file = new File([blob], Math.floor(1000 + Math.random() * 9000)+".webm", {
type: "audio/webm",
});
// Append the audio file to the form data.
formData.append("audio", file);
console.log("Sending audio file to server...");
// Send the audio file to your server.
fetch("whisper_send_data.php", {
method: "POST",
body: formData,
})
.then((response) => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text(); // Return the response as text
})
.then((data) => {
// Process the response here
let whisper_received_data = JSON.parse(data);
HandleRequest(whisper_received_data.text);
})
.catch(function (error) {
console.error("Error sending audio file to server:", error);
return false;
});
return false;
}
function HandleRequest(prompt){
console.log("Sending transcription to server...");
console.log(prompt);
document.getElementById("whisper_response_display_area").textContent =
prompt;
return false;
}
audioForm.addEventListener("submit", function(e) {
e.preventDefault();
const form = e.currentTarget;
const formData = new FormData(form);
var fileUpload = document.getElementById('fileUpload');
var fileUploading = document.getElementById('fileUploading');
fileUpload.style.display = "none";
fileUploading.style.display = "block";
console.log("Sending audio file to server...");
// Send the audio file to your server.
fetch("whisper_send_data.php", {
method: "POST",
body: formData,
})
.then((response) => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.text(); // Return the response as text
})
.then((data) => {
// Process the response here
let whisper_received_data = JSON.parse(data);
HandleRequest(whisper_received_data.text);
fileUpload.style.display = "block";
fileUploading.style.display = "none";
})
.catch(function (error) {
console.error("Error sending audio file to server:", error);
fileUpload.style.display = "block";
fileUploading.style.display = "none";
return false;
});
});
startButton.addEventListener("click", () => {
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
mediaRecorder.start();
startButton.style.backgroundColor = "#2b2b3a"; // Gray color
startButton.disabled = true;
stopButton.style.backgroundColor = "#1ed2f4"; // Main color
stopButton.disabled = false;
mediaRecorder.ondataavailable = (e) => {
chunks.push(e.data);
};
mediaRecorder.onstop = (e) => {
getRequest();
};
console.log("Recording started...");
});
});
stopButton.addEventListener("click", () => {
if (mediaRecorder) {
mediaRecorder.stop();
}
console.log("Recording stopped...");
stopButton.style.backgroundColor = "#2b2b3a"; // Gray color
stopButton.disabled = true;
startButton.style.backgroundColor = "#1ed2f4"; // Main color
startButton.disabled = false;
});