broadfield-dev commited on
Commit
dae8d04
·
verified ·
1 Parent(s): f65bd99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -35
app.py CHANGED
@@ -1,19 +1,24 @@
1
  import os
2
  from flask import Flask, render_template, request, jsonify
3
- from memvid_sdk import Memvid
 
 
4
  from huggingface_hub import hf_hub_download, upload_file, HfApi
5
 
6
  app = Flask(__name__)
7
 
 
8
  FILENAME = "knowledge.mv2"
9
  HF_TOKEN = os.environ.get("HF_TOKEN")
10
- DATASET_NAME = "memvid-storage"
11
 
 
12
  db = None
13
  DB_PATH = os.path.abspath(FILENAME)
14
- DATASET_REPO_ID = None
15
 
16
  def get_repo_id():
 
17
  global DATASET_REPO_ID
18
  if DATASET_REPO_ID:
19
  return DATASET_REPO_ID
@@ -24,24 +29,35 @@ def get_repo_id():
24
  username = api.whoami()['name']
25
  DATASET_REPO_ID = f"{username}/{DATASET_NAME}"
26
  return DATASET_REPO_ID
27
- except Exception:
 
28
  return None
29
  return None
30
 
31
  def init_db():
 
 
 
 
 
32
  global db, DATASET_REPO_ID
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:
 
40
  api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
 
 
41
  files = api.list_repo_files(repo_id=repo_id, repo_type="dataset")
42
 
43
  if FILENAME in files:
44
- hf_hub_download(
45
  repo_id=repo_id,
46
  filename=FILENAME,
47
  repo_type="dataset",
@@ -49,34 +65,42 @@ def init_db():
49
  local_dir=".",
50
  local_dir_use_symlinks=False
51
  )
 
 
 
 
52
  except Exception as e:
53
- print(f"Cloud sync error: {e}")
54
 
55
- # 2. Initialize Memvid - FIX: No parameters in constructor
56
  try:
57
- # Step 1: Initialize the handle without parameters
58
- db = Memvid()
59
-
60
- # Step 2: Open or create the database file
61
  if os.path.exists(DB_PATH):
62
- print(f"📂 File exists. Opening database at {DB_PATH}...")
63
- db.open(DB_PATH)
 
 
 
 
 
 
 
 
 
 
64
  else:
65
- print(f"✨ New file. Creating database at {DB_PATH}...")
66
  db.create(DB_PATH)
67
 
68
- print("✅ Database initialized.")
69
-
70
- except Exception as e:
71
- print(f"❌ Memvid init error: {e}")
72
- db = None
73
-
74
  def sync_to_hub():
 
75
  repo_id = get_repo_id()
 
76
  if not HF_TOKEN or not repo_id:
 
77
  return
78
 
79
  try:
 
80
  upload_file(
81
  path_or_fileobj=DB_PATH,
82
  path_in_repo=FILENAME,
@@ -85,10 +109,11 @@ def sync_to_hub():
85
  token=HF_TOKEN,
86
  commit_message="Memvid: Auto-save memory update"
87
  )
 
88
  except Exception as e:
89
- print(f"Sync failed: {e}")
90
 
91
- # Run init on start
92
  init_db()
93
 
94
  @app.route('/')
@@ -101,14 +126,14 @@ def add_memory():
101
  if not db:
102
  init_db()
103
  if not db:
104
- return jsonify({"error": "Database could not be initialized. Check Server Logs."}), 500
105
 
106
  content = request.form.get('content')
 
107
  if not content:
108
  return jsonify({"error": "No content provided"}), 400
109
 
110
  try:
