YALCINKAYA commited on
Commit
db44cd2
·
verified ·
1 Parent(s): 67d7b50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -51,20 +51,32 @@ except PermissionError as e:
51
  document_store = {}
52
 
53
  # Check if FAISS index file exists, otherwise initialize it
54
- if os.path.exists(faiss_index_file):
55
- try:
56
- index = faiss.read_index(faiss_index_file)
57
- if index.ntotal > 0:
58
- print(f"FAISS index loaded with {index.ntotal} vectors.")
59
- index.reset() # Resetting the index if non-zero entries
60
- index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Reinitialize the index
61
- else:
62
- index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Initialize with flat L2 distance
63
- except Exception as e:
64
- print(f"Error loading FAISS index: {e}, reinitializing.")
65
- index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Initialize if reading fails
66
- else:
67
- index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Initialize if file doesn't exist
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  # Function to upload document
70
  def upload_document(file_path, embed_model):
 
51
  document_store = {}
52
 
53
  # Check if FAISS index file exists, otherwise initialize it
54
+ def initialize_faiss():
55
+ if os.path.exists(faiss_index_file):
56
+ try:
57
+ index = faiss.read_index(faiss_index_file)
58
+ if index.ntotal > 0:
59
+ print(f"FAISS index loaded with {index.ntotal} vectors.")
60
+ index.reset() # Resetting the index if non-zero entries
61
+ index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Reinitialize the index
62
+ else:
63
+ index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Initialize with flat L2 distance
64
+ except Exception as e:
65
+ print(f"Error loading FAISS index: {e}, reinitializing.")
66
+ index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Initialize if reading fails
67
+ else:
68
+ index = faiss.IndexIDMap(faiss.IndexFlatL2(384)) # Initialize if file doesn't exist
69
+
70
+ # Check if GPU is available and move index to GPU if possible
71
+ if torch.cuda.is_available():
72
+ print("CUDA is available, moving FAISS index to GPU.")
73
+ index = faiss.index_cpu_to_all_gpus(index)
74
+ else:
75
+ print("CUDA not available, using CPU.")
76
+
77
+ return index
78
+
79
+ index = initialize_faiss()
80
 
81
  # Function to upload document
82
  def upload_document(file_path, embed_model):