Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,91 @@
|
|
1 |
-
import os
|
2 |
import streamlit as st
|
3 |
-
import
|
4 |
-
|
5 |
-
import sounddevice as sd
|
6 |
import numpy as np
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
st.
|
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 |
-
# Your main logic here
|
95 |
-
pass
|
96 |
-
except Exception as e:
|
97 |
-
st.error(f"An error occurred: {e}")
|
98 |
-
|
99 |
-
# Footer
|
100 |
-
st.sidebar.markdown("---")
|
101 |
-
st.sidebar.markdown("Created by Your Name")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
3 |
+
import torch
|
|
|
4 |
import numpy as np
|
5 |
+
import tempfile
|
6 |
+
import wave
|
7 |
+
|
8 |
+
# Load Wav2Vec2 model and processor
|
9 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
10 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
11 |
+
|
12 |
+
# Streamlit App
|
13 |
+
st.title("Phonics/Personalized Reading App")
|
14 |
+
st.write("Record your audio and we will transcribe it.")
|
15 |
+
|
16 |
+
# Audio recording using HTML5
|
17 |
+
record_button = st.button("Record Audio")
|
18 |
+
|
19 |
+
if record_button:
|
20 |
+
st.markdown("""
|
21 |
+
<audio id="audio" controls></audio>
|
22 |
+
<button id="start" onclick="startRecording()">Start Recording</button>
|
23 |
+
<button id="stop" onclick="stopRecording()" disabled>Stop Recording</button>
|
24 |
+
<script>
|
25 |
+
let mediaRecorder;
|
26 |
+
let audioChunks = [];
|
27 |
+
|
28 |
+
async function startRecording() {
|
29 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
30 |
+
mediaRecorder = new MediaRecorder(stream);
|
31 |
+
mediaRecorder.ondataavailable = event => {
|
32 |
+
audioChunks.push(event.data);
|
33 |
+
};
|
34 |
+
|
35 |
+
mediaRecorder.onstop = () => {
|
36 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
37 |
+
const audioUrl = URL.createObjectURL(audioBlob);
|
38 |
+
const audioElement = document.getElementById('audio');
|
39 |
+
audioElement.src = audioUrl;
|
40 |
+
|
41 |
+
// Prepare to send audio to server
|
42 |
+
const formData = new FormData();
|
43 |
+
formData.append('audio', audioBlob, 'recording.wav');
|
44 |
+
|
45 |
+
fetch('/upload', {
|
46 |
+
method: 'POST',
|
47 |
+
body: formData
|
48 |
+
}).then(response => response.json()).then(data => {
|
49 |
+
st.session_state.transcription = data.transcription;
|
50 |
+
st.experimental_rerun(); // Refresh the app to show the transcription
|
51 |
+
});
|
52 |
+
};
|
53 |
+
|
54 |
+
mediaRecorder.start();
|
55 |
+
document.getElementById('start').disabled = true;
|
56 |
+
document.getElementById('stop').disabled = false;
|
57 |
+
}
|
58 |
+
|
59 |
+
function stopRecording() {
|
60 |
+
mediaRecorder.stop();
|
61 |
+
document.getElementById('start').disabled = false;
|
62 |
+
document.getElementById('stop').disabled = true;
|
63 |
+
}
|
64 |
+
</script>
|
65 |
+
""", unsafe_allow_html=True)
|
66 |
+
|
67 |
+
# Display the transcription
|
68 |
+
if 'transcription' in st.session_state:
|
69 |
+
st.write("Transcription:")
|
70 |
+
st.write(st.session_state.transcription)
|
71 |
+
|
72 |
+
# Handle audio file upload
|
73 |
+
uploaded_file = st.file_uploader("Or upload your audio file", type=["wav", "mp3"])
|
74 |
+
|
75 |
+
if uploaded_file is not None:
|
76 |
+
# Save uploaded audio file to a temporary file
|
77 |
+
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
|
78 |
+
temp_file.write(uploaded_file.read())
|
79 |
+
temp_file.flush()
|
80 |
+
|
81 |
+
# Process the audio file for transcription
|
82 |
+
audio_input = processor(temp_file.name, sampling_rate=16000, return_tensors="pt", padding=True)
|
83 |
+
|
84 |
+
with torch.no_grad():
|
85 |
+
logits = model(audio_input.input_values).logits
|
86 |
+
|
87 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
88 |
+
transcription = processor.batch_decode(predicted_ids)
|
89 |
+
|
90 |
+
st.session_state.transcription = transcription[0] # Store transcription
|
91 |
+
st.experimental_rerun() # Refresh the app to show the transcription
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|