Spaces:
Sleeping
Sleeping
Commit
·
b74ff81
1
Parent(s):
7024dee
Refactor Milvus collection management: streamline error handling during collection drops in cleanup and startup processes, ensuring defensive programming practices. Remove redundant drop logic from cleanup function.
Browse files
app.py
CHANGED
|
@@ -38,13 +38,6 @@ def cleanup_documents():
|
|
| 38 |
for f in files:
|
| 39 |
if os.path.isfile(f):
|
| 40 |
os.remove(f)
|
| 41 |
-
# Also drop the Milvus collection to avoid stale state between restarts
|
| 42 |
-
try:
|
| 43 |
-
if milvus_client and milvus_client.has_collection(COLLECTION_NAME):
|
| 44 |
-
milvus_client.drop_collection(COLLECTION_NAME)
|
| 45 |
-
print(f"Dropped collection {COLLECTION_NAME} during cleanup.")
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"Error dropping collection during cleanup: {e}")
|
| 48 |
print("Cleanup complete.")
|
| 49 |
|
| 50 |
|
|
@@ -58,13 +51,16 @@ def reset_collection_if_no_docs():
|
|
| 58 |
os.makedirs(DOCS_DIR, exist_ok=True)
|
| 59 |
files = glob.glob(os.path.join(DOCS_DIR, "*"))
|
| 60 |
has_docs = any(os.path.isfile(f) for f in files)
|
| 61 |
-
if
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Error resetting collection on startup: {e}")
|
| 70 |
|
|
@@ -72,9 +68,13 @@ def reset_collection_if_no_docs():
|
|
| 72 |
def reset_index():
|
| 73 |
"""Reset the index."""
|
| 74 |
try:
|
| 75 |
-
if milvus_client
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error dropping collection during cleanup: {e}")
|
| 80 |
|
|
@@ -146,10 +146,7 @@ with gr.Blocks() as demo:
|
|
| 146 |
inputs=[file_input],
|
| 147 |
outputs=[index_status],
|
| 148 |
)
|
| 149 |
-
reset_index_button.click(
|
| 150 |
-
fn=reset_index,
|
| 151 |
-
inputs=[]
|
| 152 |
-
)
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
# Ensure the documents directory exists from the start
|
|
|
|
| 38 |
for f in files:
|
| 39 |
if os.path.isfile(f):
|
| 40 |
os.remove(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
print("Cleanup complete.")
|
| 42 |
|
| 43 |
|
|
|
|
| 51 |
os.makedirs(DOCS_DIR, exist_ok=True)
|
| 52 |
files = glob.glob(os.path.join(DOCS_DIR, "*"))
|
| 53 |
has_docs = any(os.path.isfile(f) for f in files)
|
| 54 |
+
if not has_docs and milvus_client:
|
| 55 |
+
# Avoid blocking on exit; wrap Milvus operations defensively
|
| 56 |
+
try:
|
| 57 |
+
if milvus_client.has_collection(COLLECTION_NAME):
|
| 58 |
+
milvus_client.drop_collection(COLLECTION_NAME)
|
| 59 |
+
print(
|
| 60 |
+
f"No documents found. Dropped existing collection {COLLECTION_NAME}."
|
| 61 |
+
)
|
| 62 |
+
except Exception as inner_e:
|
| 63 |
+
print(f"Skip dropping collection on startup due to error: {inner_e}")
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Error resetting collection on startup: {e}")
|
| 66 |
|
|
|
|
| 68 |
def reset_index():
|
| 69 |
"""Reset the index."""
|
| 70 |
try:
|
| 71 |
+
if milvus_client:
|
| 72 |
+
try:
|
| 73 |
+
if milvus_client.has_collection(COLLECTION_NAME):
|
| 74 |
+
milvus_client.drop_collection(COLLECTION_NAME)
|
| 75 |
+
print(f"Dropped collection {COLLECTION_NAME}.")
|
| 76 |
+
except Exception as inner_e:
|
| 77 |
+
print(f"Skip dropping collection due to error: {inner_e}")
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error dropping collection during cleanup: {e}")
|
| 80 |
|
|
|
|
| 146 |
inputs=[file_input],
|
| 147 |
outputs=[index_status],
|
| 148 |
)
|
| 149 |
+
reset_index_button.click(fn=reset_index, inputs=[])
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
# Ensure the documents directory exists from the start
|