|
1 | 1 | # spam_detector_ai/training/train_models.py
|
2 | 2 |
|
3 | 3 | import os
|
| 4 | +from pathlib import Path |
4 | 5 |
|
5 | 6 | from sklearn.model_selection import train_test_split
|
6 | 7 |
|
@@ -77,19 +78,17 @@ def get_directory_path(self):
|
77 | 78 | raise ValueError(f"Invalid classifier type: {self.classifier_type}")
|
78 | 79 |
|
79 | 80 | def save_model(self, model_filename, vectoriser_filename):
|
80 |
| - # Determine the directory of this file |
81 |
| - current_dir = os.path.dirname(os.path.abspath(__file__)) |
82 |
| - # Assuming the spam_detector_ai directory is one level up from the current directory |
83 |
| - base_dir = os.path.dirname(current_dir) |
| 81 | + # Use the project root to construct the paths |
| 82 | + project_root = Path(__file__).parent.parent |
| 83 | + models_dir = project_root |
84 | 84 | directory_path = self.get_directory_path()
|
85 | 85 |
|
86 |
| - # Ensure the directory exists |
87 |
| - if not os.path.exists(directory_path): |
88 |
| - os.makedirs(directory_path) |
| 86 | + model_filepath = models_dir / directory_path / model_filename |
| 87 | + vectoriser_filepath = models_dir / directory_path / vectoriser_filename |
89 | 88 |
|
90 |
| - model_filepath = os.path.join(base_dir, directory_path, model_filename) |
91 |
| - vectoriser_filepath = os.path.join(base_dir, directory_path, vectoriser_filename) |
| 89 | + # Ensure the directory exists |
| 90 | + model_filepath.parent.mkdir(parents=True, exist_ok=True) |
92 | 91 |
|
93 | 92 | self.logger.info(f'Saving model to {model_filepath}')
|
94 |
| - self.classifier.save_model(model_filepath, vectoriser_filepath) |
| 93 | + self.classifier.save_model(str(model_filepath), str(vectoriser_filepath)) |
95 | 94 | self.logger.info('Model saved.\n')
|
0 commit comments