File size: 23,388 Bytes
f7e925b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
"""
app.py β€” NotebookLM 2.0
Gradio frontend / Hugging Face Spaces entry point.

Screens:
  1. Login  β€” username + password (configured via USERS env var)
  2. Main   β€” Notebook selector, then three tabs:
              πŸ’¬ Chat | πŸ“‚ Sources | ✨ Artifacts
"""

import os
from pathlib import Path
from dotenv import load_dotenv
import gradio as gr

load_dotenv()


# ---------------------------------------------------------------------------
# Auth
# ---------------------------------------------------------------------------

def _parse_users() -> dict[str, str]:
    """
    Parse USERS env var into {username: password}.
    Format: "alice:pass1,bob:pass2"
    Falls back to demo:demo if not set.
    """
    raw = os.getenv("USERS", "demo:demo")
    users = {}
    for pair in raw.split(","):
        pair = pair.strip()
        if ":" in pair:
            u, p = pair.split(":", 1)
            users[u.strip()] = p.strip()
    return users

USERS = _parse_users()


def login(username: str, password: str) -> tuple[bool, str]:
    """Return (success, username_or_error_message)."""
    if not username or not password:
        return False, "Please enter both username and password."
    if USERS.get(username) == password:
        return True, username
    return False, "Invalid username or password."


# ---------------------------------------------------------------------------
# Backend (lazy import so HF Spaces cold-start doesn't fail on missing keys)
# ---------------------------------------------------------------------------

def _be():
    """Return dict of backend callables."""
    from backend.storage import (
        get_user_notebooks,
        create_notebook_for_user,
        delete_notebook,
    )
    from backend.ingestion import ingest_source, list_indexed_sources
    from backend.chat import chat_with_sources
    from backend.artifacts import generate_report, generate_quiz, generate_podcast
    return {
        "get_notebooks":   get_user_notebooks,
        "create_notebook": create_notebook_for_user,
        "delete_notebook": delete_notebook,
        "list_sources":    list_indexed_sources,
        "ingest":          ingest_source,
        "chat":            chat_with_sources,
        "report":          generate_report,
        "quiz":            generate_quiz,
        "podcast":         generate_podcast,
    }


# ---------------------------------------------------------------------------
# Notebook dropdown helpers
# ---------------------------------------------------------------------------

def _nb_choices(username: str) -> list[str]:
    """Return list of 'Notebook Name  [uuid]' strings for the dropdown."""
    nbs = _be()["get_notebooks"](username)
    return [f"{nb['name']}  [{nb['id']}]" for nb in nbs]


def _parse_choice(choice: str) -> tuple[str, str]:
    """Extract (name, nb_id) from a dropdown choice string."""
    if not choice:
        return "", ""
    name, _, rest = choice.rpartition("  [")
    return name.strip(), rest.rstrip("]").strip()


def _sources_md(username: str, nb_id: str) -> str:
    """Return a Markdown list of indexed source names for a notebook."""
    if not username or not nb_id:
        return "_No notebook selected._"
    try:
        sources = _be()["list_sources"](username, nb_id)
        if not sources:
            return "_No sources indexed yet. Upload files or add a URL in the Sources tab._"
        return "\n".join(f"- `{s}`" for s in sources)
    except Exception as e:
        return f"_Could not load sources: {e}_"


# ---------------------------------------------------------------------------
# CSS β€” dark editorial theme
# ---------------------------------------------------------------------------

