Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
-
#
|
| 4 |
from memvid_sdk import create, open as open_memvid
|
| 5 |
-
|
| 6 |
from huggingface_hub import hf_hub_download, upload_file, HfApi
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
|
@@ -10,12 +9,12 @@ app = Flask(__name__)
|
|
| 10 |
# CONFIGURATION
|
| 11 |
FILENAME = "knowledge.mv2"
|
| 12 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 13 |
-
DATASET_NAME = "memvid-storage"
|
| 14 |
|
| 15 |
# Global variables
|
| 16 |
db = None
|
| 17 |
DB_PATH = os.path.abspath(FILENAME)
|
| 18 |
-
DATASET_REPO_ID = None
|
| 19 |
|
| 20 |
def get_repo_id():
|
| 21 |
"""Helper to dynamically resolve 'username/dataset_name'"""
|
|
@@ -50,7 +49,7 @@ def init_db():
|
|
| 50 |
api = HfApi(token=HF_TOKEN)
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
# Create the repo if it doesn't exist
|
| 54 |
api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
|
| 55 |
|
| 56 |
# Check for file existence
|
|
@@ -72,19 +71,16 @@ def init_db():
|
|
| 72 |
except Exception as e:
|
| 73 |
print(f"⚠️ Cloud sync warning: {e}")
|
| 74 |
|
| 75 |
-
# 2. Initialize Memvid (
|
| 76 |
try:
|
| 77 |
if os.path.exists(DB_PATH):
|
| 78 |
-
db = open_memvid(DB_PATH, read_only=False)
|
| 79 |
else:
|
| 80 |
-
db = create(DB_PATH)
|
| 81 |
|
| 82 |
except ImportError:
|
| 83 |
-
# If the above imports fail, fall back to the class method style
|
| 84 |
-
# (Only do this if the library version is different than expected)
|
| 85 |
from memvid_sdk import Memvid
|
| 86 |
if os.path.exists(DB_PATH):
|
| 87 |
-
# Some bindings require an empty init first
|
| 88 |
db = Memvid()
|
| 89 |
db.open(DB_PATH)
|
| 90 |
else:
|
|
@@ -134,14 +130,11 @@ def add_memory():
|
|
| 134 |
return jsonify({"error": "No content provided"}), 400
|
| 135 |
|
| 136 |
try:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
"tag": "web-entry"
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
db.put(payload)
|
| 144 |
|
|
|
|
| 145 |
del db
|
| 146 |
db = None
|
| 147 |
|
|
@@ -150,6 +143,8 @@ def add_memory():
|
|
| 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'])
|
|
@@ -162,16 +157,13 @@ def search_memory():
|
|
| 162 |
return jsonify({"error": "No query provided"}), 400
|
| 163 |
|
| 164 |
try:
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 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:
|
|
|
|
| 1 |
import os
|
| 2 |
from flask import Flask, render_template, request, jsonify
|
| 3 |
+
# Keeping your working imports
|
| 4 |
from memvid_sdk import create, open as open_memvid
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download, upload_file, HfApi
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 9 |
# CONFIGURATION
|
| 10 |
FILENAME = "knowledge.mv2"
|
| 11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 12 |
+
DATASET_NAME = "memvid-storage"
|
| 13 |
|
| 14 |
# Global variables
|
| 15 |
db = None
|
| 16 |
DB_PATH = os.path.abspath(FILENAME)
|
| 17 |
+
DATASET_REPO_ID = None
|
| 18 |
|
| 19 |
def get_repo_id():
|
| 20 |
"""Helper to dynamically resolve 'username/dataset_name'"""
|
|
|
|
| 49 |
api = HfApi(token=HF_TOKEN)
|
| 50 |
|
| 51 |
try:
|
| 52 |
+
# Create the repo if it doesn't exist
|
| 53 |
api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
|
| 54 |
|
| 55 |
# Check for file existence
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
print(f"⚠️ Cloud sync warning: {e}")
|
| 73 |
|
| 74 |
+
# 2. Initialize Memvid (YOUR WORKING LOGIC)
|
| 75 |
try:
|
| 76 |
if os.path.exists(DB_PATH):
|
| 77 |
+
db = open_memvid(DB_PATH, read_only=False)
|
| 78 |
else:
|
| 79 |
+
db = create(DB_PATH)
|
| 80 |
|
| 81 |
except ImportError:
|
|
|
|
|
|
|
| 82 |
from memvid_sdk import Memvid
|
| 83 |
if os.path.exists(DB_PATH):
|
|
|
|
| 84 |
db = Memvid()
|
| 85 |
db.open(DB_PATH)
|
| 86 |
else:
|
|
|
|
| 130 |
return jsonify({"error": "No content provided"}), 400
|
| 131 |
|
| 132 |
try:
|
| 133 |
+
# FIX: Pass arguments as labels (Keyword Arguments), NOT as a dict object.
|
| 134 |
+
# "put requires labels" means db.put(text=...) not db.put({"text":...})
|
| 135 |
+
db.put(text=content, title="User Memory", tag="web-entry")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
# Force flush by deleting reference (Rust Drop trait)
|
| 138 |
del db
|
| 139 |
db = None
|
| 140 |
|
|
|
|
| 143 |
|
| 144 |
return jsonify({"success": True, "message": "Memory added and synced to cloud."})
|
| 145 |
except Exception as e:
|
| 146 |
+
# Re-init db if something crashed so next request works
|
| 147 |
+
if db is None: init_db()
|
| 148 |
return jsonify({"error": str(e)}), 500
|
| 149 |
|
| 150 |
@app.route('/search', methods=['POST'])
|
|
|
|
| 157 |
return jsonify({"error": "No query provided"}), 400
|
| 158 |
|
| 159 |
try:
|
| 160 |
+
# FIX: Pass query as positional string, options as kwargs.
|
| 161 |
+
# Previous error: "dict cannot be converted to PyString" -> means first arg must be string.
|
| 162 |
+
results = db.find(query, top_k=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
formatted_results = []
|
| 165 |
for hit in results:
|
| 166 |
+
# Handle result object vs dict safety
|
| 167 |
if isinstance(hit, dict):
|
| 168 |
text = hit.get('text') or hit.get('content') or "No text"
|
| 169 |
else:
|