Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -342,6 +342,7 @@ def remove_files():
|
|
| 342 |
return redirect(url_for('index'))
|
| 343 |
|
| 344 |
# API for downloading the latest trained model
|
|
|
|
| 345 |
@app.route('/download_model', methods=['GET'])
|
| 346 |
def download_latest_model():
|
| 347 |
try:
|
|
@@ -374,6 +375,49 @@ def download_latest_model():
|
|
| 374 |
except Exception as e:
|
| 375 |
flash(f"Error while downloading the model: {str(e)}", 'error')
|
| 376 |
return redirect(request.referrer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
# Route to serve uploaded files
|
| 379 |
@app.route('/<foldername>/zip', methods=['GET'])
|
|
|
|
| 342 |
return redirect(url_for('index'))
|
| 343 |
|
| 344 |
# API for downloading the latest trained model
|
| 345 |
+
'''
|
| 346 |
@app.route('/download_model', methods=['GET'])
|
| 347 |
def download_latest_model():
|
| 348 |
try:
|
|
|
|
| 375 |
except Exception as e:
|
| 376 |
flash(f"Error while downloading the model: {str(e)}", 'error')
|
| 377 |
return redirect(request.referrer)
|
| 378 |
+
'''
|
| 379 |
+
@app.route('/download_model', methods=['GET'])
|
| 380 |
+
def download_latest_model():
|
| 381 |
+
try:
|
| 382 |
+
models_dir = app.config['MODELS_FOLDER']
|
| 383 |
+
model_files = os.listdir(models_dir)
|
| 384 |
+
|
| 385 |
+
if not model_files:
|
| 386 |
+
flash('No model files found', 'error')
|
| 387 |
+
return redirect(request.referrer)
|
| 388 |
+
|
| 389 |
+
# Sort model files and get the latest one
|
| 390 |
+
latest_model_file = sorted(model_files, reverse=True)[0]
|
| 391 |
+
|
| 392 |
+
# Full path to the latest model file
|
| 393 |
+
model_path = os.path.join(models_dir, latest_model_file)
|
| 394 |
+
|
| 395 |
+
if not os.path.exists(model_path):
|
| 396 |
+
flash('Model file not found on the server', 'error')
|
| 397 |
+
return redirect(request.referrer)
|
| 398 |
+
|
| 399 |
+
# Create a zip file with the model
|
| 400 |
+
zip_filename = os.path.join(models_dir, f"{latest_model_file}.zip")
|
| 401 |
+
|
| 402 |
+
# Create a zip file that includes the whole directory structure of the model
|
| 403 |
+
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 404 |
+
# If the model is a directory, zip the whole directory
|
| 405 |
+
if os.path.isdir(model_path):
|
| 406 |
+
for foldername, subfolders, filenames in os.walk(model_path):
|
| 407 |
+
for filename in filenames:
|
| 408 |
+
file_path = os.path.join(foldername, filename)
|
| 409 |
+
# Write the file, preserving the directory structure
|
| 410 |
+
zipf.write(file_path, os.path.relpath(file_path, models_dir))
|
| 411 |
+
else:
|
| 412 |
+
# If it's just a single file, zip it
|
| 413 |
+
zipf.write(model_path, os.path.basename(model_path))
|
| 414 |
+
|
| 415 |
+
# Send the zip file as a download
|
| 416 |
+
return send_file(zip_filename, as_attachment=True)
|
| 417 |
+
|
| 418 |
+
except Exception as e:
|
| 419 |
+
flash(f"Error while downloading the model: {str(e)}", 'error')
|
| 420 |
+
return redirect(request.referrer)
|
| 421 |
|
| 422 |
# Route to serve uploaded files
|
| 423 |
@app.route('/<foldername>/zip', methods=['GET'])
|