Spaces:
Sleeping
Sleeping
cptsubtext
commited on
Commit
·
8bf0bb0
1
Parent(s):
8ec3282
first test with functions
Browse files
app.py
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
import requests
|
4 |
import os
|
5 |
|
6 |
# Variables
|
7 |
valid_api_token = st.secrets["API_TOKEN"]
|
8 |
|
|
|
|
|
|
|
9 |
# Free tier or API token option
|
10 |
use_free_tier = st.checkbox("Free Tier (Max 2 minutes)")
|
11 |
api_token = st.text_input("API Token (Unlimited)")
|
@@ -13,5 +16,36 @@ api_token = st.text_input("API Token (Unlimited)")
|
|
13 |
# Model selection
|
14 |
model_size = st.selectbox("Model Size", ("tiny", "base", "small", "medium"))
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from stable_whisper import load_model
|
3 |
import requests
|
4 |
import os
|
5 |
|
6 |
# Variables
|
7 |
valid_api_token = st.secrets["API_TOKEN"]
|
8 |
|
9 |
+
# Upload audio file
|
10 |
+
uploaded_file = st.file_uploader("Upload Audio File", type=["mp3", "wav", "mov"])
|
11 |
+
|
12 |
# Free tier or API token option
|
13 |
use_free_tier = st.checkbox("Free Tier (Max 2 minutes)")
|
14 |
api_token = st.text_input("API Token (Unlimited)")
|
|
|
16 |
# Model selection
|
17 |
model_size = st.selectbox("Model Size", ("tiny", "base", "small", "medium"))
|
18 |
|
19 |
+
def transcribe_to_subtitle(audio_bytes, model_name):
|
20 |
+
"""Transcribe audio to subtitle using OpenAI Whisper"""
|
21 |
+
# Load model based on selection
|
22 |
+
model = load_model(model_name)
|
23 |
+
|
24 |
+
# Check file size for free tier
|
25 |
+
if use_free_tier and len(audio_bytes) > 2 * 60 * 1024:
|
26 |
+
st.error("Free tier only supports audio files under 2 minutes")
|
27 |
+
return
|
28 |
+
|
29 |
+
# Transcribe audio
|
30 |
+
result = model.transcribe(audio_bytes)
|
31 |
+
|
32 |
+
# Generate subtitle file
|
33 |
+
subtitle_text = result.text
|
34 |
+
with open("audio.srt", "w") as outfile:
|
35 |
+
outfile.write(subtitle_text)
|
36 |
+
|
37 |
+
# Download option
|
38 |
+
st.success("Transcription successful! Download subtitle file?")
|
39 |
+
if st.button("Download"):
|
40 |
+
st.write("Downloading...")
|
41 |
+
with open("audio.srt", "rb") as f:
|
42 |
+
st.download_button("Download Subtitle", f, "audio.srt")
|
43 |
+
os.remove("audio.srt") # Remove temporary file
|
44 |
+
|
45 |
+
if uploaded_file is not None:
|
46 |
+
audio_bytes = uploaded_file.read()
|
47 |
+
# Check for API token if free tier is not selected
|
48 |
+
if not use_free_tier and not api_token:
|
49 |
+
st.error("API token required for non-free tier usage")
|
50 |
+
else:
|
51 |
+
transcribe_to_subtitle(audio_bytes, model_size)
|