-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.py
More file actions
179 lines (146 loc) · 5.37 KB
/
Copy pathcleanup.py
File metadata and controls
179 lines (146 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import pickle
import shutil
from datetime import datetime
from datetime import datetime
from config import (
USER_DOCUMENTS_DIR,
METADATA_FILE,
CHUNK_METADATA_FILE,
EMBEDDING_METADATA_FILE,
FAISS_INDEX_FILE
)
from utils import (
load_json,
save_json,
delete_session,
get_uploaded_documents,
delete_document
)
import faiss
import numpy as np
def are_documents_expired(session_id):
""" Checks whether any uploaded document has expired.
If expired, deletes the document metadata and returns True.
"""
documents = get_uploaded_documents(session_id)
if len(documents) == 0:
return False
expired_found = False
current_time = datetime.now()
for document in documents:
expiry_time = datetime.strptime(
document["expires_at"],
"%Y-%m-%d %H:%M:%S"
)
if current_time > expiry_time:
delete_document(
session_id=session_id,
document_id=document["id"]
)
expired_found = True
return expired_found
# =====================================================
# DELETE FAISS INDEX
# =====================================================
def delete_faiss_index(session_path):
index_path = os.path.join(session_path, FAISS_INDEX_FILE)
if os.path.exists(index_path):
os.remove(index_path)
# =====================================================
# REBUILD FAISS INDEX
# =====================================================
def rebuild_faiss_index(session_path, embeddings):
if len(embeddings) == 0:
delete_faiss_index(session_path)
return
embeddings = np.array(embeddings, dtype=np.float32)
embedding_dimension = (embeddings.shape[1])
index = faiss.IndexFlatL2(embedding_dimension)
index.add(embeddings)
faiss.write_index(index, os.path.join(session_path, FAISS_INDEX_FILE))
# =====================================================
# REMOVE EXPIRED DOCUMENTS
# =====================================================
def remove_expired_documents(session_path):
metadata_path = os.path.join(session_path, METADATA_FILE)
if not os.path.exists(metadata_path):
shutil.rmtree(session_path)
return
metadata = load_json(metadata_path)
documents = metadata.get("documents", [])
if not documents:
shutil.rmtree(session_path)
return
valid_documents = []
expired_document_ids = []
current_time = (datetime.now())
# -----------------------------------------
# CHECK EXPIRY
# -----------------------------------------
for document in documents:
expiry_time = datetime.strptime(document["expires_at"], "%Y-%m-%d %H:%M:%S")
if current_time > expiry_time:
expired_document_ids.append(document["id"])
else:
valid_documents.append(document)
# No expired documents.
if len(expired_document_ids) == 0:
return
# -----------------------------------------
# DELETE ENTIRE SESSION
# -----------------------------------------
if len(valid_documents) == 0:
session_id = os.path.basename(session_path)
delete_session(session_id)
return
# -----------------------------------------
# UPDATE DOCUMENT METADATA
# -----------------------------------------
metadata["documents"] = (valid_documents)
save_json(metadata, metadata_path)
# -----------------------------------------
# FILTER CHUNK METADATA
# -----------------------------------------
chunk_file = os.path.join(session_path, CHUNK_METADATA_FILE
)
if os.path.exists(chunk_file):
with open(chunk_file, "rb") as file:
chunks = pickle.load(file)
else:
chunks = []
updated_chunks = [
chunk for chunk in chunks if chunk["document_id"] not in expired_document_ids
]
with open(chunk_file, "wb") as file:
pickle.dump(updated_chunks, file)
# -----------------------------------------
# FILTER EMBEDDING METADATA
# -----------------------------------------
embedding_file = os.path.join(session_path, EMBEDDING_METADATA_FILE)
if os.path.exists(embedding_file):
with open(embedding_file, "rb") as file:
embeddings = pickle.load(file)
else:
embeddings = []
updated_embeddings = [
embedding for embedding in embeddings if embedding["document_id"] not in expired_document_ids
]
with open(embedding_file, "wb") as file:
pickle.dump(updated_embeddings, file)
# -----------------------------------------
# REBUILD FAISS INDEX
# -----------------------------------------
vectors = [item["embedding_vector"] for item in updated_embeddings]
rebuild_faiss_index(session_path, vectors)
# =====================================================
# CLEANUP ALL SESSIONS
# =====================================================
def cleanup_expired_documents():
if not os.path.exists(USER_DOCUMENTS_DIR):
return
sessions = os.listdir(USER_DOCUMENTS_DIR)
for session_id in sessions:
session_path = os.path.join(USER_DOCUMENTS_DIR, session_id)
if os.path.isdir(session_path):
remove_expired_documents(session_path)