MMS-ASR-Fula / app.py
yaya-sy's picture
Update app.py
b2a5d55
raw
history blame
1.88 kB
import gradio as gr
from transformers import pipeline
import torch
import librosa
import json
max_duration = int(30 * sr)
def load_model(model_name = "cawoylel/windanam_mms-1b-tts_v2"):
"""
Function to load model from hugging face
"""
pipe = pipeline("automatic-speech-recognition", model=model_name)
return pipe
pipeline = load_model()
def transcribe_audio(sample):
"""
Transcribe audio
"""
transcription = pipeline(sample)
return transcription["text"]
def transcribe(audio_file_mic=None, audio_file_upload=None):
if audio_file_mic:
audio_file = audio_file_mic
elif audio_file_upload:
audio_file = audio_file_upload
else:
return "Please upload an audio file or record one"
# Make sure audio is 16kHz
speech, sample_rate = librosa.load(audio_file)
if sample_rate != 16000:
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
duration = librosa.get_duration(speech)
if duration > 30:
speech = speech[:max_duration]
return transcribe_audio(speech)
description = '''Automatic Speech Recognition with [MMS](https://ai.facebook.com/blog/multilingual-model-speech-recognition/) (Massively Multilingual Speech) by Meta.
Supports [1162 languages](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html). Read the paper for more details: [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516).'''
iface = gr.Interface(fn=transcribe,
inputs=[
gr.Audio(source="microphone", type="filepath", label="Record Audio"),
gr.Audio(source="upload", type="filepath", label="Upload Audio"),
],
outputs=gr.Textbox(label="Transcription"),
description=description
)
iface.launch()