-
-
Notifications
You must be signed in to change notification settings - Fork 47
Backend Services Machine Learning Services Model Management
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
- Appendices
This document describes the model management system for the ChordMiniApp backend. It covers model loading and initialization, configuration file management, parameter validation, availability checking, fallback strategies, error handling, versioning and update procedures, compatibility checks, deployment and monitoring workflows, performance optimization, and integration with different model architectures. The focus is on the chord recognition pipeline and the BTC (Beat-Transformer-Chord) family of models, while also touching on SongFormer integration and Spleeter audio separation.
The model management system spans several layers:
- Application bootstrap and configuration
- Model availability and path utilities
- Detector services for different architectures
- Centralized service orchestrating model selection and fallback
- Blueprints exposing endpoints for model testing and inference
- Configuration files for BTC and student training
- Container orchestration for deployment
graph TB
subgraph "Application"
APP["Flask App<br/>app.py"]
CFG["Config<br/>config.py"]
PATHS["Paths & Checkpoints<br/>utils/paths.py"]
end
subgraph "Model Availability"
AVAIL["Availability Utils<br/>utils/model_utils.py"]
end
subgraph "Detectors"
BTC_SL["BTC-SL Detector<br/>services/detectors/btc_sl_detector.py"]
BTC_PL["BTC-PL Detector<br/>services/detectors/btc_pl_detector.py"]
CNNLSTM["Chord-CNN-LSTM Detector<br/>services/detectors/chord_cnn_lstm_detector.py"]
end
subgraph "Orchestration"
SVC["Chord Recognition Service<br/>services/audio/chord_recognition_service.py"]
ROUTES["Chord Routes<br/>blueprints/chords/routes.py"]
end
subgraph "Audio Processing"
SPLEETER["Spleeter Service<br/>services/audio/spleeter_service.py"]
end
subgraph "External Integrations"
SONGFOR["SongFormer Service<br/>services/audio/songformer_service.py"]
end
subgraph "Configs"
BTC_CFG["btc_config.yaml"]
STUDENT_CFG["student_config.yaml"]
end
APP --> CFG
APP --> PATHS
APP --> AVAIL
APP --> SVC
SVC --> BTC_SL
SVC --> BTC_PL
SVC --> CNNLSTM
SVC --> SPLEETER
SVC --> ROUTES
SONGFOR --> SVC
PATHS --> BTC_SL
PATHS --> BTC_PL
PATHS --> CNNLSTM
BTC_CFG --> BTC_SL
BTC_CFG --> BTC_PL
STUDENT_CFG --> SVC
Diagram sources
- app.py:1-186
- config.py:1-215
- paths.py:1-191
- model_utils.py:1-326
- btc_sl_detector.py:1-246
- btc_pl_detector.py:1-246
- chord_cnn_lstm_detector.py:1-249
- chord_recognition_service.py:1-322
- routes.py:1-440
- spleeter_service.py:1-286
- songformer_service.py:1-140
- btc_config.yaml:1-61
- student_config.yaml:1-94
Section sources
- Application bootstrap and configuration: Initializes environment, loads .env, applies compatibility patches, sets up model paths, and performs deferred availability checks.
- Model availability utilities: Provide checks for Spleeter, Beat-Transformer, Chord-CNN-LSTM, Genius, BTC, PyTorch, and TensorFlow without loading models.
- Detector services: Encapsulate BTC-SL, BTC-PL, and Chord-CNN-LSTM with normalized interfaces, availability checks, and result parsing.
- Centralized service: Orchestrates model selection, file-size-aware fallback, chord dictionary validation, and optional Spleeter separation.
- Routes: Expose endpoints for model testing, model info, and chord recognition with validation and error handling.
- Configuration files: btc_config.yaml defines BTC model parameters and paths; student_config.yaml defines training and runtime parameters for the student model.
Section sources
- app.py:1-186
- model_utils.py:1-326
- btc_sl_detector.py:1-246
- btc_pl_detector.py:1-246
- chord_cnn_lstm_detector.py:1-249
- chord_recognition_service.py:1-322
- routes.py:1-440
- btc_config.yaml:1-61
- student_config.yaml:1-94
The system follows a layered architecture:
- Presentation: Flask blueprints expose endpoints for model testing and inference.
- Orchestration: A centralized service selects the appropriate detector based on availability, file size, and user preferences.
- Detection: Each detector validates its own availability and executes inference, returning normalized results.
- Utilities: Availability checks, path resolution, and logging support the orchestration and detection layers.
- External integrations: SongFormer service wraps an external runtime; Spleeter provides audio separation.
sequenceDiagram
participant Client as "Client"
participant Routes as "Chord Routes"
participant Service as "ChordRecognitionService"
participant Detector as "Detector (BTC/Chord-CNN-LSTM)"
participant Spleeter as "SpleeterService"
Client->>Routes : POST /api/recognize-chords
Routes->>Service : recognize_chords(file_path, detector, chord_dict, force, use_spleeter)
Service->>Service : validate audio file and size
Service->>Service : select_detector(...)
Service->>Detector : is_available()
alt Detector available
Service->>Detector : recognize_chords(file_path, chord_dict)
Detector-->>Service : normalized results
else Use Spleeter
Service->>Spleeter : extract_vocals(file_path)
Spleeter-->>Service : vocals_path
Service->>Detector : recognize_chords(vocals_path, chord_dict)
Detector-->>Service : normalized results
end
Service-->>Routes : result
Routes-->>Client : JSON result
Diagram sources
- routes.py:43-143
- chord_recognition_service.py:173-296
- btc_sl_detector.py:87-169
- btc_pl_detector.py:87-169
- chord_cnn_lstm_detector.py:78-190
- spleeter_service.py:180-220
- Application bootstrap adds model directories to the Python path and defers heavy imports. It performs a global BTC availability check at startup and sets feature flags accordingly.
- Detector services encapsulate initialization and import-time checks. They validate directories, required files, and runtime dependencies before enabling inference.
- SongFormer service dynamically loads the external runtime module and initializes models lazily within a thread-safe context manager.
flowchart TD
Start(["App Startup"]) --> Paths["Add model dirs to sys.path"]
Paths --> Env["Load .env and compat patches"]
Env --> GlobalAvail["Global BTC availability check"]
GlobalAvail --> Detectors["Initialize detector services"]
Detectors --> BTCInit["BTC detectors import checks"]
Detectors --> CNNInit["Chord-CNN-LSTM import checks"]
Detectors --> SpleeterInit["Spleeter availability"]
BTCInit --> Ready(["Ready"])
CNNInit --> Ready
SpleeterInit --> Ready
Diagram sources
- app.py:63-146
- btc_sl_detector.py:32-85
- btc_pl_detector.py:32-85
- chord_cnn_lstm_detector.py:32-76
- songformer_service.py:54-103
Section sources
- btc_config.yaml defines audio processing parameters, experiment hyperparameters, model architecture parameters for BTC, and model paths for BTC-SL and BTC-PL.
- student_config.yaml defines training hyperparameters, model scaling factors, feature parameters aligned with teacher models, data paths, caching, and miscellaneous settings.
flowchart TD
A["btc_config.yaml"] --> B["Audio/Feature Params"]
A --> C["Experiment Params"]
A --> D["Model Params (Transformer)"]
A --> E["Model Paths"]
F["student_config.yaml"] --> G["Training Hyperparameters"]
F --> H["Model Scaling & Heads/Layers"]
F --> I["Feature Params (aligned)"]
F --> J["Data Paths & Cache"]
F --> K["Miscellaneous"]
Diagram sources
Section sources
- Availability utilities check filesystem presence of models, required Python packages, and runtime devices (PyTorch CUDA/MPS).
- Detector services implement availability checks per model, including directory validation and import-time verification.
- The centralized service selects detectors based on availability and file size, with explicit fallback logic preferring larger-capacity models for large files and smaller models for speed/efficiency.
flowchart TD
Req["Request Detector"] --> Avail["Check Availability"]
Avail --> |Unavailable| Fallback["Select Fallback Detector"]
Avail --> |Available| SizeCheck["Check File Size Limits"]
SizeCheck --> |Too Large| Fallback
SizeCheck --> |OK| Use["Use Requested Detector"]
Fallback --> Use
Diagram sources
- model_utils.py:285-326
- btc_sl_detector.py:32-85
- btc_pl_detector.py:32-85
- chord_cnn_lstm_detector.py:32-76
- chord_recognition_service.py:61-172
Section sources
- Detector services catch exceptions during import and inference, returning structured error messages and ensuring cleanup of temporary artifacts.
- Routes wrap service calls with try/catch, logging stack traces in non-production environments and returning standardized JSON responses.
- Availability utilities return safe defaults and log detailed errors for debugging.
Section sources
- btc_sl_detector.py:150-169
- btc_pl_detector.py:150-169
- chord_cnn_lstm_detector.py:172-190
- routes.py:126-143
- model_utils.py:130-138
- Model paths and checkpoints are centralized in path utilities, enabling consistent updates and migrations.
- Configuration files define model parameters and feature alignment, supporting controlled updates.
- Device availability checks (CUDA/MPS) ensure compatibility with the runtime environment.
Section sources
- Local development with Docker Compose exposes the backend on port 8080 and supports mounting model caches.
- Endpoints for testing models and retrieving model info enable monitoring and maintenance workflows.
- SongFormer service integrates an external runtime with environment-driven configuration.
Section sources
- BTC-SL and BTC-PL: Transformer-based models with large vocabulary; integrated via a unified wrapper and normalized output.
- Chord-CNN-LSTM: CNN-LSTM architecture with multiple chord dictionaries; includes mock data generation for testing.
- Spleeter: Optional audio separation service for vocal/accompaniment extraction prior to inference.
- SongFormer: External runtime integration with lazy initialization and environment-driven configuration.
Section sources
- btc_sl_detector.py:87-169
- btc_pl_detector.py:87-169
- chord_cnn_lstm_detector.py:78-190
- spleeter_service.py:17-70
- songformer_service.py:21-103
The system exhibits clear separation of concerns:
- Centralized configuration and path utilities decouple model specifics from orchestration logic.
- Detector services isolate model-specific logic and error handling.
- Routes depend on the centralized service and validators.
- External integrations (SongFormer, Spleeter) are optional and isolated behind availability checks.
graph LR
CFG["config.py"] --> APP["app.py"]
PATHS["utils/paths.py"] --> BTC_SL["btc_sl_detector.py"]
PATHS --> BTC_PL["btc_pl_detector.py"]
PATHS --> CNNLSTM["chord_cnn_lstm_detector.py"]
AVAIL["utils/model_utils.py"] --> SVC["chord_recognition_service.py"]
SVC --> BTC_SL
SVC --> BTC_PL
SVC --> CNNLSTM
SVC --> ROUTES["blueprints/chords/routes.py"]
SPLEETER["spleeter_service.py"] --> SVC
SONGFOR["songformer_service.py"] --> SVC
Diagram sources
- config.py:1-215
- paths.py:1-191
- model_utils.py:1-326
- chord_recognition_service.py:1-322
- routes.py:1-440
- spleeter_service.py:1-286
- songformer_service.py:1-140
Section sources
- config.py:1-215
- paths.py:1-191
- model_utils.py:1-326
- chord_recognition_service.py:1-322
- routes.py:1-440
- spleeter_service.py:1-286
- songformer_service.py:1-140
- Detector selection favors smaller models for small files and larger models for large files to balance latency and accuracy.
- Spleeter separation introduces overhead; it is optional and only used when requested and available.
- Availability checks and lazy initialization reduce cold-start latency.
- Device availability checks (CUDA/MPS) ensure optimal hardware utilization.
[No sources needed since this section provides general guidance]
Common issues and remedies:
- Missing models or dependencies: Use availability endpoints and logs to confirm filesystem paths and imports.
- File size limitations: Reduce file size or choose a detector with a higher size limit.
- Spleeter failures: Verify Spleeter availability and disk space; ensure temporary directories are writable.
- SongFormer runtime errors: Confirm SONGFORMER_ROOT points to a valid runtime directory and required files exist.
- Configuration mismatches: Align feature parameters (hop length, sample rate) between models and configurations.
Section sources
The model management system provides a robust, modular framework for deploying and operating multiple chord recognition models. It emphasizes availability-first design, explicit fallback strategies, and clear separation between configuration, orchestration, and detection. With comprehensive availability checks, structured error handling, and optional integrations (Spleeter, SongFormer), the system supports scalable deployment and maintenance across diverse environments.
[No sources needed since this section summarizes without analyzing specific files]
- btc_config.yaml: Audio/feature parameters, experiment settings, model architecture, and model paths for BTC variants.
- student_config.yaml: Training hyperparameters, model scaling, feature alignment, data paths, caching, and miscellaneous settings.
Section sources
- Docker Compose defines service exposure, environment variables, and optional volume mounts for model caches.
- Requirements pin compatible versions for NumPy, SciPy, TensorFlow, PyTorch, and other dependencies.
Section sources
-
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