Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,61 @@
|
|
1 |
import streamlit as st
|
2 |
-
import torch
|
3 |
-
import tempfile
|
4 |
import os
|
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 |
-
st.
|
63 |
-
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import os
|
3 |
+
import base64
|
4 |
+
import uuid
|
5 |
+
|
6 |
+
st.title("Record Audio in Browser")
|
7 |
+
|
8 |
+
# JavaScript to record audio
|
9 |
+
audio_recorder_js = """
|
10 |
+
<script>
|
11 |
+
let mediaRecorder;
|
12 |
+
let audioChunks = [];
|
13 |
+
|
14 |
+
function startRecording() {
|
15 |
+
navigator.mediaDevices.getUserMedia({ audio: true })
|
16 |
+
.then(stream => {
|
17 |
+
mediaRecorder = new MediaRecorder(stream);
|
18 |
+
mediaRecorder.ondataavailable = event => {
|
19 |
+
audioChunks.push(event.data);
|
20 |
+
};
|
21 |
+
mediaRecorder.onstop = () => {
|
22 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
23 |
+
const reader = new FileReader();
|
24 |
+
reader.readAsDataURL(audioBlob);
|
25 |
+
reader.onloadend = () => {
|
26 |
+
const base64AudioMessage = reader.result.split(',')[1];
|
27 |
+
fetch('/save_audio', {
|
28 |
+
method: 'POST',
|
29 |
+
body: JSON.stringify({ audio: base64AudioMessage }),
|
30 |
+
headers: { 'Content-Type': 'application/json' }
|
31 |
+
}).then(response => response.json()).then(data => {
|
32 |
+
console.log(data);
|
33 |
+
});
|
34 |
+
};
|
35 |
+
};
|
36 |
+
mediaRecorder.start();
|
37 |
+
});
|
38 |
+
}
|
39 |
+
|
40 |
+
function stopRecording() {
|
41 |
+
mediaRecorder.stop();
|
42 |
+
}
|
43 |
+
</script>
|
44 |
+
|
45 |
+
<button onclick="startRecording()">Start Recording</button>
|
46 |
+
<button onclick="stopRecording()">Stop Recording</button>
|
47 |
+
"""
|
48 |
+
|
49 |
+
st.components.v1.html(audio_recorder_js)
|
50 |
+
|
51 |
+
# Backend to save audio
|
52 |
+
if "audio_data" not in st.session_state:
|
53 |
+
st.session_state["audio_data"] = None
|
54 |
+
|
55 |
+
if st.session_state["audio_data"]:
|
56 |
+
audio_bytes = base64.b64decode(st.session_state["audio_data"])
|
57 |
+
file_name = f"recording_{uuid.uuid4()}.wav"
|
58 |
+
with open(file_name, "wb") as f:
|
59 |
+
f.write(audio_bytes)
|
60 |
+
st.audio(file_name, format="audio/wav")
|
61 |
+
st.success(f"Audio saved as {file_name}")
|