AI_TEST / app.py
MusIre's picture
Update app.py
76264a6
raw
history blame
1.4 kB
import subprocess
subprocess.run(["pip", "install", "gradio", "--upgrade"])
subprocess.run(["pip", "install", "transformers"])
subprocess.run(["pip", "install", "torchaudio", "--upgrade"])
import numpy as np
import gradio as gr
from transformers import WhisperProcessor, WhisperForConditionalGeneration
# Load Whisper ASR model and processor
model_name = "openai/whisper-small"
processor = WhisperProcessor.from_pretrained(model_name, sampling_rate=44_100)
model = WhisperForConditionalGeneration.from_pretrained(model_name)
forced_decoder_ids = processor.get_decoder_prompt_ids(language="italian", task="transcribe")
def transcribe_audio(input_audio):
if isinstance(input_audio, int):
# Handle the case where input_audio is an integer (error fallback)
input_audio_np = np.array([0.0]) # You can adjust this default value
else:
input_audio_np = np.array(input_audio.data)
input_features = processor(input_audio_np, return_tensors="pt").input_features
# Generate token ids
predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
# Decode token ids to text
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
return transcription[0]
audio_input = gr.Audio(sources=["microphone"])
gr.Interface(fn=transcribe_audio, inputs=audio_input, outputs="text").launch()