File size: 8,480 Bytes
9aa4daf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
System Prerequisites Checker Module

This module provides functionality to check system prerequisites including:
- CUDA/GPU availability
- Environment dependencies
- Model download with progress tracking
"""

import os
import sys
import torch
import platform
from typing import Dict, Tuple, Optional
from pathlib import Path
import importlib.metadata
from huggingface_hub import hf_hub_download, snapshot_download
from tqdm import tqdm


class SystemChecker:
    """Check system prerequisites for MLOps platform."""
    
    def __init__(self, models_dir: str = "models"):
        """
        Initialize system checker.
        
        Args:
            models_dir: Directory to store downloaded models
        """
        self.models_dir = Path(models_dir)
        self.models_dir.mkdir(parents=True, exist_ok=True)
        
    def check_cuda(self) -> Dict[str, any]:
        """
        Check CUDA/GPU availability and information.
        
        Returns:
            Dict with CUDA status, device info, and specifications
        """
        result = {
            "available": torch.cuda.is_available(),
            "device_count": 0,
            "devices": [],
            "cuda_version": None,
            "cudnn_version": None
        }
        
        if result["available"]:
            result["device_count"] = torch.cuda.device_count()
            result["cuda_version"] = torch.version.cuda
            result["cudnn_version"] = torch.backends.cudnn.version()
            
            for i in range(result["device_count"]):
                device_props = {
                    "id": i,
                    "name": torch.cuda.get_device_name(i),
                    "memory_total": torch.cuda.get_device_properties(i).total_memory / 1024**3,  # GB
                    "compute_capability": f"{torch.cuda.get_device_properties(i).major}.{torch.cuda.get_device_properties(i).minor}"
                }
                result["devices"].append(device_props)
        
        return result
    
    def check_environment(self) -> Dict[str, any]:
        """
        Check Python environment and required dependencies.
        
        Returns:
            Dict with Python version, package versions, and system info
        """
        result = {
            "python_version": sys.version,
            "platform": platform.platform(),
            "architecture": platform.machine(),
            "packages": {},
            "missing_packages": [],
            "all_satisfied": True
        }
        
        # Required packages with minimum versions
        required_packages = {
            "torch": "2.0.0",
            "transformers": "4.36.0",
            "streamlit": "1.28.0",
            "pandas": "2.0.0",
            "numpy": "1.24.0",
            "plotly": "5.18.0",
            "scikit-learn": "1.3.0"
        }
        
        for package, min_version in required_packages.items():
            try:
                version = importlib.metadata.version(package)
                result["packages"][package] = {
                    "installed": version,
                    "required": f">={min_version}",
                    "satisfied": True  # Simple check, could add version comparison
                }
            except importlib.metadata.PackageNotFoundError:
                result["packages"][package] = {
                    "installed": None,
                    "required": f">={min_version}",
                    "satisfied": False
                }
                result["missing_packages"].append(package)
                result["all_satisfied"] = False
        
        return result
    
    def download_model(
        self, 
        model_name: str, 
        progress_callback: Optional[callable] = None
    ) -> Tuple[bool, str, str]:
        """
        Download model from HuggingFace Hub to local cache.
        
        Args:
            model_name: HuggingFace model identifier (e.g., "roberta-base")
            progress_callback: Optional callback function for progress updates
            
        Returns:
            Tuple of (success: bool, model_path: str, message: str)
        """
        try:
            model_cache_path = self.models_dir / model_name.replace("/", "_")
            
            # Check if model already exists
            if model_cache_path.exists() and any(model_cache_path.iterdir()):
                return True, str(model_cache_path), f"Model '{model_name}' already exists in cache"
            
            # Download model
            if progress_callback:
                progress_callback(f"Downloading {model_name}...", 0.1)
            
            # Use snapshot_download to get all model files
            cache_dir = snapshot_download(
                repo_id=model_name,
                cache_dir=str(self.models_dir),
                local_dir=str(model_cache_path),
                local_dir_use_symlinks=False
            )
            
            if progress_callback:
                progress_callback(f"Downloaded {model_name} successfully", 1.0)
            
            return True, str(model_cache_path), f"Model '{model_name}' downloaded successfully"
            
        except Exception as e:
            error_msg = f"Failed to download model '{model_name}': {str(e)}"
            if progress_callback:
                progress_callback(error_msg, 0.0)
            return False, "", error_msg
    
    def get_model_info(self, model_name: str) -> Dict[str, any]:
        """
        Get information about a model (local or remote).
        
        Args:
            model_name: Model identifier
            
        Returns:
            Dict with model information
        """
        model_cache_path = self.models_dir / model_name.replace("/", "_")
        
        info = {
            "name": model_name,
            "local_path": str(model_cache_path),
            "exists_locally": model_cache_path.exists() and any(model_cache_path.iterdir()),
            "size_mb": 0
        }
        
        if info["exists_locally"]:
            # Calculate total size
            total_size = sum(
                f.stat().st_size 
                for f in model_cache_path.rglob('*') 
                if f.is_file()
            )
            info["size_mb"] = total_size / (1024 * 1024)
        
        return info


def format_bytes(bytes_size: float) -> str:
    """Format bytes to human-readable string."""
    for unit in ['B', 'KB', 'MB', 'GB']:
        if bytes_size < 1024.0:
            return f"{bytes_size:.2f} {unit}"
        bytes_size /= 1024.0
    return f"{bytes_size:.2f} TB"


def get_system_summary() -> str:
    """Get a formatted summary of system capabilities."""
    checker = SystemChecker()
    
    cuda_info = checker.check_cuda()
    env_info = checker.check_environment()
    
    summary = []
    summary.append("=" * 60)
    summary.append("SYSTEM SUMMARY")
    summary.append("=" * 60)
    
    # Python & Platform
    summary.append(f"\nPython: {env_info['python_version'].split()[0]}")
    summary.append(f"Platform: {env_info['platform']}")
    summary.append(f"Architecture: {env_info['architecture']}")
    
    # CUDA
    summary.append(f"\nCUDA Available: {'Yes' if cuda_info['available'] else 'No (CPU only)'}")
    if cuda_info['available']:
        summary.append(f"CUDA Version: {cuda_info['cuda_version']}")
        summary.append(f"Number of GPUs: {cuda_info['device_count']}")
        for device in cuda_info['devices']:
            summary.append(f"   - GPU {device['id']}: {device['name']}")
            summary.append(f"     Memory: {device['memory_total']:.2f} GB")
            summary.append(f"     Compute: {device['compute_capability']}")
    
    # Packages
    summary.append(f"\n📦 Required Packages: {'✅ All Satisfied' if env_info['all_satisfied'] else '⚠️ Missing Packages'}")
    if env_info['missing_packages']:
        summary.append(f"   Missing: {', '.join(env_info['missing_packages'])}")
    
    summary.append("=" * 60)
    
    return "\n".join(summary)


if __name__ == "__main__":
    # Test the system checker
    print(get_system_summary())
    
    checker = SystemChecker()
    
    # Test model download (small model for testing)
    print("\n\nTesting model download...")
    success, path, msg = checker.download_model(
        "distilbert-base-uncased",
        progress_callback=lambda msg, progress: print(f"Progress: {progress*100:.0f}% - {msg}")
    )
    print(f"Result: {msg}")