111
- # Payload must be a dict
112
  payload = {
113
  "text": content,
114
  "title": "User Memory",
@@ -117,19 +142,14 @@ def add_memory():
117
 
118
  db.put(payload)
119
 
120
- # Force flush/close to ensure data is written before upload
121
  del db
122
  db = None
123
 
124
- # Sync to Hugging Face
125
  sync_to_hub()
126
-
127
- # Re-open the database for the next request
128
  init_db()
129
 
130
  return jsonify({"success": True, "message": "Memory added and synced to cloud."})
131
  except Exception as e:
132
- print(f"Add Error: {e}")
133
  return jsonify({"error": str(e)}), 500
134
 
135
  @app.route('/search', methods=['POST'])
@@ -142,16 +162,19 @@ def search_memory():
142
  return jsonify({"error": "No query provided"}), 400
143
 
144
  try:
145
- # Query must be a string
146
- results = db.find(query, top_k=5)
 
 
 
 
 
147
 
148
  formatted_results = []
149
  for hit in results:
150
- # Handle result object vs dict
151
  if isinstance(hit, dict):
152
  text = hit.get('text') or hit.get('content') or "No text"
153
  else:
154
- # Try common attribute names for the hit object
155
  text = getattr(hit, 'text', getattr(hit, 'content', str(hit)))
156
 
157
  formatted_results.append({
@@ -160,7 +183,6 @@ def search_memory():
160
 
161
  return jsonify({"success": True, "results": formatted_results})
162
  except Exception as e:
163
- print(f"Search Error: {e}")
164
  return jsonify({"error": str(e)}), 500
165
 
166
  if __name__ == '__main__':
 
1
  import os
2
  from flask import Flask, render_template, request, jsonify
3
+ #from memvid_sdk import Memvid
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__)
9
 
10
+ # CONFIGURATION
11
  FILENAME = "knowledge.mv2"
12
  HF_TOKEN = os.environ.get("HF_TOKEN")
13
+ DATASET_NAME = "memvid-storage" # Just the name, we will append username dynamically
14
 
15
+ # Global variables
16
  db = None
17
  DB_PATH = os.path.abspath(FILENAME)
18
+ DATASET_REPO_ID = None # Will be set during initialization
19
 
20
  def get_repo_id():
21
+ """Helper to dynamically resolve 'username/dataset_name'"""
22
  global DATASET_REPO_ID
23
  if DATASET_REPO_ID:
24
  return DATASET_REPO_ID
 
29
  username = api.whoami()['name']
30
  DATASET_REPO_ID = f"{username}/{DATASET_NAME}"
31
  return DATASET_REPO_ID
32
+ except Exception as e:
33
+ print(f"⚠️ Error getting username: {e}")
34
  return None
35
  return None
36
 
37
  def init_db():
38
+ """
39
+ 1. Ensure Dataset Exists.
40
+ 2. Try to download existing DB.
41
+ 3. Initialize Memvid.
42
+ """
43
  global db, DATASET_REPO_ID
44
 
45
  repo_id = get_repo_id()
46
 
47
+ # 1. Sync / Setup Cloud Storage
48
  if HF_TOKEN and repo_id:
49
+ print(f"🔄 Checking cloud storage at {repo_id}...")
50
  api = HfApi(token=HF_TOKEN)
51
+
52
  try:
53
+ # Create the repo if it doesn't exist (Fixes your 404 error)
54
  api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
55
+
56
+ # Check for file existence
57
  files = api.list_repo_files(repo_id=repo_id, repo_type="dataset")
58
 
59
  if FILENAME in files:
60
+ downloaded_path = hf_hub_download(
61
  repo_id=repo_id,
62
  filename=FILENAME,
63
  repo_type="dataset",
 
65
  local_dir=".",
66
  local_dir_use_symlinks=False
67
  )
68
+ print(f"✅ Downloaded database to {downloaded_path}")
69
+ else:
70
+ print("⚠️ Database file not found in repo. A new one will be created and synced.")
71
+
72
  except Exception as e:
73
+ print(f"⚠️ Cloud sync warning: {e}")
74
 
75
+ # 2. Initialize Memvid (Fixes 'no attribute create' error)
76
  try:
 
 
 
 
77
  if os.path.exists(DB_PATH):
78
+ db = open_memvid(DB_PATH) # Use the imported 'open' function
79
+ else:
80
+ db = create(DB_PATH) # Use the imported 'create' function
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:
91
+ db = Memvid()
92
  db.create(DB_PATH)
93
 
 
 
 
 
 
 
94
  def sync_to_hub():
95
+ """Uploads the local .mv2 file back to Hugging Face"""
96
  repo_id = get_repo_id()
97
+
98
  if not HF_TOKEN or not repo_id:
99
+ print("⚠️ No HF_TOKEN or Repo ID found. Skipping sync.")
100
  return
101
 
102
  try:
103
+ print("☁️ Syncing to Hub...")
104
  upload_file(
105
  path_or_fileobj=DB_PATH,
106
  path_in_repo=FILENAME,
 
109
  token=HF_TOKEN,
110
  commit_message="Memvid: Auto-save memory update"
111
  )
112
+ print("✅ Sync complete.")
113
  except Exception as e:
114
+ print(f"Sync failed: {e}")
115
 
116
+ # Initialize on startup
117
  init_db()
118
 
119
  @app.route('/')
 
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",
 
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'])
 
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({
 
183
 
184
  return jsonify({"success": True, "results": formatted_results})
185
  except Exception as e:
 
186
  return jsonify({"error": str(e)}), 500
187
 
188
  if __name__ == '__main__':