Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,37 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
st.title("Audio Transcription with Whisper")
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
# Save the uploaded audio file
|
| 17 |
-
with open("uploaded_audio.wav", "wb") as f:
|
| 18 |
-
f.write(audio_file.getbuffer())
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
st.write("Transcribing...")
|
| 25 |
-
result = whisper("uploaded_audio.wav")["text"]
|
| 26 |
|
| 27 |
-
|
| 28 |
-
st.
|
| 29 |
-
|
|
|
|
|
|
|
|
|
| 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.")
|