najva / app.py
mobinln's picture
working version
128581e
import gradio as gr
import numpy as np
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import librosa
processor = WhisperProcessor.from_pretrained("Neurai/NeuraSpeech_WhisperBase")
model = WhisperForConditionalGeneration.from_pretrained("Neurai/NeuraSpeech_WhisperBase")
forced_decoder_ids = processor.get_decoder_prompt_ids(language="fa", task="transcribe")
def transcribe(audio):
if audio is None:
return "No audio input provided. Please record or upload an audio file."
array, sample_rate = librosa.load(audio)
array = array.astype(np.float32)
sr = 16000
array = librosa.to_mono(array)
array = librosa.resample(array, orig_sr=sample_rate, target_sr=sr)
input_features = processor(array, sampling_rate=sr, return_tensors="pt").input_features
predicted_ids = model.generate(input_features)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
return transcription[0]
demo = gr.Interface(
fn=transcribe,
inputs=[gr.Audio(sources=["microphone"], type='filepath')],
outputs="text",
allow_flagging="never",
)
if __name__ == "__main__":
demo.launch()