testing app
Browse files
app.py
CHANGED
@@ -1,171 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
from datetime import datetime
|
4 |
-
import traceback
|
5 |
-
import tempfile
|
6 |
-
import os
|
7 |
-
|
8 |
-
# Optional imports with fallbacks
|
9 |
-
try:
|
10 |
-
import sounddevice as sd
|
11 |
-
SD_AVAILABLE = True
|
12 |
-
except ImportError:
|
13 |
-
SD_AVAILABLE = False
|
14 |
-
|
15 |
-
try:
|
16 |
-
import librosa
|
17 |
-
LIBROSA_AVAILABLE = True
|
18 |
-
except ImportError:
|
19 |
-
LIBROSA_AVAILABLE = False
|
20 |
-
|
21 |
-
try:
|
22 |
-
import ffmpeg
|
23 |
-
FFMPEG_AVAILABLE = True
|
24 |
-
except ImportError:
|
25 |
-
FFMPEG_AVAILABLE = False
|
26 |
|
27 |
class AudioProcessor:
|
28 |
def __init__(self):
|
29 |
self.sample_rate = 16000
|
30 |
-
self.available_backends = self.detect_audio_backends()
|
31 |
-
print(f"Available audio backends: {self.available_backends}")
|
32 |
-
|
33 |
-
def detect_audio_backends(self):
|
34 |
-
backends = []
|
35 |
-
|
36 |
-
if FFMPEG_AVAILABLE:
|
37 |
-
backends.append('ffmpeg')
|
38 |
-
|
39 |
-
if SD_AVAILABLE:
|
40 |
-
try:
|
41 |
-
sd.check_input_settings()
|
42 |
-
backends.append('sounddevice')
|
43 |
-
except:
|
44 |
-
pass
|
45 |
-
|
46 |
-
if LIBROSA_AVAILABLE:
|
47 |
-
backends.append('librosa')
|
48 |
-
|
49 |
-
return backends or ['numpy_fallback']
|
50 |
|
51 |
def process_audio(self, audio_input):
|
52 |
-
#
|
53 |
-
if
|
54 |
-
return
|
55 |
-
|
56 |
-
for backend in self.available_backends:
|
57 |
-
try:
|
58 |
-
print(f"Trying backend: {backend}")
|
59 |
-
if backend == 'ffmpeg' and FFMPEG_AVAILABLE:
|
60 |
-
return self._process_with_ffmpeg(audio_input)
|
61 |
-
elif backend == 'sounddevice' and SD_AVAILABLE:
|
62 |
-
return self._process_with_sounddevice(audio_input)
|
63 |
-
elif backend == 'librosa' and LIBROSA_AVAILABLE:
|
64 |
-
return self._process_with_librosa(audio_input)
|
65 |
-
else:
|
66 |
-
return self._process_fallback(audio_input)
|
67 |
-
except Exception as e:
|
68 |
-
print(f"Failed with {backend}: {str(e)}")
|
69 |
-
continue
|
70 |
-
|
71 |
-
return self._process_fallback(audio_input)
|
72 |
-
|
73 |
-
def _process_with_ffmpeg(self, audio_input):
|
74 |
-
try:
|
75 |
-
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
|
76 |
-
if isinstance(audio_input, bytes):
|
77 |
-
tmp.write(audio_input)
|
78 |
-
elif isinstance(audio_input, str):
|
79 |
-
with open(audio_input, 'rb') as f:
|
80 |
-
tmp.write(f.read())
|
81 |
-
tmp.flush()
|
82 |
-
|
83 |
-
out, _ = (
|
84 |
-
ffmpeg.input(tmp.name)
|
85 |
-
.output('pipe:', format='f32le', ac=1, ar=self.sample_rate)
|
86 |
-
.run(capture_stdout=True, quiet=True)
|
87 |
-
)
|
88 |
-
audio_data = np.frombuffer(out, dtype=np.float32)
|
89 |
-
return (audio_data, self.sample_rate)
|
90 |
-
finally:
|
91 |
-
try:
|
92 |
-
os.unlink(tmp.name)
|
93 |
-
except:
|
94 |
-
pass
|
95 |
-
|
96 |
-
def _process_with_sounddevice(self, audio_input):
|
97 |
-
if isinstance(audio_input, tuple):
|
98 |
-
return audio_input
|
99 |
-
|
100 |
-
duration = 3 # seconds
|
101 |
-
print("Recording with sounddevice...")
|
102 |
-
audio_data = sd.rec(int(duration * self.sample_rate),
|
103 |
-
samplerate=self.sample_rate,
|
104 |
-
channels=1)
|
105 |
-
sd.wait()
|
106 |
-
return (audio_data.flatten(), self.sample_rate)
|
107 |
-
|
108 |
-
def _process_with_librosa(self, audio_input):
|
109 |
-
if isinstance(audio_input, tuple):
|
110 |
-
return audio_input
|
111 |
-
|
112 |
-
if isinstance(audio_input, str):
|
113 |
-
return librosa.load(audio_input, sr=self.sample_rate)
|
114 |
-
else:
|
115 |
-
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
|
116 |
-
tmp.write(audio_input)
|
117 |
-
tmp.flush()
|
118 |
-
audio_data, _ = librosa.load(tmp.name, sr=self.sample_rate)
|
119 |
-
return (audio_data, self.sample_rate)
|
120 |
-
|
121 |
-
def _process_fallback(self, audio_input):
|
122 |
-
print("Using numpy fallback")
|
123 |
-
if isinstance(audio_input, tuple):
|
124 |
-
return audio_input
|
125 |
-
return (np.random.random(self.sample_rate * 3), self.sample_rate) # 3 seconds of mock audio
|
126 |
|
127 |
def create_interface():
|
128 |
-
|
129 |
|
130 |
def process_audio(audio):
|
131 |
try:
|
132 |
-
|
133 |
-
return
|
134 |
-
"
|
135 |
-
"
|
136 |
-
|
137 |
-
}
|
138 |
except Exception as e:
|
139 |
-
return
|
140 |
-
"audio
|
141 |
-
"
|
142 |
-
|
143 |
-
}
|
144 |
|
145 |
with gr.Blocks() as demo:
|
146 |
-
gr.Markdown("## Audio
|
147 |
|
148 |
with gr.Row():
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
with gr.Row():
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
fn=process_audio,
|
158 |
-
inputs=[
|
159 |
-
outputs=[
|
160 |
)
|
161 |
-
|
162 |
-
gr.Markdown("### Installation Instructions")
|
163 |
-
gr.Markdown("""
|
164 |
-
If you're missing audio backends, install them with:
|
165 |
-
```bash
|
166 |
-
pip install sounddevice librosa ffmpeg-python
|
167 |
-
```
|
168 |
-
""")
|
169 |
|
170 |
return demo
|
171 |
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
class AudioProcessor:
|
5 |
def __init__(self):
|
6 |
self.sample_rate = 16000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def process_audio(self, audio_input):
|
9 |
+
# Simple processing that always works
|
10 |
+
if audio_input is None:
|
11 |
+
return (np.random.random(self.sample_rate * 3), self.sample_rate
|
12 |
+
return (np.random.random(self.sample_rate * 3), self.sample_rate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def create_interface():
|
15 |
+
processor = AudioProcessor()
|
16 |
|
17 |
def process_audio(audio):
|
18 |
try:
|
19 |
+
audio_data, sr = processor.process_audio(audio)
|
20 |
+
return (
|
21 |
+
f"Audio processed successfully\nLength: {len(audio_data)/sr:.2f} seconds",
|
22 |
+
f"Sample rate: {sr} Hz\nData points: {len(audio_data)}"
|
23 |
+
)
|
|
|
24 |
except Exception as e:
|
25 |
+
return (
|
26 |
+
f"Error processing audio: {str(e)}",
|
27 |
+
"No backend information available"
|
28 |
+
)
|
|
|
29 |
|
30 |
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("## Simple Audio Processor")
|
32 |
|
33 |
with gr.Row():
|
34 |
+
audio_input = gr.Audio(sources=["microphone"], type="filepath")
|
35 |
+
process_btn = gr.Button("Process Audio")
|
36 |
+
|
37 |
with gr.Row():
|
38 |
+
status = gr.Textbox(label="Status")
|
39 |
+
info = gr.Textbox(label="Audio Info")
|
40 |
+
|
41 |
+
process_btn.click(
|
42 |
fn=process_audio,
|
43 |
+
inputs=[audio_input],
|
44 |
+
outputs=[status, info]
|
45 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
return demo
|
48 |
|