Any-to-Any
Transformers
Safetensors
lfm3_audio
automatic-speech-recognition
liquid
lfm3
audio
speech
edge
conversational
multilingual
custom_code
Instructions to use eustlb/LFM3-Audio-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use eustlb/LFM3-Audio-2B with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForMultimodalLM model = AutoModelForMultimodalLM.from_pretrained("eustlb/LFM3-Audio-2B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Model card: Transformers usage section
Browse files
README.md
CHANGED
|
@@ -573,6 +573,215 @@ sf.write("reply.wav", waveform.float().cpu().numpy(), 24_000)
|
|
| 573 |
|
| 574 |
</details>
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
## Fine-tuning
|
| 577 |
<hr style="margin: -0.7em 0 1.2em 0; border: 0; border-top: 1px solid rgba(127, 127, 127, 0.3);" />
|
| 578 |
|
|
|
|
| 573 |
|
| 574 |
</details>
|
| 575 |
|
| 576 |
+
### Transformers usage 🤗
|
| 577 |
+
|
| 578 |
+
LFM3-Audio is supported natively in 🤗 Transformers, without `trust_remote_code`. Install 🤗 Transformers from source:
|
| 579 |
+
|
| 580 |
+
```sh
|
| 581 |
+
pip install git+https://github.com/huggingface/transformers
|
| 582 |
+
```
|
| 583 |
+
|
| 584 |
+
<details>
|
| 585 |
+
<summary>Transcription</summary>
|
| 586 |
+
|
| 587 |
+
```python
|
| 588 |
+
from transformers import AutoModelForMultimodalLM, AutoProcessor
|
| 589 |
+
|
| 590 |
+
model_id = "LiquidAI/LFM3-Audio-2B"
|
| 591 |
+
model = AutoModelForMultimodalLM.from_pretrained(model_id, device_map="auto")
|
| 592 |
+
processor = AutoProcessor.from_pretrained(model_id, device_map="auto")
|
| 593 |
+
|
| 594 |
+
audio_url = (
|
| 595 |
+
"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/librispeech_mr_quilter.wav"
|
| 596 |
+
)
|
| 597 |
+
conversation = [
|
| 598 |
+
{"role": "system", "content": "Perform ASR in English."},
|
| 599 |
+
{"role": "user", "content": [{"type": "audio", "url": audio_url}]},
|
| 600 |
+
]
|
| 601 |
+
inputs = processor.apply_chat_template(
|
| 602 |
+
conversation,
|
| 603 |
+
add_generation_prompt=True,
|
| 604 |
+
tokenize=True,
|
| 605 |
+
return_dict=True,
|
| 606 |
+
return_tensors="pt",
|
| 607 |
+
).to(model.device, dtype=model.dtype)
|
| 608 |
+
|
| 609 |
+
outputs = model.generate(**inputs, max_new_tokens=128)
|
| 610 |
+
transcription = processor.decode(outputs.sequences[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
|
| 611 |
+
print(transcription)
|
| 612 |
+
```
|
| 613 |
+
|
| 614 |
+
</details>
|
| 615 |
+
|
| 616 |
+
<details>
|
| 617 |
+
<summary>Text to speech</summary>
|
| 618 |
+
|
| 619 |
+
```python
|
| 620 |
+
from transformers import AutoModelForMultimodalLM, AutoProcessor
|
| 621 |
+
|
| 622 |
+
model_id = "LiquidAI/LFM3-Audio-2B"
|
| 623 |
+
model = AutoModelForMultimodalLM.from_pretrained(model_id, device_map="auto")
|
| 624 |
+
processor = AutoProcessor.from_pretrained(model_id, device_map="auto")
|
| 625 |
+
|
| 626 |
+
conversation = [
|
| 627 |
+
{"role": "system", "content": "Perform TTS in American English using a female voice."},
|
| 628 |
+
{"role": "user", "content": "The quick brown fox jumps over the lazy dog."},
|
| 629 |
+
]
|
| 630 |
+
# the answer opens with <|audio_start|>: speech until an end-of-audio frame
|
| 631 |
+
inputs = processor.apply_chat_template(
|
| 632 |
+
conversation,
|
| 633 |
+
add_generation_prompt=True,
|
| 634 |
+
generation_mode="audio",
|
| 635 |
+
tokenize=True,
|
| 636 |
+
return_dict=True,
|
| 637 |
+
return_tensors="pt",
|
| 638 |
+
).to(model.device, dtype=model.dtype)
|
| 639 |
+
|
| 640 |
+
outputs = model.generate(**inputs, max_new_tokens=256)
|
| 641 |
+
audio = processor.decode(outputs.audio_sequences, outputs.audio_sequences_mask)
|
| 642 |
+
processor.save_audio(audio, "tts.wav")
|
| 643 |
+
```
|
| 644 |
+
|
| 645 |
+
</details>
|
| 646 |
+
|
| 647 |
+
<details>
|
| 648 |
+
<summary>Speech conversation</summary>
|
| 649 |
+
|
| 650 |
+
The answer interleaves text and speech; the text is the transcript of the speech.
|
| 651 |
+
|
| 652 |
+
```python
|
| 653 |
+
from transformers import AutoModelForMultimodalLM, AutoProcessor
|
| 654 |
+
|
| 655 |
+
model_id = "LiquidAI/LFM3-Audio-2B"
|
| 656 |
+
model = AutoModelForMultimodalLM.from_pretrained(model_id, device_map="auto")
|
| 657 |
+
processor = AutoProcessor.from_pretrained(model_id, device_map="auto")
|
| 658 |
+
|
| 659 |
+
audio_url = (
|
| 660 |
+
"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/librispeech_mr_quilter.wav"
|
| 661 |
+
)
|
| 662 |
+
conversation = [
|
| 663 |
+
{"role": "system", "content": "Respond with interleaved text and audio."},
|
| 664 |
+
{"role": "user", "content": [{"type": "audio", "url": audio_url}]},
|
| 665 |
+
]
|
| 666 |
+
# the answer opens with <|mixed_start|>: blocks of text, then of speech
|
| 667 |
+
inputs = processor.apply_chat_template(
|
| 668 |
+
conversation,
|
| 669 |
+
add_generation_prompt=True,
|
| 670 |
+
generation_mode="interleaved",
|
| 671 |
+
tokenize=True,
|
| 672 |
+
return_dict=True,
|
| 673 |
+
return_tensors="pt",
|
| 674 |
+
).to(model.device, dtype=model.dtype)
|
| 675 |
+
|
| 676 |
+
outputs = model.generate(**inputs, max_new_tokens=1024)
|
| 677 |
+
text = processor.decode(outputs.sequences[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
|
| 678 |
+
print(text)
|
| 679 |
+
audio = processor.decode(outputs.audio_sequences, outputs.audio_sequences_mask)
|
| 680 |
+
processor.save_audio(audio, "answer.wav")
|
| 681 |
+
```
|
| 682 |
+
|
| 683 |
+
</details>
|
| 684 |
+
|
| 685 |
+
<details>
|
| 686 |
+
<summary>Batching</summary>
|
| 687 |
+
|
| 688 |
+
A batch can mix outputs: pass one `generation_mode` per conversation.
|
| 689 |
+
|
| 690 |
+
```python
|
| 691 |
+
from transformers import AutoModelForMultimodalLM, AutoProcessor
|
| 692 |
+
|
| 693 |
+
model_id = "LiquidAI/LFM3-Audio-2B"
|
| 694 |
+
model = AutoModelForMultimodalLM.from_pretrained(model_id, device_map="auto")
|
| 695 |
+
processor = AutoProcessor.from_pretrained(model_id, device_map="auto")
|
| 696 |
+
|
| 697 |
+
audio_url = (
|
| 698 |
+
"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/librispeech_mr_quilter.wav"
|
| 699 |
+
)
|
| 700 |
+
transcribe = [
|
| 701 |
+
{"role": "system", "content": "Perform ASR in English."},
|
| 702 |
+
{"role": "user", "content": [{"type": "audio", "url": audio_url}]},
|
| 703 |
+
]
|
| 704 |
+
speak = [
|
| 705 |
+
{"role": "system", "content": "Perform TTS in American English using a female voice."},
|
| 706 |
+
{"role": "user", "content": "Hello there!"},
|
| 707 |
+
]
|
| 708 |
+
inputs = processor.apply_chat_template(
|
| 709 |
+
[transcribe, speak],
|
| 710 |
+
add_generation_prompt=True,
|
| 711 |
+
generation_mode=["text", "audio"],
|
| 712 |
+
tokenize=True,
|
| 713 |
+
return_dict=True,
|
| 714 |
+
return_tensors="pt",
|
| 715 |
+
).to(model.device, dtype=model.dtype)
|
| 716 |
+
|
| 717 |
+
outputs = model.generate(**inputs, max_new_tokens=128)
|
| 718 |
+
transcription = processor.decode(outputs.sequences[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
|
| 719 |
+
print(transcription)
|
| 720 |
+
# one waveform per speech segment: only the second row speaks
|
| 721 |
+
audio = processor.decode(outputs.audio_sequences, outputs.audio_sequences_mask)
|
| 722 |
+
processor.save_audio(audio, ["batched_speech.wav"])
|
| 723 |
+
```
|
| 724 |
+
|
| 725 |
+
</details>
|
| 726 |
+
|
| 727 |
+
<details>
|
| 728 |
+
<summary>Live transcription</summary>
|
| 729 |
+
|
| 730 |
+
Pass `input_features` as an iterator to transcribe a stream: each item is the processor's output on a 0.96 s chunk,
|
| 731 |
+
and the text of each chunk goes to the streamer as it is written.
|
| 732 |
+
|
| 733 |
+
```python
|
| 734 |
+
import itertools
|
| 735 |
+
|
| 736 |
+
from transformers import AutoModelForMultimodalLM, AutoProcessor, TextStreamer
|
| 737 |
+
from transformers.audio_utils import load_audio
|
| 738 |
+
|
| 739 |
+
model_id = "LiquidAI/LFM3-Audio-2B"
|
| 740 |
+
model = AutoModelForMultimodalLM.from_pretrained(model_id, device_map="auto")
|
| 741 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 742 |
+
|
| 743 |
+
audio = load_audio(
|
| 744 |
+
"https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/dude_where_is_my_car.wav",
|
| 745 |
+
sampling_rate=16000,
|
| 746 |
+
)
|
| 747 |
+
window, hop = processor.num_samples_per_audio_chunk, processor.audio_chunk_hop_length
|
| 748 |
+
|
| 749 |
+
|
| 750 |
+
def input_features_generator():
|
| 751 |
+
# a microphone would yield its samples as they come; consecutive windows overlap by `window - hop` samples
|
| 752 |
+
starts = range(0, len(audio) - (window - hop), hop)
|
| 753 |
+
for start in starts:
|
| 754 |
+
yield processor(
|
| 755 |
+
audio=audio[start : start + window],
|
| 756 |
+
sampling_rate=16000,
|
| 757 |
+
is_last_audio_chunk=start == starts[-1],
|
| 758 |
+
return_tensors="pt",
|
| 759 |
+
).to(model.device, dtype=model.dtype)
|
| 760 |
+
|
| 761 |
+
|
| 762 |
+
prompt = processor.apply_chat_template(
|
| 763 |
+
[{"role": "system", "content": "Perform live ASR."}],
|
| 764 |
+
add_generation_prompt=True,
|
| 765 |
+
tokenize=True,
|
| 766 |
+
return_dict=True,
|
| 767 |
+
return_tensors="pt",
|
| 768 |
+
).to(model.device)
|
| 769 |
+
streamer = TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 770 |
+
|
| 771 |
+
# one context holds at most 30 s of audio, as in training: past it, a new context continues the stream
|
| 772 |
+
chunks = input_features_generator()
|
| 773 |
+
caches = {}
|
| 774 |
+
while segment := list(itertools.islice(chunks, processor.max_audio_chunks_per_context)):
|
| 775 |
+
outputs = model.generate(
|
| 776 |
+
**prompt, input_features=iter(segment), streamer=streamer, return_dict_in_generate=True, **caches
|
| 777 |
+
)
|
| 778 |
+
caches = {"padding_cache": outputs.padding_cache, "encoder_cache": outputs.encoder_cache}
|
| 779 |
+
```
|
| 780 |
+
|
| 781 |
+
</details>
|
| 782 |
+
|
| 783 |
+
See the [Transformers documentation](https://huggingface.co/docs/transformers/main/en/model_doc/lfm3_audio) for more.
|
| 784 |
+
|
| 785 |
## Fine-tuning
|
| 786 |
<hr style="margin: -0.7em 0 1.2em 0; border: 0; border-top: 1px solid rgba(127, 127, 127, 0.3);" />
|
| 787 |
|