Spaces:
Sleeping
Sleeping
Samir Abukar commited on
Refactor app.py for improved code structure
Browse files
app.py
CHANGED
|
@@ -35,6 +35,7 @@ def _parse_users() -> dict[str, str]:
|
|
| 35 |
users[u.strip()] = p.strip()
|
| 36 |
return users
|
| 37 |
|
|
|
|
| 38 |
USERS = _parse_users()
|
| 39 |
|
| 40 |
|
|
@@ -62,15 +63,15 @@ def _be():
|
|
| 62 |
from backend.chat import chat_with_sources
|
| 63 |
from backend.artifacts import generate_report, generate_quiz, generate_podcast
|
| 64 |
return {
|
| 65 |
-
"get_notebooks":
|
| 66 |
"create_notebook": create_notebook_for_user,
|
| 67 |
"delete_notebook": delete_notebook,
|
| 68 |
-
"list_sources":
|
| 69 |
-
"ingest":
|
| 70 |
-
"chat":
|
| 71 |
-
"report":
|
| 72 |
-
"quiz":
|
| 73 |
-
"podcast":
|
| 74 |
}
|
| 75 |
|
| 76 |
|
|
@@ -150,6 +151,7 @@ body, .gradio-container {
|
|
| 150 |
border-radius: 18px;
|
| 151 |
box-shadow: 0 0 80px var(--glow);
|
| 152 |
}
|
|
|
|
| 153 |
#login-title {
|
| 154 |
font-family: var(--display);
|
| 155 |
font-size: 2rem;
|
|
@@ -159,6 +161,7 @@ body, .gradio-container {
|
|
| 159 |
color: var(--text);
|
| 160 |
letter-spacing: -0.5px;
|
| 161 |
}
|
|
|
|
| 162 |
#login-sub {
|
| 163 |
font-size: 0.72rem;
|
| 164 |
color: var(--muted);
|
|
@@ -176,11 +179,13 @@ body, .gradio-container {
|
|
| 176 |
background: var(--bg2);
|
| 177 |
border-bottom: 1px solid var(--border);
|
| 178 |
}
|
|
|
|
| 179 |
#hdr-title {
|
| 180 |
font-family: var(--display);
|
| 181 |
font-size: 1.35rem;
|
| 182 |
color: var(--text);
|
| 183 |
}
|
|
|
|
| 184 |
#hdr-title span { color: var(--accent2); }
|
| 185 |
|
| 186 |
#nb-bar {
|
|
@@ -200,16 +205,19 @@ body, .gradio-container {
|
|
| 200 |
border-radius: var(--r) !important;
|
| 201 |
transition: all 0.18s ease !important;
|
| 202 |
}
|
|
|
|
| 203 |
.gr-button-primary {
|
| 204 |
background: var(--accent) !important;
|
| 205 |
color: #fff !important;
|
| 206 |
border: none !important;
|
| 207 |
}
|
|
|
|
| 208 |
.gr-button-primary:hover {
|
| 209 |
background: var(--accent2) !important;
|
| 210 |
box-shadow: 0 0 18px var(--glow) !important;
|
| 211 |
transform: translateY(-1px) !important;
|
| 212 |
}
|
|
|
|
| 213 |
.gr-button-stop {
|
| 214 |
background: transparent !important;
|
| 215 |
color: var(--err) !important;
|
|
@@ -225,6 +233,7 @@ input, textarea, .gr-textbox textarea {
|
|
| 225 |
font-size: 0.85rem !important;
|
| 226 |
transition: border-color 0.2s, box-shadow 0.2s !important;
|
| 227 |
}
|
|
|
|
| 228 |
input:focus, textarea:focus {
|
| 229 |
border-color: var(--accent) !important;
|
| 230 |
box-shadow: 0 0 0 3px var(--glow) !important;
|
|
@@ -253,6 +262,7 @@ label, .gr-block-label {
|
|
| 253 |
padding: 12px 20px !important;
|
| 254 |
transition: all 0.2s !important;
|
| 255 |
}
|
|
|
|
| 256 |
.tab-nav button.selected {
|
| 257 |
color: var(--accent2) !important;
|
| 258 |
border-bottom-color: var(--accent) !important;
|
|
@@ -274,6 +284,7 @@ label, .gr-block-label {
|
|
| 274 |
line-height: 1.75 !important;
|
| 275 |
color: var(--text) !important;
|
| 276 |
}
|
|
|
|
| 277 |
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3 {
|
| 278 |
font-family: var(--display) !important;
|
| 279 |
color: var(--text) !important;
|
|
@@ -294,9 +305,9 @@ select, .gr-dropdown select {
|
|
| 294 |
margin-bottom: 8px !important;
|
| 295 |
}
|
| 296 |
|
| 297 |
-
.ok { color: var(--ok)
|
| 298 |
.warn { color: var(--warn) !important; }
|
| 299 |
-
.err { color: var(--err)
|
| 300 |
|
| 301 |
.gr-file-upload {
|
| 302 |
background: var(--bg3) !important;
|
|
@@ -318,11 +329,14 @@ audio { width: 100%; border-radius: var(--r); }
|
|
| 318 |
# ---------------------------------------------------------------------------
|
| 319 |
|
| 320 |
def build_ui() -> gr.Blocks:
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
# ββ Persistent state βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 325 |
-
s_user
|
| 326 |
s_nb_id = gr.State("")
|
| 327 |
|
| 328 |
# ==================================================================
|
|
@@ -331,8 +345,8 @@ def build_ui() -> gr.Blocks:
|
|
| 331 |
with gr.Column(elem_id="login-wrap", visible=True) as screen_login:
|
| 332 |
gr.HTML('<div id="login-title">NotebookLM 2.0</div>')
|
| 333 |
gr.HTML('<div id="login-sub">AI Research Assistant</div>')
|
| 334 |
-
inp_user
|
| 335 |
-
inp_pass
|
| 336 |
btn_login = gr.Button("Sign In", variant="primary")
|
| 337 |
msg_login = gr.Markdown("")
|
| 338 |
|
|
@@ -348,10 +362,15 @@ def build_ui() -> gr.Blocks:
|
|
| 348 |
|
| 349 |
# Notebook bar
|
| 350 |
with gr.Row(elem_id="nb-bar"):
|
| 351 |
-
dd_nb
|
| 352 |
-
inp_nb_name = gr.Textbox(
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
md_nb_status = gr.Markdown("")
|
| 357 |
|
|
@@ -365,11 +384,15 @@ def build_ui() -> gr.Blocks:
|
|
| 365 |
height=460,
|
| 366 |
show_label=False,
|
| 367 |
bubble_full_width=False,
|
|
|
|
| 368 |
)
|
| 369 |
with gr.Row():
|
| 370 |
inp_chat = gr.Textbox(
|
| 371 |
placeholder="Ask anything about your sourcesβ¦",
|
| 372 |
-
label="",
|
|
|
|
|
|
|
|
|
|
| 373 |
)
|
| 374 |
btn_send = gr.Button("Send β", scale=1, variant="primary")
|
| 375 |
btn_clear = gr.Button("Clear chat", size="sm")
|
|
@@ -379,7 +402,7 @@ def build_ui() -> gr.Blocks:
|
|
| 379 |
with gr.Row():
|
| 380 |
with gr.Column(scale=1):
|
| 381 |
gr.Markdown("### Upload Files")
|
| 382 |
-
inp_files
|
| 383 |
label="PDF / PPTX / TXT",
|
| 384 |
file_types=[".pdf", ".pptx", ".txt"],
|
| 385 |
file_count="multiple",
|
|
@@ -393,14 +416,14 @@ def build_ui() -> gr.Blocks:
|
|
| 393 |
md_ingest = gr.Markdown("")
|
| 394 |
gr.Markdown("---")
|
| 395 |
gr.Markdown("### Indexed Sources")
|
| 396 |
-
md_sources
|
| 397 |
btn_refresh = gr.Button("β» Refresh", size="sm")
|
| 398 |
|
| 399 |
# TAB 3 β ARTIFACTS
|
| 400 |
with gr.TabItem("β¨ Artifacts"):
|
| 401 |
with gr.Row():
|
| 402 |
-
btn_report
|
| 403 |
-
btn_quiz
|
| 404 |
btn_podcast = gr.Button("ποΈ Generate Podcast", variant="primary", scale=1)
|
| 405 |
|
| 406 |
md_art_status = gr.Markdown("")
|
|
@@ -411,7 +434,7 @@ def build_ui() -> gr.Blocks:
|
|
| 411 |
md_quiz = gr.Markdown("")
|
| 412 |
with gr.Accordion("ποΈ Podcast", open=False):
|
| 413 |
md_transcript = gr.Markdown("")
|
| 414 |
-
aud_podcast
|
| 415 |
|
| 416 |
# ==================================================================
|
| 417 |
# EVENT HANDLERS
|
|
@@ -422,18 +445,25 @@ def build_ui() -> gr.Blocks:
|
|
| 422 |
ok, result = login(username, password)
|
| 423 |
if ok:
|
| 424 |
choices = _nb_choices(result)
|
|
|
|
|
|
|
| 425 |
return (
|
| 426 |
gr.update(visible=False),
|
| 427 |
gr.update(visible=True),
|
| 428 |
result,
|
| 429 |
-
|
|
|
|
| 430 |
f"β¦ **{result}**",
|
| 431 |
"",
|
| 432 |
)
|
|
|
|
| 433 |
return (
|
| 434 |
gr.update(visible=True),
|
| 435 |
gr.update(visible=False),
|
| 436 |
-
"",
|
|
|
|
|
|
|
|
|
|
| 437 |
f"β {result}",
|
| 438 |
)
|
| 439 |
|
|
@@ -441,7 +471,7 @@ def build_ui() -> gr.Blocks:
|
|
| 441 |
trigger(
|
| 442 |
do_login,
|
| 443 |
inputs=[inp_user, inp_pass],
|
| 444 |
-
outputs=[screen_login, screen_main, s_user, dd_nb, lbl_user, msg_login],
|
| 445 |
)
|
| 446 |
|
| 447 |
# ββ Notebook selection βββββββββββββββββββββββββοΏ½οΏ½βββββββββββββββββββ
|
|
@@ -454,44 +484,81 @@ def build_ui() -> gr.Blocks:
|
|
| 454 |
# ββ Create notebook ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 455 |
def do_create(username, name):
|
| 456 |
if not name.strip():
|
| 457 |
-
return gr.update(), "", "β Enter a notebook name."
|
| 458 |
if not username:
|
| 459 |
-
return gr.update(), "", "β Not signed in."
|
|
|
|
| 460 |
_be()["create_notebook"](username, name.strip())
|
| 461 |
choices = _nb_choices(username)
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
btn_create.click(
|
| 465 |
do_create,
|
| 466 |
inputs=[s_user, inp_nb_name],
|
| 467 |
-
outputs=[dd_nb, inp_nb_name, md_nb_status],
|
| 468 |
)
|
| 469 |
|
| 470 |
# ββ Delete notebook ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 471 |
def do_delete(username, nb_id):
|
| 472 |
if not nb_id:
|
| 473 |
-
return gr.update(), "β No notebook selected."
|
|
|
|
| 474 |
_be()["delete_notebook"](username, nb_id)
|
| 475 |
choices = _nb_choices(username)
|
| 476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
-
btn_delete.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
|
| 480 |
# ββ Chat βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 481 |
def do_chat(message, nb_id, username, history):
|
| 482 |
if not message.strip():
|
| 483 |
return history, ""
|
|
|
|
|
|
|
|
|
|
| 484 |
if not nb_id:
|
| 485 |
-
history
|
| 486 |
return history, ""
|
| 487 |
-
|
| 488 |
-
history
|
| 489 |
-
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
return history, ""
|
| 492 |
|
| 493 |
-
btn_send.click(
|
| 494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
btn_clear.click(lambda: [], outputs=[chatbot])
|
| 496 |
|
| 497 |
# ββ Ingest files βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -500,18 +567,26 @@ def build_ui() -> gr.Blocks:
|
|
| 500 |
return "β Select a notebook first.", _sources_md(username, nb_id)
|
| 501 |
if not files:
|
| 502 |
return "β No files selected.", _sources_md(username, nb_id)
|
|
|
|
| 503 |
msgs = []
|
| 504 |
for f in files:
|
| 505 |
path = f.name if hasattr(f, "name") else str(f)
|
| 506 |
-
raw
|
| 507 |
try:
|
| 508 |
res = _be()["ingest"](username, nb_id, path, raw_bytes=raw)
|
| 509 |
-
msgs.append(
|
|
|
|
|
|
|
| 510 |
except Exception as e:
|
| 511 |
msgs.append(f"β {e}")
|
|
|
|
| 512 |
return "\n\n".join(msgs), _sources_md(username, nb_id)
|
| 513 |
|
| 514 |
-
btn_upload.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
# ββ Ingest URL βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 517 |
def do_url(url, nb_id, username):
|
|
@@ -519,14 +594,20 @@ def build_ui() -> gr.Blocks:
|
|
| 519 |
return "β Select a notebook first.", _sources_md(username, nb_id)
|
| 520 |
if not url.strip():
|
| 521 |
return "β Enter a URL.", _sources_md(username, nb_id)
|
|
|
|
| 522 |
try:
|
| 523 |
res = _be()["ingest"](username, nb_id, url.strip())
|
| 524 |
msg = f"β¦ **{res['source_name']}** β {res['chunk_count']} chunks ({res['strategy']})"
|
| 525 |
except Exception as e:
|
| 526 |
msg = f"β {e}"
|
|
|
|
| 527 |
return msg, _sources_md(username, nb_id)
|
| 528 |
|
| 529 |
-
btn_url.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
|
| 531 |
# ββ Refresh sources ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 532 |
btn_refresh.click(
|
|
@@ -541,7 +622,11 @@ def build_ui() -> gr.Blocks:
|
|
| 541 |
return "β Select a notebook first.", ""
|
| 542 |
return "β¦ Report generated.", _be()["report"](nb_id, username)
|
| 543 |
|
| 544 |
-
btn_report.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
|
| 546 |
# ββ Quiz βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 547 |
def do_quiz(nb_id, username):
|
|
@@ -549,14 +634,23 @@ def build_ui() -> gr.Blocks:
|
|
| 549 |
return "β Select a notebook first.", ""
|
| 550 |
return "β¦ Quiz generated.", _be()["quiz"](nb_id, username)
|
| 551 |
|
| 552 |
-
btn_quiz.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
|
| 554 |
# ββ Podcast ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 555 |
def do_podcast(nb_id, username):
|
| 556 |
if not nb_id:
|
| 557 |
return "β Select a notebook first.", "", gr.update(visible=False)
|
|
|
|
| 558 |
transcript, audio_path = _be()["podcast"](nb_id, username)
|
| 559 |
-
audio_update =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
return "β¦ Podcast generated.", transcript, audio_update
|
| 561 |
|
| 562 |
btn_podcast.click(
|
|
@@ -574,10 +668,8 @@ def build_ui() -> gr.Blocks:
|
|
| 574 |
|
| 575 |
if __name__ == "__main__":
|
| 576 |
demo = build_ui()
|
| 577 |
-
demo.launch(
|
| 578 |
server_name="0.0.0.0",
|
| 579 |
server_port=7860,
|
| 580 |
share=False,
|
| 581 |
-
|
| 582 |
-
theme=gr.themes.Base(),
|
| 583 |
-
)
|
|
|
|
| 35 |
users[u.strip()] = p.strip()
|
| 36 |
return users
|
| 37 |
|
| 38 |
+
|
| 39 |
USERS = _parse_users()
|
| 40 |
|
| 41 |
|
|
|
|
| 63 |
from backend.chat import chat_with_sources
|
| 64 |
from backend.artifacts import generate_report, generate_quiz, generate_podcast
|
| 65 |
return {
|
| 66 |
+
"get_notebooks": get_user_notebooks,
|
| 67 |
"create_notebook": create_notebook_for_user,
|
| 68 |
"delete_notebook": delete_notebook,
|
| 69 |
+
"list_sources": list_indexed_sources,
|
| 70 |
+
"ingest": ingest_source,
|
| 71 |
+
"chat": chat_with_sources,
|
| 72 |
+
"report": generate_report,
|
| 73 |
+
"quiz": generate_quiz,
|
| 74 |
+
"podcast": generate_podcast,
|
| 75 |
}
|
| 76 |
|
| 77 |
|
|
|
|
| 151 |
border-radius: 18px;
|
| 152 |
box-shadow: 0 0 80px var(--glow);
|
| 153 |
}
|
| 154 |
+
|
| 155 |
#login-title {
|
| 156 |
font-family: var(--display);
|
| 157 |
font-size: 2rem;
|
|
|
|
| 161 |
color: var(--text);
|
| 162 |
letter-spacing: -0.5px;
|
| 163 |
}
|
| 164 |
+
|
| 165 |
#login-sub {
|
| 166 |
font-size: 0.72rem;
|
| 167 |
color: var(--muted);
|
|
|
|
| 179 |
background: var(--bg2);
|
| 180 |
border-bottom: 1px solid var(--border);
|
| 181 |
}
|
| 182 |
+
|
| 183 |
#hdr-title {
|
| 184 |
font-family: var(--display);
|
| 185 |
font-size: 1.35rem;
|
| 186 |
color: var(--text);
|
| 187 |
}
|
| 188 |
+
|
| 189 |
#hdr-title span { color: var(--accent2); }
|
| 190 |
|
| 191 |
#nb-bar {
|
|
|
|
| 205 |
border-radius: var(--r) !important;
|
| 206 |
transition: all 0.18s ease !important;
|
| 207 |
}
|
| 208 |
+
|
| 209 |
.gr-button-primary {
|
| 210 |
background: var(--accent) !important;
|
| 211 |
color: #fff !important;
|
| 212 |
border: none !important;
|
| 213 |
}
|
| 214 |
+
|
| 215 |
.gr-button-primary:hover {
|
| 216 |
background: var(--accent2) !important;
|
| 217 |
box-shadow: 0 0 18px var(--glow) !important;
|
| 218 |
transform: translateY(-1px) !important;
|
| 219 |
}
|
| 220 |
+
|
| 221 |
.gr-button-stop {
|
| 222 |
background: transparent !important;
|
| 223 |
color: var(--err) !important;
|
|
|
|
| 233 |
font-size: 0.85rem !important;
|
| 234 |
transition: border-color 0.2s, box-shadow 0.2s !important;
|
| 235 |
}
|
| 236 |
+
|
| 237 |
input:focus, textarea:focus {
|
| 238 |
border-color: var(--accent) !important;
|
| 239 |
box-shadow: 0 0 0 3px var(--glow) !important;
|
|
|
|
| 262 |
padding: 12px 20px !important;
|
| 263 |
transition: all 0.2s !important;
|
| 264 |
}
|
| 265 |
+
|
| 266 |
.tab-nav button.selected {
|
| 267 |
color: var(--accent2) !important;
|
| 268 |
border-bottom-color: var(--accent) !important;
|
|
|
|
| 284 |
line-height: 1.75 !important;
|
| 285 |
color: var(--text) !important;
|
| 286 |
}
|
| 287 |
+
|
| 288 |
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3 {
|
| 289 |
font-family: var(--display) !important;
|
| 290 |
color: var(--text) !important;
|
|
|
|
| 305 |
margin-bottom: 8px !important;
|
| 306 |
}
|
| 307 |
|
| 308 |
+
.ok { color: var(--ok) !important; }
|
| 309 |
.warn { color: var(--warn) !important; }
|
| 310 |
+
.err { color: var(--err) !important; }
|
| 311 |
|
| 312 |
.gr-file-upload {
|
| 313 |
background: var(--bg3) !important;
|
|
|
|
| 329 |
# ---------------------------------------------------------------------------
|
| 330 |
|
| 331 |
def build_ui() -> gr.Blocks:
|
| 332 |
+
with gr.Blocks(
|
| 333 |
+
title="NotebookLM 2.0",
|
| 334 |
+
css=CSS,
|
| 335 |
+
theme=gr.themes.Base(),
|
| 336 |
+
) as demo:
|
| 337 |
|
| 338 |
# ββ Persistent state βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 339 |
+
s_user = gr.State("")
|
| 340 |
s_nb_id = gr.State("")
|
| 341 |
|
| 342 |
# ==================================================================
|
|
|
|
| 345 |
with gr.Column(elem_id="login-wrap", visible=True) as screen_login:
|
| 346 |
gr.HTML('<div id="login-title">NotebookLM 2.0</div>')
|
| 347 |
gr.HTML('<div id="login-sub">AI Research Assistant</div>')
|
| 348 |
+
inp_user = gr.Textbox(label="Username", placeholder="username")
|
| 349 |
+
inp_pass = gr.Textbox(label="Password", placeholder="password", type="password")
|
| 350 |
btn_login = gr.Button("Sign In", variant="primary")
|
| 351 |
msg_login = gr.Markdown("")
|
| 352 |
|
|
|
|
| 362 |
|
| 363 |
# Notebook bar
|
| 364 |
with gr.Row(elem_id="nb-bar"):
|
| 365 |
+
dd_nb = gr.Dropdown(choices=[], label="Active Notebook", scale=4, interactive=True)
|
| 366 |
+
inp_nb_name = gr.Textbox(
|
| 367 |
+
placeholder="New notebook nameβ¦",
|
| 368 |
+
label="",
|
| 369 |
+
scale=3,
|
| 370 |
+
show_label=False
|
| 371 |
+
)
|
| 372 |
+
btn_create = gr.Button("οΌ Create", scale=1, variant="primary")
|
| 373 |
+
btn_delete = gr.Button("β Delete", scale=1, variant="stop")
|
| 374 |
|
| 375 |
md_nb_status = gr.Markdown("")
|
| 376 |
|
|
|
|
| 384 |
height=460,
|
| 385 |
show_label=False,
|
| 386 |
bubble_full_width=False,
|
| 387 |
+
type="tuples",
|
| 388 |
)
|
| 389 |
with gr.Row():
|
| 390 |
inp_chat = gr.Textbox(
|
| 391 |
placeholder="Ask anything about your sourcesβ¦",
|
| 392 |
+
label="",
|
| 393 |
+
show_label=False,
|
| 394 |
+
scale=5,
|
| 395 |
+
lines=1,
|
| 396 |
)
|
| 397 |
btn_send = gr.Button("Send β", scale=1, variant="primary")
|
| 398 |
btn_clear = gr.Button("Clear chat", size="sm")
|
|
|
|
| 402 |
with gr.Row():
|
| 403 |
with gr.Column(scale=1):
|
| 404 |
gr.Markdown("### Upload Files")
|
| 405 |
+
inp_files = gr.File(
|
| 406 |
label="PDF / PPTX / TXT",
|
| 407 |
file_types=[".pdf", ".pptx", ".txt"],
|
| 408 |
file_count="multiple",
|
|
|
|
| 416 |
md_ingest = gr.Markdown("")
|
| 417 |
gr.Markdown("---")
|
| 418 |
gr.Markdown("### Indexed Sources")
|
| 419 |
+
md_sources = gr.Markdown("_No sources indexed yet._")
|
| 420 |
btn_refresh = gr.Button("β» Refresh", size="sm")
|
| 421 |
|
| 422 |
# TAB 3 β ARTIFACTS
|
| 423 |
with gr.TabItem("β¨ Artifacts"):
|
| 424 |
with gr.Row():
|
| 425 |
+
btn_report = gr.Button("π Generate Report", variant="primary", scale=1)
|
| 426 |
+
btn_quiz = gr.Button("β Generate Quiz", variant="primary", scale=1)
|
| 427 |
btn_podcast = gr.Button("ποΈ Generate Podcast", variant="primary", scale=1)
|
| 428 |
|
| 429 |
md_art_status = gr.Markdown("")
|
|
|
|
| 434 |
md_quiz = gr.Markdown("")
|
| 435 |
with gr.Accordion("ποΈ Podcast", open=False):
|
| 436 |
md_transcript = gr.Markdown("")
|
| 437 |
+
aud_podcast = gr.Audio(label="Listen", visible=False)
|
| 438 |
|
| 439 |
# ==================================================================
|
| 440 |
# EVENT HANDLERS
|
|
|
|
| 445 |
ok, result = login(username, password)
|
| 446 |
if ok:
|
| 447 |
choices = _nb_choices(result)
|
| 448 |
+
first_choice = choices[0] if choices else None
|
| 449 |
+
_, first_nb_id = _parse_choice(first_choice) if first_choice else ("", "")
|
| 450 |
return (
|
| 451 |
gr.update(visible=False),
|
| 452 |
gr.update(visible=True),
|
| 453 |
result,
|
| 454 |
+
first_nb_id,
|
| 455 |
+
gr.update(choices=choices, value=first_choice),
|
| 456 |
f"β¦ **{result}**",
|
| 457 |
"",
|
| 458 |
)
|
| 459 |
+
|
| 460 |
return (
|
| 461 |
gr.update(visible=True),
|
| 462 |
gr.update(visible=False),
|
| 463 |
+
"",
|
| 464 |
+
"",
|
| 465 |
+
gr.update(),
|
| 466 |
+
"",
|
| 467 |
f"β {result}",
|
| 468 |
)
|
| 469 |
|
|
|
|
| 471 |
trigger(
|
| 472 |
do_login,
|
| 473 |
inputs=[inp_user, inp_pass],
|
| 474 |
+
outputs=[screen_login, screen_main, s_user, s_nb_id, dd_nb, lbl_user, msg_login],
|
| 475 |
)
|
| 476 |
|
| 477 |
# ββ Notebook selection βββββββββββββββββββββββββοΏ½οΏ½βββββββββββββββββββ
|
|
|
|
| 484 |
# ββ Create notebook ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 485 |
def do_create(username, name):
|
| 486 |
if not name.strip():
|
| 487 |
+
return gr.update(), "", "", "β Enter a notebook name."
|
| 488 |
if not username:
|
| 489 |
+
return gr.update(), "", "", "β Not signed in."
|
| 490 |
+
|
| 491 |
_be()["create_notebook"](username, name.strip())
|
| 492 |
choices = _nb_choices(username)
|
| 493 |
+
selected = choices[-1] if choices else None
|
| 494 |
+
_, nb_id = _parse_choice(selected) if selected else ("", "")
|
| 495 |
+
return (
|
| 496 |
+
gr.update(choices=choices, value=selected),
|
| 497 |
+
"",
|
| 498 |
+
nb_id,
|
| 499 |
+
f"β¦ Created **{name.strip()}**",
|
| 500 |
+
)
|
| 501 |
|
| 502 |
btn_create.click(
|
| 503 |
do_create,
|
| 504 |
inputs=[s_user, inp_nb_name],
|
| 505 |
+
outputs=[dd_nb, inp_nb_name, s_nb_id, md_nb_status],
|
| 506 |
)
|
| 507 |
|
| 508 |
# ββ Delete notebook ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 509 |
def do_delete(username, nb_id):
|
| 510 |
if not nb_id:
|
| 511 |
+
return gr.update(), "", "β No notebook selected."
|
| 512 |
+
|
| 513 |
_be()["delete_notebook"](username, nb_id)
|
| 514 |
choices = _nb_choices(username)
|
| 515 |
+
selected = choices[0] if choices else None
|
| 516 |
+
_, new_nb_id = _parse_choice(selected) if selected else ("", "")
|
| 517 |
+
return (
|
| 518 |
+
gr.update(choices=choices, value=selected),
|
| 519 |
+
new_nb_id,
|
| 520 |
+
"β¦ Deleted.",
|
| 521 |
+
)
|
| 522 |
|
| 523 |
+
btn_delete.click(
|
| 524 |
+
do_delete,
|
| 525 |
+
inputs=[s_user, s_nb_id],
|
| 526 |
+
outputs=[dd_nb, s_nb_id, md_nb_status],
|
| 527 |
+
)
|
| 528 |
|
| 529 |
# ββ Chat βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 530 |
def do_chat(message, nb_id, username, history):
|
| 531 |
if not message.strip():
|
| 532 |
return history, ""
|
| 533 |
+
|
| 534 |
+
history = history or []
|
| 535 |
+
|
| 536 |
if not nb_id:
|
| 537 |
+
history.append((message, "β Select a notebook first."))
|
| 538 |
return history, ""
|
| 539 |
+
|
| 540 |
+
# Convert tuple chat history into dict format expected by backend.chat
|
| 541 |
+
formatted_history = []
|
| 542 |
+
for user_msg, assistant_msg in history:
|
| 543 |
+
if user_msg:
|
| 544 |
+
formatted_history.append({"role": "user", "content": user_msg})
|
| 545 |
+
if assistant_msg:
|
| 546 |
+
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
| 547 |
+
|
| 548 |
+
reply = _be()["chat"](message, nb_id, username, formatted_history)
|
| 549 |
+
history.append((message, reply))
|
| 550 |
return history, ""
|
| 551 |
|
| 552 |
+
btn_send.click(
|
| 553 |
+
do_chat,
|
| 554 |
+
inputs=[inp_chat, s_nb_id, s_user, chatbot],
|
| 555 |
+
outputs=[chatbot, inp_chat]
|
| 556 |
+
)
|
| 557 |
+
inp_chat.submit(
|
| 558 |
+
do_chat,
|
| 559 |
+
inputs=[inp_chat, s_nb_id, s_user, chatbot],
|
| 560 |
+
outputs=[chatbot, inp_chat]
|
| 561 |
+
)
|
| 562 |
btn_clear.click(lambda: [], outputs=[chatbot])
|
| 563 |
|
| 564 |
# ββ Ingest files βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 567 |
return "β Select a notebook first.", _sources_md(username, nb_id)
|
| 568 |
if not files:
|
| 569 |
return "β No files selected.", _sources_md(username, nb_id)
|
| 570 |
+
|
| 571 |
msgs = []
|
| 572 |
for f in files:
|
| 573 |
path = f.name if hasattr(f, "name") else str(f)
|
| 574 |
+
raw = Path(path).read_bytes()
|
| 575 |
try:
|
| 576 |
res = _be()["ingest"](username, nb_id, path, raw_bytes=raw)
|
| 577 |
+
msgs.append(
|
| 578 |
+
f"β¦ **{res['source_name']}** β {res['chunk_count']} chunks ({res['strategy']})"
|
| 579 |
+
)
|
| 580 |
except Exception as e:
|
| 581 |
msgs.append(f"β {e}")
|
| 582 |
+
|
| 583 |
return "\n\n".join(msgs), _sources_md(username, nb_id)
|
| 584 |
|
| 585 |
+
btn_upload.click(
|
| 586 |
+
do_upload,
|
| 587 |
+
inputs=[inp_files, s_nb_id, s_user],
|
| 588 |
+
outputs=[md_ingest, md_sources]
|
| 589 |
+
)
|
| 590 |
|
| 591 |
# ββ Ingest URL βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 592 |
def do_url(url, nb_id, username):
|
|
|
|
| 594 |
return "β Select a notebook first.", _sources_md(username, nb_id)
|
| 595 |
if not url.strip():
|
| 596 |
return "β Enter a URL.", _sources_md(username, nb_id)
|
| 597 |
+
|
| 598 |
try:
|
| 599 |
res = _be()["ingest"](username, nb_id, url.strip())
|
| 600 |
msg = f"β¦ **{res['source_name']}** β {res['chunk_count']} chunks ({res['strategy']})"
|
| 601 |
except Exception as e:
|
| 602 |
msg = f"β {e}"
|
| 603 |
+
|
| 604 |
return msg, _sources_md(username, nb_id)
|
| 605 |
|
| 606 |
+
btn_url.click(
|
| 607 |
+
do_url,
|
| 608 |
+
inputs=[inp_url, s_nb_id, s_user],
|
| 609 |
+
outputs=[md_ingest, md_sources]
|
| 610 |
+
)
|
| 611 |
|
| 612 |
# ββ Refresh sources ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 613 |
btn_refresh.click(
|
|
|
|
| 622 |
return "β Select a notebook first.", ""
|
| 623 |
return "β¦ Report generated.", _be()["report"](nb_id, username)
|
| 624 |
|
| 625 |
+
btn_report.click(
|
| 626 |
+
do_report,
|
| 627 |
+
inputs=[s_nb_id, s_user],
|
| 628 |
+
outputs=[md_art_status, md_report]
|
| 629 |
+
)
|
| 630 |
|
| 631 |
# ββ Quiz βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 632 |
def do_quiz(nb_id, username):
|
|
|
|
| 634 |
return "β Select a notebook first.", ""
|
| 635 |
return "β¦ Quiz generated.", _be()["quiz"](nb_id, username)
|
| 636 |
|
| 637 |
+
btn_quiz.click(
|
| 638 |
+
do_quiz,
|
| 639 |
+
inputs=[s_nb_id, s_user],
|
| 640 |
+
outputs=[md_art_status, md_quiz]
|
| 641 |
+
)
|
| 642 |
|
| 643 |
# ββ Podcast ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 644 |
def do_podcast(nb_id, username):
|
| 645 |
if not nb_id:
|
| 646 |
return "β Select a notebook first.", "", gr.update(visible=False)
|
| 647 |
+
|
| 648 |
transcript, audio_path = _be()["podcast"](nb_id, username)
|
| 649 |
+
audio_update = (
|
| 650 |
+
gr.update(value=audio_path, visible=True)
|
| 651 |
+
if audio_path
|
| 652 |
+
else gr.update(visible=False)
|
| 653 |
+
)
|
| 654 |
return "β¦ Podcast generated.", transcript, audio_update
|
| 655 |
|
| 656 |
btn_podcast.click(
|
|
|
|
| 668 |
|
| 669 |
if __name__ == "__main__":
|
| 670 |
demo = build_ui()
|
| 671 |
+
demo.queue().launch(
|
| 672 |
server_name="0.0.0.0",
|
| 673 |
server_port=7860,
|
| 674 |
share=False,
|
| 675 |
+
)
|
|
|
|
|
|