Spaces:
Sleeping
Sleeping
add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load pidgin model via pipeline
|
| 6 |
+
transcriber = pipeline("automatic-speech-recognition", model="asr-nigerian-pidgin/pidgin-wav2vec2-xlsr53")
|
| 7 |
+
|
| 8 |
+
# Transcription function
|
| 9 |
+
def transcribe(audio):
|
| 10 |
+
if audio is None:
|
| 11 |
+
return "No audio provided."
|
| 12 |
+
|
| 13 |
+
sr, y = audio
|
| 14 |
+
|
| 15 |
+
# Convert to mono if stereo
|
| 16 |
+
if y.ndim > 1:
|
| 17 |
+
y = y.mean(axis=1)
|
| 18 |
+
|
| 19 |
+
y = y.astype(np.float32)
|
| 20 |
+
y /= np.max(np.abs(y))
|
| 21 |
+
|
| 22 |
+
return transcriber({"sampling_rate": sr, "raw": y})["text"]
|
| 23 |
+
|
| 24 |
+
# Define the Gradio UI components
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("# π£οΈ Nigerian Pidgin ASR Demo")
|
| 27 |
+
gr.Markdown("Upload or record audio in Nigerian Pidgin to get transcription.")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
audio_input = gr.Audio(
|
| 31 |
+
sources=["microphone", "upload"],
|
| 32 |
+
type="numpy",
|
| 33 |
+
label="π€ Record or Upload Audio",
|
| 34 |
+
interactive=True,
|
| 35 |
+
max_length=30,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 40 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
output_text = gr.Textbox(
|
| 44 |
+
label="π Transcription Output",
|
| 45 |
+
interactive=False,
|
| 46 |
+
show_copy_button=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
flag_btn = gr.Button("π© Flag this output as incorrect", size="sm")
|
| 51 |
+
share_btn = gr.Button("π Share", size="sm")
|
| 52 |
+
|
| 53 |
+
#
|
| 54 |
+
submit_btn.click(fn=transcribe, inputs=audio_input, outputs=output_text)
|
| 55 |
+
clear_btn.click(fn=lambda: (None, ""), inputs=None, outputs=[audio_input, output_text])
|
| 56 |
+
flag_btn.click(fn=lambda: "Thank you for your feedback.", inputs=None, outputs=output_text)
|
| 57 |
+
|
| 58 |
+
demo.launch(share=True)
|