Spaces:
Sleeping
Sleeping
tyrwh
commited on
Commit
·
7c12ea7
1
Parent(s):
cd7bce8
Adding cleanup logic to startup routine
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
import traceback
|
| 6 |
import sys
|
|
|
|
| 7 |
import io
|
| 8 |
import zipfile
|
| 9 |
import cv2
|
|
@@ -58,22 +57,6 @@ def index():
|
|
| 58 |
|
| 59 |
# Load model once at startup, use CUDA if available
|
| 60 |
MODEL_DEVICE = 'cuda' if cuda.is_available() else 'cpu'
|
| 61 |
-
_model = None
|
| 62 |
-
def get_model():
|
| 63 |
-
global _model
|
| 64 |
-
if _model is None:
|
| 65 |
-
_model = YOLO(WEIGHTS_FILE)
|
| 66 |
-
if MODEL_DEVICE == 'cuda':
|
| 67 |
-
_model.to('cuda')
|
| 68 |
-
return _model
|
| 69 |
-
|
| 70 |
-
def cleanup_session(session_id):
|
| 71 |
-
# Remove files for this session
|
| 72 |
-
upload_dir = Path(app.config['UPLOAD_FOLDER']) / session_id
|
| 73 |
-
results_dir = Path(app.config['RESULTS_FOLDER']) / session_id
|
| 74 |
-
for d in [upload_dir, results_dir]:
|
| 75 |
-
if d.exists():
|
| 76 |
-
shutil.rmtree(d)
|
| 77 |
|
| 78 |
# save the uploaded files
|
| 79 |
@app.route('/uploads', methods=['POST'])
|
|
@@ -165,7 +148,6 @@ def start_processing():
|
|
| 165 |
"sessionId": session_id
|
| 166 |
}
|
| 167 |
session['job_state'] = job_state
|
| 168 |
-
filename_map = session.get('filename_map', {})
|
| 169 |
upload_dir = Path(app.config['UPLOAD_FOLDER']) / session_id
|
| 170 |
results_dir = Path(app.config['RESULTS_FOLDER']) / session_id
|
| 171 |
# clean out old results if needed
|
|
@@ -446,18 +428,20 @@ def print_startup_info():
|
|
| 446 |
print(f"Could not get NemaQuant script details: {e}")
|
| 447 |
|
| 448 |
@app.before_request
|
| 449 |
-
def
|
|
|
|
| 450 |
if 'id' not in session:
|
| 451 |
session['id'] = str(uuid.uuid4())
|
| 452 |
-
|
| 453 |
-
#
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
|
|
|
| 461 |
|
| 462 |
if __name__ == '__main__':
|
| 463 |
print_startup_info()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
import traceback
|
| 4 |
import sys
|
| 5 |
+
import time
|
| 6 |
import io
|
| 7 |
import zipfile
|
| 8 |
import cv2
|
|
|
|
| 57 |
|
| 58 |
# Load model once at startup, use CUDA if available
|
| 59 |
MODEL_DEVICE = 'cuda' if cuda.is_available() else 'cpu'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# save the uploaded files
|
| 62 |
@app.route('/uploads', methods=['POST'])
|
|
|
|
| 148 |
"sessionId": session_id
|
| 149 |
}
|
| 150 |
session['job_state'] = job_state
|
|
|
|
| 151 |
upload_dir = Path(app.config['UPLOAD_FOLDER']) / session_id
|
| 152 |
results_dir = Path(app.config['RESULTS_FOLDER']) / session_id
|
| 153 |
# clean out old results if needed
|
|
|
|
| 428 |
print(f"Could not get NemaQuant script details: {e}")
|
| 429 |
|
| 430 |
@app.before_request
|
| 431 |
+
def startup_routine():
|
| 432 |
+
# Ensure session id
|
| 433 |
if 'id' not in session:
|
| 434 |
session['id'] = str(uuid.uuid4())
|
| 435 |
+
print('Running periodic cleanup of old sessions...')
|
| 436 |
+
# Cleanup old session folders
|
| 437 |
+
max_age_hours = 24
|
| 438 |
+
now = time.time()
|
| 439 |
+
for base_dir in [UPLOAD_FOLDER, RESULTS_FOLDER, ANNOT_FOLDER]:
|
| 440 |
+
for session_dir in Path(base_dir).iterdir():
|
| 441 |
+
if session_dir.is_dir():
|
| 442 |
+
mtime = session_dir.stat().st_mtime
|
| 443 |
+
if now - mtime > max_age_hours * 3600:
|
| 444 |
+
shutil.rmtree(session_dir)
|
| 445 |
|
| 446 |
if __name__ == '__main__':
|
| 447 |
print_startup_info()
|