broadfield-dev commited on
Commit
4a5b3e1
·
verified ·
1 Parent(s): 44a7ead

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -38
app.py CHANGED
@@ -1,30 +1,95 @@
1
  import os
2
  from flask import Flask, render_template, request, jsonify
3
- # Importing Memvid based on the SDK package name
4
- from memvid_sdk import Memvid
5
 
6
  app = Flask(__name__)
7
 
8
  # CONFIGURATION
9
- # We store the .mv2 file in a writable path.
10
- # On HF Spaces, /tmp is writable, or you can use the current directory
11
- # if you don't need persistence across reboots.
12
- DB_PATH = "knowledge.mv2"
13
-
14
- # Initialize Memvid
15
- # We use a global variable to hold the reference
16
- try:
17
- # Attempt to open or create.
18
- # Python SDK usually follows: Memvid(path) or Memvid.create(path)
19
- # Adjust this line based on exact SDK docs.
20
- if os.path.exists(DB_PATH):
21
- db = Memvid.open(DB_PATH)
22
- else:
23
- db = Memvid.create(DB_PATH)
24
- print(f"Memvid loaded at {DB_PATH}")
25
- except Exception as e:
26
- print(f"Error initializing Memvid: {e}")
27
- db = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  @app.route('/')
30
  def index():
@@ -32,25 +97,34 @@ def index():
32
 
33
  @app.route('/add', methods=['POST'])
34
  def add_memory():
 
35
  if not db:
36
- return jsonify({"error": "Database not initialized"}), 500
 
 
 
37
 
38
  content = request.form.get('content')
39
- tags = request.form.get('tags', '')
40
-
41
  if not content:
42
  return jsonify({"error": "No content provided"}), 400
43
 
44
  try:
45
- # Assuming the Python SDK has a .put() or .add() method
46
- # and accepts string content.
47
- # We simulate "PutOptions" via kwargs or a dict.
48
- db.put(content, tags=tags)
49
 
50
- # Depending on the SDK, you might need an explicit commit
51
- # db.commit()
 
 
 
 
 
52
 
53
- return jsonify({"success": True, "message": "Memory added successfully."})
54
  except Exception as e:
55
  return jsonify({"error": str(e)}), 500
56
 
@@ -60,21 +134,19 @@ def search_memory():
60
  return jsonify({"error": "Database not initialized"}), 500
61
 
62
  query = request.form.get('query')
63
-
64
  if not query:
65
  return jsonify({"error": "No query provided"}), 400
66
 
67
  try:
68
- # Perform search. Assuming .search returns a list of result objects
69
  results = db.search(query, top_k=5)
70
 
71
- # Format results for JSON
72
- # Assuming results have .text and .score attributes
73
  formatted_results = []
74
  for hit in results:
75
  formatted_results.append({
76
- "text": hit.text,
77
- # "score": hit.score # specific attributes depend on SDK
78
  })
79
 
80
  return jsonify({"success": True, "results": formatted_results})
@@ -82,5 +154,4 @@ def search_memory():
82
  return jsonify({"error": str(e)}), 500
83
 
84
  if __name__ == '__main__':
85
- # Hugging Face Spaces runs on port 7860 by default
86
  app.run(host='0.0.0.0', port=7860)
 
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
  # CONFIGURATION
