import streamlit as st from tempfile import NamedTemporaryFile st.write("## Whisper Speech to Text 🎧") import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline @st.cache_resource def load_model(): device = "cuda:0" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 model_id = "openai/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) pipe = pipeline( "automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, torch_dtype=torch_dtype, device=device, ) return pipe @st.cache_data def transcribe(_pipe, sample): result = _pipe(sample, return_timestamps=True) return result["text"] audio = st.file_uploader("Upload your Audio Here") model = load_model() if st.button("Transcribe"): if audio is not None: st.write("Transcribing ... ") with NamedTemporaryFile(suffix="mp3") as temp: temp.write(audio.getvalue()) temp.seek(0) transcript = transcribe(model, temp.name) transcript # MAGIC!