CSS = """
@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700&family=JetBrains+Mono:wght@300;400;500&display=swap');

:root {
    --bg:         #0a0a0f;
    --bg2:        #111118;
    --bg3:        #16161f;
    --bg4:        #1c1c28;
    --accent:     #7c6af7;
    --accent2:    #9d8fff;
    --glow:       rgba(124,106,247,0.15);
    --text:       #e8e6f0;
    --text2:      #8b87a8;
    --muted:      #504d6e;
    --border:     #2a2840;
    --border2:    #3d3960;
    --ok:         #5af0a0;
    --warn:       #f0c45a;
    --err:        #f05a7a;
    --r:          10px;
    --mono:       'JetBrains Mono', 'Fira Code', monospace;
    --display:    'Playfair Display', Georgia, serif;
}

*, *::before, *::after { box-sizing: border-box; }

body, .gradio-container {
    background: var(--bg) !important;
    color: var(--text) !important;
    font-family: var(--mono) !important;
}

#login-wrap {
    max-width: 420px;
    margin: 72px auto 0;
    padding: 48px 40px 40px;
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: 18px;
    box-shadow: 0 0 80px var(--glow);
}
#login-title {
    font-family: var(--display);
    font-size: 2rem;
    font-weight: 700;
    text-align: center;
    margin-bottom: 4px;
    color: var(--text);
    letter-spacing: -0.5px;
}
#login-sub {
    font-size: 0.72rem;
    color: var(--muted);
    text-align: center;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    margin-bottom: 36px;
}

#hdr {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 28px;
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
}
#hdr-title {
    font-family: var(--display);
    font-size: 1.35rem;
    color: var(--text);
}
#hdr-title span { color: var(--accent2); }

#nb-bar {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    padding: 12px 20px;
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
}

.gr-button, button {
    font-family: var(--mono) !important;
    font-size: 0.8rem !important;
    font-weight: 500 !important;
    letter-spacing: 0.04em !important;
    border-radius: var(--r) !important;
    transition: all 0.18s ease !important;
}
.gr-button-primary {
    background: var(--accent) !important;
    color: #fff !important;
    border: none !important;
}
.gr-button-primary:hover {
    background: var(--accent2) !important;
    box-shadow: 0 0 18px var(--glow) !important;
    transform: translateY(-1px) !important;
}
.gr-button-stop {
    background: transparent !important;
    color: var(--err) !important;
    border: 1px solid var(--err) !important;
}

input, textarea, .gr-textbox textarea {
    background: var(--bg4) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--r) !important;
    color: var(--text) !important;
    font-family: var(--mono) !important;
    font-size: 0.85rem !important;
    transition: border-color 0.2s, box-shadow 0.2s !important;
}
input:focus, textarea:focus {
    border-color: var(--accent) !important;
    box-shadow: 0 0 0 3px var(--glow) !important;
    outline: none !important;
}

label, .gr-block-label {
    color: var(--text2) !important;
    font-family: var(--mono) !important;
    font-size: 0.72rem !important;
    font-weight: 500 !important;
    letter-spacing: 0.08em !important;
    text-transform: uppercase !important;
}

.tab-nav button {
    background: transparent !important;
    border: none !important;
    border-bottom: 2px solid transparent !important;
    color: var(--text2) !important;
    font-family: var(--mono) !important;
    font-size: 0.78rem !important;
    font-weight: 500 !important;
    letter-spacing: 0.1em !important;
    text-transform: uppercase !important;
    padding: 12px 20px !important;
    transition: all 0.2s !important;
}
.tab-nav button.selected {
    color: var(--accent2) !important;
    border-bottom-color: var(--accent) !important;
}

.chatbot {
    background: var(--bg) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--r) !important;
}
.message.user {
    background: var(--bg4) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--r) !important;
    font-family: var(--mono) !important;
    font-size: 0.85rem !important;
    color: var(--text) !important;
}
.message.bot {
    background: var(--bg3) !important;
    border: 1px solid var(--border2) !important;
    border-left: 3px solid var(--accent) !important;
    border-radius: var(--r) !important;
    font-family: var(--mono) !important;
    font-size: 0.85rem !important;
    color: var(--text) !important;
}

.gr-markdown {
    background: var(--bg3) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--r) !important;
    padding: 20px !important;
    font-family: var(--mono) !important;
    font-size: 0.84rem !important;
    line-height: 1.75 !important;
    color: var(--text) !important;
}
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3 {
    font-family: var(--display) !important;
    color: var(--text) !important;
}

select, .gr-dropdown select {
    background: var(--bg4) !important;
    border: 1px solid var(--border) !important;
    color: var(--text) !important;
    font-family: var(--mono) !important;
    border-radius: var(--r) !important;
}

.gr-accordion {
    background: var(--bg3) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--r) !important;
    margin-bottom: 8px !important;
}

.ok   { color: var(--ok)   !important; }
.warn { color: var(--warn) !important; }
.err  { color: var(--err)  !important; }

.gr-file-upload {
    background: var(--bg3) !important;
    border: 1px dashed var(--border2) !important;
    border-radius: var(--r) !important;
}

::-webkit-scrollbar            { width: 5px; height: 5px; }
::-webkit-scrollbar-track      { background: var(--bg); }
::-webkit-scrollbar-thumb      { background: var(--border2); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover{ background: var(--accent); }

audio { width: 100%; border-radius: var(--r); }
"""


