Spaces:
Sleeping
Sleeping
import gradio as gr | |
import librosa | |
import torch | |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor | |
# Load pre-trained model and processor directly from Hugging Face Hub | |
model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija") | |
processor = Wav2Vec2Processor.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija") | |
def transcribe_audio(audio): | |
# Load the audio file from Gradio interface | |
audio_array, sr = librosa.load(audio, sr=16000) | |
# Tokenize the audio file | |
input_values = processor(audio_array, return_tensors="pt", padding=True).input_values | |
# Get the model's logits (predicted token scores) | |
logits = model(input_values).logits | |
# Get the predicted tokens | |
tokens = torch.argmax(logits, axis=-1) | |
# Decode the tokens into text | |
transcription = processor.decode(tokens[0]) | |
return transcription | |
# Create a Gradio interface for uploading audio or recording from the browser | |
demo = gr.Interface(fn=transcribe_audio, | |
inputs=gr.Audio(source="upload", type="filepath"), | |
outputs="text") | |
demo.launch() | |