Views
No views yet
1pip install --upgrade pip
2pip install --upgrade git+https://github.com/huggingface/transformers accelerateNote: AF3 processes audio in 30-second windows with a 10-minute total cap per sample. Longer inputs are truncated.
1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/audio-flamingo-3-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {"type": "text", "text": "Transcribe the input speech."},
12 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/WhDJDIviAOg_120_10.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)
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 AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/audio-flamingo-3-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {
12 "type": "text",
13 "text": "Instruction: How does the tone of female speech change throughout the audio? Choose the correct option among the options below: (A) Sad to happy (B) Happy to sad (C) Neutral to happy (D) Happy to neutral.",
14 },
15 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/000000786159.31.wav"},
16 ],
17 },
18 {
19 "role": "assistant",
20 "content": [{"type": "text", "text": "(A) Sad to happy"}],
21 },
22 {
23 "role": "user",
24 "content": [
25 {"type": "text", "text": "Why do you think so?"},
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)
36
37outputs = model.generate(**inputs, max_new_tokens=500)
38
39decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
40print(decoded_outputs)1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/audio-flamingo-3-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversations = [
8 [
9 {
10 "role": "user",
11 "content": [
12 {"type": "text", "text": "Transcribe the input speech."},
13 {
14 "type": "audio",
15 "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/t_837b89f2-26aa-4ee2-bdf6-f73f0dd59b26.wav",
16 },
17 ],
18 }
19 ],
20 [
21 {
22 "role": "user",
23 "content": [
24 {
25 "type": "text",
26 "text": "This track feels really peaceful and introspective. What elements make it feel so calming and meditative?",
27 },
28 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/FPSbCAANfbJLVSwD.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)
40
41outputs = model.generate(**inputs, max_new_tokens=500)
42
43decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
44print(decoded_outputs)1# text-only
2conv = [{"role": "user", "content": [{"type": "text", "text": "What is the capital of France?"}]}]
3batch = processor.apply_chat_template(conv, tokenize=True, add_generation_prompt=True, return_dict=True).to(device)
4print(processor.batch_decode(model.generate(**batch)[:, batch["input_ids"].shape[1]:], skip_special_tokens=True)[0])
5
6# audio-only
7conv = [{"role": "user", "content": [{"type": "audio", "path": "https://.../sample.wav"}]}]
8batch = processor.apply_chat_template(conv, tokenize=True, add_generation_prompt=True, return_dict=True).to(device)
9print(processor.batch_decode(model.generate(**batch)[:, batch["input_ids"].shape[1]:], skip_special_tokens=True)[0])The spoken content of the audio is "<text>".. Passing strip_prefix=True removes that canned prefix and the surrounding quotes so you only keep the transcription.1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/audio-flamingo-3-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7inputs = processor.apply_transcription_request(audio="https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/t_837b89f2-26aa-4ee2-bdf6-f73f0dd59b26.wav").to(model.device)
8
9outputs = model.generate(**inputs, max_new_tokens=500)
10decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True, strip_prefix=True)
11
12print(decoded_outputs)1import os
2
3import torch
4from huggingface_hub import snapshot_download
5from peft import PeftModel
6
7from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
8
9
10model_id = "nvidia/audio-flamingo-3-hf"
11local_id = snapshot_download(model_id)
12
13processor = AutoProcessor.from_pretrained(local_id)
14model = AudioFlamingo3ForConditionalGeneration.from_pretrained(local_id, device_map="auto")
15
16non_lora_path = os.path.join(local_id, "think", "non_lora_trainables.bin")
17non_lora_trainables = torch.load(non_lora_path)
18model.load_state_dict(non_lora_trainables, strict=False)
19
20model = PeftModel.from_pretrained(model, local_id, subfolder="think")
21
22conversation = [
23 {
24 "role": "user",
25 "content": [
26 {
27 "type": "text",
28 "text": "Generate a detailed caption for the input audio, describing all notable speech, sound, and musical events comprehensively. In the caption, transcribe all spoken content by all speakers in the audio precisely.\nPlease think and reason about the input music before you respond.",
29 },
30 {
31 "type": "audio",
32 "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/videoplayback_superman.wav",
33 },
34 ],
35 }
36]
37
38inputs = processor.apply_chat_template(
39 conversation,
40 tokenize=True,
41 add_generation_prompt=True,
42 return_dict=True,
43).to(model.device)
44
45outputs = model.generate(**inputs, max_new_tokens=1024)
46
47decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1] :], skip_special_tokens=True)
48print(decoded_outputs)1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/audio-flamingo-3-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6model.train()
7
8conversation = [
9 [
10 {
11 "role": "user",
12 "content": [
13 {"type": "text", "text": "Transcribe the input speech."},
14 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/WhDJDIviAOg_120_10.mp3"},
15 ],
16 },
17 {
18 "role": "assistant",
19 "content": [{"type": "text", "text": "The transcription of the audio is 'summer follows spring the days grow longer and the nights are warm'."}],
20 }
21 ],
22 [
23 {
24 "role": "user",
25 "content": [
26 {
27 "type": "text",
28 "text": "This track feels really peaceful and introspective. What elements make it feel so calming and meditative?",
29 },
30 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/AudioSkills/resolve/main/assets/FPSbCAANfbJLVSwD.mp3"},
31 ],
32 },
33 {
34 "role": "assistant",
35 "content": [{"type": "text", "text": "The transcription of the audio is 'some transcription of the audio'."}],
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)
48
49loss = model(**inputs).loss
50loss.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 = AudioFlamingo3ForConditionalGeneration.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 = AudioFlamingo3ForConditionalGeneration.from_pretrained(
2 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="sdpa"
3).to(device)
