Spaces:
Sleeping
Sleeping
poemsforaphrodite
commited on
Commit
•
cf30482
1
Parent(s):
9d66ad1
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,60 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
import torch
|
4 |
-
from tqdm import tqdm # Progress bar
|
5 |
import whisper
|
|
|
|
|
6 |
|
7 |
-
def transcribe_audio(
|
8 |
"""
|
9 |
Transcribe a single audio file using OpenAI's Whisper model locally.
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
model (whisper.Whisper): Loaded Whisper model.
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
"""
|
18 |
-
# Perform transcription
|
19 |
-
result = model.transcribe(audio_path)
|
20 |
-
|
21 |
-
# Extract the transcribed text
|
22 |
-
transcriptions = result["text"].strip()
|
23 |
-
|
24 |
-
return transcriptions
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
print(f"Using device: {device}")
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
audio_files = [
|
48 |
-
os.path.join(root, file)
|
49 |
-
for root, dirs, files in os.walk(directory)
|
50 |
-
for file in files
|
51 |
-
if file.lower().endswith((".wav", ".mp3", ".m4a", ".flac", ".aac"))
|
52 |
-
]
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
print(f"Transcribing: {file_path}")
|
57 |
-
transcription = transcribe_audio(file_path, model)
|
58 |
-
transcriptions[file_name] = transcription
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
-
|
68 |
-
output_json = "transcriptions.json"
|
69 |
-
model_size = "large"
|
70 |
-
transcribe_all_audios(directory, output_json, model_size)
|
|
|
1 |
import os
|
2 |
import json
|
3 |
import torch
|
|
|
4 |
import whisper
|
5 |
+
import streamlit as st
|
6 |
+
from tempfile import NamedTemporaryFile
|
7 |
|
8 |
+
def transcribe_audio(audio_file, model):
|
9 |
"""
|
10 |
Transcribe a single audio file using OpenAI's Whisper model locally.
|
11 |
+
"""
|
12 |
+
result = model.transcribe(audio_file)
|
13 |
+
return result["text"].strip()
|
14 |
|
15 |
+
def main():
|
16 |
+
st.title("Audio Transcription with Whisper")
|
|
|
17 |
|
18 |
+
# File uploader
|
19 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "m4a", "flac", "aac"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
if uploaded_file is not None:
|
22 |
+
# Display audio file details
|
23 |
+
file_details = {"Filename": uploaded_file.name, "FileSize": uploaded_file.size}
|
24 |
+
st.write(file_details)
|
25 |
|
26 |
+
# Play audio
|
27 |
+
st.audio(uploaded_file, format='audio/wav')
|
28 |
+
|
29 |
+
if st.button('Transcribe Audio'):
|
30 |
+
with st.spinner('Transcribing audio using Whisper large model...'):
|
31 |
+
# Check if CUDA is available
|
32 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
33 |
+
st.info(f"Using device: {device}")
|
34 |
|
35 |
+
# Load the Whisper model
|
36 |
+
model = whisper.load_model("large", device=device)
|
|
|
37 |
|
38 |
+
# Save uploaded file temporarily and transcribe
|
39 |
+
with NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
|
40 |
+
tmp_file.write(uploaded_file.getvalue())
|
41 |
+
tmp_file_path = tmp_file.name
|
42 |
|
43 |
+
transcription = transcribe_audio(tmp_file_path, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Remove temporary file
|
46 |
+
os.unlink(tmp_file_path)
|
|
|
|
|
|
|
47 |
|
48 |
+
# Display transcription
|
49 |
+
st.subheader("Transcription:")
|
50 |
+
st.write(transcription)
|
51 |
|
52 |
+
# Save transcription to JSON
|
53 |
+
output_json = 'transcription.json'
|
54 |
+
with open(output_json, 'w', encoding='utf-8') as f:
|
55 |
+
json.dump({uploaded_file.name: transcription}, f, ensure_ascii=False, indent=4)
|
56 |
+
|
57 |
+
st.success(f"Transcription saved to {output_json}")
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
+
main()
|
|
|
|
|
|