Spaces:
Running
Running
Fix: auto-load sample docs at startup so state survives restarts
Browse files
app.py
CHANGED
|
@@ -45,6 +45,24 @@ _graph = build_graph(_retriever)
|
|
| 45 |
_raw_docs: List[Tuple[str, str]] = [] # (original_text, filename) for safety checker
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# ── Model call helper (shared by agent + safety checker) ──────────────────────
|
| 49 |
|
| 50 |
def _call_model(prompt: str, max_tokens: int = 600, json_mode: bool = False) -> str:
|
|
|
|
| 45 |
_raw_docs: List[Tuple[str, str]] = [] # (original_text, filename) for safety checker
|
| 46 |
|
| 47 |
|
| 48 |
+
def _auto_load_samples() -> None:
|
| 49 |
+
"""Pre-load sample docs at startup so the app is ready immediately after any restart."""
|
| 50 |
+
if not SAMPLE_DIR.exists():
|
| 51 |
+
return
|
| 52 |
+
docs = [
|
| 53 |
+
(p.read_text(encoding="utf-8", errors="ignore"), p.name)
|
| 54 |
+
for p in sorted(SAMPLE_DIR.glob("*.txt"))
|
| 55 |
+
]
|
| 56 |
+
if not docs:
|
| 57 |
+
return
|
| 58 |
+
_retriever.add_documents(docs)
|
| 59 |
+
_raw_docs.clear()
|
| 60 |
+
_raw_docs.extend(docs)
|
| 61 |
+
print(f"[startup] Auto-loaded {len(docs)} sample docs → {len(_retriever.chunks)} chunks ready")
|
| 62 |
+
|
| 63 |
+
_auto_load_samples()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
# ── Model call helper (shared by agent + safety checker) ──────────────────────
|
| 67 |
|
| 68 |
def _call_model(prompt: str, max_tokens: int = 600, json_mode: bool = False) -> str:
|