# ---------------------------------------------------------------------------
# Build UI
# ---------------------------------------------------------------------------

def build_ui() -> gr.Blocks:

    with gr.Blocks(css=CSS, title="NotebookLM 2.0", theme=gr.themes.Base()) as demo:

        # ── Persistent state ───────────────────────────────────────────────
        s_user  = gr.State("")
        s_nb_id = gr.State("")

        # ==================================================================
        # LOGIN SCREEN
        # ==================================================================
        with gr.Column(elem_id="login-wrap", visible=True) as screen_login:
            gr.HTML('<div id="login-title">NotebookLM 2.0</div>')
            gr.HTML('<div id="login-sub">AI Research Assistant</div>')
            inp_user  = gr.Textbox(label="Username", placeholder="username")
            inp_pass  = gr.Textbox(label="Password", placeholder="password", type="password")
            btn_login = gr.Button("Sign In", variant="primary")
            msg_login = gr.Markdown("")

        # ==================================================================
        # MAIN APP
        # ==================================================================
        with gr.Column(visible=False) as screen_main:

            # Header
            with gr.Row(elem_id="hdr"):
                gr.HTML('<div id="hdr-title">Notebook<span>LM</span> 2.0</div>')
                lbl_user = gr.Markdown("", elem_classes=["ok"])

            # Notebook bar
            with gr.Row(elem_id="nb-bar"):
                dd_nb       = gr.Dropdown(choices=[], label="Active Notebook", scale=4, interactive=True)
                inp_nb_name = gr.Textbox(placeholder="New notebook name…", label="", scale=3, show_label=False)
                btn_create  = gr.Button("οΌ‹ Create", scale=1, variant="primary")
                btn_delete  = gr.Button("βœ• Delete",  scale=1, variant="stop")

            md_nb_status = gr.Markdown("")

            # ── Tabs ───────────────────────────────────────────────────────
            with gr.Tabs():

                # TAB 1 β€” CHAT
                with gr.TabItem("πŸ’¬  Chat"):
                    chatbot = gr.Chatbot(
                        label="", height=460, type="messages",
                        show_label=False, bubble_full_width=False,
                    )
                    with gr.Row():
                        inp_chat  = gr.Textbox(
                            placeholder="Ask anything about your sources…",
                            label="", show_label=False, scale=5, lines=1,
                        )
                        btn_send  = gr.Button("Send β†’", scale=1, variant="primary")
                    btn_clear = gr.Button("Clear chat", size="sm")

                # TAB 2 β€” SOURCES
                with gr.TabItem("πŸ“‚  Sources"):
                    with gr.Row():
                        with gr.Column(scale=1):
                            gr.Markdown("### Upload Files")
                            inp_files  = gr.File(
                                label="PDF / PPTX / TXT",
                                file_types=[".pdf", ".pptx", ".txt"],
                                file_count="multiple",
                            )
                            btn_upload = gr.Button("Ingest Files", variant="primary")
                        with gr.Column(scale=1):
                            gr.Markdown("### Add URL")
                            inp_url  = gr.Textbox(label="Web URL", placeholder="https://…")
                            btn_url  = gr.Button("Ingest URL", variant="primary")

                    md_ingest = gr.Markdown("")
                    gr.Markdown("---")
                    gr.Markdown("### Indexed Sources")
                    md_sources  = gr.Markdown("_No sources indexed yet._")
                    btn_refresh = gr.Button("↻ Refresh", size="sm")

                # TAB 3 β€” ARTIFACTS
                with gr.TabItem("✨  Artifacts"):
                    with gr.Row():
                        btn_report  = gr.Button("πŸ“„  Generate Report",  variant="primary", scale=1)
                        btn_quiz    = gr.Button("❓  Generate Quiz",    variant="primary", scale=1)
                        btn_podcast = gr.Button("πŸŽ™οΈ  Generate Podcast", variant="primary", scale=1)

                    md_art_status = gr.Markdown("")

                    with gr.Accordion("πŸ“„  Report", open=False):
                        md_report = gr.Markdown("")
                    with gr.Accordion("❓  Quiz", open=False):
                        md_quiz = gr.Markdown("")
                    with gr.Accordion("πŸŽ™οΈ  Podcast", open=False):
                        md_transcript = gr.Markdown("")
                        aud_podcast   = gr.Audio(label="Listen", visible=False)

        # ==================================================================
        # EVENT HANDLERS
        # ==================================================================

        # ── Login ──────────────────────────────────────────────────────────
        def do_login(username, password):
            ok, result = login(username, password)
            if ok:
                choices = _nb_choices(result)
                return (
                    gr.update(visible=False),
                    gr.update(visible=True),
                    result,
                    gr.update(choices=choices, value=choices[0] if choices else None),
                    f"✦ **{result}**",
                    "",
                )
            return (
                gr.update(visible=True),
                gr.update(visible=False),
                "", gr.update(), "",
                f"⚠ {result}",
            )

        for trigger in [btn_login.click, inp_pass.submit]:
            trigger(
                do_login,
                inputs=[inp_user, inp_pass],
                outputs=[screen_login, screen_main, s_user, dd_nb, lbl_user, msg_login],
            )

        # ── Notebook selection ─────────────────────────────────────────────
        def on_nb_select(choice):
            _, nb_id = _parse_choice(choice)
            return nb_id

        dd_nb.change(on_nb_select, inputs=[dd_nb], outputs=[s_nb_id])

        # ── Create notebook ────────────────────────────────────────────────
        def do_create(username, name):
            if not name.strip():
                return gr.update(), "", "⚠ Enter a notebook name."
            if not username:
                return gr.update(), "", "⚠ Not signed in."
            _be()["create_notebook"](username, name.strip())
            choices = _nb_choices(username)
            return gr.update(choices=choices, value=choices[-1]), "", f"✦ Created **{name.strip()}**"

        btn_create.click(
            do_create,
            inputs=[s_user, inp_nb_name],
            outputs=[dd_nb, inp_nb_name, md_nb_status],
        )

        # ── Delete notebook ────────────────────────────────────────────────
        def do_delete(username, nb_id):
            if not nb_id:
                return gr.update(), "⚠ No notebook selected."
            _be()["delete_notebook"](username, nb_id)
            choices = _nb_choices(username)
            return gr.update(choices=choices, value=choices[0] if choices else None), "✦ Deleted."

        btn_delete.click(do_delete, inputs=[s_user, s_nb_id], outputs=[dd_nb, md_nb_status])

        # ── Chat ───────────────────────────────────────────────────────────
        def do_chat(message, nb_id, username, history):
            if not message.strip():
                return history, ""
            if not nb_id:
                history = (history or []) + [{"role": "assistant", "content": "⚠ Select a notebook first."}]
                return history, ""
            history = list(history or [])
            history.append({"role": "user", "content": message})
            reply = _be()["chat"](message, nb_id, username, history[:-1])
            history.append({"role": "assistant", "content": reply})
            return history, ""

        btn_send.click(do_chat, inputs=[inp_chat, s_nb_id, s_user, chatbot], outputs=[chatbot, inp_chat])
        inp_chat.submit(do_chat, inputs=[inp_chat, s_nb_id, s_user, chatbot], outputs=[chatbot, inp_chat])
        btn_clear.click(lambda: [], outputs=[chatbot])

        # ── Ingest files ───────────────────────────────────────────────────
        def do_upload(files, nb_id, username):
            if not nb_id:
                return "⚠ Select a notebook first.", _sources_md(username, nb_id)
            if not files:
                return "⚠ No files selected.", _sources_md(username, nb_id)
            msgs = []
            for f in files:
                path = f.name if hasattr(f, "name") else str(f)
                raw  = Path(path).read_bytes()
                try:
                    res = _be()["ingest"](username, nb_id, path, raw_bytes=raw)
                    msgs.append(f"✦ **{res['source_name']}** β€” {res['chunk_count']} chunks ({res['strategy']})")
                except Exception as e:
                    msgs.append(f"βœ• {e}")
            return "\n\n".join(msgs), _sources_md(username, nb_id)

        btn_upload.click(do_upload, inputs=[inp_files, s_nb_id, s_user], outputs=[md_ingest, md_sources])

        # ── Ingest URL ─────────────────────────────────────────────────────
        def do_url(url, nb_id, username):
            if not nb_id:
                return "⚠ Select a notebook first.", _sources_md(username, nb_id)
            if not url.strip():
                return "⚠ Enter a URL.", _sources_md(username, nb_id)
            try:
                res = _be()["ingest"](username, nb_id, url.strip())
                msg = f"✦ **{res['source_name']}** β€” {res['chunk_count']} chunks ({res['strategy']})"
            except Exception as e:
                msg = f"βœ• {e}"
            return msg, _sources_md(username, nb_id)

        btn_url.click(do_url, inputs=[inp_url, s_nb_id, s_user], outputs=[md_ingest, md_sources])

        # ── Refresh sources ────────────────────────────────────────────────
        btn_refresh.click(
            lambda u, n: _sources_md(u, n),
            inputs=[s_user, s_nb_id],
            outputs=[md_sources],
        )

        # ── Report ─────────────────────────────────────────────────────────
        def do_report(nb_id, username):
            if not nb_id:
                return "⚠ Select a notebook first.", ""
            return "✦ Report generated.", _be()["report"](nb_id, username)

        btn_report.click(do_report, inputs=[s_nb_id, s_user], outputs=[md_art_status, md_report])

        # ── Quiz ───────────────────────────────────────────────────────────
        def do_quiz(nb_id, username):
            if not nb_id:
                return "⚠ Select a notebook first.", ""
            return "✦ Quiz generated.", _be()["quiz"](nb_id, username)

        btn_quiz.click(do_quiz, inputs=[s_nb_id, s_user], outputs=[md_art_status, md_quiz])

        # ── Podcast ────────────────────────────────────────────────────────
        def do_podcast(nb_id, username):
            if not nb_id:
                return "⚠ Select a notebook first.", "", gr.update(visible=False)
            transcript, audio_path = _be()["podcast"](nb_id, username)
            audio_update = gr.update(value=audio_path, visible=True) if audio_path else gr.update(visible=False)
            return "✦ Podcast generated.", transcript, audio_update

        btn_podcast.click(
            do_podcast,
            inputs=[s_nb_id, s_user],
            outputs=[md_art_status, md_transcript, aud_podcast],
        )

    return demo


# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    demo = build_ui()
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)