Spaces:
Runtime error
Runtime error
wolfgangspoony commited on
Commit Β·
7842baf
1
Parent(s): 22bebc6
Fix: async/sync reload_memory bug, add tempfile import, fix export visibility
Browse files- app.py +5 -7
- knowledge.py +19 -3
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import shutil
|
|
|
|
| 4 |
import time
|
| 5 |
import uuid
|
| 6 |
from datetime import datetime, timedelta
|
|
@@ -8,7 +9,7 @@ from pathlib import Path
|
|
| 8 |
from knowledge import (
|
| 9 |
run_agent,
|
| 10 |
load_memory,
|
| 11 |
-
|
| 12 |
get_or_build_index,
|
| 13 |
SearchTool,
|
| 14 |
MEMORY_FILE,
|
|
@@ -105,9 +106,6 @@ async def load_history():
|
|
| 105 |
|
| 106 |
def export_memory():
|
| 107 |
"""Export memory.json for download."""
|
| 108 |
-
import tempfile
|
| 109 |
-
import shutil
|
| 110 |
-
|
| 111 |
if not MEMORY_FILE.exists():
|
| 112 |
return None
|
| 113 |
|
|
@@ -144,7 +142,7 @@ def import_memory(file):
|
|
| 144 |
shutil.copy(uploaded_path, MEMORY_FILE)
|
| 145 |
|
| 146 |
# Reload memory from disk
|
| 147 |
-
shared_memory =
|
| 148 |
|
| 149 |
session_count = len(shared_memory.get("sessions", []))
|
| 150 |
return f"β
Memory imported successfully! {session_count} sessions loaded."
|
|
@@ -278,8 +276,8 @@ with gr.Blocks(title="Brain", theme=gr.themes.Soft()) as demo:
|
|
| 278 |
interactive=False,
|
| 279 |
show_copy_button=True,
|
| 280 |
)
|
| 281 |
-
#
|
| 282 |
-
export_file = gr.File(label="Download
|
| 283 |
|
| 284 |
def lock_history():
|
| 285 |
return gr.update(visible=False), gr.update(visible=True), ""
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import shutil
|
| 4 |
+
import tempfile
|
| 5 |
import time
|
| 6 |
import uuid
|
| 7 |
from datetime import datetime, timedelta
|
|
|
|
| 9 |
from knowledge import (
|
| 10 |
run_agent,
|
| 11 |
load_memory,
|
| 12 |
+
reload_memory_sync,
|
| 13 |
get_or_build_index,
|
| 14 |
SearchTool,
|
| 15 |
MEMORY_FILE,
|
|
|
|
| 106 |
|
| 107 |
def export_memory():
|
| 108 |
"""Export memory.json for download."""
|
|
|
|
|
|
|
|
|
|
| 109 |
if not MEMORY_FILE.exists():
|
| 110 |
return None
|
| 111 |
|
|
|
|
| 142 |
shutil.copy(uploaded_path, MEMORY_FILE)
|
| 143 |
|
| 144 |
# Reload memory from disk
|
| 145 |
+
shared_memory = reload_memory_sync()
|
| 146 |
|
| 147 |
session_count = len(shared_memory.get("sessions", []))
|
| 148 |
return f"β
Memory imported successfully! {session_count} sessions loaded."
|
|
|
|
| 276 |
interactive=False,
|
| 277 |
show_copy_button=True,
|
| 278 |
)
|
| 279 |
+
# File component for download
|
| 280 |
+
export_file = gr.File(label="Download Memory")
|
| 281 |
|
| 282 |
def lock_history():
|
| 283 |
return gr.update(visible=False), gr.update(visible=True), ""
|
knowledge.py
CHANGED
|
@@ -453,11 +453,27 @@ def init():
|
|
| 453 |
return _search_index, _memory_cache
|
| 454 |
|
| 455 |
|
| 456 |
-
def
|
| 457 |
-
"""Force reload memory from disk."""
|
| 458 |
global _memory_cache
|
| 459 |
_memory_cache = None
|
| 460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
|
| 463 |
print("Brain module loaded.")
|
|
|
|
| 453 |
return _search_index, _memory_cache
|
| 454 |
|
| 455 |
|
| 456 |
+
def reload_memory_sync():
|
| 457 |
+
"""Force reload memory from disk (synchronous version for non-async contexts)."""
|
| 458 |
global _memory_cache
|
| 459 |
_memory_cache = None
|
| 460 |
+
|
| 461 |
+
if MEMORY_FILE.exists():
|
| 462 |
+
try:
|
| 463 |
+
_memory_cache = json.loads(MEMORY_FILE.read_text(encoding="utf-8"))
|
| 464 |
+
return _memory_cache
|
| 465 |
+
except Exception:
|
| 466 |
+
pass
|
| 467 |
+
|
| 468 |
+
_memory_cache = {"sessions": []}
|
| 469 |
+
return _memory_cache
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
async def reload_memory():
|
| 473 |
+
"""Force reload memory from disk (async version)."""
|
| 474 |
+
global _memory_cache
|
| 475 |
+
_memory_cache = None
|
| 476 |
+
return await load_memory()
|
| 477 |
|
| 478 |
|
| 479 |
print("Brain module loaded.")
|