cryogenic22 commited on
Commit
4339a4c
·
verified ·
1 Parent(s): 0c149f7

Update utils/vector_store.py

Browse files
Files changed (1) hide show
  1. utils/vector_store.py +19 -4
utils/vector_store.py CHANGED
@@ -1,3 +1,4 @@
 
1
  # utils/vector_store.py
2
  import faiss
3
  import numpy as np
@@ -7,18 +8,28 @@ import os
7
  from pathlib import Path
8
 
9
  class VectorStore:
10
- def __init__(self, persist_directory: str = "./data/faiss"):
11
- self.persist_directory = persist_directory
 
12
  self.index = None
13
  self.documents = []
14
  self.metadata = []
15
 
16
- # Create directory if it doesn't exist
17
- Path(persist_directory).mkdir(parents=True, exist_ok=True)
18
 
19
  # Try to load existing index and data
20
  self._load_or_create_index()
21
 
 
 
 
 
 
 
 
 
 
22
  def _load_or_create_index(self):
23
  """Load existing index or create new one"""
24
  index_path = os.path.join(self.persist_directory, "faiss.index")
@@ -26,6 +37,7 @@ class VectorStore:
26
 
27
  try:
28
  if os.path.exists(index_path) and os.path.exists(data_path):
 
29
  # Load existing index
30
  self.index = faiss.read_index(index_path)
31
 
@@ -34,7 +46,9 @@ class VectorStore:
34
  data = pickle.load(f)
35
  self.documents = data['documents']
36
  self.metadata = data['metadata']
 
37
  else:
 
38
  # Create new index
39
  self.index = None # Will be created when first vectors are added
40
  self.documents = []
@@ -45,6 +59,7 @@ class VectorStore:
45
  self.documents = []
46
  self.metadata = []
47
 
 
48
  def _save_index(self):
49
  """Save index and data to disk"""
50
  if self.index is not None:
 
1
+
2
  # utils/vector_store.py
3
  import faiss
4
  import numpy as np
 
8
  from pathlib import Path
9
 
10
  class VectorStore:
11
+ def __init__(self):
12
+ # Use absolute path for HF Spaces
13
+ self.persist_directory = "/data/faiss"
14
  self.index = None
15
  self.documents = []
16
  self.metadata = []
17
 
18
+ # Ensure directories exist
19
+ self._create_data_directories()
20
 
21
  # Try to load existing index and data
22
  self._load_or_create_index()
23
 
24
+ def _create_data_directories(self):
25
+ """Create necessary data directories"""
26
+ # Create main data directory
27
+ Path("/data").mkdir(parents=True, exist_ok=True)
28
+ # Create FAISS specific directory
29
+ Path(self.persist_directory).mkdir(parents=True, exist_ok=True)
30
+ # Create uploads directory
31
+ Path("/data/uploads").mkdir(parents=True, exist_ok=True)
32
+
33
  def _load_or_create_index(self):
34
  """Load existing index or create new one"""
35
  index_path = os.path.join(self.persist_directory, "faiss.index")
 
37
 
38
  try:
39
  if os.path.exists(index_path) and os.path.exists(data_path):
40
+ print(f"Loading existing index from {index_path}")
41
  # Load existing index
42
  self.index = faiss.read_index(index_path)
43
 
 
46
  data = pickle.load(f)
47
  self.documents = data['documents']
48
  self.metadata = data['metadata']
49
+ print(f"Loaded {len(self.documents)} documents from existing index")
50
  else:
51
+ print("No existing index found, creating new one")
52
  # Create new index
53
  self.index = None # Will be created when first vectors are added
54
  self.documents = []
 
59
  self.documents = []
60
  self.metadata = []
61
 
62
+
63
  def _save_index(self):
64
  """Save index and data to disk"""
65
  if self.index is not None: