Spaces:
Running
Running
Bug fix about authentication
Browse files
app.py
CHANGED
|
@@ -66,6 +66,24 @@ def _require_user(request: gr.Request | None) -> str:
|
|
| 66 |
return get_current_user(request)
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def _notebook_choices(notebooks: list[NotebookRecord]) -> list[tuple[str, str]]:
|
| 70 |
"""Map notebook records into dropdown choices."""
|
| 71 |
|
|
@@ -116,30 +134,36 @@ def _refresh_notebook_state(
|
|
| 116 |
return _render_login_status(username), gr.Dropdown(choices=choices, value=value)
|
| 117 |
|
| 118 |
|
| 119 |
-
def load_session(
|
|
|
|
|
|
|
|
|
|
| 120 |
"""Initialize login status and notebook selector when the UI loads."""
|
| 121 |
|
| 122 |
try:
|
| 123 |
-
username: str =
|
| 124 |
login_status, notebook_dropdown = _refresh_notebook_state(username)
|
| 125 |
except NotAuthenticatedError:
|
| 126 |
login_status = _render_logged_out_status()
|
| 127 |
notebook_dropdown = gr.Dropdown(choices=[], value=None)
|
|
|
|
| 128 |
empty_chat: list[tuple[str, str]] = []
|
| 129 |
artifact_dropdown = gr.Dropdown(choices=[], value=None)
|
| 130 |
-
return login_status, notebook_dropdown, empty_chat, artifact_dropdown
|
| 131 |
|
| 132 |
|
| 133 |
def create_notebook_ui(
|
| 134 |
notebook_name: str,
|
|
|
|
|
|
|
| 135 |
request: gr.Request,
|
| 136 |
-
) -> tuple[str, gr.Dropdown, str]:
|
| 137 |
"""Create a notebook and refresh the selector."""
|
| 138 |
|
| 139 |
-
username: str =
|
| 140 |
notebook: NotebookRecord = create_notebook(username, notebook_name)
|
| 141 |
login_status, dropdown = _refresh_notebook_state(username, notebook["id"])
|
| 142 |
-
return login_status, dropdown, ""
|
| 143 |
|
| 144 |
|
| 145 |
def on_notebook_change(_notebook_id: str | None) -> tuple[list[tuple[str, str]], gr.Dropdown, str]:
|
|
@@ -199,11 +223,13 @@ def _ingest_text(
|
|
| 199 |
def ingest_upload_ui(
|
| 200 |
notebook_id: str | None,
|
| 201 |
file_path: str | None,
|
|
|
|
|
|
|
| 202 |
request: gr.Request,
|
| 203 |
) -> str:
|
| 204 |
"""Ingest an uploaded local file through the backend ingestion APIs."""
|
| 205 |
|
| 206 |
-
username: str =
|
| 207 |
if not notebook_id:
|
| 208 |
raise gr.Error("Select a notebook before uploading a source.")
|
| 209 |
if not file_path:
|
|
@@ -222,11 +248,13 @@ def ingest_upload_ui(
|
|
| 222 |
def ingest_url_ui(
|
| 223 |
notebook_id: str | None,
|
| 224 |
url: str,
|
|
|
|
|
|
|
| 225 |
request: gr.Request,
|
| 226 |
) -> str:
|
| 227 |
"""Ingest a URL source through the backend ingestion APIs."""
|
| 228 |
|
| 229 |
-
username: str =
|
| 230 |
if not notebook_id:
|
| 231 |
raise gr.Error("Select a notebook before ingesting a URL.")
|
| 232 |
if not url or not url.strip():
|
|
@@ -246,11 +274,13 @@ def send_chat_ui(
|
|
| 246 |
notebook_id: str | None,
|
| 247 |
question: str,
|
| 248 |
history: list[tuple[str, str]] | None,
|
|
|
|
|
|
|
| 249 |
request: gr.Request,
|
| 250 |
) -> tuple[list[tuple[str, str]], str]:
|
| 251 |
"""Send one chat question and append the grounded answer to the chat history."""
|
| 252 |
|
| 253 |
-
username: str =
|
| 254 |
if not notebook_id:
|
| 255 |
raise gr.Error("Select a notebook before asking a question.")
|
| 256 |
if not question or not question.strip():
|
|
@@ -279,11 +309,13 @@ def _append_artifact_path(current_paths: list[str] | None, artifact: ArtifactRef
|
|
| 279 |
def generate_report_ui(
|
| 280 |
notebook_id: str | None,
|
| 281 |
artifact_paths: list[str] | None,
|
|
|
|
|
|
|
| 282 |
request: gr.Request,
|
| 283 |
) -> tuple[list[str], gr.Dropdown]:
|
| 284 |
"""Generate a report artifact and update the download list."""
|
| 285 |
|
| 286 |
-
username: str =
|
| 287 |
if not notebook_id:
|
| 288 |
raise gr.Error("Select a notebook before generating a report.")
|
| 289 |
artifact = generate_report(username, notebook_id)
|
|
@@ -293,11 +325,13 @@ def generate_report_ui(
|
|
| 293 |
def generate_quiz_ui(
|
| 294 |
notebook_id: str | None,
|
| 295 |
artifact_paths: list[str] | None,
|
|
|
|
|
|
|
| 296 |
request: gr.Request,
|
| 297 |
) -> tuple[list[str], gr.Dropdown]:
|
| 298 |
"""Generate a quiz artifact and update the download list."""
|
| 299 |
|
| 300 |
-
username: str =
|
| 301 |
if not notebook_id:
|
| 302 |
raise gr.Error("Select a notebook before generating a quiz.")
|
| 303 |
artifact = generate_quiz(username, notebook_id)
|
|
@@ -307,11 +341,13 @@ def generate_quiz_ui(
|
|
| 307 |
def generate_podcast_ui(
|
| 308 |
notebook_id: str | None,
|
| 309 |
artifact_paths: list[str] | None,
|
|
|
|
|
|
|
| 310 |
request: gr.Request,
|
| 311 |
) -> tuple[list[str], gr.Dropdown]:
|
| 312 |
"""Generate a podcast transcript artifact and update the download list."""
|
| 313 |
|
| 314 |
-
username: str =
|
| 315 |
if not notebook_id:
|
| 316 |
raise gr.Error("Select a notebook before generating a transcript.")
|
| 317 |
artifact = generate_podcast_transcript(username, notebook_id)
|
|
@@ -326,10 +362,15 @@ def select_artifact_download(artifact_path: str | None) -> Path | None:
|
|
| 326 |
return Path(artifact_path)
|
| 327 |
|
| 328 |
|
| 329 |
-
def export_notebook_ui(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
"""Export the selected notebook as a zip archive."""
|
| 331 |
|
| 332 |
-
username: str =
|
| 333 |
if not notebook_id:
|
| 334 |
raise gr.Error("Select a notebook before exporting.")
|
| 335 |
return export_notebook_zip(username, notebook_id)
|
|
@@ -337,6 +378,7 @@ def export_notebook_ui(notebook_id: str | None, request: gr.Request) -> Path:
|
|
| 337 |
|
| 338 |
with gr.Blocks(title="NotebookLM Clone") as demo:
|
| 339 |
artifact_paths_state = gr.State(value=[])
|
|
|
|
| 340 |
|
| 341 |
gr.Markdown("# NotebookLM Clone")
|
| 342 |
with gr.Row():
|
|
@@ -389,13 +431,13 @@ with gr.Blocks(title="NotebookLM Clone") as demo:
|
|
| 389 |
demo.load(
|
| 390 |
load_session,
|
| 391 |
inputs=None,
|
| 392 |
-
outputs=[login_status, notebook_dropdown, chat_history, artifact_dropdown],
|
| 393 |
)
|
| 394 |
|
| 395 |
create_notebook_button.click(
|
| 396 |
create_notebook_ui,
|
| 397 |
-
inputs=[new_notebook_name],
|
| 398 |
-
outputs=[login_status, notebook_dropdown, new_notebook_name],
|
| 399 |
)
|
| 400 |
|
| 401 |
notebook_dropdown.change(
|
|
@@ -410,37 +452,37 @@ with gr.Blocks(title="NotebookLM Clone") as demo:
|
|
| 410 |
|
| 411 |
upload_button.click(
|
| 412 |
ingest_upload_ui,
|
| 413 |
-
inputs=[notebook_dropdown, file_input],
|
| 414 |
outputs=[ingest_status],
|
| 415 |
)
|
| 416 |
|
| 417 |
url_button.click(
|
| 418 |
ingest_url_ui,
|
| 419 |
-
inputs=[notebook_dropdown, url_input],
|
| 420 |
outputs=[ingest_status],
|
| 421 |
)
|
| 422 |
|
| 423 |
ask_button.click(
|
| 424 |
send_chat_ui,
|
| 425 |
-
inputs=[notebook_dropdown, question_input, chat_history],
|
| 426 |
outputs=[chat_history, question_input],
|
| 427 |
)
|
| 428 |
|
| 429 |
report_button.click(
|
| 430 |
generate_report_ui,
|
| 431 |
-
inputs=[notebook_dropdown, artifact_paths_state],
|
| 432 |
outputs=[artifact_paths_state, artifact_dropdown],
|
| 433 |
)
|
| 434 |
|
| 435 |
quiz_button.click(
|
| 436 |
generate_quiz_ui,
|
| 437 |
-
inputs=[notebook_dropdown, artifact_paths_state],
|
| 438 |
outputs=[artifact_paths_state, artifact_dropdown],
|
| 439 |
)
|
| 440 |
|
| 441 |
podcast_button.click(
|
| 442 |
generate_podcast_ui,
|
| 443 |
-
inputs=[notebook_dropdown, artifact_paths_state],
|
| 444 |
outputs=[artifact_paths_state, artifact_dropdown],
|
| 445 |
)
|
| 446 |
|
|
@@ -452,7 +494,7 @@ with gr.Blocks(title="NotebookLM Clone") as demo:
|
|
| 452 |
|
| 453 |
export_button.click(
|
| 454 |
export_notebook_ui,
|
| 455 |
-
inputs=[notebook_dropdown],
|
| 456 |
outputs=[export_download],
|
| 457 |
)
|
| 458 |
|
|
|
|
| 66 |
return get_current_user(request)
|
| 67 |
|
| 68 |
|
| 69 |
+
def _resolve_username(
|
| 70 |
+
profile: gr.OAuthProfile | None,
|
| 71 |
+
request: gr.Request | None,
|
| 72 |
+
current_username: str | None = None,
|
| 73 |
+
) -> str:
|
| 74 |
+
"""Resolve the authenticated username from OAuth profile, request, or stored state."""
|
| 75 |
+
|
| 76 |
+
if profile is not None:
|
| 77 |
+
username: str | None = getattr(profile, "username", None)
|
| 78 |
+
if isinstance(username, str) and username.strip():
|
| 79 |
+
return username.strip()
|
| 80 |
+
|
| 81 |
+
if current_username is not None and current_username.strip():
|
| 82 |
+
return current_username.strip()
|
| 83 |
+
|
| 84 |
+
return _require_user(request)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
def _notebook_choices(notebooks: list[NotebookRecord]) -> list[tuple[str, str]]:
|
| 88 |
"""Map notebook records into dropdown choices."""
|
| 89 |
|
|
|
|
| 134 |
return _render_login_status(username), gr.Dropdown(choices=choices, value=value)
|
| 135 |
|
| 136 |
|
| 137 |
+
def load_session(
|
| 138 |
+
profile: gr.OAuthProfile | None,
|
| 139 |
+
request: gr.Request,
|
| 140 |
+
) -> tuple[str, gr.Dropdown, list[tuple[str, str]], gr.Dropdown, str]:
|
| 141 |
"""Initialize login status and notebook selector when the UI loads."""
|
| 142 |
|
| 143 |
try:
|
| 144 |
+
username: str = _resolve_username(profile, request)
|
| 145 |
login_status, notebook_dropdown = _refresh_notebook_state(username)
|
| 146 |
except NotAuthenticatedError:
|
| 147 |
login_status = _render_logged_out_status()
|
| 148 |
notebook_dropdown = gr.Dropdown(choices=[], value=None)
|
| 149 |
+
username = ""
|
| 150 |
empty_chat: list[tuple[str, str]] = []
|
| 151 |
artifact_dropdown = gr.Dropdown(choices=[], value=None)
|
| 152 |
+
return login_status, notebook_dropdown, empty_chat, artifact_dropdown, username
|
| 153 |
|
| 154 |
|
| 155 |
def create_notebook_ui(
|
| 156 |
notebook_name: str,
|
| 157 |
+
current_username: str,
|
| 158 |
+
profile: gr.OAuthProfile | None,
|
| 159 |
request: gr.Request,
|
| 160 |
+
) -> tuple[str, gr.Dropdown, str, str]:
|
| 161 |
"""Create a notebook and refresh the selector."""
|
| 162 |
|
| 163 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 164 |
notebook: NotebookRecord = create_notebook(username, notebook_name)
|
| 165 |
login_status, dropdown = _refresh_notebook_state(username, notebook["id"])
|
| 166 |
+
return login_status, dropdown, "", username
|
| 167 |
|
| 168 |
|
| 169 |
def on_notebook_change(_notebook_id: str | None) -> tuple[list[tuple[str, str]], gr.Dropdown, str]:
|
|
|
|
| 223 |
def ingest_upload_ui(
|
| 224 |
notebook_id: str | None,
|
| 225 |
file_path: str | None,
|
| 226 |
+
current_username: str,
|
| 227 |
+
profile: gr.OAuthProfile | None,
|
| 228 |
request: gr.Request,
|
| 229 |
) -> str:
|
| 230 |
"""Ingest an uploaded local file through the backend ingestion APIs."""
|
| 231 |
|
| 232 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 233 |
if not notebook_id:
|
| 234 |
raise gr.Error("Select a notebook before uploading a source.")
|
| 235 |
if not file_path:
|
|
|
|
| 248 |
def ingest_url_ui(
|
| 249 |
notebook_id: str | None,
|
| 250 |
url: str,
|
| 251 |
+
current_username: str,
|
| 252 |
+
profile: gr.OAuthProfile | None,
|
| 253 |
request: gr.Request,
|
| 254 |
) -> str:
|
| 255 |
"""Ingest a URL source through the backend ingestion APIs."""
|
| 256 |
|
| 257 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 258 |
if not notebook_id:
|
| 259 |
raise gr.Error("Select a notebook before ingesting a URL.")
|
| 260 |
if not url or not url.strip():
|
|
|
|
| 274 |
notebook_id: str | None,
|
| 275 |
question: str,
|
| 276 |
history: list[tuple[str, str]] | None,
|
| 277 |
+
current_username: str,
|
| 278 |
+
profile: gr.OAuthProfile | None,
|
| 279 |
request: gr.Request,
|
| 280 |
) -> tuple[list[tuple[str, str]], str]:
|
| 281 |
"""Send one chat question and append the grounded answer to the chat history."""
|
| 282 |
|
| 283 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 284 |
if not notebook_id:
|
| 285 |
raise gr.Error("Select a notebook before asking a question.")
|
| 286 |
if not question or not question.strip():
|
|
|
|
| 309 |
def generate_report_ui(
|
| 310 |
notebook_id: str | None,
|
| 311 |
artifact_paths: list[str] | None,
|
| 312 |
+
current_username: str,
|
| 313 |
+
profile: gr.OAuthProfile | None,
|
| 314 |
request: gr.Request,
|
| 315 |
) -> tuple[list[str], gr.Dropdown]:
|
| 316 |
"""Generate a report artifact and update the download list."""
|
| 317 |
|
| 318 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 319 |
if not notebook_id:
|
| 320 |
raise gr.Error("Select a notebook before generating a report.")
|
| 321 |
artifact = generate_report(username, notebook_id)
|
|
|
|
| 325 |
def generate_quiz_ui(
|
| 326 |
notebook_id: str | None,
|
| 327 |
artifact_paths: list[str] | None,
|
| 328 |
+
current_username: str,
|
| 329 |
+
profile: gr.OAuthProfile | None,
|
| 330 |
request: gr.Request,
|
| 331 |
) -> tuple[list[str], gr.Dropdown]:
|
| 332 |
"""Generate a quiz artifact and update the download list."""
|
| 333 |
|
| 334 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 335 |
if not notebook_id:
|
| 336 |
raise gr.Error("Select a notebook before generating a quiz.")
|
| 337 |
artifact = generate_quiz(username, notebook_id)
|
|
|
|
| 341 |
def generate_podcast_ui(
|
| 342 |
notebook_id: str | None,
|
| 343 |
artifact_paths: list[str] | None,
|
| 344 |
+
current_username: str,
|
| 345 |
+
profile: gr.OAuthProfile | None,
|
| 346 |
request: gr.Request,
|
| 347 |
) -> tuple[list[str], gr.Dropdown]:
|
| 348 |
"""Generate a podcast transcript artifact and update the download list."""
|
| 349 |
|
| 350 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 351 |
if not notebook_id:
|
| 352 |
raise gr.Error("Select a notebook before generating a transcript.")
|
| 353 |
artifact = generate_podcast_transcript(username, notebook_id)
|
|
|
|
| 362 |
return Path(artifact_path)
|
| 363 |
|
| 364 |
|
| 365 |
+
def export_notebook_ui(
|
| 366 |
+
notebook_id: str | None,
|
| 367 |
+
current_username: str,
|
| 368 |
+
profile: gr.OAuthProfile | None,
|
| 369 |
+
request: gr.Request,
|
| 370 |
+
) -> Path:
|
| 371 |
"""Export the selected notebook as a zip archive."""
|
| 372 |
|
| 373 |
+
username: str = _resolve_username(profile, request, current_username)
|
| 374 |
if not notebook_id:
|
| 375 |
raise gr.Error("Select a notebook before exporting.")
|
| 376 |
return export_notebook_zip(username, notebook_id)
|
|
|
|
| 378 |
|
| 379 |
with gr.Blocks(title="NotebookLM Clone") as demo:
|
| 380 |
artifact_paths_state = gr.State(value=[])
|
| 381 |
+
username_state = gr.State(value="")
|
| 382 |
|
| 383 |
gr.Markdown("# NotebookLM Clone")
|
| 384 |
with gr.Row():
|
|
|
|
| 431 |
demo.load(
|
| 432 |
load_session,
|
| 433 |
inputs=None,
|
| 434 |
+
outputs=[login_status, notebook_dropdown, chat_history, artifact_dropdown, username_state],
|
| 435 |
)
|
| 436 |
|
| 437 |
create_notebook_button.click(
|
| 438 |
create_notebook_ui,
|
| 439 |
+
inputs=[new_notebook_name, username_state],
|
| 440 |
+
outputs=[login_status, notebook_dropdown, new_notebook_name, username_state],
|
| 441 |
)
|
| 442 |
|
| 443 |
notebook_dropdown.change(
|
|
|
|
| 452 |
|
| 453 |
upload_button.click(
|
| 454 |
ingest_upload_ui,
|
| 455 |
+
inputs=[notebook_dropdown, file_input, username_state],
|
| 456 |
outputs=[ingest_status],
|
| 457 |
)
|
| 458 |
|
| 459 |
url_button.click(
|
| 460 |
ingest_url_ui,
|
| 461 |
+
inputs=[notebook_dropdown, url_input, username_state],
|
| 462 |
outputs=[ingest_status],
|
| 463 |
)
|
| 464 |
|
| 465 |
ask_button.click(
|
| 466 |
send_chat_ui,
|
| 467 |
+
inputs=[notebook_dropdown, question_input, chat_history, username_state],
|
| 468 |
outputs=[chat_history, question_input],
|
| 469 |
)
|
| 470 |
|
| 471 |
report_button.click(
|
| 472 |
generate_report_ui,
|
| 473 |
+
inputs=[notebook_dropdown, artifact_paths_state, username_state],
|
| 474 |
outputs=[artifact_paths_state, artifact_dropdown],
|
| 475 |
)
|
| 476 |
|
| 477 |
quiz_button.click(
|
| 478 |
generate_quiz_ui,
|
| 479 |
+
inputs=[notebook_dropdown, artifact_paths_state, username_state],
|
| 480 |
outputs=[artifact_paths_state, artifact_dropdown],
|
| 481 |
)
|
| 482 |
|
| 483 |
podcast_button.click(
|
| 484 |
generate_podcast_ui,
|
| 485 |
+
inputs=[notebook_dropdown, artifact_paths_state, username_state],
|
| 486 |
outputs=[artifact_paths_state, artifact_dropdown],
|
| 487 |
)
|
| 488 |
|
|
|
|
| 494 |
|
| 495 |
export_button.click(
|
| 496 |
export_notebook_ui,
|
| 497 |
+
inputs=[notebook_dropdown, username_state],
|
| 498 |
outputs=[export_download],
|
| 499 |
)
|
| 500 |
|