|
import gradio as gr |
|
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, *args): |
|
print(audio, args) |
|
if audio is None: |
|
return "No audio input provided. Please record or upload an audio file." |
|
|
|
|
|
try: |
|
array, sample_rate = librosa.load(audio, sr=16000) |
|
except Exception as e: |
|
return f"Error loading audio file: {str(e)}" |
|
|
|
|
|
array = librosa.to_mono(array) |
|
input_features = processor(array, sampling_rate=sample_rate, return_tensors="pt").input_features |
|
|
|
|
|
predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids) |
|
|
|
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True) |
|
print(transcription) |
|
return transcription[0] |
|
|
|
|
|
demo = gr.Interface( |
|
fn=transcribe, |
|
inputs=[gr.Audio(sources=["microphone"], type="filepath")], |
|
outputs="text" |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|