Spaces:
Running
Running
modified for mp3 files too
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ import torchaudio
|
|
5 |
import torch
|
6 |
import os
|
7 |
import soundfile as sf
|
|
|
|
|
|
|
8 |
|
9 |
model_name = "mispeech/ced-tiny"
|
10 |
feature_extractor = CedFeatureExtractor.from_pretrained(model_name)
|
@@ -20,9 +23,17 @@ if audio_file is not None:
|
|
20 |
st.write(f"Uploaded file: {audio_file.name}")
|
21 |
|
22 |
try:
|
|
|
23 |
temp_file_path = "temp.wav"
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
try:
|
28 |
audio, sampling_rate = torchaudio.load(temp_file_path)
|
@@ -51,4 +62,4 @@ if audio_file is not None:
|
|
51 |
except Exception as e:
|
52 |
st.error(f"An error occurred: {e}")
|
53 |
else:
|
54 |
-
st.info("Please upload
|
|
|
5 |
import torch
|
6 |
import os
|
7 |
import soundfile as sf
|
8 |
+
# New imports for handling MP3 and M4A files
|
9 |
+
from pydub import AudioSegment
|
10 |
+
import io
|
11 |
|
12 |
model_name = "mispeech/ced-tiny"
|
13 |
feature_extractor = CedFeatureExtractor.from_pretrained(model_name)
|
|
|
23 |
st.write(f"Uploaded file: {audio_file.name}")
|
24 |
|
25 |
try:
|
26 |
+
# New code block for handling different audio formats
|
27 |
temp_file_path = "temp.wav"
|
28 |
+
if audio_file.name.lower().endswith(('.mp3', '.m4a')):
|
29 |
+
# Convert MP3/M4A to WAV
|
30 |
+
audio_bytes = audio_file.read()
|
31 |
+
audio = AudioSegment.from_file(io.BytesIO(audio_bytes), format=audio_file.name.split('.')[-1])
|
32 |
+
audio.export(temp_file_path, format="wav")
|
33 |
+
else:
|
34 |
+
# For WAV files, write directly
|
35 |
+
with open(temp_file_path, "wb") as f:
|
36 |
+
f.write(audio_file.read())
|
37 |
|
38 |
try:
|
39 |
audio, sampling_rate = torchaudio.load(temp_file_path)
|
|
|
62 |
except Exception as e:
|
63 |
st.error(f"An error occurred: {e}")
|
64 |
else:
|
65 |
+
st.info("Please upload an audio file (WAV, MP3, or M4A) to continue.")
|