broadfield-dev commited on
Commit
9b45948
·
verified ·
1 Parent(s): b9432fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from flask import Flask, render_template, request, jsonify
3
+ # Importing Memvid based on the SDK package name
4
+ from memvid 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():
31
+ return render_template('index.html')
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
+
57
+ @app.route('/search', methods=['POST'])
58
+ def search_memory():
59
+ if not db:
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})
81
+ except Exception as e:
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)