File size: 2,411 Bytes
82e14f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Load model directly
import streamlit as st
import torch
import numpy as np
import librosa
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq, pipeline
from io import BytesIO

# Configure Streamlit page settings
st.set_page_config(
    page_title="Transcribe with Whisper",
    page_icon=":rocket:",
    layout="centered"
)

st.title("πŸŽ™οΈ Whisper Audio Transcriber")
st.divider()

# Set up device and data type
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

# Load the Whisper model and processor
with st.spinner("πŸš€ Loading Whisper model... please wait!"):
    model_name = "openai/whisper-large-v3"
    model = AutoModelForSpeechSeq2Seq.from_pretrained(
        model_name, 
        torch_dtype=torch_dtype, 
        low_cpu_mem_usage=True, 
        use_safetensors=True
    )
    model.to(device)
    processor = AutoProcessor.from_pretrained(model_name)

# Initialize ASR pipeline
asr_pipe = pipeline(
    task="automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    torch_dtype=torch_dtype,
    device=device,
)

st.markdown("Upload your audio files, and let the Whisper model transcribe them instantly. πŸš€")

# File uploader for audio files
uploaded_files = st.file_uploader("πŸ“‚ Select audio files to transcribe", type=["wav","mp3"], accept_multiple_files=True)

# Transcription button and result display
if uploaded_files:
    if st.button("✍️ Transcribe"):
        results = []

        for idx, audio_file in enumerate(uploaded_files):
            try:
                # Read audio file
                audio_data, sr = librosa.load(BytesIO(audio_file.read()), sr=16000)

                # Run ASR pipeline
                result = asr_pipe(audio_data)
                transcription = result['text']
                results.append((audio_file.name, transcription))
            except Exception as e:
                st.error(f"Error processing '{audio_file.name}':{e}")

        # Display results
        st.subheader("Transcriptions")
        for filename, transcription in results:
            st.text_area(f"πŸ“‚ **{filename}**:", value=transcription)
else:
    st.info("πŸ“€ Please upload audio files to start transcription.")