dmat / app.py
Chris Bracegirdle
First go
9fbfc52
raw
history blame
1.85 kB
import gradio as gr
import torch
import librosa
import json
# Load model directly
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
processor = AutoProcessor.from_pretrained("dmatekenya/whisper-large-v3-chichewa")
model = AutoModelForSpeechSeq2Seq.from_pretrained("dmatekenya/whisper-large-v3-chichewa")
def transcribe(audio_file_mic=None, audio_file_upload=None, language="English (eng)"):
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)
# Keep the same model in memory and simply switch out the language adapters by calling load_adapter() for the model and set_target_lang() for the tokenizer
# language_code = iso_codes[language]
# processor.tokenizer.set_target_lang(language_code)
# model.load_adapter(language_code)
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs).logits
ids = torch.argmax(outputs, dim=-1)[0]
transcription = processor.decode(ids)
return transcription
description = ''''''
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()