Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,7 @@ def init_db():
|
|
| 33 |
|
| 34 |
repo_id = get_repo_id()
|
| 35 |
|
|
|
|
| 36 |
if HF_TOKEN and repo_id:
|
| 37 |
api = HfApi(token=HF_TOKEN)
|
| 38 |
try:
|
|
@@ -51,17 +52,40 @@ def init_db():
|
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Cloud sync error: {e}")
|
| 53 |
|
|
|
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
-
print(f"Memvid init
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
db = None
|
| 66 |
|
| 67 |
def sync_to_hub():
|
|
@@ -81,6 +105,7 @@ def sync_to_hub():
|
|
| 81 |
except Exception as e:
|
| 82 |
print(f"Sync failed: {e}")
|
| 83 |
|
|
|
|
| 84 |
init_db()
|
| 85 |
|
| 86 |
@app.route('/')
|
|
@@ -93,14 +118,14 @@ def add_memory():
|
|
| 93 |
if not db:
|
| 94 |
init_db()
|
| 95 |
if not db:
|
| 96 |
-
return jsonify({"error": "Database could not be initialized."}), 500
|
| 97 |
|
| 98 |
content = request.form.get('content')
|
| 99 |
-
|
| 100 |
if not content:
|
| 101 |
return jsonify({"error": "No content provided"}), 400
|
| 102 |
|
| 103 |
try:
|
|
|
|
| 104 |
payload = {
|
| 105 |
"text": content,
|
| 106 |
"title": "User Memory",
|
|
@@ -109,14 +134,19 @@ def add_memory():
|
|
| 109 |
|
| 110 |
db.put(payload)
|
| 111 |
|
|
|
|
| 112 |
del db
|
| 113 |
db = None
|
| 114 |
|
|
|
|
| 115 |
sync_to_hub()
|
|
|
|
|
|
|
| 116 |
init_db()
|
| 117 |
|
| 118 |
return jsonify({"success": True, "message": "Memory added and synced to cloud."})
|
| 119 |
except Exception as e:
|
|
|
|
| 120 |
return jsonify({"error": str(e)}), 500
|
| 121 |
|
| 122 |
@app.route('/search', methods=['POST'])
|
|
@@ -129,16 +159,16 @@ def search_memory():
|
|
| 129 |
return jsonify({"error": "No query provided"}), 400
|
| 130 |
|
| 131 |
try:
|
| 132 |
-
# FIX
|
| 133 |
-
# The previous error "cannot convert dict to PyString" means 'find' expects a string arg.
|
| 134 |
-
# We pass top_k as a keyword argument.
|
| 135 |
results = db.find(query, top_k=5)
|
| 136 |
|
| 137 |
formatted_results = []
|
| 138 |
for hit in results:
|
|
|
|
| 139 |
if isinstance(hit, dict):
|
| 140 |
text = hit.get('text') or hit.get('content') or "No text"
|
| 141 |
else:
|
|
|
|
| 142 |
text = getattr(hit, 'text', getattr(hit, 'content', str(hit)))
|
| 143 |
|
| 144 |
formatted_results.append({
|
|
@@ -147,6 +177,7 @@ def search_memory():
|
|
| 147 |
|
| 148 |
return jsonify({"success": True, "results": formatted_results})
|
| 149 |
except Exception as e:
|
|
|
|
| 150 |
return jsonify({"error": str(e)}), 500
|
| 151 |
|
| 152 |
if __name__ == '__main__':
|
|
|
|
| 33 |
|
| 34 |
repo_id = get_repo_id()
|
| 35 |
|
| 36 |
+
# 1. Cloud Sync
|
| 37 |
if HF_TOKEN and repo_id:
|
| 38 |
api = HfApi(token=HF_TOKEN)
|
| 39 |
try:
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Cloud sync error: {e}")
|
| 54 |
|
| 55 |
+
# 2. Initialize Memvid (The Fix)
|
| 56 |
try:
|
| 57 |
+
if os.path.exists(DB_PATH):
|
| 58 |
+
print(f"📂 Opening existing database at {DB_PATH}")
|
| 59 |
+
# FIX: Use the static .open() method.
|
| 60 |
+
# The constructor Memvid(path) defaults to ReadOnly.
|
| 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("❌ Database object is None after init.")
|
| 81 |
+
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"❌ Memvid init CRITICAL ERROR: {e}")
|
| 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():
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Sync failed: {e}")
|
| 107 |
|
| 108 |
+
# Run init on start
|
| 109 |
init_db()
|
| 110 |
|
| 111 |
@app.route('/')
|
|
|
|
| 118 |
if not db:
|
| 119 |
init_db()
|
| 120 |
if not db:
|
| 121 |
+
return jsonify({"error": "Database could not be initialized. Check Server Logs."}), 500
|
| 122 |
|
| 123 |
content = request.form.get('content')
|
|
|
|
| 124 |
if not content:
|
| 125 |
return jsonify({"error": "No content provided"}), 400
|
| 126 |
|
| 127 |
try:
|
| 128 |
+
# Payload must be a dictionary
|
| 129 |
payload = {
|
| 130 |
"text": content,
|
| 131 |
"title": "User Memory",
|
|
|
|
| 134 |
|
| 135 |
db.put(payload)
|
| 136 |
|
| 137 |
+
# Force flush/close to save data before sync
|
| 138 |
del db
|
| 139 |
db = None
|
| 140 |
|
| 141 |
+
# Sync to Hugging Face
|
| 142 |
sync_to_hub()
|
| 143 |
+
|
| 144 |
+
# Re-open the database for the next request
|
| 145 |
init_db()
|
| 146 |
|
| 147 |
return jsonify({"success": True, "message": "Memory added and synced to cloud."})
|
| 148 |
except Exception as e:
|
| 149 |
+
print(f"Add Error: {e}")
|
| 150 |
return jsonify({"error": str(e)}), 500
|
| 151 |
|
| 152 |
@app.route('/search', methods=['POST'])
|
|
|
|
| 159 |
return jsonify({"error": "No query provided"}), 400
|
| 160 |
|
| 161 |
try:
|
| 162 |
+
# FIX: query must be string, top_k is keyword arg
|
|
|
|
|
|
|
| 163 |
results = db.find(query, top_k=5)
|
| 164 |
|
| 165 |
formatted_results = []
|
| 166 |
for hit in results:
|
| 167 |
+
# Handle result object vs dict
|
| 168 |
if isinstance(hit, dict):
|
| 169 |
text = hit.get('text') or hit.get('content') or "No text"
|
| 170 |
else:
|
| 171 |
+
# Try common attribute names for the hit object
|
| 172 |
text = getattr(hit, 'text', getattr(hit, 'content', str(hit)))
|
| 173 |
|
| 174 |
formatted_results.append({
|
|
|
|
| 177 |
|
| 178 |
return jsonify({"success": True, "results": formatted_results})
|
| 179 |
except Exception as e:
|
| 180 |
+
print(f"Search Error: {e}")
|
| 181 |
return jsonify({"error": str(e)}), 500
|
| 182 |
|
| 183 |
if __name__ == '__main__':
|