Spaces:
Runtime error
Runtime error
File size: 4,759 Bytes
f68a655 f35fb87 f68a655 f35fb87 f68a655 f35fb87 f68a655 f35fb87 f68a655 f35fb87 f68a655 f35fb87 f68a655 f35fb87 f68a655 d8f5bd1 f35fb87 f68a655 d8f5bd1 f68a655 f35fb87 e0af6e8 d8f5bd1 f68a655 d8f5bd1 f35fb87 d8f5bd1 f68a655 d8f5bd1 f35fb87 d8f5bd1 f68a655 f35fb87 f68a655 | 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 | import gradio as gr
from notebook_store import (
create_notebook,
delete_notebook as delete_notebook_on_disk,
duplicate_notebook as duplicate_notebook_on_disk,
list_source_events,
rename_notebook as rename_notebook_on_disk,
)
def _choices(notebooks: list[dict]) -> list[tuple[str, str]]:
return [(n["name"], n["id"]) for n in notebooks]
def _sources_table(notebook_id: str) -> list[list[str]]:
events = list_source_events(notebook_id)
rows: list[list[str]] = []
for e in events:
kind = e.get("kind")
if kind == "file":
rows.append([str(e.get("original_filename") or e.get("source_name") or "")])
elif kind == "url":
rows.append([str(e.get("url") or e.get("source_name") or "")])
return [r for r in rows if r and r[0]]
def delete_notebook(selected_id, notebooks):
selected_id = (selected_id or "").strip()
notebooks = list(notebooks or [])
if not selected_id:
return gr.update(), notebooks, selected_id, _sources_table(selected_id) if selected_id else []
delete_notebook_on_disk(selected_id)
notebooks = [n for n in notebooks if n.get("id") != selected_id]
if not notebooks:
nb = create_notebook("Notebook 1")
notebooks = [{"id": nb.id, "name": nb.name}]
next_id = notebooks[0]["id"]
return gr.update(choices=_choices(notebooks), value=next_id), notebooks, next_id, _sources_table(next_id)
def rename_notebook(selected_id, new_name, notebooks):
selected_id = (selected_id or "").strip()
new_name = (new_name or "").strip()
notebooks = list(notebooks or [])
if not selected_id or not new_name:
return gr.update(), notebooks, "", selected_id, _sources_table(selected_id) if selected_id else []
existing_names = {str(n.get("name")) for n in notebooks}
current = next((n for n in notebooks if n.get("id") == selected_id), None)
if current is None:
return gr.update(), notebooks, "", selected_id, _sources_table(selected_id)
if new_name in existing_names and new_name != current.get("name"):
return gr.update(), notebooks, "", selected_id, _sources_table(selected_id)
meta = rename_notebook_on_disk(selected_id, new_name)
current["name"] = meta.name
current["id"] = meta.id
notebooks.sort(key=lambda n: str(n.get("name", "")).lower())
return gr.update(choices=_choices(notebooks), value=meta.id), notebooks, "", meta.id, _sources_table(meta.id)
def duplicate_notebook(selected_id, notebooks):
selected_id = (selected_id or "").strip()
notebooks = list(notebooks or [])
if not selected_id:
return gr.update(), notebooks, selected_id, _sources_table(selected_id) if selected_id else []
current = next((n for n in notebooks if n.get("id") == selected_id), None)
base_name = str(current.get("name") if current else "Notebook")
new_name = make_copy_name(base_name, [str(n.get("name")) for n in notebooks])
meta = duplicate_notebook_on_disk(selected_id, new_name=new_name)
notebooks.append({"id": meta.id, "name": meta.name})
notebooks.sort(key=lambda n: str(n.get("name", "")).lower())
return gr.update(choices=_choices(notebooks), value=meta.id), notebooks, meta.id, _sources_table(meta.id)
def make_copy_name(base: str, choices: list[str]) -> str:
base = (base or "").strip()
if not base:
return ""
candidate = f"{base} (copy)"
if candidate not in choices:
return candidate
i = 2
while True:
candidate = f"{base} (copy {i})"
if candidate not in choices:
return candidate
i += 1
def ManageNotebook(notebook, notebooks_state, notebook_id_state, ingested_list):
with gr.Accordion("Manage Notebook", open=False, elem_classes=["section-card"]):
updated_name = gr.Textbox(label="Update name", interactive=True)
rename_nb = gr.Button("Rename", variant="secondary", elem_classes=["full-width"])
delete_nb = gr.Button("Delete", variant="stop", elem_classes=["full-width"])
duplicate_nb = gr.Button("Duplicate", variant="secondary", elem_classes=["full-width"])
delete_nb.click(
delete_notebook,
inputs=[notebook, notebooks_state],
outputs=[notebook, notebooks_state, notebook_id_state, ingested_list],
)
rename_nb.click(
rename_notebook,
inputs=[notebook, updated_name, notebooks_state],
outputs=[notebook, notebooks_state, updated_name, notebook_id_state, ingested_list],
)
duplicate_nb.click(
duplicate_notebook,
inputs=[notebook, notebooks_state],
outputs=[notebook, notebooks_state, notebook_id_state, ingested_list],
) |