Eric Xu commited on
Restore saved sessions from disk on server startup
Browse files- web/app.py +38 -0
web/app.py
CHANGED
|
@@ -662,6 +662,44 @@ async def get_results(sid: str):
|
|
| 662 |
}
|
| 663 |
|
| 664 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
if __name__ == "__main__":
|
| 666 |
import uvicorn
|
| 667 |
print(f"\n SGO Web Interface")
|
|
|
|
| 662 |
}
|
| 663 |
|
| 664 |
|
| 665 |
+
@app.on_event("startup")
|
| 666 |
+
async def restore_sessions():
|
| 667 |
+
"""Restore saved sessions from results/ on startup."""
|
| 668 |
+
results_dir = PROJECT_ROOT / "results"
|
| 669 |
+
if not results_dir.exists():
|
| 670 |
+
return
|
| 671 |
+
for d in results_dir.iterdir():
|
| 672 |
+
if not d.is_dir():
|
| 673 |
+
continue
|
| 674 |
+
raw = d / "raw_results.json"
|
| 675 |
+
cohort_file = d / "cohort.json"
|
| 676 |
+
meta_file = d / "meta.json"
|
| 677 |
+
if raw.exists() and cohort_file.exists():
|
| 678 |
+
try:
|
| 679 |
+
with open(raw) as f:
|
| 680 |
+
eval_results = json.load(f)
|
| 681 |
+
with open(cohort_file) as f:
|
| 682 |
+
cohort = json.load(f)
|
| 683 |
+
entity_text = ""
|
| 684 |
+
if meta_file.exists():
|
| 685 |
+
with open(meta_file) as f:
|
| 686 |
+
meta = json.load(f)
|
| 687 |
+
entity_text = meta.get("entity", "")
|
| 688 |
+
sid = d.name.replace("web_", "")
|
| 689 |
+
sessions[sid] = {
|
| 690 |
+
"id": sid,
|
| 691 |
+
"entity_text": entity_text,
|
| 692 |
+
"cohort": cohort,
|
| 693 |
+
"eval_results": eval_results,
|
| 694 |
+
"gradient": None,
|
| 695 |
+
"created": "",
|
| 696 |
+
}
|
| 697 |
+
valid = [r for r in eval_results if isinstance(r, dict) and "score" in r]
|
| 698 |
+
print(f" Restored session {sid}: {len(valid)} results, {len(cohort)} cohort")
|
| 699 |
+
except Exception as e:
|
| 700 |
+
print(f" Failed to restore {d.name}: {e}")
|
| 701 |
+
|
| 702 |
+
|
| 703 |
if __name__ == "__main__":
|
| 704 |
import uvicorn
|
| 705 |
print(f"\n SGO Web Interface")
|