Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -24,11 +24,18 @@ else:
|
|
24 |
|
25 |
model_id = "sudoping01/maliba-asr-v1"
|
26 |
transcriber = WhosperTranscriber(model_id=model_id)
|
27 |
-
logger.info(f"
|
28 |
|
29 |
def resample_audio(audio_path, target_sample_rate=16000):
|
|
|
30 |
"""
|
31 |
Converts the audio file to the target sampling rate (16000 Hz).
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
try:
|
34 |
waveform, original_sample_rate = torchaudio.load(audio_path)
|
@@ -47,15 +54,25 @@ def resample_audio(audio_path, target_sample_rate=16000):
|
|
47 |
|
48 |
@spaces.GPU()
|
49 |
def transcribe_audio(audio_file):
|
|
|
50 |
"""
|
51 |
Transcribes the provided audio file into Bambara text using Whosper.
|
|
|
|
|
|
|
|
|
|
|
52 |
"""
|
|
|
53 |
if audio_file is None:
|
54 |
return "Please provide an audio file for transcription."
|
55 |
|
56 |
try:
|
57 |
logger.info(f"Transcribing audio file: {audio_file}")
|
|
|
|
|
58 |
result = transcriber.transcribe_audio(audio_file)
|
|
|
59 |
logger.info("Transcription successful.")
|
60 |
return result
|
61 |
|
@@ -64,14 +81,16 @@ def transcribe_audio(audio_file):
|
|
64 |
return f"Error during transcription: {str(e)}"
|
65 |
|
66 |
def get_example_files(directory="./examples"):
|
|
|
67 |
"""
|
68 |
-
Returns a list of audio files from the examples directory
|
69 |
|
70 |
Args:
|
71 |
directory (str): The directory to search for audio files.
|
72 |
Returns:
|
73 |
-
list: A list of
|
74 |
"""
|
|
|
75 |
if not os.path.exists(directory):
|
76 |
logger.warning(f"Examples directory {directory} not found.")
|
77 |
return []
|
@@ -82,14 +101,10 @@ def get_example_files(directory="./examples"):
|
|
82 |
|
83 |
try:
|
84 |
files = os.listdir(directory)
|
85 |
-
=
|
86 |
-
files.sort()
|
87 |
-
|
88 |
for file in files:
|
89 |
if any(file.lower().endswith(ext) for ext in audio_extensions):
|
90 |
full_path = os.path.abspath(os.path.join(directory, file))
|
91 |
-
|
92 |
-
audio_files.append([full_path])
|
93 |
|
94 |
logger.info(f"Found {len(audio_files)} example audio files.")
|
95 |
return audio_files[:5]
|
@@ -98,73 +113,127 @@ def get_example_files(directory="./examples"):
|
|
98 |
logger.error(f"Error reading examples directory: {e}")
|
99 |
return []
|
100 |
|
101 |
-
def
|
102 |
"""
|
103 |
-
|
104 |
"""
|
105 |
-
logger.info("Starting MALIBA-ASR-v1 Gradio interface.")
|
106 |
-
|
107 |
|
108 |
example_files = get_example_files()
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
)
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
|
|
|
|
168 |
interface.launch(
|
169 |
share=False,
|
170 |
server_name="0.0.0.0",
|
|
|
24 |
|
25 |
model_id = "sudoping01/maliba-asr-v1"
|
26 |
transcriber = WhosperTranscriber(model_id=model_id)
|
27 |
+
logger.info(f"Transcriber initialized with model: {model_id}")
|
28 |
|
29 |
def resample_audio(audio_path, target_sample_rate=16000):
|
30 |
+
|
31 |
"""
|
32 |
Converts the audio file to the target sampling rate (16000 Hz).
|
33 |
+
|
34 |
+
Args:
|
35 |
+
audio_path (str): Path to the audio file.
|
36 |
+
target_sample_rate (int): The desired sample rate.
|
37 |
+
Returns:
|
38 |
+
A tensor containing the resampled audio data and the target sample rate.
|
39 |
"""
|
40 |
try:
|
41 |
waveform, original_sample_rate = torchaudio.load(audio_path)
|
|
|
54 |
|
55 |
@spaces.GPU()
|
56 |
def transcribe_audio(audio_file):
|
57 |
+
|
58 |
"""
|
59 |
Transcribes the provided audio file into Bambara text using Whosper.
|
60 |
+
|
61 |
+
Args:
|
62 |
+
audio_file: The path to the audio file to transcribe.
|
63 |
+
Returns:
|
64 |
+
A string representing the transcribed Bambara text.
|
65 |
"""
|
66 |
+
|
67 |
if audio_file is None:
|
68 |
return "Please provide an audio file for transcription."
|
69 |
|
70 |
try:
|
71 |
logger.info(f"Transcribing audio file: {audio_file}")
|
72 |
+
|
73 |
+
|
74 |
result = transcriber.transcribe_audio(audio_file)
|
75 |
+
|
76 |
logger.info("Transcription successful.")
|
77 |
return result
|
78 |
|
|
|
81 |
return f"Error during transcription: {str(e)}"
|
82 |
|
83 |
def get_example_files(directory="./examples"):
|
84 |
+
|
85 |
"""
|
86 |
+
Returns a list of audio files from the examples directory.
|
87 |
|
88 |
Args:
|
89 |
directory (str): The directory to search for audio files.
|
90 |
Returns:
|
91 |
+
list: A list of paths to the audio files.
|
92 |
"""
|
93 |
+
|
94 |
if not os.path.exists(directory):
|
95 |
logger.warning(f"Examples directory {directory} not found.")
|
96 |
return []
|
|
|
101 |
|
102 |
try:
|
103 |
files = os.listdir(directory)
|
|
|
|
|
|
|
104 |
for file in files:
|
105 |
if any(file.lower().endswith(ext) for ext in audio_extensions):
|
106 |
full_path = os.path.abspath(os.path.join(directory, file))
|
107 |
+
audio_files.append(full_path)
|
|
|
108 |
|
109 |
logger.info(f"Found {len(audio_files)} example audio files.")
|
110 |
return audio_files[:5]
|
|
|
113 |
logger.error(f"Error reading examples directory: {e}")
|
114 |
return []
|
115 |
|
116 |
+
def build_interface():
|
117 |
"""
|
118 |
+
Builds the Gradio interface for Bambara speech recognition.
|
119 |
"""
|
|
|
|
|
120 |
|
121 |
example_files = get_example_files()
|
122 |
|
123 |
+
with gr.Blocks(title="Bambara Speech Recognition") as demo:
|
124 |
+
gr.Markdown(
|
125 |
+
"""
|
126 |
+
# π€ Bambara Automatic Speech Recognition
|
127 |
+
|
128 |
+
**Powered by MALIBA-AI**
|
129 |
+
|
130 |
+
Convert Bambara speech to text using our state-of-the-art ASR model. You can either:
|
131 |
+
- ποΈ **Record** your voice directly
|
132 |
+
- π **Upload** an audio file
|
133 |
+
- π΅ **Try** our example audio files
|
134 |
+
|
135 |
+
## Supported Audio Formats
|
136 |
+
WAV, MP3, M4A, FLAC, OGG
|
137 |
+
"""
|
138 |
+
)
|
139 |
+
|
140 |
+
with gr.Row():
|
141 |
+
with gr.Column():
|
142 |
+
|
143 |
+
audio_input = gr.Audio(
|
144 |
+
label="π€ Record or Upload Audio",
|
145 |
+
type="filepath",
|
146 |
+
sources=["microphone", "upload"]
|
147 |
+
)
|
148 |
+
|
149 |
+
transcribe_btn = gr.Button(
|
150 |
+
"π Transcribe Audio",
|
151 |
+
variant="primary",
|
152 |
+
size="lg"
|
153 |
+
)
|
154 |
+
|
155 |
+
|
156 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
157 |
+
|
158 |
+
with gr.Column():
|
159 |
+
output_text = gr.Textbox(
|
160 |
+
label="π Transcribed Text (Bambara)",
|
161 |
+
lines=8,
|
162 |
+
placeholder="Your transcribed Bambara text will appear here...",
|
163 |
+
interactive=False
|
164 |
+
)
|
165 |
+
|
166 |
+
# Examples section
|
167 |
+
if example_files:
|
168 |
+
gr.Markdown("## π΅ Try These Examples")
|
169 |
+
gr.Examples(
|
170 |
+
examples=[[f] for f in example_files],
|
171 |
+
inputs=[audio_input],
|
172 |
+
outputs=output_text,
|
173 |
+
fn=transcribe_audio,
|
174 |
+
cache_examples=False,
|
175 |
+
label="Example Audio Files"
|
176 |
)
|
177 |
+
|
178 |
+
# Information section
|
179 |
+
gr.Markdown(
|
180 |
+
"""
|
181 |
+
---
|
182 |
+
|
183 |
+
## βΉοΈ About This Model
|
184 |
+
|
185 |
+
- **Model:** [sudoping01/maliba-asr-v1](https://huggingface.co/sudoping01/maliba-asr-v1)
|
186 |
+
- **Developer:** MALIBA-AI
|
187 |
+
- **Language:** Bambara (bm)
|
188 |
+
- **Task:** Automatic Speech Recognition (ASR)
|
189 |
+
- **Sample Rate:** 16kHz (automatically resampled)
|
190 |
+
|
191 |
+
## π How to Use
|
192 |
+
|
193 |
+
1. **Record Audio:** Click the microphone button and speak in Bambara
|
194 |
+
2. **Upload File:** Click the upload button to select an audio file
|
195 |
+
3. **Transcribe:** Click the "Transcribe Audio" button
|
196 |
+
4. **View Results:** See your transcribed text in Bambara
|
197 |
+
|
198 |
+
## π Performance Notes
|
199 |
+
|
200 |
+
- Best results with clear speech and minimal background noise
|
201 |
+
- Supports various audio formats and durations
|
202 |
+
- Optimized for Bambara language patterns and phonetics
|
203 |
+
"""
|
204 |
+
)
|
205 |
+
|
206 |
+
|
207 |
+
transcribe_btn.click(
|
208 |
+
fn=transcribe_audio,
|
209 |
+
inputs=[audio_input],
|
210 |
+
outputs=output_text,
|
211 |
+
show_progress=True
|
212 |
+
)
|
213 |
+
|
214 |
+
clear_btn.click(
|
215 |
+
fn=lambda: (None, ""),
|
216 |
+
outputs=[audio_input, output_text]
|
217 |
+
)
|
218 |
+
|
219 |
+
|
220 |
+
audio_input.change(
|
221 |
+
fn=transcribe_audio,
|
222 |
+
inputs=[audio_input],
|
223 |
+
outputs=output_text,
|
224 |
+
show_progress=True
|
225 |
+
)
|
226 |
+
|
227 |
+
return demo
|
228 |
+
|
229 |
+
def main():
|
230 |
+
"""
|
231 |
+
Main function to launch the Gradio interface.
|
232 |
+
"""
|
233 |
+
logger.info("Starting Bambara ASR Gradio interface.")
|
234 |
|
235 |
+
|
236 |
+
interface = build_interface()
|
237 |
interface.launch(
|
238 |
share=False,
|
239 |
server_name="0.0.0.0",
|