Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from flask import Flask, render_template, request, jsonify
|
| 3 |
from memvid_sdk import create, open as open_memvid
|
| 4 |
from huggingface_hub import hf_hub_download, upload_file, HfApi
|
| 5 |
|
|
@@ -116,37 +116,56 @@ def index():
|
|
| 116 |
|
| 117 |
@app.route('/add', methods=['POST'])
|
| 118 |
def add_memory():
|
|
|
|
| 119 |
global db
|
| 120 |
-
if not db:
|
| 121 |
-
init_db()
|
| 122 |
-
if not db:
|
| 123 |
-
return jsonify({"error": "Database could not be initialized."}), 500
|
| 124 |
-
|
| 125 |
content = request.form.get('content')
|
| 126 |
|
| 127 |
if not content:
|
| 128 |
return jsonify({"error": "No content provided"}), 400
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
del db
|
| 141 |
-
db = None
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
return jsonify({"success": True, "message": "Memory added and synced to cloud."})
|
| 147 |
-
except Exception as e:
|
| 148 |
-
if db is None: init_db()
|
| 149 |
-
return jsonify({"error": str(e)}), 500
|
| 150 |
|
| 151 |
@app.route('/search', methods=['POST'])
|
| 152 |
def search_memory():
|
|
|
|
| 1 |
import os
|
| 2 |
+
from flask import Flask, render_template, request, jsonify, stream_with_context, Response
|
| 3 |
from memvid_sdk import create, open as open_memvid
|
| 4 |
from huggingface_hub import hf_hub_download, upload_file, HfApi
|
| 5 |
|
|
|
|
| 116 |
|
| 117 |
@app.route('/add', methods=['POST'])
|
| 118 |
def add_memory():
|
| 119 |
+
# 1. Setup Validation
|
| 120 |
global db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
content = request.form.get('content')
|
| 122 |
|
| 123 |
if not content:
|
| 124 |
return jsonify({"error": "No content provided"}), 400
|
| 125 |
|
| 126 |
+
# 2. Define the Stream Generator
|
| 127 |
+
def generate():
|
| 128 |
+
try:
|
| 129 |
+
# Step A: Re-init if needed inside the stream
|
| 130 |
+
global db
|
| 131 |
+
if not db:
|
| 132 |
+
init_db()
|
| 133 |
+
if not db:
|
| 134 |
+
yield '{"status": "error", "message": "Database init failed"}\n'
|
| 135 |
+
return
|
| 136 |
+
|
| 137 |
+
# Step B: Database Put
|
| 138 |
+
yield '{"status": "processing", "message": "Ingesting content..."}\n'
|
| 139 |
+
|
| 140 |
+
payload = {
|
| 141 |
+
"text": content,
|
| 142 |
+
"labels": ["web-entry"],
|
| 143 |
+
"title": "User Memory"
|
| 144 |
+
}
|
| 145 |
+
db.put(payload)
|
| 146 |
+
|
| 147 |
+
# Step C: Flush to Disk
|
| 148 |
+
yield '{"status": "processing", "message": "Flushing to disk..."}\n'
|
| 149 |
+
del db
|
| 150 |
+
db = None
|
| 151 |
+
|
| 152 |
+
# Step D: Sync
|
| 153 |
+
yield '{"status": "processing", "message": "Syncing to cloud (this may take a moment)..."}\n'
|
| 154 |
+
sync_to_hub()
|
| 155 |
+
|
| 156 |
+
# Step E: Reload
|
| 157 |
+
yield '{"status": "processing", "message": "Reloading index..."}\n'
|
| 158 |
+
init_db()
|
| 159 |
+
|
| 160 |
+
# Final Success Message
|
| 161 |
+
yield '{"status": "success", "message": "Memory added and synced."}\n'
|
| 162 |
|
| 163 |
+
except Exception as e:
|
| 164 |
+
# Capture any errors during the process
|
| 165 |
+
yield f'{{"status": "error", "message": "{str(e)}"}}\n'
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
# 3. Return the Stream
|
| 168 |
+
return Response(stream_with_context(generate()), mimetype='application/x-ndjson')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
@app.route('/search', methods=['POST'])
|
| 171 |
def search_memory():
|