snyamson commited on
Commit
49a31b4
·
1 Parent(s): 1c4efab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py CHANGED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torchaudio
3
+ from transformers import WhisperProcessor, WhisperForConditionalGeneration
4
+
5
+ # Load the Whisper model and processor
6
+ processor = WhisperProcessor.from_pretrained("openai/whisper-tiny.en")
7
+ model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
8
+
9
+ # Sidebar for file upload
10
+ st.sidebar.title("Upload your audio file")
11
+ uploaded_file = st.sidebar.file_uploader("Choose an audio file", type=["mp3", "wav", "mp4"])
12
+
13
+ if uploaded_file:
14
+ st.sidebar.audio(uploaded_file)
15
+
16
+ # Process the uploaded file
17
+ audio_tensor, sampling_rate = torchaudio.load(uploaded_file)
18
+ resampler = torchaudio.transforms.Resample(sampling_rate, 16000)
19
+ resampled_waveform = resampler(audio_tensor)
20
+
21
+ segment_duration = 120 # Segment duration in seconds (2 minutes)
22
+ num_segments = len(resampled_waveform[0]) // (segment_duration * 16000)
23
+ segment_transcriptions = []
24
+
25
+ # Transcribe each segment
26
+ for i in range(num_segments):
27
+ start = i * segment_duration * 16000
28
+ end = min(len(resampled_waveform[0]), (i + 1) * segment_duration * 16000)
29
+ segment = resampled_waveform[0][start:end]
30
+
31
+ # Transcribe the segment
32
+ input_features = processor(
33
+ segment, sampling_rate=16000, return_tensors="pt"
34
+ ).input_features
35
+
36
+ predicted_ids = model.generate(input_features)
37
+ transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
38
+
39
+ segment_transcriptions.append(transcription[0])
40
+
41
+ # Combine segment transcriptions into the full transcript
42
+ full_transcript = " ".join(segment_transcriptions)
43
+
44
+ # Display the transcript
45
+ st.header("Transcription")
46
+ st.write(full_transcript)