Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
import numpy as np
|
4 |
-
import
|
5 |
from io import BytesIO
|
6 |
from audio_recorder_streamlit import audio_recorder
|
|
|
7 |
|
8 |
# Load Whisper model
|
9 |
@st.cache_resource
|
@@ -25,33 +26,51 @@ with col1:
|
|
25 |
text_input = st.text_area("Type your text here:", height=200)
|
26 |
|
27 |
with col2:
|
28 |
-
# Audio input
|
29 |
st.write("Record your voice:")
|
30 |
audio_bytes = audio_recorder()
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
if audio_bytes:
|
34 |
try:
|
35 |
-
# Convert bytes to
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
# Convert stereo to mono if needed
|
40 |
-
if
|
41 |
-
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
52 |
|
|
|
53 |
except Exception as e:
|
54 |
-
st.error(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Combine inputs when button is clicked
|
57 |
if st.button("Submit"):
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
import numpy as np
|
4 |
+
import torchaudio
|
5 |
from io import BytesIO
|
6 |
from audio_recorder_streamlit import audio_recorder
|
7 |
+
import torch
|
8 |
|
9 |
# Load Whisper model
|
10 |
@st.cache_resource
|
|
|
26 |
text_input = st.text_area("Type your text here:", height=200)
|
27 |
|
28 |
with col2:
|
29 |
+
# Audio input
|
30 |
st.write("Record your voice:")
|
31 |
audio_bytes = audio_recorder()
|
32 |
+
if audio_bytes:
|
33 |
+
st.audio(audio_bytes, format="audio/wav")
|
34 |
|
35 |
+
def process_audio(audio_bytes):
|
|
|
36 |
try:
|
37 |
+
# Convert bytes to numpy array
|
38 |
+
waveform, sample_rate = torchaudio.load(BytesIO(audio_bytes))
|
39 |
+
|
|
|
40 |
# Convert stereo to mono if needed
|
41 |
+
if waveform.shape[0] > 1:
|
42 |
+
waveform = torch.mean(waveform, dim=0, keepdim=True)
|
43 |
|
44 |
+
# Resample to 16kHz if needed (Whisper's expected sample rate)
|
45 |
+
if sample_rate != 16000:
|
46 |
+
resampler = torchaudio.transforms.Resample(
|
47 |
+
orig_freq=sample_rate,
|
48 |
+
new_freq=16000
|
49 |
+
)
|
50 |
+
waveform = resampler(waveform)
|
51 |
+
sample_rate = 16000
|
52 |
+
|
53 |
+
# Convert to numpy array
|
54 |
+
audio_np = waveform.numpy().squeeze()
|
55 |
|
56 |
+
return {"raw": audio_np, "sampling_rate": sample_rate}
|
57 |
except Exception as e:
|
58 |
+
st.error(f"Audio processing error: {str(e)}")
|
59 |
+
return None
|
60 |
+
|
61 |
+
# Process audio when recording is available
|
62 |
+
if audio_bytes:
|
63 |
+
audio_input = process_audio(audio_bytes)
|
64 |
+
if audio_input:
|
65 |
+
try:
|
66 |
+
# Transcribe audio
|
67 |
+
whisper = load_model()
|
68 |
+
transcribed_text = whisper(audio_input)["text"]
|
69 |
+
|
70 |
+
# Update session state
|
71 |
+
st.session_state.combined_text = f"{text_input}\n{transcribed_text}".strip()
|
72 |
+
except Exception as e:
|
73 |
+
st.error(f"Transcription error: {str(e)}")
|
74 |
|
75 |
# Combine inputs when button is clicked
|
76 |
if st.button("Submit"):
|