Peiiiiiiiiru commited on
Commit
5b8529f
1 Parent(s): 8bd1d5c

Create static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +45 -0
static/script.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ let mediaRecorder;
2
+ let audioChunks = [];
3
+
4
+ function startRecording() {
5
+ navigator.mediaDevices.getUserMedia({ audio: true })
6
+ .then(stream => {
7
+ mediaRecorder = new MediaRecorder(stream);
8
+ mediaRecorder.start();
9
+
10
+ mediaRecorder.ondataavailable = event => {
11
+ audioChunks.push(event.data);
12
+ };
13
+
14
+ mediaRecorder.onstop = () => {
15
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
16
+ document.getElementById("audioPlayback").src = URL.createObjectURL(audioBlob);
17
+ audioChunks = [];
18
+ };
19
+ });
20
+ }
21
+
22
+ function stopRecording() {
23
+ mediaRecorder.stop();
24
+ }
25
+
26
+ function uploadAudio() {
27
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
28
+ const formData = new FormData();
29
+ formData.append("file", audioBlob, "recording.wav");
30
+
31
+ fetch("/upload_audio", {
32
+ method: "POST",
33
+ body: formData
34
+ })
35
+ .then(response => response.json())
36
+ .then(data => {
37
+ document.getElementById("result").innerHTML = `
38
+ <p><strong>Transcription:</strong> ${data.transcription}</p>
39
+ <p><strong>Emotions:</strong> ${JSON.stringify(data.emotions)}</p>
40
+ <p><strong>Advice:</strong> ${data.advice_text}</p>
41
+ <audio controls src="${data.advice_audio}"></audio>
42
+ `;
43
+ })
44
+ .catch(err => console.error(err));
45
+ }