Hemanth-05 commited on
Commit
79eb9e2
Β·
1 Parent(s): 9b69919

Added thread-based background ingestion worker

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  """NotebookLM β€” AI-Powered Study Companion (Gradio)."""
2
 
 
3
  import gradio as gr
4
 
5
  from state import (
@@ -42,6 +43,9 @@ from pages.artifacts import (
42
  has_any_summary,
43
  )
44
 
 
 
 
45
 
46
  # ── Helpers ──────────────────────────────────────────────────────────────────
47
 
@@ -514,6 +518,13 @@ with gr.Blocks(css=CUSTOM_CSS, theme=dark_theme, title="NotebookLM") as demo:
514
  ).then(fn=None, js=MARK_DIRTY_JS)
515
 
516
  # ── Sources: Process pending sources ─────────────────────────────────────
 
 
 
 
 
 
 
517
  def handle_process_sources(state):
518
  nb = get_active_notebook(state)
519
  if not nb:
@@ -529,13 +540,26 @@ with gr.Blocks(css=CUSTOM_CSS, theme=dark_theme, title="NotebookLM") as demo:
529
  )
530
  return refresh_all(state) + (msg,)
531
 
532
- state, _, _, _ = run_ingestion_pipeline(state)
533
- nb_after = get_active_notebook(state)
534
- ready_count = sum(1 for s in nb_after.sources if s.status == "ready")
535
- failed_count = sum(1 for s in nb_after.sources if s.status == "failed")
 
 
 
 
 
 
 
 
 
 
 
 
 
536
  msg = (
537
  '<p style="font-size:0.82rem; color:#22c55e; margin:6px 0 0 0;">'
538
- f'Processing complete: {ready_count} ready, {failed_count} failed.'
539
  '</p>'
540
  )
541
  return refresh_all(state) + (msg,)
 
1
  """NotebookLM β€” AI-Powered Study Companion (Gradio)."""
2
 
3
+ import threading
4
  import gradio as gr
5
 
6
  from state import (
 
43
  has_any_summary,
44
  )
45
 
46
+ _INGESTION_LOCK = threading.Lock()
47
+ _INGESTION_RUNNING_NOTEBOOKS: set[str] = set()
48
+
49
 
50
  # ── Helpers ──────────────────────────────────────────────────────────────────
51
 
 
518
  ).then(fn=None, js=MARK_DIRTY_JS)
519
 
520
  # ── Sources: Process pending sources ─────────────────────────────────────
521
+ def _run_ingestion_background(state: UserData, notebook_id: str):
522
+ try:
523
+ run_ingestion_pipeline(state)
524
+ finally:
525
+ with _INGESTION_LOCK:
526
+ _INGESTION_RUNNING_NOTEBOOKS.discard(notebook_id)
527
+
528
  def handle_process_sources(state):
529
  nb = get_active_notebook(state)
530
  if not nb:
 
540
  )
541
  return refresh_all(state) + (msg,)
542
 
543
+ with _INGESTION_LOCK:
544
+ if nb.id in _INGESTION_RUNNING_NOTEBOOKS:
545
+ msg = (
546
+ '<p style="font-size:0.82rem; color:#9090a8; margin:6px 0 0 0;">'
547
+ 'Processing already running for this notebook.'
548
+ '</p>'
549
+ )
550
+ return refresh_all(state) + (msg,)
551
+ _INGESTION_RUNNING_NOTEBOOKS.add(nb.id)
552
+
553
+ worker = threading.Thread(
554
+ target=_run_ingestion_background,
555
+ args=(state, nb.id),
556
+ daemon=True,
557
+ )
558
+ worker.start()
559
+
560
  msg = (
561
  '<p style="font-size:0.82rem; color:#22c55e; margin:6px 0 0 0;">'
562
+ 'Processing started in background. You can switch tabs; return to Sources to check status.'
563
  '</p>'
564
  )
565
  return refresh_all(state) + (msg,)