File size: 10,217 Bytes
e6341fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1377fb1
e6341fe
 
 
 
 
1377fb1
e6341fe
1377fb1
b08ba59
 
1377fb1
e6341fe
 
1377fb1
 
 
 
 
 
 
e6341fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1377fb1
 
 
 
e6341fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
Model Manager for Fine-Tuned Model Deployment and Versioning

Handles loading, deploying, and rolling back fine-tuned models.
"""

import os
import json
import shutil
from typing import Optional, Dict
from datetime import datetime
import logging

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

logger = logging.getLogger(__name__)


class ModelManager:
    """Manage fine-tuned model deployment and versioning"""

    def __init__(self, models_dir: str = None):
        """
        Initialize ModelManager.

        Args:
            models_dir: Base directory for storing fine-tuned models
                       (defaults to MODELS_DIR env var or './models/finetuned')
        """
        if models_dir is None:
            # Use environment variable or /data path for HF Spaces
            models_dir = os.getenv('MODELS_DIR', '/data/models/finetuned')
        
        self.models_dir = models_dir
        self.base_model_name = "facebook/bart-large-mnli"
        
        # Create directory if it doesn't exist
        try:
            os.makedirs(models_dir, exist_ok=True)
        except PermissionError:
            logger.error(f"Permission denied creating models directory: {models_dir}")
            raise

    def get_model_path(self, run_id: int) -> str:
        """Get path to model for a specific training run"""
        return os.path.join(self.models_dir, f"run_{run_id}")

    def load_model(self, run_id: Optional[int] = None):
        """
        Load a fine-tuned model or base model.

        Args:
            run_id: Training run ID (None for base model)

        Returns:
            Tuple of (model, tokenizer)
        """
        if run_id is None:
            logger.info("Loading base model")
            model_name = self.base_model_name
        else:
            model_path = self.get_model_path(run_id)
            if not os.path.exists(model_path):
                raise FileNotFoundError(f"Model not found: {model_path}")
            logger.info(f"Loading fine-tuned model from run {run_id}")
            model_name = model_path

        tokenizer = AutoTokenizer.from_pretrained(model_name)
        model = AutoModelForSequenceClassification.from_pretrained(
            model_name,
            ignore_mismatched_sizes=True
        )

        return model, tokenizer

    def deploy_model(self, run_id: int, db_session) -> Dict:
        """
        Deploy a fine-tuned model (set as active).

        Args:
            run_id: Training run ID to deploy
            db_session: Database session for updating FineTuningRun

        Returns:
            Dict with deployment info
        """
        from app.models.models import FineTuningRun

        logger.info(f"Deploying model from run {run_id}")

        # Verify model exists
        model_path = self.get_model_path(run_id)
        if not os.path.exists(model_path):
            raise FileNotFoundError(f"Model not found: {model_path}")

        # Get the run record
        run = db_session.query(FineTuningRun).filter_by(id=run_id).first()
        if not run:
            raise ValueError(f"Training run {run_id} not found")

        if run.status != 'completed':
            raise ValueError(f"Cannot deploy non-completed run (status: {run.status})")

        # Deactivate all other models
        db_session.query(FineTuningRun).update({'is_active_model': False})

        # Activate this model
        run.is_active_model = True
        db_session.commit()

        logger.info(f"Model from run {run_id} is now active")

        return {
            'run_id': run_id,
            'deployed_at': datetime.utcnow().isoformat(),
            'model_path': model_path
        }

    def rollback_to_baseline(self, db_session) -> Dict:
        """
        Rollback to base model (deactivate all fine-tuned models).

        Args:
            db_session: Database session

        Returns:
            Dict with rollback info
        """
        from app.models.models import FineTuningRun

        logger.info("Rolling back to base model")

        # Deactivate all fine-tuned models
        active_count = db_session.query(FineTuningRun).filter_by(is_active_model=True).count()
        db_session.query(FineTuningRun).update({'is_active_model': False})
        db_session.commit()

        logger.info(f"Deactivated {active_count} fine-tuned model(s)")

        return {
            'rolled_back_at': datetime.utcnow().isoformat(),
            'deactivated_models': active_count,
            'active_model': 'base'
        }

    def get_active_model_info(self, db_session) -> Optional[Dict]:
        """
        Get information about the currently active model.

        Args:
            db_session: Database session

        Returns:
            Dict with active model info, or None if base model is active
        """
        from app.models.models import FineTuningRun

        active_run = db_session.query(FineTuningRun).filter_by(is_active_model=True).first()

        if not active_run:
            return None

        return {
            'run_id': active_run.id,
            'model_path': self.get_model_path(active_run.id),
            'created_at': active_run.created_at.isoformat() if active_run.created_at else None,
            'results': active_run.get_results(),
            'config': active_run.get_config()
        }

    def export_model(self, run_id: int, export_path: str) -> str:
        """
        Export model for backup or sharing.

        Args:
            run_id: Training run ID
            export_path: Destination path for export

        Returns:
            Path to exported model
        """
        logger.info(f"Exporting model from run {run_id}")

        model_path = self.get_model_path(run_id)
        if not os.path.exists(model_path):
            raise FileNotFoundError(f"Model not found: {model_path}")

        # Create export directory
        os.makedirs(export_path, exist_ok=True)

        # Copy all model files
        export_model_path = os.path.join(export_path, f"model_run_{run_id}")
        shutil.copytree(model_path, export_model_path, dirs_exist_ok=True)

        # Create model card
        model_card = {
            'run_id': run_id,
            'export_date': datetime.utcnow().isoformat(),
            'base_model': self.base_model_name,
            'model_type': 'BART with LoRA fine-tuning',
            'task': 'Multi-class text classification',
            'categories': ['Vision', 'Problem', 'Objectives', 'Directives', 'Values', 'Actions']
        }

        with open(os.path.join(export_model_path, 'model_card.json'), 'w') as f:
            json.dump(model_card, f, indent=2)

        logger.info(f"Model exported to {export_model_path}")

        return export_model_path

    def import_model(self, import_path: str, run_id: int) -> str:
        """
        Import a previously exported model.

        Args:
            import_path: Path to imported model directory
            run_id: Training run ID to assign

        Returns:
            Path to imported model in models directory
        """
        logger.info(f"Importing model to run {run_id}")

        if not os.path.exists(import_path):
            raise FileNotFoundError(f"Import path not found: {import_path}")

        # Verify it's a valid model directory
        required_files = ['config.json', 'pytorch_model.bin']  # or adapter_model.bin for LoRA
        has_required = any(os.path.exists(os.path.join(import_path, f)) for f in required_files)

        if not has_required:
            raise ValueError(f"Import path does not contain a valid model")

        # Copy to models directory
        model_path = self.get_model_path(run_id)
        shutil.copytree(import_path, model_path, dirs_exist_ok=True)

        logger.info(f"Model imported to {model_path}")

        return model_path

    def delete_model(self, run_id: int) -> None:
        """
        Delete a fine-tuned model from disk.

        Args:
            run_id: Training run ID
        """
        logger.info(f"Deleting model from run {run_id}")

        model_path = self.get_model_path(run_id)
        if os.path.exists(model_path):
            shutil.rmtree(model_path)
            logger.info(f"Model deleted: {model_path}")
        else:
            logger.warning(f"Model not found: {model_path}")

    def get_model_size(self, run_id: int) -> Dict:
        """
        Get size information for a model.

        Args:
            run_id: Training run ID

        Returns:
            Dict with size info
        """
        model_path = self.get_model_path(run_id)

        if not os.path.exists(model_path):
            return {'exists': False}

        # Calculate directory size
        total_size = 0
        file_count = 0

        for dirpath, dirnames, filenames in os.walk(model_path):
            for filename in filenames:
                filepath = os.path.join(dirpath, filename)
                total_size += os.path.getsize(filepath)
                file_count += 1

        return {
            'exists': True,
            'total_size_bytes': total_size,
            'total_size_mb': round(total_size / (1024 * 1024), 2),
            'file_count': file_count,
            'path': model_path
        }

    def list_available_models(self, db_session) -> list:
        """
        List all available fine-tuned models.

        Args:
            db_session: Database session

        Returns:
            List of dicts with model info
        """
        from app.models.models import FineTuningRun

        runs = db_session.query(FineTuningRun).filter_by(status='completed').all()

        models = []
        for run in runs:
            model_path = self.get_model_path(run.id)
            size_info = self.get_model_size(run.id)

            models.append({
                'run_id': run.id,
                'created_at': run.created_at.isoformat() if run.created_at else None,
                'is_active': run.is_active_model,
                'results': run.get_results(),
                'model_exists': size_info.get('exists', False),
                'size_mb': size_info.get('total_size_mb', 0)
            })

        return models