Spaces:
Running
Running
Download backend/routes/library.py from diptidhawade/NoteScanner: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/diptidhawade/NoteScanner/resolve/main/backend/routes/library.py
- Command line
-
hf download hf://spaces/diptidhawade/NoteScanner/backend/routes/library.py
-
curl -L -o library.py https://huggingface.co/spaces/diptidhawade/NoteScanner/resolve/main/backend/routes/library.py
1.76 kB
| """The shared course library: which PDFs are ready for questions, and admin controls for indexing.""" | |
| from fastapi import APIRouter, Form | |
| from backend import library, settings | |
| from backend.deps import SignedInUser | |
| from backend.routes.admin import _require_admin | |
| router = APIRouter() | |
| def library_status(): | |
| """Public: the course PDFs (storage paths) students can ask about. No account needed.""" | |
| return library.public_status() | |
| def admin_library(user_id: SignedInUser = None): | |
| """Per-file indexing details and the last sync's summary (admins only).""" | |
| _require_admin(user_id) | |
| files = library.indexed_files() | |
| by_status: dict[str, int] = {} | |
| for m in files.values(): | |
| by_status[m.get("status") or "unknown"] = by_status.get(m.get("status") or "unknown", 0) + 1 | |
| keep = ("path", "status", "pages", "ocr_pieces", "sarvam_pieces", "chunks", "method", "error", "updated_at") | |
| return { | |
| "enabled": settings.LIBRARY_ENABLED, | |
| "ocr": library._ocr_mode(), | |
| "sync_hours": settings.LIBRARY_SYNC_HOURS, | |
| "by_status": by_status, | |
| "sync": library.sync_state(), | |
| "files": sorted(({k: m.get(k) for k in keep} for m in files.values()), key=lambda f: f["path"]), | |
| } | |
| def admin_library_sync(force: bool = Form(False), user_id: SignedInUser = None): | |
| """Start indexing new or changed course PDFs in the background (admins only).""" | |
| _require_admin(user_id) | |
| started = library.sync_in_background(force=force) | |
| return { | |
| "started": started, | |
| "message": "Library sync started." if started else "A library sync is already running.", | |
| "sync": library.sync_state(), | |
| } | |