-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
144 lines (120 loc) · 4.15 KB
/
Copy pathapp.py
File metadata and controls
144 lines (120 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import re
from uuid import uuid4
import streamlit as st
from src.download import video_title, download_subtitles, postprocess
from src.llm import create_timecodes
from dotenv import load_dotenv
load_dotenv()
def main():
st.title("YouTube Timecodes Generator")
# Language selection
languages = {
"Arabic": "ar",
"Bengali": "bn",
"Bulgarian": "bg",
"Catalan": "ca",
"Chinese": "cn",
"Croatian": "hr",
"Czech": "cs",
"Danish": "da",
"Dutch": "nl",
"English": "en",
"Finnish": "fi",
"French": "fr",
"German": "de",
"Greek": "el",
"Gujarati": "gu",
"Hebrew": "he",
"Hindi": "hi",
"Hungarian": "hu",
"Indonesian": "id",
"Italian": "it",
"Japanese": "ja",
"Kannada": "kn",
"Korean": "ko",
"Latvian": "lv",
"Lithuanian": "lt",
"Malay": "ms",
"Malayalam": "ml",
"Marathi": "mr",
"Norwegian": "no",
"Polish": "pl",
"Portuguese": "pt",
"Romanian": "ro",
"Russian": "ru",
"Serbian": "sr",
"Slovak": "sk",
"Slovenian": "sl",
"Spanish": "es",
"Swedish": "sv",
"Tamil": "ta",
"Telugu": "te",
"Thai": "th",
"Turkish": "tr",
"Ukrainian": "uk",
"Vietnamese": "vi",
}
selected_language = st.selectbox(
"Select subtitle language:",
options=list(languages.keys()),
index=9, # Default to English
)
# Paste url to youtube video
youtube_url = st.text_input("Enter the link to the video on YouTube:")
# Regex check youtube url
if re.match(r"^https://www.youtube.com/watch\?v=[a-zA-Z0-9_-]*$", youtube_url) or re.match(
r"^https://www.youtube.com/shorts/[a-zA-Z0-9_-]*$", youtube_url
):
# Display video
st.video(youtube_url)
transcribe_button = st.empty()
title_placeholder = st.empty()
progress_placeholder = st.empty()
# Button to download audio from youtube video
if transcribe_button.button("Generate timecodes"):
# Download audio
try:
transcribe_button.empty()
# Set video title
title = video_title(youtube_url)
title_placeholder.title(title)
progress_placeholder.text("Downloading subtitles...")
# Create a runtimes folder and runtime id
directory = os.getcwd() + "/runtimes"
if not os.path.exists(directory):
os.makedirs(directory)
runtime_id = str(uuid4())
output_path = directory + "/" + runtime_id
# Download audio to runtimes/ folder with selected language
output_path = download_subtitles(
youtube_url, output_path, language=languages[selected_language]
)
print(f"Subtitle file path: {output_path}")
subtitle_text = postprocess(output_path)
raise Exception("Stop here")
except Exception as e:
print(e)
st.error(
"Please enter correct YouTube link or subtitles might not be available in selected language!"
)
transcribe_button.empty()
title_placeholder.empty()
progress_placeholder.empty()
st.stop()
# Summarize
try:
assert os.environ["LLM_API_KEY"], "LLM_API_KEY not found!"
progress_placeholder.text("Timecodes creating...")
# Summarize text
timecodes = create_timecodes(subtitle_text, selected_language)
st.text_area("Result", timecodes, height=300)
except Exception as e:
print(e)
st.error("Timecodes generation error. Please try again!")
title_placeholder.empty()
progress_placeholder.empty()
st.stop()
progress_placeholder.empty()
if __name__ == "__main__":
main()