Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torchaudio
|
3 |
+
import torch
|
4 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
+
|
6 |
+
# Load model and processor
|
7 |
+
processor = Wav2Vec2Processor.from_pretrained("Mustafaa4a/ASR-Somali")
|
8 |
+
model = Wav2Vec2ForCTC.from_pretrained("Mustafaa4a/ASR-Somali")
|
9 |
+
|
10 |
+
def transcribe(audio):
|
11 |
+
waveform, sample_rate = torchaudio.load(audio)
|
12 |
+
|
13 |
+
if sample_rate != 16000:
|
14 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
|
15 |
+
waveform = resampler(waveform)
|
16 |
+
|
17 |
+
inputs = processor(waveform.squeeze(), sampling_rate=16000, return_tensors="pt")
|
18 |
+
with torch.no_grad():
|
19 |
+
logits = model(**inputs).logits
|
20 |
+
|
21 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
22 |
+
transcription = processor.decode(predicted_ids[0])
|
23 |
+
return transcription
|
24 |
+
|
25 |
+
# Gradio Interface setup
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=transcribe,
|
28 |
+
inputs=gr.Audio(type="filepath", label="Upload Somali Audio (.wav)"),
|
29 |
+
outputs=gr.Textbox(label="Transcription"),
|
30 |
+
title="Somali-speech_to_text",
|
31 |
+
description="Upload a Somali speech audio file (mono WAV, 16kHz) and get the text transcription."
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the Gradio app and make it publicly available by using 'share=True'
|
35 |
+
interface.launch() # Don't use share=True in Hugging Face Spaces
|