nirajandhakal's picture
Create app.py
80f8097 verified
raw
history blame
1.42 kB
import gradio as gr
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset
# Set up the device (GPU or CPU)
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 = "ylacombe/whisper-large-v3-turbo"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
# Create a pipeline for speech recognition
pipe = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
torch_dtype=torch_dtype,
device=device,
)
def transcribe_audio(audio):
# Preprocess the audio
audio_input = processor(audio, return_tensors="pt", sampling_rate=16000)
audio_input = audio_input.to(device)
# Run the pipeline to get the transcription
result = pipe(audio_input)
return result["text"]
# Create a Gradio interface
demo = gr.Interface(
transcribe_audio,
inputs=gr.Audio(source="upload", type="file"),
outputs="text",
title="Speech-to-Text Transcription",
description="Upload an audio file to transcribe its content.",
)
# Launch the Gradio app
demo.launch()