Views
No views yet

1pip install --upgrade pip
2pip install --upgrade transformers accelerateNote: MF processes audio in 30-second windows with a 20-minute total cap per sample. Longer inputs are truncated.
1from transformers import MusicFlamingoForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-2601-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = MusicFlamingoForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {"type": "text", "text": "Describe this track in full detail - tell me the genre, tempo, and key, then dive into the instruments, production style, and overall mood it creates."},
12 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_1.mp3"},
13 ],
14 }
15]
16
17inputs = processor.apply_chat_template(
18 conversation,
19 tokenize=True,
20 add_generation_prompt=True,
21 return_dict=True,
22).to(model.device)
23inputs["input_features"] = inputs["input_features"].to(model.dtype)
24
25outputs = model.generate(**inputs, max_new_tokens=500)
26
27decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
28print(decoded_outputs)1from transformers import MusicFlamingoForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-2601-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = MusicFlamingoForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {
12 "type": "text",
13 "text": "Write a rich caption that blends the technical details (genre, BPM, key, chords, mix) with how the song feels emotionally and dynamically as it unfolds.",
14 },
15 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_1.mp3"},
16 ],
17 },
18 {
19 "role": "assistant",
20 "content": [{"type": "text", "text": "This energetic Eurodance anthem at 150 BPM in E major combines bright synth arpeggios with a punchy four-on-the-floor beat..."}],
21 },
22 {
23 "role": "user",
24 "content": [
25 {"type": "text", "text": "What instruments stand out the most?"},
26 ],
27 },
28]
29
30inputs = processor.apply_chat_template(
31 conversation,
32 tokenize=True,
33 add_generation_prompt=True,
34 return_dict=True,
35).to(model.device)
36inputs["input_features"] = inputs["input_features"].to(model.dtype)
37
38outputs = model.generate(**inputs, max_new_tokens=500)
39
40decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
41print(decoded_outputs)1from transformers import MusicFlamingoForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-2601-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = MusicFlamingoForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {"type": "text", "text": "What is the capital of France?"},
12 ],
13 }
14]
15
16inputs = processor.apply_chat_template(
17 conversation,
18 tokenize=True,
19 add_generation_prompt=True,
20 return_dict=True,
21).to(model.device)
22
23outputs = model.generate(**inputs, max_new_tokens=500)
24
25decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
26print(decoded_outputs)1from transformers import MusicFlamingoForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-2601-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = MusicFlamingoForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_2.mp3"},
12 ],
13 }
14]
15
16inputs = processor.apply_chat_template(
17 conversation,
18 tokenize=True,
19 add_generation_prompt=True,
20 return_dict=True,
21).to(model.device)
22inputs["input_features"] = inputs["input_features"].to(model.dtype)
23
24outputs = model.generate(**inputs, max_new_tokens=500)
25
26decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
27print(decoded_outputs)1from transformers import MusicFlamingoForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-2601-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = MusicFlamingoForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversations = [
8 [
9 {
10 "role": "user",
11 "content": [
12 {"type": "text", "text": "Describe this track in full detail - tell me the genre, tempo, and key, then dive into the instruments, production style, and overall mood it creates."},
13 {
14 "type": "audio",
15 "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_1.mp3",
16 },
17 ],
18 }
19 ],
20 [
21 {
22 "role": "user",
23 "content": [
24 {
25 "type": "text",
26 "text": "Generate a structured lyric sheet from the input music.",
27 },
28 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_2.mp3"},
29 ],
30 }
31 ],
32]
33
34inputs = processor.apply_chat_template(
35 conversations,
36 tokenize=True,
37 add_generation_prompt=True,
38 return_dict=True,
39).to(model.device)
40inputs["input_features"] = inputs["input_features"].to(model.dtype)
41
42outputs = model.generate(**inputs, max_new_tokens=500)
43
44decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
45print(decoded_outputs)1from transformers import MusicFlamingoForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-2601-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = MusicFlamingoForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6model.train()
7
8conversation = [
9 [
10 {
11 "role": "user",
12 "content": [
13 {"type": "text", "text": "Break the track down like a critic - list its tempo, key, and chordal motion, then explain the textures, dynamics, and emotional impact of the performance."},
14 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_1.mp3"},
15 ],
16 },
17 {
18 "role": "assistant",
19 "content": [{"type": "text", "text": "This Eurodance track operates at 150 BPM in E major, with harmonic movement centering on the I-vi-IV-V family. The production features layered synth arpeggios, a four-on-the-floor kick pattern, and a mezzo-soprano lead vocal with bright timbre. Dynamically, the track builds through verses into an anthemic chorus with full synth orchestration and backing vocals, creating an uplifting, euphoric atmosphere characteristic of late 2000s dance-pop."}],
20 }
21 ],
22 [
23 {
24 "role": "user",
25 "content": [
26 {
27 "type": "text",
28 "text": "Describe this song from both a technical and artistic lens: mention tempo, harmony, and instrumentation, but also mood, lyrical themes, and structure.",
29 },
30 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/song_2.mp3"},
31 ],
32 },
33 {
34 "role": "assistant",
35 "content": [{"type": "text", "text": "This electronic pop track combines upbeat production with playful lyrical themes centered around late-night pizza cravings. The structure follows a verse-chorus format with recurring melodic motifs and rhythmic patterns that emphasize the celebratory, lighthearted mood of the piece."}],
36 }
37
38 ]
39]
40
41inputs = processor.apply_chat_template(
42 conversation,
43 tokenize=True,
44 add_generation_prompt=True,
45 return_dict=True,
46 output_labels=True,
47).to(model.device)
48inputs["input_features"] = inputs["input_features"].to(model.dtype)
49
50loss = model(**inputs).loss
51loss.backward()1generate_kwargs = {
2 "max_new_tokens": 256,
3 "do_sample": True,
4 "temperature": 0.7,
5 "top_p": 0.9,
6}
7out = model.generate(**batch, **generate_kwargs)torch.compile, install Flash-Attention and enable it at load time:pip install flash-attn --no-build-isolation1model = MusicFlamingoForConditionalGeneration.from_pretrained(
2 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="flash_attention_2"
3).to(device)torch.compile for significant speed-ups:1import torch
2torch.set_float32_matmul_precision("high")
3
4model.generation_config.cache_implementation = "static"
5model.generation_config.max_new_tokens = 256
6model.forward = torch.compile(model.forward, mode="reduce-overhead", fullgraph=True)torch.compileis not compatible with Flash Attention 2 at the same time.
1model = MusicFlamingoForConditionalGeneration.from_pretrained(
2 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="sdpa"
3).to(device)