|
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 |
|
|
|
|
|
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): |
|
|
|
input_audio_np = np.array([0.0]) |
|
else: |
|
input_audio_np = np.array(input_audio.data) |
|
|
|
input_features = processor(input_audio_np, 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) |
|
|
|
return transcription[0] |
|
|
|
|
|
audio_input = gr.Audio(sources=["microphone"]) |
|
gr.Interface(fn=transcribe_audio, inputs=audio_input, outputs="text").launch() |
|
|
|
|
|
|
|
|