Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -202,6 +202,72 @@ def retrieve_ids_value(conn, name):
|
|
202 |
rows = cursor.fetchall()
|
203 |
return [bytes_to_int(row[0]) for row in rows]
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
@st.cache_resource(show_spinner=False)
|
206 |
def load_models_and_documents():
|
207 |
container = st.empty()
|
@@ -514,7 +580,7 @@ if __name__ == '__main__':
|
|
514 |
st.session_state.df = pd.DataFrame()
|
515 |
os.mkdir(data_editor_path)
|
516 |
|
517 |
-
def on_change_data_editor(conn, client, collection_name):
|
518 |
print(f'Check : {st.session_state.key_data_editor}')
|
519 |
|
520 |
if st.session_state.key_data_editor['deleted'] and (st.session_state.key_data_editor['deleted'][-1] in st.session_state.df):
|
@@ -522,12 +588,13 @@ if __name__ == '__main__':
|
|
522 |
name = st.session_state.df.loc[st.session_state.key_data_editor['deleted'][-1], 'document']
|
523 |
print(f'{name}')
|
524 |
os.remove(os.path.join(embeddings_path, name + '_ids.npy'))
|
525 |
-
ids_values = retrieve_ids_value(conn, name)
|
526 |
|
527 |
client.delete(
|
528 |
collection_name=collection_name,
|
529 |
points_selector=PointIdsList(points=ids_values)
|
530 |
)
|
|
|
531 |
|
532 |
|
533 |
if menu_id == 'Documents':
|
@@ -537,7 +604,7 @@ if __name__ == '__main__':
|
|
537 |
use_container_width=True,
|
538 |
hide_index=True,
|
539 |
on_change=on_change_data_editor,
|
540 |
-
args=(conn, client, collection_name),
|
541 |
key='key_data_editor',
|
542 |
column_config={
|
543 |
'icon': st.column_config.ImageColumn(
|
|
|
202 |
rows = cursor.fetchall()
|
203 |
return [bytes_to_int(row[0]) for row in rows]
|
204 |
|
205 |
+
|
206 |
+
To delete the ids_value entries from table_ids alongside their associated key (i.e., the document name) from table_names, you'll need to perform the following steps:
|
207 |
+
|
208 |
+
Delete the associated entries from table_ids first.
|
209 |
+
Then delete the entry from table_names.
|
210 |
+
Here’s how you can structure your code to achieve this:
|
211 |
+
|
212 |
+
Updated Functions
|
213 |
+
Function to Delete Entries
|
214 |
+
python
|
215 |
+
Copy code
|
216 |
+
def delete_document(conn, cursor, name):
|
217 |
+
# Begin a transaction
|
218 |
+
conn.execute('BEGIN')
|
219 |
+
|
220 |
+
try:
|
221 |
+
# First, delete related entries from table_ids
|
222 |
+
cursor.execute('DELETE FROM table_ids WHERE name = ?', (name,))
|
223 |
+
|
224 |
+
# Then, delete the document name from table_names
|
225 |
+
cursor.execute('DELETE FROM table_names WHERE doc_name = ?', (name,))
|
226 |
+
|
227 |
+
# Commit the transaction
|
228 |
+
conn.commit()
|
229 |
+
print(f"Deleted document '{name}' and its associated IDs.")
|
230 |
+
except sqlite3.Error as e:
|
231 |
+
# Rollback the transaction on error
|
232 |
+
conn.rollback()
|
233 |
+
print(f"An error occurred: {e}")
|
234 |
+
Example Usage
|
235 |
+
Here’s a complete example demonstrating how to use the delete_document function:
|
236 |
+
|
237 |
+
python
|
238 |
+
Copy code
|
239 |
+
import sqlite3
|
240 |
+
|
241 |
+
def int_to_bytes(value):
|
242 |
+
return value.to_bytes(4, byteorder='big')
|
243 |
+
|
244 |
+
def bytes_to_int(b):
|
245 |
+
return int.from_bytes(b, byteorder='big')
|
246 |
+
|
247 |
+
def insert_data(conn, cursor, name, ids_array):
|
248 |
+
cursor.execute('INSERT INTO table_names (doc_name) VALUES (?)', (name,))
|
249 |
+
for ids in ids_array:
|
250 |
+
cursor.execute('INSERT INTO table_ids (name, ids_value) VALUES (?, ?)', (name, int_to_bytes(ids)))
|
251 |
+
conn.commit()
|
252 |
+
|
253 |
+
def retrieve_ids_value(conn, cursor, name):
|
254 |
+
cursor.execute('SELECT ids_value FROM table_ids WHERE name = ?', (name,))
|
255 |
+
rows = cursor.fetchall()
|
256 |
+
return [bytes_to_int(row[0]) for row in rows]
|
257 |
+
|
258 |
+
def delete_document(conn, cursor, name):
|
259 |
+
conn.execute('BEGIN')
|
260 |
+
|
261 |
+
try:
|
262 |
+
cursor.execute('DELETE FROM table_ids WHERE name = ?', (name,))
|
263 |
+
cursor.execute('DELETE FROM table_names WHERE doc_name = ?', (name,))
|
264 |
+
|
265 |
+
conn.commit()
|
266 |
+
print(f"Deleted document '{name}' and its associated IDs.")
|
267 |
+
except sqlite3.Error as e:
|
268 |
+
conn.rollback()
|
269 |
+
print(f"An error occurred: {e}")
|
270 |
+
|
271 |
@st.cache_resource(show_spinner=False)
|
272 |
def load_models_and_documents():
|
273 |
container = st.empty()
|
|
|
580 |
st.session_state.df = pd.DataFrame()
|
581 |
os.mkdir(data_editor_path)
|
582 |
|
583 |
+
def on_change_data_editor(conn, cursor, client, collection_name):
|
584 |
print(f'Check : {st.session_state.key_data_editor}')
|
585 |
|
586 |
if st.session_state.key_data_editor['deleted'] and (st.session_state.key_data_editor['deleted'][-1] in st.session_state.df):
|
|
|
588 |
name = st.session_state.df.loc[st.session_state.key_data_editor['deleted'][-1], 'document']
|
589 |
print(f'{name}')
|
590 |
os.remove(os.path.join(embeddings_path, name + '_ids.npy'))
|
591 |
+
ids_values = retrieve_ids_value(conn, cursor, name)
|
592 |
|
593 |
client.delete(
|
594 |
collection_name=collection_name,
|
595 |
points_selector=PointIdsList(points=ids_values)
|
596 |
)
|
597 |
+
delete_document(conn, cursor, name)
|
598 |
|
599 |
|
600 |
if menu_id == 'Documents':
|
|
|
604 |
use_container_width=True,
|
605 |
hide_index=True,
|
606 |
on_change=on_change_data_editor,
|
607 |
+
args=(conn, cursor, client, collection_name),
|
608 |
key='key_data_editor',
|
609 |
column_config={
|
610 |
'icon': st.column_config.ImageColumn(
|