Copy Boss commited on
Commit
8f2192a
·
1 Parent(s): 7f7fe66
Files changed (2) hide show
  1. app.py +7 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -3,6 +3,7 @@ import stable_whisper
3
  import json
4
  import torch
5
  import soundfile as sf
 
6
  from io import BytesIO
7
 
8
  # Create a dropdown to select the model
@@ -20,9 +21,13 @@ audiofile = st.file_uploader("Upload an audio file", type=["mp3", "wav"])
20
  if st.button('Transcribe'):
21
  if audiofile is not None:
22
  # Read the audio file into a numpy array
23
- audio_data, _ = sf.read(BytesIO(audiofile.read()))
 
 
 
 
24
  # Convert the audio data to float
25
- audio_data = torch.from_numpy(audio_data)
26
  # Transcribe the audio file
27
  result = model.transcribe(audio_data)
28
  # Convert the result to JSON and display it
 
3
  import json
4
  import torch
5
  import soundfile as sf
6
+ import librosa
7
  from io import BytesIO
8
 
9
  # Create a dropdown to select the model
 
21
  if st.button('Transcribe'):
22
  if audiofile is not None:
23
  # Read the audio file into a numpy array
24
+ audio_data, sample_rate = sf.read(BytesIO(audiofile.read()))
25
+ # Resample the audio data if necessary
26
+ expected_sample_rate = 16000 # replace with the sample rate expected by the model
27
+ if sample_rate != expected_sample_rate:
28
+ audio_data = librosa.resample(audio_data, sample_rate, expected_sample_rate)
29
  # Convert the audio data to float
30
+ audio_data = torch.from_numpy(audio_data).float()
31
  # Transcribe the audio file
32
  result = model.transcribe(audio_data)
33
  # Convert the result to JSON and display it
requirements.txt CHANGED
@@ -2,4 +2,5 @@ streamlit
2
  stable-ts
3
  torch
4
  numpy
5
- soundfile
 
 
2
  stable-ts
3
  torch
4
  numpy
5
+ soundfile
6
+ librosa