-
-
Notifications
You must be signed in to change notification settings - Fork 48
Architecture and Design Backend Architecture Machine Learning Integration Model Loading and Initialization
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
This document explains the model loading and initialization system powering audio analysis in the backend. It covers the application factory pattern, dynamic model loading, initialization procedures for detection algorithms, model availability checking, fallback mechanisms, error handling, caching and memory strategies, configuration management, version compatibility, and integration with the Flask application factory. It also describes how models are initialized during service startup and how the system gracefully handles missing or corrupted models.
The model system spans several modules:
- Application factory initializes services and sets up model paths.
- Services encapsulate detection logic and model orchestration.
- Detectors wrap individual models with normalized interfaces.
- Utilities provide availability checks and path management.
- Configuration controls feature toggles and environment-specific behavior.
graph TB
subgraph "Flask App Factory"
AF["app_factory.py<br/>create_app()"]
EXT["extensions.py<br/>init_extensions()"]
CFG["config.py<br/>Config classes"]
end
subgraph "Services"
BDS["beat_detection_service.py<br/>BeatDetectionService"]
CRS["chord_recognition_service.py<br/>ChordRecognitionService"]
SPF["spleeter_service.py<br/>SpleeterService"]
SFS["songformer_service.py<br/>SongFormerService"]
end
subgraph "Detectors"
BTS["beat_transformer_detector.py<br/>BeatTransformerDetectorService"]
BTL["btc_sl_detector.py<br/>BTCSLDetectorService"]
BTP["btc_pl_detector.py<br/>BTCPLDetectorService"]
CCL["chord_cnn_lstm_detector.py<br/>ChordCNNLSTMDetectorService"]
end
subgraph "Utilities"
PTH["paths.py<br/>setup_model_paths(), paths"]
MUL["model_utils.py<br/>availability checks"]
end
AF --> EXT
AF --> CFG
AF --> BDS
AF --> CRS
AF --> SFS
CRS --> SPF
CRS --> CCL
CRS --> BTL
CRS --> BTP
BDS --> BTS
BDS --> BTL
BDS --> BTP
BDS --> CCL
PTH --> BDS
PTH --> CRS
MUL --> BDS
MUL --> CRS
Diagram sources
- app_factory.py:27-65
- extensions.py:81-92
- config.py:16-215
- paths.py:45-62
- beat_detection_service.py:20-348
- chord_recognition_service.py:25-322
- btc_sl_detector.py:17-246
- btc_pl_detector.py:17-246
- chord_cnn_lstm_detector.py:17-249
- spleeter_service.py:17-286
- songformer_service.py:21-140
Section sources
- Application factory pattern: Centralized creation and configuration of the Flask app, including extension initialization, blueprints registration, and service container setup.
- Service container: Initializes core services (beat detection, chord recognition, lyrics orchestrator, SongFormer) with graceful failure handling.
- Detector services: Encapsulate individual models with normalized interfaces, availability checks, and fallback logic.
- Availability utilities: Lightweight checks for model presence and dependencies without loading heavy weights.
- Path management: Adds model directories to Python path and validates checkpoint locations.
- Configuration: Controls feature toggles, rate limits, CORS, and environment-specific behavior.
Section sources
- app_factory.py:103-162
- beat_detection_service.py:20-348
- chord_recognition_service.py:25-322
- model_utils.py:12-326
- paths.py:45-184
- config.py:62-70
The system follows a layered architecture:
- Application factory creates the Flask app, loads configuration, initializes extensions, registers blueprints, and constructs a service container.
- Services orchestrate detectors and apply fallback strategies based on availability and file size constraints.
- Detectors implement model-specific loading and inference with normalized outputs.
- Utilities provide availability checks and path management to ensure deterministic loading.
sequenceDiagram
participant Client as "Client"
participant App as "Flask App"
participant Factory as "app_factory.create_app()"
participant Ext as "extensions.init_extensions()"
participant Cfg as "config.get_config()"
participant Paths as "paths.setup_model_paths()"
participant Services as "Service Container"
Client->>App : Start server
App->>Factory : create_app(config_name)
Factory->>Ext : init_extensions(app, config)
Factory->>Cfg : get_config(config_name)
Factory->>Paths : setup_model_paths()
Factory->>Services : init_services(app, config)
Services-->>Factory : services dict
Factory-->>App : configured Flask app
App-->>Client : ready to serve
Diagram sources
- Compatibility patches are applied early to ensure third-party libraries are adapted before heavy imports.
- Configuration is loaded from environment-aware classes and applied to the Flask app.
- Extensions (CORS, rate limiter, logging) are initialized centrally.
- Blueprints are registered conditionally (e.g., debug blueprint only outside production).
- Service container is constructed with lazy initialization and error-handling fallbacks for each service.
flowchart TD
Start(["create_app(config_name)"]) --> Patches["Apply compatibility patches"]
Patches --> FlaskApp["Create Flask app"]
FlaskApp --> LoadCfg["Load config via get_config()"]
LoadCfg --> InitExt["init_extensions(app, config)"]
InitExt --> RegBP["register_blueprints(app, config)"]
RegBP --> InitSvc["init_services(app, config)"]
InitSvc --> Done(["Return configured Flask app"])
Diagram sources
Section sources
- Dedicated utilities check for model presence and dependencies without importing heavy modules.
- Checks include:
- Spleeter availability
- Beat-Transformer availability
- Chord-CNN-LSTM presence (directory and required files)
- Genius API availability
- BTC models (SL/PL) with config and checkpoint validation
- PyTorch/TensorFlow availability and device info
- Aggregated availability report with counts and percentages.
flowchart TD
A["check_all_models()"] --> B["check_spleeter_availability()"]
A --> C["check_beat_transformer_availability()"]
A --> D["check_chord_cnn_lstm_availability()"]
A --> E["check_genius_availability()"]
A --> F["check_btc_availability()"]
A --> G["check_pytorch_availability()"]
A --> H["check_tensorflow_availability()"]
B --> R["Aggregate availability"]
C --> R
D --> R
E --> R
F --> R
G --> R
H --> R
Diagram sources
Section sources
- Beat Detection Service composes multiple detectors (Beat-Transformer, madmom, librosa) and selects based on availability and file size.
- Chord Recognition Service composes detectors (Chord-CNN-LSTM, BTC-SL, BTC-PL) with similar selection logic and integrates Spleeter for vocal separation.
- Detectors implement lazy availability checks and import-time validation.
- SongFormer Service dynamically loads the packaged runtime, initializes models on demand, and manages device context.
classDiagram
class BeatDetectionService {
+detectors : dict
+size_limits : dict
+get_available_detectors() List[str]
+select_detector(req, size, force) str
+detect_beats(file_path, detector, force) Dict
}
class ChordRecognitionService {
+detectors : dict
+size_limits : dict
+spleeter_service : SpleeterService
+get_available_detectors() List[str]
+select_detector(req, size, force) str
+recognize_chords(file_path, detector, dict, force, use_spleeter) Dict
}
class BTCSLDetectorService {
+is_available() bool
+recognize_chords(file_path, chord_dict) Dict
}
class BTCPLDetectorService {
+is_available() bool
+recognize_chords(file_path, chord_dict) Dict
}
class ChordCNNLSTMDetectorService {
+is_available() bool
+recognize_chords(file_path, chord_dict) Dict
}
class SpleeterService {
+is_available() bool
+separate_audio(...)
+extract_vocals(...)
+cleanup_stems(...)
}
BeatDetectionService --> BTCSLDetectorService : "uses"
BeatDetectionService --> BTCPLDetectorService : "uses"
BeatDetectionService --> ChordCNNLSTMDetectorService : "uses"
ChordRecognitionService --> BTCSLDetectorService : "uses"
ChordRecognitionService --> BTCPLDetectorService : "uses"
ChordRecognitionService --> ChordCNNLSTMDetectorService : "uses"
ChordRecognitionService --> SpleeterService : "uses"
Diagram sources
- beat_detection_service.py:20-348
- chord_recognition_service.py:25-322
- btc_sl_detector.py:17-246
- btc_pl_detector.py:17-246
- chord_cnn_lstm_detector.py:17-249
- spleeter_service.py:17-286
Section sources
- beat_detection_service.py:20-348
- chord_recognition_service.py:25-322
- btc_sl_detector.py:32-85
- btc_pl_detector.py:32-85
- chord_cnn_lstm_detector.py:32-77
- spleeter_service.py:27-47
- Beat Detection Service:
- Detects available detectors and applies size-based selection policy.
- Normalizes results and adds metadata (file size, duration, processing time).
- Chord Recognition Service:
- Orchestrates detector selection, chord dictionary validation, optional Spleeter separation, and normalization of outputs.
- Provides detector info and supported dictionaries.
- SongFormer Service:
- Loads the runtime module dynamically from a configured root path.
- Initializes models on first use and executes inference with post-processing.
sequenceDiagram
participant Client as "Client"
participant CRS as "ChordRecognitionService"
participant Sel as "Detector Selector"
participant Det as "Detector (e.g., BTC-SL)"
participant SPF as "SpleeterService"
Client->>CRS : recognize_chords(file_path, detector, chord_dict, use_spleeter)
CRS->>Sel : select_detector(detector, file_size, force)
Sel-->>CRS : selected_detector
alt use_spleeter and available
CRS->>SPF : extract_vocals(file_path)
SPF-->>CRS : vocals_path or error
end
CRS->>Det : recognize_chords(audio_file_to_process, chord_dict)
Det-->>CRS : normalized results
CRS-->>Client : results with metadata
Diagram sources
Section sources
- Detector selection falls back to alternatives when requested detector is unavailable or file size exceeds limits.
- Services catch exceptions and return structured error responses with processing time.
- Availability checks return conservative defaults to keep the system operational.
- Spleeter cleanup ensures temporary files are removed on success or error.
flowchart TD
Req["Request detector"] --> Avail{"Detector available?"}
Avail --> |No| Fallback["Select fallback detector"]
Avail --> |Yes| SizeCheck{"Within size limit?"}
SizeCheck --> |No| Fallback
SizeCheck --> |Yes| Use["Use requested detector"]
Fallback --> Use
Use --> Run["Run inference"]
Run --> Err{"Exception?"}
Err --> |Yes| ErrResp["Return error response"]
Err --> |No| OkResp["Return success response"]
Diagram sources
Section sources
- SpleeterService creates a new Separator instance per request to avoid memory leaks and reduce contention.
- SongFormerService uses thread locks and a context manager to ensure safe, one-time initialization of models.
- Detectors cache availability status to avoid repeated filesystem checks.
- Path management adds model directories to sys.path once during app initialization to streamline imports.
Section sources
- spleeter_service.py:48-70
- songformer_service.py:34-44
- songformer_service.py:85-103
- chord_cnn_lstm_detector.py:32-77
- paths.py:45-62
- Configuration classes define feature toggles (e.g., USE_BTC_SL, USE_BTC_PL) and environment-specific settings.
- SongFormer configuration is loaded from a YAML file describing model dimensions, training parameters, and dataset settings.
- PyTorch availability checks determine CUDA/MPS/CPU devices and expose version/device info for diagnostics.
Section sources
- The system does not implement automated model update mechanisms. Availability checks and path validation are static and rely on the presence of expected files and directories. To update models, administrators should place updated artifacts in the expected paths and restart the service.
[No sources needed since this section summarizes behavior without analyzing specific files]
The following diagram highlights key dependencies among components involved in model loading and initialization.
graph TB
AF["app_factory.py"] --> EXT["extensions.py"]
AF --> CFG["config.py"]
AF --> PTH["utils/paths.py"]
AF --> BDS["services/audio/beat_detection_service.py"]
AF --> CRS["services/audio/chord_recognition_service.py"]
AF --> SFS["services/audio/songformer_service.py"]
CRS --> SPF["services/audio/spleeter_service.py"]
CRS --> CCL["services/detectors/chord_cnn_lstm_detector.py"]
CRS --> BTL["services/detectors/btc_sl_detector.py"]
CRS --> BTP["services/detectors/btc_pl_detector.py"]
BDS --> BTS["beat_transformer_detector.py"]
BDS --> BTL
BDS --> BTP
BDS --> CCL
PTH --> CRS
PTH --> BDS
MUL["utils/model_utils.py"] --> BDS
MUL --> CRS
Diagram sources
- app_factory.py:103-162
- paths.py:45-62
- beat_detection_service.py:20-348
- chord_recognition_service.py:25-322
- btc_sl_detector.py:17-246
- btc_pl_detector.py:17-246
- chord_cnn_lstm_detector.py:17-249
- spleeter_service.py:17-286
- songformer_service.py:21-140
- model_utils.py:12-326
Section sources
- app_factory.py:103-162
- paths.py:45-62
- beat_detection_service.py:20-348
- chord_recognition_service.py:25-322
- Prefer smaller models for short audio clips to reduce latency.
- Use Spleeter only when beneficial; it introduces overhead and requires disk I/O.
- Keep model directories on fast storage to minimize import delays.
- Monitor device utilization (CUDA/MPS/CPU) via availability checks to choose optimal models.
- Avoid repeated model imports by leveraging cached availability and lazy initialization.
[No sources needed since this section provides general guidance]
- Missing models: Use availability utilities to diagnose missing files or dependencies. Verify model directories and required files exist.
- Import failures: Detectors catch ImportError and mark themselves unavailable; confirm environment dependencies (e.g., PyTorch) are installed.
- File size limits: If a detector is unavailable for a given file, the service selects a fallback detector within size limits.
- Spleeter cleanup: On errors, ensure temporary directories are cleaned; the service attempts cleanup automatically.
- Device issues: Check PyTorch availability and device info to confirm CUDA/MPS availability.
Section sources
- model_utils.py:12-326
- chord_cnn_lstm_detector.py:66-76
- spleeter_service.py:160-178
- model_utils.py:141-181
The model loading and initialization system is designed for resilience and flexibility. It leverages the Flask application factory pattern, centralized configuration, and service/container composition to orchestrate multiple detection algorithms. Availability checks, fallback strategies, and careful memory management ensure robust operation even when models are partially missing or underpowered environments are present. Integration with SongFormer and Spleeter is handled through dynamic loading and controlled initialization, while configuration toggles allow operators to adapt features to deployment constraints.
-
Backend Architecture
- Blueprint Organization
- Machine Learning Integration
- Service Layer Architecture
- Backend Architecture
- Error Handling and Logging
- Flask Application Factory
- Frontend Architecture
- Architecture and Design
- Deployment Architecture
- Audio Pipeline
- Audio Playback System
- Audio Processing and Analysis
- Real-time Audio Analysis
- YouTube Integration
- Blueprint Services
- Machine Learning Services
- Backend Services
- External Integrations
- Flask Application Architecture
- Melody Transcription
- Song Segmentation
- Experimental Feature Management
- Experimental Features
- API Integration and Service Layer
-
Component Library and UI System
- Analysis Interface Components
- Chatbot Interface Component
- Chord Analysis Components
- Chord Playback Components
- Common Components
- Component Library and UI System
- Homepage and Landing Components
- Layout and Utility Components
- Lyrics Display Components
- Piano Visualizer Components
- Settings and Configuration Components
- State Management and Data Flow
- Frontend Application
- Next.js Application Architecture
- Beat Detection Models
- Chord Recognition Models
- Adding New Models
- Machine Learning Models
- Model Management
- Model Training and Evaluation