Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,40 +52,26 @@ def init_db():
|
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Cloud sync error: {e}")
|
| 54 |
|
| 55 |
-
# 2. Initialize Memvid
|
| 56 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if os.path.exists(DB_PATH):
|
| 58 |
-
print(f"π
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Memvid.open(path) typically opens in Read/Write mode.
|
| 62 |
-
if hasattr(Memvid, 'open'):
|
| 63 |
-
db = Memvid.open(DB_PATH)
|
| 64 |
-
else:
|
| 65 |
-
# Fallback if .open isn't static
|
| 66 |
-
db = Memvid(DB_PATH)
|
| 67 |
-
else:
|
| 68 |
-
print(f"β¨ Creating new database at {DB_PATH}")
|
| 69 |
-
# FIX: Use the static .create() method for new files.
|
| 70 |
-
if hasattr(Memvid, 'create'):
|
| 71 |
-
db = Memvid.create(DB_PATH)
|
| 72 |
-
else:
|
| 73 |
-
# Fallback
|
| 74 |
-
db = Memvid(DB_PATH)
|
| 75 |
-
|
| 76 |
-
# Double check we have a valid object
|
| 77 |
-
if db:
|
| 78 |
-
print("β
Database initialized successfully.")
|
| 79 |
else:
|
| 80 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
print(f"β Memvid init
|
| 84 |
-
# Print attributes to help debug if it fails again
|
| 85 |
-
try:
|
| 86 |
-
print(f"Available attributes: {dir(Memvid)}")
|
| 87 |
-
except:
|
| 88 |
-
pass
|
| 89 |
db = None
|
| 90 |
|
| 91 |
def sync_to_hub():
|
|
@@ -125,7 +111,7 @@ def add_memory():
|
|
| 125 |
return jsonify({"error": "No content provided"}), 400
|
| 126 |
|
| 127 |
try:
|
| 128 |
-
# Payload must be a
|
| 129 |
payload = {
|
| 130 |
"text": content,
|
| 131 |
"title": "User Memory",
|
|
@@ -134,7 +120,7 @@ def add_memory():
|
|
| 134 |
|
| 135 |
db.put(payload)
|
| 136 |
|
| 137 |
-
# Force flush/close to
|
| 138 |
del db
|
| 139 |
db = None
|
| 140 |
|
|
@@ -159,7 +145,8 @@ def search_memory():
|
|
| 159 |
return jsonify({"error": "No query provided"}), 400
|
| 160 |
|
| 161 |
try:
|
| 162 |
-
# FIX: query must be
|
|
|
|
| 163 |
results = db.find(query, top_k=5)
|
| 164 |
|
| 165 |
formatted_results = []
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Cloud sync error: {e}")
|
| 54 |
|
| 55 |
+
# 2. Initialize Memvid
|
| 56 |
try:
|
| 57 |
+
# Step 1: Initialize the handle (This works, but defaults to ReadOnly)
|
| 58 |
+
db = Memvid(DB_PATH)
|
| 59 |
+
|
| 60 |
+
# Step 2: Explicitly switch to Write Mode using instance methods
|
| 61 |
+
# Based on dir(db) showing 'create' and 'open'
|
| 62 |
if os.path.exists(DB_PATH):
|
| 63 |
+
print(f"π File exists. Calling db.open() to enable write access...")
|
| 64 |
+
if hasattr(db, 'open'):
|
| 65 |
+
db.open()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
+
print(f"β¨ New file. Calling db.create() to initialize...")
|
| 68 |
+
if hasattr(db, 'create'):
|
| 69 |
+
db.create()
|
| 70 |
+
|
| 71 |
+
print("β
Database initialized.")
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
print(f"β Memvid init error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
db = None
|
| 76 |
|
| 77 |
def sync_to_hub():
|
|
|
|
| 111 |
return jsonify({"error": "No content provided"}), 400
|
| 112 |
|
| 113 |
try:
|
| 114 |
+
# FIX: Payload must be a DICT (Solved 'str' cannot be converted to 'PyDict')
|
| 115 |
payload = {
|
| 116 |
"text": content,
|
| 117 |
"title": "User Memory",
|
|
|
|
| 120 |
|
| 121 |
db.put(payload)
|
| 122 |
|
| 123 |
+
# Force flush/close to ensure data is written before upload
|
| 124 |
del db
|
| 125 |
db = None
|
| 126 |
|
|
|
|
| 145 |
return jsonify({"error": "No query provided"}), 400
|
| 146 |
|
| 147 |
try:
|
| 148 |
+
# FIX: query must be STRING (Solved 'dict' cannot be converted to 'PyString')
|
| 149 |
+
# Use .find() based on your logs
|
| 150 |
results = db.find(query, top_k=5)
|
| 151 |
|
| 152 |
formatted_results = []
|