Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -126,22 +126,30 @@ def add_memory():
|
|
| 126 |
if not db:
|
| 127 |
init_db()
|
| 128 |
if not db:
|
| 129 |
-
return jsonify({"error": "Database could not be initialized.
|
| 130 |
|
| 131 |
content = request.form.get('content')
|
| 132 |
-
|
| 133 |
-
|
| 134 |
if not content:
|
| 135 |
return jsonify({"error": "No content provided"}), 400
|
| 136 |
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
sync_to_hub()
|
|
|
|
| 141 |
|
| 142 |
return jsonify({"success": True, "message": "Memory added and synced to cloud."})
|
| 143 |
except Exception as e:
|
| 144 |
-
print(f"Error adding memory: {e}")
|
| 145 |
return jsonify({"error": str(e)}), 500
|
| 146 |
|
| 147 |
@app.route('/search', methods=['POST'])
|
|
@@ -149,23 +157,28 @@ def search_memory():
|
|
| 149 |
if not db:
|
| 150 |
return jsonify({"error": "Database not initialized"}), 500
|
| 151 |
|
| 152 |
-
# --- DEBUGGING SNIPPET ---
|
| 153 |
-
print("🔎 AVAILABLE METHODS ON DB OBJECT:")
|
| 154 |
-
print([method for method in dir(db) if not method.startswith('_')])
|
| 155 |
-
# -------------------------
|
| 156 |
-
|
| 157 |
query = request.form.get('query')
|
| 158 |
if not query:
|
| 159 |
return jsonify({"error": "No query provided"}), 400
|
| 160 |
|
| 161 |
try:
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
formatted_results = []
|
| 166 |
for hit in results:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
formatted_results.append({
|
| 168 |
-
"text":
|
| 169 |
})
|
| 170 |
|
| 171 |
return jsonify({"success": True, "results": formatted_results})
|
|
|
|
| 126 |
if not db:
|
| 127 |
init_db()
|
| 128 |
if not db:
|
| 129 |
+
return jsonify({"error": "Database could not be initialized."}), 500
|
| 130 |
|
| 131 |
content = request.form.get('content')
|
| 132 |
+
|
|
|
|
| 133 |
if not content:
|
| 134 |
return jsonify({"error": "No content provided"}), 400
|
| 135 |
|
| 136 |
try:
|
| 137 |
+
payload = {
|
| 138 |
+
"text": content,
|
| 139 |
+
"title": "User Memory",
|
| 140 |
+
"tag": "web-entry"
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
db.put(payload)
|
| 144 |
+
|
| 145 |
+
del db
|
| 146 |
+
db = None
|
| 147 |
+
|
| 148 |
sync_to_hub()
|
| 149 |
+
init_db()
|
| 150 |
|
| 151 |
return jsonify({"success": True, "message": "Memory added and synced to cloud."})
|
| 152 |
except Exception as e:
|
|
|
|
| 153 |
return jsonify({"error": str(e)}), 500
|
| 154 |
|
| 155 |
@app.route('/search', methods=['POST'])
|
|
|
|
| 157 |
if not db:
|
| 158 |
return jsonify({"error": "Database not initialized"}), 500
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
query = request.form.get('query')
|
| 161 |
if not query:
|
| 162 |
return jsonify({"error": "No query provided"}), 400
|
| 163 |
|
| 164 |
try:
|
| 165 |
+
search_req = {
|
| 166 |
+
"query": query,
|
| 167 |
+
"top_k": 5,
|
| 168 |
+
"snippet_chars": 200
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
results = db.find(search_req)
|
| 172 |
|
| 173 |
formatted_results = []
|
| 174 |
for hit in results:
|
| 175 |
+
if isinstance(hit, dict):
|
| 176 |
+
text = hit.get('text') or hit.get('content') or "No text"
|
| 177 |
+
else:
|
| 178 |
+
text = getattr(hit, 'text', getattr(hit, 'content', str(hit)))
|
| 179 |
+
|
| 180 |
formatted_results.append({
|
| 181 |
+
"text": text
|
| 182 |
})
|
| 183 |
|
| 184 |
return jsonify({"success": True, "results": formatted_results})
|