Team Ai
Apppublic

taham655/Transcriptor

sourceHugging Faceupdated 3y agoView on Hugging Face
2likes
app.py63 linesDownload Raw Back to root
1import streamlit as st2import os3import soundfile as sf4import torch5from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline6 7 8 9device = "cuda:0" if torch.cuda.is_available() else "cpu"10torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float3211 12model_id = "distil-whisper/distil-large-v2"13 14model = AutoModelForSpeechSeq2Seq.from_pretrained(15    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True16)17model.to(device)18 19processor = AutoProcessor.from_pretrained(model_id)20 21pipe = pipeline(22    "automatic-speech-recognition",23    model=model,24    tokenizer=processor.tokenizer,25    feature_extractor=processor.feature_extractor,26    max_new_tokens=128,27    chunk_length_s=15,28    batch_size=16,29    torch_dtype=torch_dtype,30    device=device,31)32 33def transcribe_audio(audio_file):34    # Save the audio file to a temporary file35    with open("temp_audio_file", "wb") as f:36        f.write(audio_file.getbuffer())37    38    # Transcribe the audio file using the Whisper model39    result = pipe("temp_audio_file")40    return result["text"]41 42# Streamlit app43def main():44    st.title('BETTER TRANSCRIBER')45    46    # Audio file uploader47    uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "m4a", "ogg", "flac"])48 49    if uploaded_file is not None:50        # Show a button to start the transcription process51        if st.button('Transcribe'):52            # Show a message while transcribing53            with st.spinner('Transcribing...'):54                text = transcribe_audio(uploaded_file)55            56            # Show the transcription57            st.subheader('Transcription:')58            st.write(text)59        else:60            st.write('Upload an audio file to get started.')61 62if __name__ == "__main__":63    main()