9
+ # Format: "username/dataset-name"
10
+ DATASET_REPO_ID = "YOUR_USERNAME/memvid-storage"
11
+ FILENAME = "knowledge.mv2"
12
+ HF_TOKEN = os.environ.get("HF_TOKEN")
13
+
14
+ # Global DB reference
15
+ db = None
16
+ DB_PATH = os.path.abspath(FILENAME)
17
+
18
+ def init_db():
19
+ """
20
+ 1. Try to download existing DB from HF Hub.
21
+ 2. If not found, create a new one locally.
22
+ 3. Load Memvid.
23
+ """
24
+ global db
25
+
26
+ # 1. Try to sync from Hub
27
+ if HF_TOKEN:
28
+ print(f"🔄 Attempting to download {FILENAME} from {DATASET_REPO_ID}...")
29
+ try:
30
+ # This downloads the file to the local cache and returns the path
31
+ # We copy it or link it to our working dir if needed, but usually
32
+ # we just want it in the current directory for Memvid to write to.
33
+
34
+ # Check if file exists in repo first
35
+ api = HfApi(token=HF_TOKEN
36
+ username = api.whoami()
37
+ print(username)
38
+ print(username.name)
39
+ files = api.list_repo_files(repo_id=DATASET_REPO_ID, repo_type="dataset")
40
+
41
+ if FILENAME in files:
42
+ downloaded_path = hf_hub_download(
43
+ repo_id=DATASET_REPO_ID,
44
+ filename=FILENAME,
45
+ repo_type="dataset",
46
+ token=HF_TOKEN,
47
+ local_dir=".", # Download to current directory
48
+ local_dir_use_symlinks=False # We need the actual file to write to it
49
+ )
50
+ print(f"✅ Downloaded database to {downloaded_path}")
51
+ else:
52
+ print("⚠️ Database file not found in repo. Creating new one.")
53
+ except Exception as e:
54
+ print(f"⚠️ Could not download from Hub (might be first run): {e}")
55
+
56
+ # 2. Open or Create Memvid
57
+ try:
58
+ # Memvid 2.0 pattern:
59
+ # If file exists, open it. If not, create it.
60
+ if os.path.exists(DB_PATH):
61
+ db = Memvid.open(DB_PATH)
62
+ print(f"📂 Memvid opened at {DB_PATH}")
63
+ else:
64
+ db = Memvid.create(DB_PATH)
65
+ print(f"✨ New Memvid created at {DB_PATH}")
66
+
67
+ except Exception as e:
68
+ print(f"❌ CRITICAL ERROR initializing Memvid: {e}")
69
+ db = None
70
+
71
+ def sync_to_hub():
72
+ """Uploads the local .mv2 file back to Hugging Face"""
73
+ if not HF_TOKEN:
74
+ print("⚠️ No HF_TOKEN found. Skipping sync.")
75
+ return
76
+
77
+ try:
78
+ print("☁️ Syncing to Hub...")
79
+ upload_file(
80
+ path_or_fileobj=DB_PATH,
81
+ path_in_repo=FILENAME,
82
+ repo_id=DATASET_REPO_ID,
83
+ repo_type="dataset",
84
+ token=HF_TOKEN,
85
+ commit_message="Memvid: Auto-save memory update"
86
+ )
87
+ print("✅ Sync complete.")
88
+ except Exception as e:
89
+ print(f"❌ Sync failed: {e}")
90
+
91
+ # Initialize on startup
92
+ init_db()
93
 
94
  @app.route('/')
95
  def index():
 
97
 
98
  @app.route('/add', methods=['POST'])
99
  def add_memory():
100
+ global db
101
  if not db:
102
+ # Try to re-init if it failed before
103
+ init_db()
104
+ if not db:
105
+ return jsonify({"error": "Database could not be initialized. Check logs."}), 500
106
 
107
  content = request.form.get('content')
108
+ tags = request.form.get('tags', '') # Not used in basic put, but good for expansion
109
+
110
  if not content:
111
  return jsonify({"error": "No content provided"}), 400
112
 
113
  try:
114
+ # Add the memory
115
+ # Note: Check if your SDK version requires explicit transaction/commit
116
+ # Some versions use db.put(content), others db.add(content)
117
+ db.put(content)
118
 
119
+ # IMPORTANT: Force a commit/flush to disk before uploading
120
+ # If the SDK has a .commit() or .flush(), call it here.
121
+ if hasattr(db, 'commit'):
122
+ db.commit()
123
+
124
+ # Sync back to cloud
125
+ sync_to_hub()
126
 
127
+ return jsonify({"success": True, "message": "Memory added and synced to cloud."})
128
  except Exception as e:
129
  return jsonify({"error": str(e)}), 500
130
 
 
134
  return jsonify({"error": "Database not initialized"}), 500
135
 
136
  query = request.form.get('query')
 
137
  if not query:
138
  return jsonify({"error": "No query provided"}), 400
139
 
140
  try:
141
+ # Search
142
  results = db.search(query, top_k=5)
143
 
144
+ # Transform results based on SDK object structure
 
145
  formatted_results = []
146
  for hit in results:
147
  formatted_results.append({
148
+ "text": hit.text,
149
+ # "score": hit.score # Uncomment if available
150
  })
151
 
152
  return jsonify({"success": True, "results": formatted_results})
 
154
  return jsonify({"error": str(e)}), 500
155
 
156
  if __name__ == '__main__':
 
157
  app.run(host='0.0.0.0', port=7860)