File size: 1,407 Bytes
db12eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Load model directly
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
import torchaudio
import streamlit as st

processor = AutoProcessor.from_pretrained("mohammed/whisper-small-arabic-cv-11")
model = AutoModelForSpeechSeq2Seq.from_pretrained("mohammed/whisper-small-arabic-cv-11")

st.title("Arabic Whisper model v2")

audio_file = st.file_uploader("Upload audio", type=["mp3", "wav", "m4a"])

if st.sidebar.button("Trascribe Audio"):
    if audio_file is not None:
        st.sidebar.success("Transcribing audio") # on success audio file
        
        audio_tensor, sample_rate = torchaudio.load(audio_file)
        
        if sample_rate != 16000:
            resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
            audio_tensor = resampler(audio_tensor)
            
        audio_np = audio_tensor.squeeze().numpy()
        
        # processing audio
        inputs = processor(audio_np, sample_rate=16000, return_tensors="pt")
        
        # generating transcript
        generated_ids = model.generate(inputs["input_features"])
        
        transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)
        
        # display transcription
        st.sidebar.success("Transcription is complete")
        st.text(transcription[0])
        
    else:
        st.sidebar.error("Please upload a valid audio file")