File size: 1,505 Bytes
3a50503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torchaudio
import streamlit as st
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq


# Load the ASR model and processor
processor = AutoProcessor.from_pretrained("mohammed/whisper-small-arabic-cv-11")
model = AutoModelForSpeechSeq2Seq.from_pretrained("mohammed/whisper-small-arabic-cv-11")

audio_file = st.file_uploader("Upload Audio", type=["wav", "mp3", "m4a"])
st.title("Arabic ASR model")

if st.sidebar.button("Transcribe Audio"):
    if audio_file is not None:
        st.sidebar.success("Transcribing Audio >>>>")

        # Load the audio file
        audio_tensor, sample_rate = torchaudio.load(audio_file)
        
        # Resample if necessary
        if sample_rate != 16000:
            resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
            audio_tensor = resampler(audio_tensor)

        # Convert the audio tensor to a numpy array
        audio_np = audio_tensor.squeeze().numpy()

        # Process the audio
        inputs = processor(audio_np, sampling_rate=16000, return_tensors="pt")
        
        # Generate transcription
        generated_ids = model.generate(inputs["input_features"])
        transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)
        
        # Display transcription
        st.sidebar.success("Transcription Complete!")
        st.text(transcription[0])
    else:
        st.sidebar.error("Please upload a valid audio file")