File size: 8,202 Bytes
aed1d05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Simple API for MIDI Generation

Provides a clean interface for DAW integration and batch generation.
"""

import json
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Optional, Union

import torch

from modelw.tokenizer import MIDITokenizer
from modelw.model import MIDITransformer, MIDITransformerConfig
from modelw.generate import MIDIGenerator, GenerationConfig


@dataclass 
class MIDIPrompt:
    """Prompt specification for MIDI generation."""
    
    tempo: int = 120
    instrument: str = "piano"
    mood: str = "happy"
    
    # Optional fine-grained control
    time_signature: str = "4/4"
    key: Optional[str] = None
    duration_bars: int = 16


class ModelW:
    """
    Main API for MODEL-W MIDI generation.
    
    Example:
        model = ModelW.load("./checkpoints")
        midi = model.generate(tempo=120, instrument="piano", mood="happy")
        midi.write("output.mid")
    """
    
    def __init__(
        self,
        model: MIDITransformer,
        tokenizer: MIDITokenizer,
        device: str = "auto",
        default_config: Optional[GenerationConfig] = None,
    ):
        if device == "auto":
            device = "cuda" if torch.cuda.is_available() else "cpu"
        
        self.device = device
        self.model = model.to(device)
        self.tokenizer = tokenizer
        self.config = default_config or GenerationConfig()
        self._generator = MIDIGenerator(model, tokenizer, self.config, device)
    
    @classmethod
    def load(
        cls,
        checkpoint_path: Union[str, Path],
        device: str = "auto",
    ) -> "ModelW":
        """
        Load a trained model from checkpoint.
        
        Args:
            checkpoint_path: Path to checkpoint directory or .pt file
            device: Device to load model on ("auto", "cuda", "cpu")
            
        Returns:
            ModelW instance ready for generation
        """
        checkpoint_path = Path(checkpoint_path)
        
        # Handle directory or file
        if checkpoint_path.is_dir():
            model_path = checkpoint_path / "best_model.pt"
            tokenizer_path = checkpoint_path / "tokenizer"
        else:
            model_path = checkpoint_path
            tokenizer_path = checkpoint_path.parent / "tokenizer"
        
        # Load tokenizer
        tokenizer = MIDITokenizer.load(tokenizer_path)
        
        # Load model
        if device == "auto":
            device = "cuda" if torch.cuda.is_available() else "cpu"
        
        checkpoint = torch.load(model_path, map_location=device)
        
        config_dict = checkpoint.get("config", {})
        if isinstance(config_dict, MIDITransformerConfig):
            config = config_dict
        else:
            config = MIDITransformerConfig(**config_dict)
        config.vocab_size = tokenizer.vocab_size
        
        model = MIDITransformer(config)
        model.load_state_dict(checkpoint["model_state_dict"])
        
        return cls(model, tokenizer, device)
    
    def generate(
        self,
        tempo: int = 120,
        instrument: str = "piano",
        mood: str = "happy",
        max_length: int = 1024,
        temperature: float = 0.9,
        top_p: float = 0.92,
        output_path: Optional[str] = None,
    ):
        """
        Generate a single MIDI file.
        
        Args:
            tempo: BPM (40-240)
            instrument: Instrument type (piano, guitar, bass, strings, etc.)
            mood: Mood/emotion (happy, sad, energetic, calm, etc.)
            max_length: Maximum sequence length
            temperature: Sampling temperature (higher = more random)
            top_p: Nucleus sampling threshold
            output_path: Optional path to save MIDI file
            
        Returns:
            pretty_midi.PrettyMIDI object
        """
        # Update config
        self.config.max_length = max_length
        self.config.temperature = temperature
        self.config.top_p = top_p
        
        # Generate
        results = self._generator.generate_batch([
            {"tempo": tempo, "instrument": instrument, "mood": mood}
        ], show_progress=False)
        
        # Decode
        midi = self.tokenizer.decode(results[0]["tokens"], output_path)
        
        return midi
    
    def generate_batch(
        self,
        prompts: list[dict],
        output_dir: Optional[str] = None,
        **kwargs,
    ) -> list:
        """
        Generate multiple MIDI files.
        
        Args:
            prompts: List of prompt dicts with tempo, instrument, mood
            output_dir: Directory to save MIDI files
            **kwargs: Generation parameters
            
        Returns:
            List of pretty_midi.PrettyMIDI objects
        """
        # Update config
        for k, v in kwargs.items():
            if hasattr(self.config, k):
                setattr(self.config, k, v)
        
        # Generate
        results = self._generator.generate_batch(prompts)
        
        # Decode
        midis = []
        output_path = Path(output_dir) if output_dir else None
        
        for i, result in enumerate(results):
            save_path = None
            if output_path:
                output_path.mkdir(parents=True, exist_ok=True)
                save_path = output_path / f"generated_{i:04d}.mid"
            
            midi = self.tokenizer.decode(result["tokens"], save_path)
            midis.append(midi)
        
        return midis
    
    def generate_dataset(
        self,
        num_samples: int,
        output_dir: str,
        **kwargs,
    ) -> dict:
        """
        Generate a large dataset of MIDI files.
        
        Args:
            num_samples: Number of samples to generate
            output_dir: Output directory
            **kwargs: Generation parameters
            
        Returns:
            Generation statistics
        """
        return self._generator.generate_dataset(
            num_samples=num_samples,
            output_dir=output_dir,
            **kwargs,
        )
    
    @property
    def available_instruments(self) -> list[str]:
        """List of available instrument types."""
        return [
            t[6:-1].lower() for t in self.tokenizer.instrument_tokens
        ]
    
    @property
    def available_moods(self) -> list[str]:
        """List of available mood types."""
        return [
            t[6:-1].lower() for t in self.tokenizer.mood_tokens
        ]
    
    def __repr__(self) -> str:
        num_params = self.model.get_num_params()
        return f"ModelW(params={num_params/1e6:.1f}M, device={self.device})"


# Simple functional API
_default_model: Optional[ModelW] = None


def load_model(checkpoint_path: str, device: str = "auto") -> ModelW:
    """Load a MODEL-W checkpoint."""
    global _default_model
    _default_model = ModelW.load(checkpoint_path, device)
    return _default_model


def generate(
    tempo: int = 120,
    instrument: str = "piano", 
    mood: str = "happy",
    output_path: Optional[str] = None,
    **kwargs,
):
    """
    Generate MIDI with the default loaded model.
    
    Must call load_model() first.
    """
    if _default_model is None:
        raise RuntimeError("No model loaded. Call load_model() first.")
    
    return _default_model.generate(
        tempo=tempo,
        instrument=instrument,
        mood=mood,
        output_path=output_path,
        **kwargs,
    )


if __name__ == "__main__":
    # Quick test
    import fire
    
    def main(
        checkpoint: str,
        tempo: int = 120,
        instrument: str = "piano",
        mood: str = "happy",
        output: str = "output.mid",
    ):
        """Generate MIDI from command line."""
        model = ModelW.load(checkpoint)
        print(f"Loaded {model}")
        
        print(f"\nGenerating: tempo={tempo}, instrument={instrument}, mood={mood}")
        midi = model.generate(tempo, instrument, mood, output_path=output)
        
        print(f"Saved to {output}")
        print(f"Notes: {len(midi.instruments[0].notes)}")
        print(f"Duration: {midi.get_end_time():.1f}s")
    
    fire.Fire(main)