Spaces:
Sleeping
Sleeping
cptsubtext
commited on
Commit
·
04c40a3
1
Parent(s):
9a42449
update example
Browse files- app.py +77 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from stable_whisper import load_model
|
| 3 |
+
from stable_whisper import load_hf_whisper
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
import webvtt
|
| 6 |
+
import pysrt
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Variables
|
| 11 |
+
#valid_api_token = st.secrets["API_TOKEN"]
|
| 12 |
+
|
| 13 |
+
st.title("Speech-to-Text")
|
| 14 |
+
|
| 15 |
+
with st.expander("README"):
|
| 16 |
+
st.write("This little tool accepts and audiofile. After choosing the model a WebVTT file will be generated. The content of the WebVTT will be shown and a user can choose to download it. This can be used as Subtitle file e.g. in Davinci Resolve Import Subtitles" )
|
| 17 |
+
|
| 18 |
+
# Upload audio file
|
| 19 |
+
uploaded_file = st.file_uploader("Upload Audio File", type=["mp3", "wav", "mov"])
|
| 20 |
+
|
| 21 |
+
# Free tier or API token option
|
| 22 |
+
use_free_tier = st.checkbox("Free Tier (Max 2 minutes)")
|
| 23 |
+
api_token = st.text_input("API Token (Unlimited)")
|
| 24 |
+
|
| 25 |
+
# Should we translate to english?
|
| 26 |
+
translate = st.checkbox("Would you like a translation to english?")
|
| 27 |
+
|
| 28 |
+
# Model selection
|
| 29 |
+
model_size = st.selectbox("Model Size", ("tiny", "base", "small", "medium"))
|
| 30 |
+
|
| 31 |
+
def transcribe_to_subtitle(audio_bytes, model_name):
|
| 32 |
+
"""Transcribe audio to subtitle using OpenAI Whisper"""
|
| 33 |
+
# Load model based on selection
|
| 34 |
+
model = load_model(model_name)
|
| 35 |
+
#speedmodel = load_hf_whisper(model_name)
|
| 36 |
+
|
| 37 |
+
# Check how long the audio is free tier
|
| 38 |
+
# newAudio = AudioSegment.from_wav("audiofiles/download.wav")
|
| 39 |
+
#if use_free_tier and len(audio_bytes) > 0.048 * 2 * 60 * 1024:
|
| 40 |
+
# st.error(len(audio_bytes))
|
| 41 |
+
# st.error("Free tier only supports audio files under 2 minutes")
|
| 42 |
+
# return
|
| 43 |
+
|
| 44 |
+
# Transcribe audio
|
| 45 |
+
try:
|
| 46 |
+
if translate:
|
| 47 |
+
result = model.transcribe(audio_bytes, verbose=True, task = 'translate')
|
| 48 |
+
result.to_srt_vtt('audio.srt')
|
| 49 |
+
else:
|
| 50 |
+
result = model.transcribe(audio_bytes, verbose=True)
|
| 51 |
+
result.to_srt_vtt('audio.srt')
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return {"error": f"Error during transcription: {str(e)}"}
|
| 54 |
+
|
| 55 |
+
captions = pysrt.open("audio.srt")
|
| 56 |
+
for caption in captions:
|
| 57 |
+
print(caption.start)
|
| 58 |
+
print(caption.text)
|
| 59 |
+
print(caption.end)
|
| 60 |
+
print()
|
| 61 |
+
|
| 62 |
+
output = captions.text
|
| 63 |
+
st.markdown(output, unsafe_allow_html=True)
|
| 64 |
+
|
| 65 |
+
# Download option
|
| 66 |
+
st.success("Transcription successful! Download subtitle file?")
|
| 67 |
+
with open("audio.srt", "rb") as f:
|
| 68 |
+
st.download_button("Download Subtitle in WebVtt Format", f, "audio.srt")
|
| 69 |
+
os.remove("audio.srt") # Remove temporary file
|
| 70 |
+
|
| 71 |
+
if uploaded_file is not None:
|
| 72 |
+
audio_bytes = uploaded_file.read()
|
| 73 |
+
# Check for API token if free tier is not selected
|
| 74 |
+
if not use_free_tier and not api_token:
|
| 75 |
+
st.error("API token required for non-free tier usage")
|
| 76 |
+
else:
|
| 77 |
+
transcribe_to_subtitle(audio_bytes, model_size)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
numpy
|
| 3 |
+
tzdata
|
| 4 |
+
urllib3
|
| 5 |
+
watchdog
|
| 6 |
+
webvtt-py
|
| 7 |
+
pydub
|
| 8 |
+
pysrt
|