tbaig1605 commited on
Commit
e936b80
·
verified ·
1 Parent(s): 3ddc6a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -1,29 +1,37 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
 
 
3
 
4
- # Load Whisper model (you can choose base, large, etc.)
5
- whisper = pipeline("automatic-speech-recognition", model="openai/whisper-base")
6
 
7
- # Streamlit app layout
8
- st.title("Audio Transcription with Whisper")
9
 
10
- st.write("Upload an audio file and let the model transcribe it into text.")
 
 
 
 
 
 
11
 
12
- # Upload audio file
13
- audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav", "flac", "opus", "m4a"])
 
 
14
 
15
- if audio_file is not None:
16
- # Save the uploaded audio file
17
- with open("uploaded_audio.wav", "wb") as f:
18
- f.write(audio_file.getbuffer())
19
 
20
- # Display the audio file name
21
- st.audio(audio_file, format="audio/wav")
22
-
23
- # Run transcription
24
  st.write("Transcribing...")
25
- result = whisper("uploaded_audio.wav")["text"]
26
 
27
- # Show the transcription result
28
- st.subheader("Transcription:")
29
- st.write(result)
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import torchaudio
4
+ import os
5
+ import ffmpeg
6
 
7
+ # Step 1: Upload and convert audio to WAV format
8
+ st.title("Audio Transcription with Whisper (Small Model)")
9
 
10
+ uploaded_file = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "opus"])
 
11
 
12
+ if uploaded_file is not None:
13
+ # Save the uploaded file
14
+ audio_path = os.path.join("uploads", uploaded_file.name)
15
+ with open(audio_path, "wb") as f:
16
+ f.write(uploaded_file.getbuffer())
17
+
18
+ st.audio(audio_path, format="audio/ogg")
19
 
20
+ # Convert to WAV format using ffmpeg if it's not WAV already
21
+ wav_path = audio_path.replace(os.path.splitext(audio_path)[-1], ".wav")
22
+ if not audio_path.endswith(".wav"):
23
+ ffmpeg.input(audio_path).output(wav_path).run()
24
 
25
+ st.success("Audio converted to WAV format")
 
 
 
26
 
27
+ # Step 2: Load Whisper small model for transcription
28
+ whisper = pipeline("automatic-speech-recognition", model="openai/whisper-small")
29
+
30
+ # Step 3: Transcribe the audio
31
  st.write("Transcribing...")
 
32
 
33
+ transcription = whisper(wav_path)
34
+ st.write("Transcription result: ", transcription['text'])
35
+
36
+ else:
37
+ st.info("Please upload an audio file to transcribe.")