File size: 1,211 Bytes
56899b5
 
264ad82
 
b5de207
1aea5eb
5fcc362
 
e74df3c
 
5fcc362
 
 
 
e74df3c
5fcc362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import subprocess

subprocess.run(["pip", "install", "datasets"])
subprocess.run(["pip", "install", "transformers"])
subprocess.run(["pip", "install", "torch==1.9.1+cpu", "torchvision==0.10.1+cpu", "torchaudio==0.9.1+cpu", "-f", "https://download.pytorch.org/whl/torch_stable.html"])

from transformers import WhisperProcessor, WhisperForConditionalGeneration
from datasets import load_dataset
import gradio as gr

# Load model and processor
processor = WhisperProcessor.from_pretrained("openai/whisper-large")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large")
model.config.forced_decoder_ids = None

# Function to perform ASR on audio data
def transcribe_audio(audio_data):
    # Process audio data using the Whisper processor
    input_features = processor(audio_data, return_tensors="pt").input_features

    # Generate token ids
    predicted_ids = model.generate(input_features)

    # Decode token ids to text
    transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)

    return transcription[0]

# Create Gradio interface
audio_input = gr.Audio(preprocessing_fn=None)
gr.Interface(fn=transcribe_audio, inputs=audio_input, outputs="text").launch()