-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
512 lines (452 loc) · 17.4 KB
/
Copy pathapp.py
File metadata and controls
512 lines (452 loc) · 17.4 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from __future__ import annotations
import logging
from dataclasses import asdict
from textwrap import dedent
import streamlit as st
from dotenv import load_dotenv
from youtube_transcript_api import (
CouldNotRetrieveTranscript,
NoTranscriptFound,
TranscriptsDisabled,
YouTubeTranscriptApiException,
)
from youtube_study_tool.demo import build_demo_transcript
from youtube_study_tool.fallback import generate_fallback_bundle
from youtube_study_tool.generation import StudyPackGenerator
from youtube_study_tool.manual import build_manual_transcript
from youtube_study_tool.models import (
MAX_TRANSCRIPT_CHARS,
AnalysisBundle,
TranscriptBundle,
)
from youtube_study_tool.transcripts import (
TranscriptRetrievalError,
TranscriptService,
normalize_languages,
)
from youtube_study_tool.utils import (
escape_html_text,
format_seconds,
sanitize_untrusted_markdown,
timestamp_reference,
)
load_dotenv()
logger = logging.getLogger(__name__)
MAX_RENDERED_SEGMENTS = 200
MAX_TRANSCRIPT_PREVIEW_CHARS = 60_000
MAX_PAID_SUBMISSIONS_PER_SESSION = 3
st.set_page_config(
page_title="YouTube Study Lab",
page_icon="YT",
layout="wide",
)
st.markdown(
"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;700&family=Source+Serif+4:wght@400;600;700&display=swap');
:root {
--paper: #fcf8ef;
--ink: #17222d;
--accent: #9a4f2f;
--accent-soft: #f5d3b6;
--card: rgba(255, 255, 255, 0.82);
--border: rgba(23, 34, 45, 0.1);
}
.stApp {
background:
radial-gradient(circle at top left, rgba(248, 209, 163, 0.6), transparent 30%),
radial-gradient(circle at top right, rgba(175, 214, 197, 0.55), transparent 28%),
linear-gradient(180deg, #fff8eb 0%, #f3efe5 48%, #edf4ef 100%);
color: var(--ink);
}
.block-container {
max-width: 1180px;
padding-top: 2rem;
padding-bottom: 4rem;
}
h1, h2, h3 {
font-family: "Space Grotesk", sans-serif;
color: var(--ink);
letter-spacing: -0.02em;
}
p, li, label, .stMarkdown, .stTextInput, .stTextArea {
font-family: "Source Serif 4", serif;
}
.hero {
background: var(--card);
border: 1px solid var(--border);
border-radius: 28px;
padding: 1.6rem 1.7rem;
box-shadow: 0 20px 60px rgba(62, 43, 31, 0.08);
margin-bottom: 1.25rem;
}
.hero-kicker {
display: inline-block;
padding: 0.25rem 0.6rem;
border-radius: 999px;
background: var(--accent-soft);
color: var(--ink);
font-family: "Space Grotesk", sans-serif;
font-size: 0.84rem;
margin-bottom: 0.65rem;
}
.hero h1 {
margin-bottom: 0.35rem;
}
.hero-copy {
max-width: 760px;
font-size: 1.08rem;
line-height: 1.6;
margin-bottom: 1rem;
}
.trust-row {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.trust-pill {
background: rgba(23, 34, 45, 0.06);
border: 1px solid var(--border);
border-radius: 999px;
color: var(--ink);
font-family: "Space Grotesk", sans-serif;
font-size: 0.78rem;
font-weight: 500;
padding: 0.38rem 0.66rem;
}
.section-label {
color: var(--accent);
font-family: "Space Grotesk", sans-serif;
font-size: 0.78rem;
font-weight: 700;
letter-spacing: 0.08em;
margin: 0.2rem 0 0.5rem;
text-transform: uppercase;
}
div.stButton > button[kind="primary"] {
background: var(--accent);
border-color: var(--accent);
color: #ffffff;
font-family: "Space Grotesk", sans-serif;
font-weight: 700;
}
div.stButton > button[kind="primary"]:hover {
background: #7f3f26;
border-color: #7f3f26;
color: #ffffff;
}
.meta-card {
background: rgba(255, 255, 255, 0.78);
border: 1px solid var(--border);
border-radius: 22px;
padding: 1rem 1.1rem;
min-height: 100%;
}
.meta-label {
font-family: "Space Grotesk", sans-serif;
font-size: 0.82rem;
text-transform: uppercase;
letter-spacing: 0.08em;
opacity: 0.72;
}
.meta-value {
font-family: "Space Grotesk", sans-serif;
font-size: 1.2rem;
margin-top: 0.3rem;
}
</style>
""",
unsafe_allow_html=True,
)
def render_hero() -> None:
st.markdown(
"""
<section class="hero">
<div class="hero-kicker">One link. A complete study pack.</div>
<h1>YouTube Study Lab</h1>
<p class="hero-copy">Turn a captioned YouTube video into a structured summary, revision notes, and an active-recall quiz—without scrubbing through the timeline again.</p>
<div class="trust-row">
<span class="trust-pill">No API key required</span>
<span class="trust-pill">Timestamped sources</span>
<span class="trust-pill">18-question quiz</span>
<span class="trust-pill">Markdown export</span>
</div>
</section>
""",
unsafe_allow_html=True,
)
def render_meta(bundle: TranscriptBundle, analysis: AnalysisBundle) -> None:
col1, col2, col3, col4 = st.columns(4)
cards = [
("Transcript", bundle.language_name),
(
"Source",
bundle.source_label
or ("Auto captions" if bundle.is_generated else "Manual captions"),
),
("Type", analysis.classification.video_type.title()),
("Generator", f"{analysis.provider} ({analysis.model})"),
]
for column, (label, value) in zip((col1, col2, col3, col4), cards):
safe_label = escape_html_text(label)
safe_value = escape_html_text(value)
column.markdown(
f"""
<div class="meta-card">
<div class="meta-label">{safe_label}</div>
<div class="meta-value">{safe_value}</div>
</div>
""",
unsafe_allow_html=True,
)
def render_transcript_tab(bundle: TranscriptBundle) -> None:
st.download_button(
label="Download transcript (.txt)",
data=bundle.transcript_text,
file_name=f"{bundle.video_id}-transcript.txt",
mime="text/plain",
use_container_width=True,
)
preview = bundle.transcript_text[:MAX_TRANSCRIPT_PREVIEW_CHARS]
if len(bundle.transcript_text) > MAX_TRANSCRIPT_PREVIEW_CHARS:
preview += "\n\n[Preview truncated; download the transcript for the full text.]"
st.text_area("Transcript preview", value=preview, height=320)
if bundle.duration_seconds > 0:
with st.expander("Timestamped transcript"):
for segment in bundle.segments[:MAX_RENDERED_SEGMENTS]:
reference = timestamp_reference(
bundle.video_id,
segment.start,
linked=bool(bundle.source_url),
)
if bundle.source_url:
st.markdown(reference)
st.text(segment.text)
if len(bundle.segments) > MAX_RENDERED_SEGMENTS:
st.caption(
f"Showing the first {MAX_RENDERED_SEGMENTS} caption segments. "
"Download the transcript for the complete text."
)
else:
st.caption("Timestamps are unavailable for pasted transcript text.")
def render_classification_tab(analysis: AnalysisBundle) -> None:
classification = analysis.classification
st.markdown(f"### {classification.video_type.title()}")
st.markdown(f"**Confidence:** {classification.confidence:.2f}")
st.text(f"Reason: {classification.reason}")
st.text(f"Best summary style: {classification.best_summary_style}")
st.text(f"Best note style: {classification.best_note_style}")
st.json(asdict(classification))
def compile_study_pack(bundle: TranscriptBundle, analysis: AnalysisBundle) -> str:
title = sanitize_untrusted_markdown(bundle.video_title or bundle.video_id)
classification_reason = sanitize_untrusted_markdown(analysis.classification.reason)
return dedent(
f"""
# {title}
Source: {bundle.source_url or bundle.source_label or "Unknown"}
Transcript language: {bundle.language_name} ({bundle.language_code})
Duration: {format_seconds(bundle.duration_seconds)}
Generated with: {analysis.provider} ({analysis.model})
Video type: {analysis.classification.video_type} ({analysis.classification.confidence:.2f})
Classification reason: {classification_reason}
{sanitize_untrusted_markdown(analysis.summary)}
{sanitize_untrusted_markdown(analysis.study_notes)}
{sanitize_untrusted_markdown(analysis.quiz)}
"""
).strip()
def generate_study_pack(
generator: StudyPackGenerator, bundle: TranscriptBundle
) -> AnalysisBundle:
"""Cap paid submissions per Streamlit session and keep the local fallback available."""
if not generator.is_ready:
return generator.generate(bundle)
used = int(st.session_state.get("paid_generation_submissions", 0))
if used >= MAX_PAID_SUBMISSIONS_PER_SESSION:
st.info(
"The provider-session limit has been reached. This pack uses local generation "
"so repeated submissions cannot create unbounded paid calls."
)
return generate_fallback_bundle(bundle)
st.session_state["paid_generation_submissions"] = used + 1
return generator.generate(bundle)
def run() -> None:
render_hero()
transcript_service = TranscriptService()
generator = StudyPackGenerator()
with st.sidebar:
st.header("Study settings")
language_input = st.text_input(
"Preferred transcript languages", value="en,en-US,en-GB"
)
if generator.settings.provider == "heuristic":
st.success(
"No-key mode ready. Study packs are generated with local rules after transcript retrieval."
)
elif generator.is_ready:
st.success(f"{generator.provider_label} is ready.")
else:
st.warning(
f"{generator.provider_label} is not fully configured; no-key mode will be used."
)
with st.expander("Provider details"):
st.caption(
"Provider and model are controlled through `.env`; keys are never entered in this interface."
)
st.markdown(f"**Provider:** {generator.provider_label}")
st.markdown(f"**Model/deployment:** `{generator.model_name}`")
st.markdown(
f"**Summary profile:** `{generator.settings.summary_style}` / `{generator.settings.summary_detail}`"
)
st.caption(generator.status_message)
st.caption(
"A public caption track is required when you use your own YouTube link."
)
st.markdown('<div class="section-label">Start here</div>', unsafe_allow_html=True)
demo_requested = st.button(
"See a complete study pack instantly",
key="instant-demo",
use_container_width=True,
type="primary",
)
st.caption(
"Uses an original sample transcript and local generation—no YouTube request or API key."
)
st.markdown(
'<div class="section-label">Or use your own video</div>', unsafe_allow_html=True
)
with st.form("analyze-form"):
source = st.text_input(
"YouTube URL or video ID",
placeholder="https://www.youtube.com/watch?v=...",
)
submitted = st.form_submit_button("Build study pack", use_container_width=True)
with st.expander("YouTube blocked? Paste a transcript instead"):
st.caption(
"Useful on hosted servers where YouTube blocks transcript requests. No timestamps are invented."
)
with st.form("manual-transcript-form"):
manual_title = st.text_input(
"Title",
placeholder="My lecture notes",
key="manual-title",
)
manual_text = st.text_area(
"Transcript text",
placeholder="Paste public or personal transcript text here...",
height=180,
max_chars=MAX_TRANSCRIPT_CHARS,
key="manual-transcript",
)
manual_submitted = st.form_submit_button(
"Build from pasted transcript",
use_container_width=True,
key="manual-submit",
)
if demo_requested or submitted or manual_submitted:
# Results belong to the last submitted input. Clear them before any
# validation or retrieval so a failed submission cannot resurrect an
# older study pack on the next rerun.
st.session_state.pop("transcript_bundle", None)
st.session_state.pop("analysis_bundle", None)
if demo_requested:
with st.spinner("Loading the network-free demo..."):
transcript = build_demo_transcript()
analysis = generate_fallback_bundle(transcript)
st.session_state["transcript_bundle"] = transcript
st.session_state["analysis_bundle"] = analysis
st.toast("Instant demo ready", icon="✅")
elif submitted:
if not source.strip():
st.warning("Paste a YouTube URL or ID to get started.")
return
languages = normalize_languages(language_input)
try:
with st.spinner("Pulling transcript from YouTube..."):
transcript = transcript_service.fetch(source, languages)
with st.spinner("Building summary, notes, and quiz..."):
analysis = generate_study_pack(generator, transcript)
except ValueError as error:
st.error(str(error))
return
except (
NoTranscriptFound,
TranscriptsDisabled,
CouldNotRetrieveTranscript,
TranscriptRetrievalError,
YouTubeTranscriptApiException,
) as error:
logger.warning("Transcript extraction failed: %s", error, exc_info=True)
st.error(
"Transcript extraction failed. The video may be unavailable, "
"blocked, or missing a public caption track."
)
return
except Exception: # keep the UI alive for provider failures.
logger.exception("Unexpected error while building a YouTube study pack")
st.error("Could not build the study pack. Please try again.")
return
st.session_state["transcript_bundle"] = transcript
st.session_state["analysis_bundle"] = analysis
elif manual_submitted:
try:
transcript = build_manual_transcript(manual_text, title=manual_title)
with st.spinner("Building summary, notes, and quiz..."):
analysis = generate_study_pack(generator, transcript)
except ValueError as error:
st.error(str(error))
return
except Exception: # keep the UI alive for generation failures.
logger.exception("Unexpected error while building a pasted study pack")
st.error("Could not build the study pack. Please try again.")
return
st.session_state["transcript_bundle"] = transcript
st.session_state["analysis_bundle"] = analysis
transcript_bundle = st.session_state.get("transcript_bundle")
analysis_bundle = st.session_state.get("analysis_bundle")
if not transcript_bundle or not analysis_bundle:
st.markdown(
"""
### What this app does
- Fetches public YouTube captions or accepts pasted transcript text
- Builds a structured summary for fast review
- Generates study notes for revision
- Creates quiz questions for active recall
"""
)
return
title = transcript_bundle.video_title or transcript_bundle.video_id
st.markdown(f"### {sanitize_untrusted_markdown(title)}")
if transcript_bundle.source_url:
st.video(transcript_bundle.source_url)
elif transcript_bundle.source_label == "Pasted transcript":
st.info(
"Pasted transcript: the study pack was generated without a YouTube request or invented timestamps."
)
else:
st.info(
"Instant demo: an original sample transcript is being processed locally without YouTube or an API key."
)
render_meta(transcript_bundle, analysis_bundle)
pack_text = compile_study_pack(transcript_bundle, analysis_bundle)
st.download_button(
label="Download complete study pack (.md)",
data=pack_text,
file_name=f"{transcript_bundle.video_id}-study-pack.md",
mime="text/markdown",
use_container_width=True,
)
summary_tab, notes_tab, quiz_tab, classification_tab, transcript_tab = st.tabs(
["Summary", "Study Notes", "Quiz", "Classification", "Transcript"]
)
with summary_tab:
st.markdown(sanitize_untrusted_markdown(analysis_bundle.summary))
with notes_tab:
st.markdown(sanitize_untrusted_markdown(analysis_bundle.study_notes))
with quiz_tab:
st.markdown(sanitize_untrusted_markdown(analysis_bundle.quiz))
with classification_tab:
render_classification_tab(analysis_bundle)
with transcript_tab:
render_transcript_tab(transcript_bundle)
if __name__ == "__main__":
run()