-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathapp.py
More file actions
395 lines (270 loc) · 10.6 KB
/
Copy pathapp.py
File metadata and controls
395 lines (270 loc) · 10.6 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import streamlit as st
import os
from dotenv import load_dotenv
from src.video_info import GetVideo
from src.model import Model
from src.prompt import Prompt
from src.misc import Misc
from src.timestamp_formatter import TimestampFormatter
from src.copy_module_edit import ModuleEditor
from st_copy_to_clipboard import st_copy_to_clipboard
@st.cache_data(show_spinner=False)
def get_transcript_cached(url):
try:
return GetVideo.transcript(url)
except Exception:
return None
@st.cache_data(show_spinner=False)
def get_transcript_time_cached(url):
try:
return GetVideo.transcript_time(url)
except Exception:
return None
class AIVideoSummarizer:
def __init__(self):
load_dotenv()
self.youtube_url = None
self.video_id = None
self.video_title = None
self.video_transcript = None
self.video_transcript_time = None
self.summary = None
self.time_stamps = None
self.transcript = None
self.model_name = None
self.gemini_model_type = "gemini-2.5-flash"
self.openai_model_type = "gpt-5-nano"
self.model_env_checker = []
def header(self):
st.markdown("""
<style>
.main-title{
text-align:center;
font-size:40px;
font-weight:700;
margin-bottom:10px;
}
.sub-title{
text-align:center;
font-size:18px;
opacity:0.7;
margin-bottom:30px;
}
.card{
padding:20px;
border-radius:12px;
border:1px solid rgba(255,255,255,0.1);
background:rgba(255,255,255,0.02);
}
.video-title{
font-size:22px;
font-weight:600;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<div class="main-title">🎬 AI Video Summarizer</div>', unsafe_allow_html=True)
st.markdown('<div class="sub-title">Generate AI summaries, timestamps, and transcripts from YouTube videos</div>', unsafe_allow_html=True)
def get_youtube_info(self):
with st.container():
st.markdown("### 🔗 YouTube Video")
self.youtube_url = st.text_input(
"Paste video link",
placeholder="https://youtube.com/watch?v=..."
)
if os.getenv("GOOGLE_GEMINI_API_KEY"):
self.model_env_checker.append("Gemini")
if os.getenv("OPENAI_API_KEY"):
self.model_env_checker.append("OpenAI")
if not self.model_env_checker:
st.warning("No API keys detected", icon="⚠️")
st.markdown("### 🤖 AI Model")
self.model_name = st.selectbox(
"Select Model",
self.model_env_checker
)
if self.model_name == "Gemini":
gemini_models = [
"gemini-2.5-flash",
"gemini-2.5-flash-lite",
"gemini-3-flash-preview",
"Custom"
]
selected_model = st.selectbox(
"Gemini Model",
gemini_models
)
if selected_model == "Custom":
self.gemini_model_type = st.text_input(
"Enter Gemini model name",
placeholder="ex: gemini-2.5-pro"
)
else:
self.gemini_model_type = selected_model
if self.model_name == "OpenAI":
openai_models = [
"gpt-5-nano",
"gpt-5-mini",
"gpt-4.1",
"gpt-4.1-mini",
"gpt-4.1-nano",
"Custom"
]
selected_model = st.selectbox(
"OpenAI Model",
openai_models
)
if selected_model == "Custom":
self.openai_model_type = st.text_input(
"Enter OpenAI model name",
placeholder="ex: gpt-4o-mini or gpt-5"
)
else:
self.openai_model_type = selected_model
if self.model_name == "OpenAI" and not self.openai_model_type:
st.warning("Please enter an OpenAI model name")
st.stop()
if self.model_name == "Gemini" and not self.gemini_model_type:
st.warning("Please enter a Gemini model name")
st.stop()
if self.youtube_url:
self.video_id = GetVideo.Id(self.youtube_url)
if self.video_id is None:
st.error("Invalid YouTube link")
st.stop()
self.video_title = GetVideo.title(self.youtube_url)
st.divider()
st.markdown(
f'<div class="video-title">{self.video_title}</div>',
unsafe_allow_html=True
)
st.video(self.youtube_url)
# st.image(
# f"https://img.youtube.com/vi/{self.video_id}/maxresdefault.jpg",
# use_container_width=True
# )
def generate_summary(self, loader):
if st.button("✨ Generate Summary", use_container_width=True, disabled=not self.youtube_url):
with st.status(loader, expanded=True) as status:
status.update(label="Fetching transcript...", state="running")
self.video_transcript = get_transcript_cached(self.youtube_url)
status.update(label="Generating AI summary...", state="running")
if self.model_name == "Gemini":
self.summary = Model.google_gemini(
transcript=self.video_transcript,
prompt=Prompt.prompt1(),
model_type=self.gemini_model_type
)
else:
self.summary = Model.openai_gpt(
transcript=self.video_transcript,
prompt=Prompt.prompt1(),
model_type=self.openai_model_type
)
status.update(label="Summary generated!", state="complete")
st.markdown("## 📄 Summary")
st.write(self.summary)
col1, col2 = st.columns(2)
with col1:
st_copy_to_clipboard(self.summary)
with col2:
st.download_button(
"Download",
self.summary,
file_name=f"{self.video_title}_summary.txt"
)
def generate_time_stamps(self, loader):
if st.button("⏱ Generate Timestamps", use_container_width=True, disabled=not self.youtube_url):
with st.status(loader, expanded=True) as status:
status.update(label="Fetching transcript...", state="running")
self.video_transcript_time = get_transcript_time_cached(self.youtube_url)
status.update(label="Generating timestamps...", state="running")
youtube_url_full = f"https://youtube.com/watch?v={self.video_id}"
if self.model_name == "Gemini":
self.time_stamps = Model.google_gemini(
self.video_transcript_time,
Prompt.prompt1(ID='timestamp'),
extra=youtube_url_full,
model_type=self.gemini_model_type
)
else:
self.time_stamps = Model.openai_gpt(
self.video_transcript_time,
Prompt.prompt1(ID='timestamp'),
extra=youtube_url_full,
model_type=self.openai_model_type
)
st.markdown("## ⏰ Timestamps")
st.markdown(self.time_stamps)
cp_text = TimestampFormatter.format(self.time_stamps)
col1, col2 = st.columns(2)
with col1:
st_copy_to_clipboard(cp_text)
with col2:
st.download_button(
"Download",
cp_text,
file_name=f"{self.video_title}_timestamps.txt"
)
def generate_transcript(self, loader):
if st.button("📜 Get Transcript", use_container_width=True, disabled=not self.youtube_url):
with st.status(loader, expanded=True) as status:
status.update(label="Fetching transcript...", state="running")
self.video_transcript = get_transcript_cached(self.youtube_url)
self.transcript = self.video_transcript
status.update(label="Transcript ready!", state="complete")
st.markdown("## Transcript")
st.download_button(
"Download Transcript",
self.transcript,
file_name=f"{self.video_title}_transcript.txt"
)
st.text_area(
"Transcript",
self.transcript,
height=400
)
st_copy_to_clipboard(self.transcript)
def validate_inputs(self):
if not self.youtube_url:
st.warning("Please paste a YouTube URL first.", icon="⚠️")
return False
if not self.video_id:
st.error("Invalid YouTube URL.")
return False
if not self.model_name:
st.warning("Please select an AI model.", icon="⚠️")
return False
return True
def run(self):
st.set_page_config(
page_title="AI Video Summarizer",
page_icon="🎬",
layout="wide"
)
editor = ModuleEditor('st_copy_to_clipboard')
editor.modify_frontend_files()
ran_loader = Misc.loaderx()
n, loader = ran_loader[0], ran_loader[1]
self.header()
left, right = st.columns([1, 1.2], gap="large")
with left:
self.get_youtube_info()
with right:
st.markdown("### ⚡ Generate")
mode = st.radio(
"Select Output",
[":rainbow[**AI Summary**]", ":rainbow[**AI Timestamps**]", "**Transcript**"],
horizontal=True
)
st.divider()
if mode == ":rainbow[**AI Summary**]":
self.generate_summary(loader[n])
elif mode == ":rainbow[**AI Timestamps**]":
self.generate_time_stamps(loader[n])
else:
self.generate_transcript(loader[0])
st.divider()
st.write(Misc.footer(), unsafe_allow_html=True)
if __name__ == "__main__":
app = AIVideoSummarizer()
app.run()