|
1 |
| -# core/tasks.py |
2 |
| - |
3 |
| -# from celery import shared_task |
4 | 1 | import os
|
5 | 2 | from django.conf import settings
|
6 |
| - |
7 | 3 | from utils.encryption_utils import encrypt_model
|
8 | 4 |
|
9 |
| -# @shared_task |
10 | 5 | def encrypt_model_if_needed():
|
| 6 | + # Paths for the model and encrypted model |
11 | 7 | model_path = os.path.join(settings.MEDIA_ROOT, 'models', 'model.onnx')
|
| 8 | + encrypted_model_path = os.path.join(settings.MEDIA_ROOT, 'encrypted_models', 'model.onnx.enc') |
12 | 9 | model_version_path = os.path.join(settings.MEDIA_ROOT, 'models', 'model_version.txt')
|
13 | 10 |
|
14 |
| - # Check if the model needs to be updated |
15 |
| - if os.path.exists(model_version_path): |
16 |
| - with open(model_version_path, 'r') as f: |
17 |
| - stored_version = f.read().strip() |
18 |
| - |
19 |
| - # Compare version or timestamp to determine if update is needed |
20 |
| - current_version = get_model_version(model_path) # Implement this function based on your versioning strategy |
| 11 | + # Ensure the encrypted_models directory exists |
| 12 | + if not os.path.exists(os.path.dirname(encrypted_model_path)): |
| 13 | + os.makedirs(os.path.dirname(encrypted_model_path)) |
| 14 | + |
| 15 | + # Check if the encrypted model already exists |
| 16 | + if not os.path.exists(encrypted_model_path): |
| 17 | + print("Encrypted model not found or directory is empty. Encrypting the model.") |
21 | 18 |
|
22 |
| - if current_version != stored_version: |
23 |
| - encrypt_model(model_path) # Call encryption function |
24 |
| - # Update stored version file |
| 19 | + # Encrypt the model and save it to the encrypted_models folder |
| 20 | + encrypt_model(model_path, encrypted_model_path) # Pass the path to save the encrypted model |
| 21 | + |
| 22 | + # After encryption, create or update the model version file |
| 23 | + model_timestamp = str(int(os.path.getmtime(model_path))) |
| 24 | + with open(model_version_path, 'w') as f: |
| 25 | + f.write(model_timestamp) # Store the current model's last modified timestamp |
| 26 | + else: |
| 27 | + # If encrypted model exists, check if an update is needed based on timestamps |
| 28 | + if os.path.exists(model_version_path): |
| 29 | + with open(model_version_path, 'r') as f: |
| 30 | + stored_timestamp = f.read().strip() # Read the stored timestamp |
| 31 | + |
| 32 | + # Get the last modified timestamp of the model file |
| 33 | + model_timestamp = str(int(os.path.getmtime(model_path))) # Last modified time as a string |
| 34 | + |
| 35 | + # Compare timestamps to determine if update is needed |
| 36 | + if model_timestamp != stored_timestamp: |
| 37 | + print("Model has been updated. Encrypting the updated model.") |
| 38 | + |
| 39 | + # Encrypt the model and save it to the encrypted_models folder |
| 40 | + encrypt_model(model_path, encrypted_model_path) # Pass the path to save the encrypted model |
| 41 | + |
| 42 | + # Update the stored timestamp file |
| 43 | + with open(model_version_path, 'w') as f: |
| 44 | + f.write(model_timestamp) # Store the current model's last modified timestamp |
| 45 | + else: |
| 46 | + # If model_version.txt doesn't exist, assume the model needs to be encrypted |
| 47 | + print("Model version file not found. Encrypting the model.") |
| 48 | + model_timestamp = str(int(os.path.getmtime(model_path))) |
| 49 | + |
| 50 | + # Encrypt the model and save it to the encrypted_models folder |
| 51 | + encrypt_model(model_path, encrypted_model_path) # Pass the path to save the encrypted model |
| 52 | + |
25 | 53 | with open(model_version_path, 'w') as f:
|
26 |
| - f.write(current_version) |
| 54 | + f.write(model_timestamp) # Create the timestamp file with the current model's timestamp |
0 commit comments