Spaces:
Sleeping
Sleeping
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
import soundfile as sf
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
# --- EDIT THIS: map display names to your HF Hub model IDs ---
|
9 |
+
language_models = {
|
10 |
+
"Akan (Asanti Twi)": "FarmerlineML/w2v-bert-2.0_twi_alpha_v1",
|
11 |
+
"Akan (Fante Twi)": "your-username/english-asr-model",
|
12 |
+
"Ewe": "FarmerlineML/w2v-bert-2.0_ewe_2",
|
13 |
+
"Kiswahili": "FarmerlineML/w2v-bert-2.0_swahili_alpha",
|
14 |
+
"Luganda": "FarmerlineML/w2v-bert-2.0_luganda",
|
15 |
+
"Akan (Asanti Twi)": "your-username/english-asr-model",
|
16 |
+
"Brazilian Portuguese": "FarmerlineML/w2v-bert-2.0_brazilian_portugese_alpha",
|
17 |
+
# add more as needed
|
18 |
+
}
|
19 |
+
|
20 |
+
# Pre-load pipelines for each language
|
21 |
+
asr_pipelines = {
|
22 |
+
lang: pipeline(
|
23 |
+
task="automatic-speech-recognition",
|
24 |
+
model=model_id,
|
25 |
+
# device=0, # uncomment if you have GPU
|
26 |
+
chunk_length_s=30 # adjust if your audio can be longer
|
27 |
+
)
|
28 |
+
for lang, model_id in language_models.items()
|
29 |
+
}
|
30 |
+
|
31 |
+
|
32 |
+
def transcribe(audio_path: str, language: str) -> str:
|
33 |
+
"""
|
34 |
+
Load the audio file, convert to mono if needed,
|
35 |
+
and run it through the selected ASR pipeline.
|
36 |
+
"""
|
37 |
+
if audio_path is None:
|
38 |
+
return "⚠️ Please upload or record an audio clip."
|
39 |
+
|
40 |
+
# Read the file
|
41 |
+
speech, sr = sf.read(audio_path)
|
42 |
+
# Stereo → mono
|
43 |
+
if speech.ndim > 1:
|
44 |
+
speech = np.mean(speech, axis=1)
|
45 |
+
|
46 |
+
result = asr_pipelines[language]({
|
47 |
+
"sampling_rate": sr,
|
48 |
+
"raw": speech
|
49 |
+
})
|
50 |
+
return result.get("text", "")
|
51 |
+
|
52 |
+
|
53 |
+
with gr.Blocks(title="🌐 Multilingual ASR Demo") as demo:
|
54 |
+
gr.Markdown(
|
55 |
+
"""
|
56 |
+
## 🎙️ Multilingual Speech-to-Text
|
57 |
+
Upload an audio file or record via your microphone,
|
58 |
+
then choose the language/model and hit **Transcribe**.
|
59 |
+
"""
|
60 |
+
)
|
61 |
+
|
62 |
+
with gr.Row():
|
63 |
+
lang = gr.Dropdown(
|
64 |
+
choices=list(language_models.keys()),
|
65 |
+
value=list(language_models.keys())[0],
|
66 |
+
label="Select Language / Model"
|
67 |
+
)
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
audio = gr.Audio(
|
71 |
+
source=["upload", "microphone"],
|
72 |
+
type="filepath",
|
73 |
+
label="Upload or Record Audio"
|
74 |
+
)
|
75 |
+
|
76 |
+
btn = gr.Button("Transcribe")
|
77 |
+
output = gr.Textbox(label="Transcription")
|
78 |
+
|
79 |
+
btn.click(fn=transcribe, inputs=[audio, lang], outputs=output)
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
demo.launch()
|