Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|