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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""
Utility Functions

Helper functions for file handling, validation, progress tracking,
and system management for the knowledge distillation application.
"""

import os
import logging
import asyncio
import hashlib
import mimetypes
import shutil
import psutil
import time
from typing import Dict, Any, List, Optional, Union
from pathlib import Path
import json
import tempfile
from datetime import datetime, timedelta

import torch
import numpy as np
from fastapi import UploadFile

# Configure logging
def setup_logging(level: str = "INFO", log_file: Optional[str] = None) -> None:
    """
    Setup application logging

    Args:
        level: Logging level (DEBUG, INFO, WARNING, ERROR)
        log_file: Optional log file path
    """
    log_level = getattr(logging, level.upper(), logging.INFO)

    # Configure logging format
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )

    # Setup handlers
    handlers = []

    # Console handler (always available)
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(formatter)
    handlers.append(console_handler)

    # File handler (only if writable)
    try:
        # Create logs directory if it doesn't exist and is writable
        logs_dir = Path("logs")
        logs_dir.mkdir(exist_ok=True)

        if log_file is None:
            log_file = f"logs/app_{datetime.now().strftime('%Y%m%d')}.log"

        # Test if we can write to the log file
        test_file = Path(log_file)
        test_file.touch()

        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)
        handlers.append(file_handler)

    except (PermissionError, OSError):
        # If we can't write to file, just use console logging
        print(f"Warning: Cannot write to log file, using console logging only")

    # Configure root logger
    logging.basicConfig(
        level=log_level,
        handlers=handlers,
        force=True
    )

    logger = logging.getLogger(__name__)
    logger.info(f"Logging initialized with level: {level}")

def validate_file(file: UploadFile) -> Dict[str, Any]:
    """
    Validate uploaded file for security and format compliance
    
    Args:
        file: FastAPI UploadFile object
        
    Returns:
        Validation result dictionary
    """
    try:
        # File size limits (in bytes)
        MAX_FILE_SIZE = 5 * 1024 * 1024 * 1024  # 5GB
        MIN_FILE_SIZE = 1024  # 1KB
        
        # Allowed file extensions
        ALLOWED_EXTENSIONS = {
            '.pt', '.pth', '.bin', '.safetensors', 
            '.onnx', '.h5', '.pkl', '.joblib'
        }
        
        # Allowed MIME types
        ALLOWED_MIME_TYPES = {
            'application/octet-stream',
            'application/x-pytorch',
            'application/x-pickle',
            'application/x-hdf5'
        }
        
        # Check file size
        if hasattr(file, 'size') and file.size:
            if file.size > MAX_FILE_SIZE:
                return {
                    'valid': False,
                    'error': f'File too large. Maximum size: {MAX_FILE_SIZE // (1024**3)}GB'
                }
            if file.size < MIN_FILE_SIZE:
                return {
                    'valid': False,
                    'error': f'File too small. Minimum size: {MIN_FILE_SIZE} bytes'
                }
        
        # Check file extension
        file_extension = Path(file.filename).suffix.lower()
        if file_extension not in ALLOWED_EXTENSIONS:
            return {
                'valid': False,
                'error': f'Invalid file extension. Allowed: {", ".join(ALLOWED_EXTENSIONS)}'
            }
        
        # Check MIME type
        mime_type, _ = mimetypes.guess_type(file.filename)
        if mime_type and mime_type not in ALLOWED_MIME_TYPES:
            # Allow octet-stream as fallback for binary files
            if mime_type != 'application/octet-stream':
                logging.warning(f"Unexpected MIME type: {mime_type} for {file.filename}")
        
        # Check filename for security
        if not _is_safe_filename(file.filename):
            return {
                'valid': False,
                'error': 'Invalid filename. Contains unsafe characters.'
            }
        
        return {
            'valid': True,
            'extension': file_extension,
            'mime_type': mime_type,
            'size': getattr(file, 'size', None)
        }
        
    except Exception as e:
        return {
            'valid': False,
            'error': f'Validation error: {str(e)}'
        }

def _is_safe_filename(filename: str) -> bool:
    """Check if filename is safe (no path traversal, etc.)"""
    if not filename:
        return False
    
    # Check for path traversal attempts
    if '..' in filename or '/' in filename or '\\' in filename:
        return False
    
    # Check for null bytes
    if '\x00' in filename:
        return False
    
    # Check for control characters
    if any(ord(c) < 32 for c in filename):
        return False
    
    return True

def get_system_info() -> Dict[str, Any]:
    """
    Get system information for monitoring and debugging
    
    Returns:
        System information dictionary
    """
    try:
        # CPU information
        cpu_info = {
            'count': psutil.cpu_count(),
            'usage_percent': psutil.cpu_percent(interval=1),
            'frequency': psutil.cpu_freq()._asdict() if psutil.cpu_freq() else None
        }
        
        # Memory information
        memory = psutil.virtual_memory()
        memory_info = {
            'total_gb': round(memory.total / (1024**3), 2),
            'available_gb': round(memory.available / (1024**3), 2),
            'used_gb': round(memory.used / (1024**3), 2),
            'percent': memory.percent
        }
        
        # Disk information
        disk = psutil.disk_usage('/')
        disk_info = {
            'total_gb': round(disk.total / (1024**3), 2),
            'free_gb': round(disk.free / (1024**3), 2),
            'used_gb': round(disk.used / (1024**3), 2),
            'percent': round((disk.used / disk.total) * 100, 2)
        }
        
        # GPU information
        gpu_info = {}
        if torch.cuda.is_available():
            gpu_info = {
                'available': True,
                'count': torch.cuda.device_count(),
                'current_device': torch.cuda.current_device(),
                'device_name': torch.cuda.get_device_name(),
                'memory_allocated_gb': round(torch.cuda.memory_allocated() / (1024**3), 2),
                'memory_reserved_gb': round(torch.cuda.memory_reserved() / (1024**3), 2)
            }
        else:
            gpu_info = {'available': False}
        
        return {
            'cpu': cpu_info,
            'memory': memory_info,
            'disk': disk_info,
            'gpu': gpu_info,
            'python_version': f"{psutil.sys.version_info.major}.{psutil.sys.version_info.minor}.{psutil.sys.version_info.micro}",
            'platform': psutil.os.name
        }
        
    except Exception as e:
        logging.error(f"Error getting system info: {e}")
        return {'error': str(e)}

def cleanup_temp_files(max_age_hours: int = 24) -> Dict[str, Any]:
    """
    Clean up temporary files older than specified age
    
    Args:
        max_age_hours: Maximum age of files to keep (in hours)
        
    Returns:
        Cleanup statistics
    """
    try:
        cleanup_stats = {
            'files_removed': 0,
            'bytes_freed': 0,
            'directories_cleaned': []
        }
        
        cutoff_time = time.time() - (max_age_hours * 3600)
        
        # Directories to clean
        temp_dirs = ['temp', 'uploads']
        
        for dir_name in temp_dirs:
            dir_path = Path(dir_name)
            if not dir_path.exists():
                continue
                
            files_removed = 0
            bytes_freed = 0
            
            for file_path in dir_path.rglob('*'):
                if file_path.is_file():
                    try:
                        # Check file age
                        if file_path.stat().st_mtime < cutoff_time:
                            file_size = file_path.stat().st_size
                            file_path.unlink()
                            files_removed += 1
                            bytes_freed += file_size
                    except Exception as e:
                        logging.warning(f"Error removing file {file_path}: {e}")
            
            if files_removed > 0:
                cleanup_stats['directories_cleaned'].append({
                    'directory': str(dir_path),
                    'files_removed': files_removed,
                    'bytes_freed': bytes_freed
                })
                
                cleanup_stats['files_removed'] += files_removed
                cleanup_stats['bytes_freed'] += bytes_freed
        
        logging.info(f"Cleanup completed: {cleanup_stats['files_removed']} files removed, "
                    f"{cleanup_stats['bytes_freed'] / (1024**2):.2f} MB freed")
        
        return cleanup_stats
        
    except Exception as e:
        logging.error(f"Error during cleanup: {e}")
        return {'error': str(e)}

def calculate_file_hash(file_path: Union[str, Path], algorithm: str = 'sha256') -> str:
    """
    Calculate hash of a file
    
    Args:
        file_path: Path to the file
        algorithm: Hash algorithm (md5, sha1, sha256, etc.)
        
    Returns:
        Hexadecimal hash string
    """
    try:
        hash_obj = hashlib.new(algorithm)
        
        with open(file_path, 'rb') as f:
            for chunk in iter(lambda: f.read(8192), b""):
                hash_obj.update(chunk)
        
        return hash_obj.hexdigest()
        
    except Exception as e:
        logging.error(f"Error calculating hash for {file_path}: {e}")
        raise

def format_bytes(bytes_value: int) -> str:
    """
    Format bytes into human-readable string
    
    Args:
        bytes_value: Number of bytes
        
    Returns:
        Formatted string (e.g., "1.5 GB")
    """
    for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
        if bytes_value < 1024.0:
            return f"{bytes_value:.1f} {unit}"
        bytes_value /= 1024.0
    return f"{bytes_value:.1f} PB"

def format_duration(seconds: float) -> str:
    """
    Format duration in seconds to human-readable string
    
    Args:
        seconds: Duration in seconds
        
    Returns:
        Formatted string (e.g., "2h 30m 15s")
    """
    if seconds < 60:
        return f"{seconds:.1f}s"
    elif seconds < 3600:
        minutes = int(seconds // 60)
        secs = int(seconds % 60)
        return f"{minutes}m {secs}s"
    else:
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = int(seconds % 60)
        return f"{hours}h {minutes}m {secs}s"

def create_progress_tracker():
    """
    Create a progress tracking utility
    
    Returns:
        Progress tracker instance
    """
    class ProgressTracker:
        def __init__(self):
            self.start_time = time.time()
            self.last_update = self.start_time
            self.steps_completed = 0
            self.total_steps = 0
            
        def update(self, current_step: int, total_steps: int, message: str = ""):
            self.steps_completed = current_step
            self.total_steps = total_steps
            self.last_update = time.time()
            
            # Calculate progress metrics
            progress = current_step / total_steps if total_steps > 0 else 0
            elapsed = self.last_update - self.start_time
            
            if progress > 0:
                eta = (elapsed / progress) * (1 - progress)
                eta_str = format_duration(eta)
            else:
                eta_str = "Unknown"
            
            return {
                'progress': progress,
                'current_step': current_step,
                'total_steps': total_steps,
                'elapsed': format_duration(elapsed),
                'eta': eta_str,
                'message': message
            }
    
    return ProgressTracker()

def safe_json_load(file_path: Union[str, Path]) -> Optional[Dict[str, Any]]:
    """
    Safely load JSON file with error handling
    
    Args:
        file_path: Path to JSON file
        
    Returns:
        Loaded JSON data or None if error
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except Exception as e:
        logging.warning(f"Error loading JSON from {file_path}: {e}")
        return None

def safe_json_save(data: Dict[str, Any], file_path: Union[str, Path]) -> bool:
    """
    Safely save data to JSON file
    
    Args:
        data: Data to save
        file_path: Path to save file
        
    Returns:
        True if successful, False otherwise
    """
    try:
        # Ensure directory exists
        Path(file_path).parent.mkdir(parents=True, exist_ok=True)
        
        with open(file_path, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
        return True
    except Exception as e:
        logging.error(f"Error saving JSON to {file_path}: {e}")
        return False

def get_available_memory() -> float:
    """
    Get available system memory in GB
    
    Returns:
        Available memory in GB
    """
    try:
        memory = psutil.virtual_memory()
        return memory.available / (1024**3)
    except Exception:
        return 0.0

def check_disk_space(path: str = ".", min_gb: float = 1.0) -> bool:
    """
    Check if there's enough disk space
    
    Args:
        path: Path to check
        min_gb: Minimum required space in GB
        
    Returns:
        True if enough space available
    """
    try:
        disk = psutil.disk_usage(path)
        free_gb = disk.free / (1024**3)
        return free_gb >= min_gb
    except Exception:
        return False