File size: 10,323 Bytes
77a71b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
323
324
325
326
"""
Model availability checking utilities for ChordMini Flask application.

This module provides functions to check the availability of various ML models
and their dependencies without actually loading the models.
"""

from pathlib import Path
from utils.logging import log_info, log_error, log_debug


def check_spleeter_availability():
    """
    Check if Spleeter is available without loading models.
    
    Returns:
        bool: True if Spleeter is available
    """
    try:
        import spleeter
        log_debug("Spleeter is available")
        return True
    except ImportError as e:
        log_debug(f"Spleeter not available: {e}")
        return False


def check_beat_transformer_availability():
    """
    Check if Beat-Transformer is available without loading it.
    
    Returns:
        bool: True if Beat-Transformer is available
    """
    try:
        from models.beat_transformer import is_beat_transformer_available
        available = is_beat_transformer_available()
        log_debug(f"Beat-Transformer availability: {available}")
        return available
    except Exception as e:
        log_debug(f"Beat-Transformer availability check failed: {e}")
        return False


def check_chord_cnn_lstm_availability():
    """
    Check if Chord-CNN-LSTM is available without loading it.
    
    Returns:
        bool: True if Chord-CNN-LSTM is available
    """
    try:
        # Get the model directory path
        chord_cnn_lstm_dir = Path(__file__).parent.parent / "models" / "Chord-CNN-LSTM"
        
        # Check if the model directory exists and has required files
        if chord_cnn_lstm_dir.exists():
            # Check for key files that indicate the model is present
            required_files = ['chord_recognition.py']
            for file in required_files:
                if not (chord_cnn_lstm_dir / file).exists():
                    log_debug(f"Chord-CNN-LSTM missing required file: {file}")
                    return False
            log_debug("Chord-CNN-LSTM is available")
            return True
        else:
            log_debug(f"Chord-CNN-LSTM directory not found: {chord_cnn_lstm_dir}")
            return False
    except Exception as e:
        log_debug(f"Chord-CNN-LSTM availability check failed: {e}")
        return False


def check_genius_availability():
    """
    Check if Genius API is available.
    
    Returns:
        bool: True if lyricsgenius library is available
    """
    try:
        import lyricsgenius
        log_debug("Genius API (lyricsgenius) is available")
        return True
    except ImportError as e:
        log_debug(f"Genius API not available: {e}")
        return False


def check_btc_availability():
    """
    Check if BTC models and dependencies are available.
    
    Returns:
        dict: Detailed availability information for BTC models
    """
    try:
        btc_dir = Path(__file__).parent.parent / "models" / "ChordMini"

        # Check for model files
        sl_model = btc_dir / "checkpoints" / "SL" / "btc_model_large_voca.pt"
        pl_model = btc_dir / "checkpoints" / "btc" / "btc_combined_best.pth"
        config_file = btc_dir / "config" / "btc_config.yaml"

        sl_available = sl_model.exists()
        pl_available = pl_model.exists()
        config_available = config_file.exists()

        # Check for required Python modules
        try:
            import torch
            import numpy as np
            torch_available = True
            log_debug("PyTorch and NumPy are available for BTC models")
        except ImportError as e:
            torch_available = False
            log_debug(f"PyTorch/NumPy not available for BTC models: {e}")

        result = {
            'sl_available': sl_available and config_available and torch_available,
            'pl_available': pl_available and config_available and torch_available,
            'sl_model_path': str(sl_model),
            'pl_model_path': str(pl_model),
            'config_path': str(config_file)
        }
        
        log_debug(f"BTC availability check: SL={result['sl_available']}, PL={result['pl_available']}")
        return result
        
    except Exception as e:
        log_error(f"Error checking BTC availability: {e}")
        return {
            'sl_available': False,
            'pl_available': False,
            'sl_model_path': '',
            'pl_model_path': '',
            'config_path': ''
        }


