Spaces:
Sleeping
Sleeping
Commit
·
455095d
1
Parent(s):
7036bcd
Working
Browse files
api.py
CHANGED
@@ -97,8 +97,21 @@ async def upload(files: List[UploadFile] = File(...)):
|
|
97 |
|
98 |
# Explicitly clear memory before processing new files
|
99 |
logger.info("Clearing previous vector store from memory...")
|
|
|
100 |
store["value"] = None
|
|
|
|
|
101 |
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
logger.info("Memory cleared.")
|
103 |
|
104 |
logger.info("Starting document processing...")
|
@@ -240,3 +253,30 @@ async def status():
|
|
240 |
logger.info(f"Status response: {status_info}")
|
241 |
|
242 |
return status_info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# Explicitly clear memory before processing new files
|
99 |
logger.info("Clearing previous vector store from memory...")
|
100 |
+
old_store_had_value = store.get("value") is not None
|
101 |
store["value"] = None
|
102 |
+
|
103 |
+
# Force garbage collection
|
104 |
gc.collect()
|
105 |
+
|
106 |
+
# More aggressive memory cleanup if needed
|
107 |
+
if old_store_had_value:
|
108 |
+
try:
|
109 |
+
if hasattr(gc, 'collect'):
|
110 |
+
for i in range(3): # Run multiple collection cycles
|
111 |
+
gc.collect(i)
|
112 |
+
except Exception as e:
|
113 |
+
logger.warning(f"Error during aggressive garbage collection: {e}")
|
114 |
+
|
115 |
logger.info("Memory cleared.")
|
116 |
|
117 |
logger.info("Starting document processing...")
|
|
|
253 |
logger.info(f"Status response: {status_info}")
|
254 |
|
255 |
return status_info
|
256 |
+
|
257 |
+
@app.post("/clear")
|
258 |
+
async def clear_context():
|
259 |
+
"""Clear the current document context and free up memory."""
|
260 |
+
global store
|
261 |
+
logger.info("Clearing document context...")
|
262 |
+
|
263 |
+
# Clear the store
|
264 |
+
if store.get("value") is not None:
|
265 |
+
store["value"] = None
|
266 |
+
|
267 |
+
# Force garbage collection
|
268 |
+
gc.collect()
|
269 |
+
|
270 |
+
# Run a more aggressive memory cleanup
|
271 |
+
try:
|
272 |
+
if hasattr(gc, 'collect'):
|
273 |
+
for i in range(3): # Run multiple collection cycles
|
274 |
+
gc.collect(i)
|
275 |
+
except Exception as e:
|
276 |
+
logger.warning(f"Error during aggressive garbage collection: {e}")
|
277 |
+
|
278 |
+
logger.info("Document context cleared successfully.")
|
279 |
+
return {"status": "success", "message": "Document context cleared successfully."}
|
280 |
+
else:
|
281 |
+
logger.info("No document context to clear.")
|
282 |
+
return {"status": "info", "message": "No document context was loaded."}
|