Tonic's picture
Create app.py
ad3bc83
raw
history blame
1.87 kB
import gradio as gr
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset
import soundfile as sf
# Check if CUDA is available and set the device
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
# Load the model and processor
model_id = "openai/whisper-large-v3"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
).to(device)
processor = AutoProcessor.from_pretrained(model_id)
# Define the ASR pipeline
asr_pipeline = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=30,
batch_size=16,
return_timestamps=True,
torch_dtype=torch_dtype,
device=device,
)
# Function to process audio in chunks and return the combined text
def process_audio(file_info):
path = file_info["path"]
audio_stream = sf.SoundFile(path, 'r')
results = []
while True:
data = audio_stream.read(dtype='float32')
if len(data) == 0:
break
result = asr_pipeline(data)
results.append(result)
audio_stream.close()
combined_text = " ".join([r["text"] for r in results])
return combined_text
# Create the Gradio interface
iface = gr.Interface(
fn=process_audio,
inputs=gr.inputs.Audio(source="upload", type="file", label="Upload your audio file"),
outputs="text",
title="👋🏻Welcome To 🙋🏻‍♂️Patrick's Whisper🌬️",
description="Upload a large audio file to transcribe it into text using [Whisper3Large](https://huggingface.co/openai/whisper-large-v3) !",
)
# Launch the application
iface.launch()