def check_pytorch_availability():
    """
    Check if PyTorch is available and get device information.
    
    Returns:
        dict: PyTorch availability and device information
    """
    try:
        import torch
        
        result = {
            'available': True,
            'version': torch.__version__,
            'cuda_available': torch.cuda.is_available(),
            'cuda_device_count': torch.cuda.device_count() if torch.cuda.is_available() else 0,
            'mps_available': hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(),
            'device_name': 'cpu'
        }
        
        # Determine best available device
        if result['cuda_available']:
            result['device_name'] = 'cuda'
            result['cuda_device_name'] = torch.cuda.get_device_name(0) if result['cuda_device_count'] > 0 else 'Unknown'
        elif result['mps_available']:
            result['device_name'] = 'mps'
        
        log_debug(f"PyTorch available: {result['device_name']} device")
        return result
        
    except ImportError as e:
        log_debug(f"PyTorch not available: {e}")
        return {
            'available': False,
            'error': str(e),
            'version': None,
            'cuda_available': False,
            'cuda_device_count': 0,
            'mps_available': False,
            'device_name': 'cpu'
        }


def check_tensorflow_availability():
    """
    Check if TensorFlow is available.
    
    Returns:
        dict: TensorFlow availability information
    """
    try:
        import tensorflow as tf
        
        # Suppress TensorFlow warnings for this check
        import os
        old_level = os.environ.get('TF_CPP_MIN_LOG_LEVEL', '0')
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
        
        try:
            gpu_available = len(tf.config.list_physical_devices('GPU')) > 0
        except:
            gpu_available = False
        
        # Restore log level
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = old_level
        
        result = {
            'available': True,
            'version': tf.__version__,
            'gpu_available': gpu_available,
            'gpu_count': len(tf.config.list_physical_devices('GPU')) if gpu_available else 0
        }
        
        log_debug(f"TensorFlow available: version {result['version']}, GPU: {gpu_available}")
        return result
        
    except ImportError as e:
        log_debug(f"TensorFlow not available: {e}")
        return {
            'available': False,
            'error': str(e),
            'version': None,
            'gpu_available': False,
            'gpu_count': 0
        }


def get_model_directory_info(model_name):
    """
    Get information about a model directory.
    
    Args:
        model_name: Name of the model directory
        
    Returns:
        dict: Directory information
    """
    try:
        models_dir = Path(__file__).parent.parent / "models"
        model_dir = models_dir / model_name
        
        if not model_dir.exists():
            return {
                'exists': False,
                'path': str(model_dir),
                'files': [],
                'subdirectories': [],
                'size_mb': 0
            }
        
        files = []
        subdirectories = []
        total_size = 0
        
        for item in model_dir.rglob('*'):
            if item.is_file():
                files.append(str(item.relative_to(model_dir)))
                try:
                    total_size += item.stat().st_size
                except:
                    pass
            elif item.is_dir() and item != model_dir:
                subdirectories.append(str(item.relative_to(model_dir)))
        
        return {
            'exists': True,
            'path': str(model_dir),
            'files': sorted(files),
            'subdirectories': sorted(subdirectories),
            'file_count': len(files),
            'directory_count': len(subdirectories),
            'size_mb': round(total_size / (1024 * 1024), 2)
        }
        
    except Exception as e:
        log_error(f"Error getting model directory info for {model_name}: {e}")
        return {
            'exists': False,
            'path': '',
            'files': [],
            'subdirectories': [],
            'error': str(e)
        }


def get_all_model_availability():
    """
    Get availability status for all models.
    
    Returns:
        dict: Comprehensive model availability information
    """
    try:
        availability = {
            'spleeter': check_spleeter_availability(),
            'beat_transformer': check_beat_transformer_availability(),
            'chord_cnn_lstm': check_chord_cnn_lstm_availability(),
            'genius': check_genius_availability(),
            'btc': check_btc_availability(),
            'pytorch': check_pytorch_availability(),
            'tensorflow': check_tensorflow_availability()
        }
        
        # Count available models
        available_count = sum(1 for key, value in availability.items() 
                            if (isinstance(value, bool) and value) or 
                               (isinstance(value, dict) and value.get('available', False)))
        
        availability['summary'] = {
            'total_models': len(availability) - 1,  # Exclude summary itself
            'available_models': available_count,
            'availability_percentage': round((available_count / (len(availability) - 1)) * 100, 1)
        }
        
        return availability
        
    except Exception as e:
        log_error(f"Error getting model availability: {e}")
        return {
            'error': str(e),
            'summary': {
                'total_models': 0,
                'available_models': 0,
                'availability_percentage': 0.0
            }
        }