mohammed commited on
Commit
5d87326
1 Parent(s): 18cefe5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torchaudio
2
+ import streamlit as st
3
+ from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
4
+
5
+
6
+ # Load the ASR model and processor
7
+ processor = AutoProcessor.from_pretrained("mohammed/whisper-large-arabic-cv-11")
8
+ model = AutoModelForSpeechSeq2Seq.from_pretrained("mohammed/whisper-large-arabic-cv-11")
9
+
10
+ audio_file = st.file_uploader("Upload Audio", type=["wav", "mp3", "m4a"])
11
+ st.title("Arabic ASR model")
12
+
13
+ if st.sidebar.button("Transcribe Audio"):
14
+ if audio_file is not None:
15
+ st.sidebar.success("Transcribing Audio >>>>")
16
+
17
+ # Load the audio file
18
+ audio_tensor, sample_rate = torchaudio.load(audio_file)
19
+
20
+ # Resample if necessary
21
+ if sample_rate != 16000:
22
+ resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
23
+ audio_tensor = resampler(audio_tensor)
24
+
25
+ # Convert the audio tensor to a numpy array
26
+ audio_np = audio_tensor.squeeze().numpy()
27
+
28
+ # Process the audio
29
+ inputs = processor(audio_np, sampling_rate=16000, return_tensors="pt")
30
+
31
+ # Generate transcription
32
+ generated_ids = model.generate(inputs["input_features"])
33
+ transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)
34
+
35
+ # Display transcription
36
+ st.sidebar.success("Transcription Complete!")
37
+ st.text(transcription[0])
38
+ else:
39
+ st.sidebar.error("Please upload a valid audio file")