sanchit-gandhi HF staff commited on
Commit
a05d7f5
1 Parent(s): fc81d8b

Update README.md with HF implementation

Browse files
Files changed (1) hide show
  1. README.md +42 -26
README.md CHANGED
@@ -6,9 +6,9 @@ license: cc-by-nc-4.0
6
 
7
  # MusicGen - Small - 300M
8
 
9
- Audiocraft provides the code and models for MusicGen, a simple and controllable model for music generation.
10
- MusicGen is a single stage auto-regressive Transformer model trained over a 32kHz EnCodec tokenizer with 4 codebooks sampled at 50 Hz.
11
- Unlike existing methods like MusicLM, MusicGen doesn't not require a self-supervised semantic representation, and it generates all 4 codebooks in one pass.
12
  By introducing a small delay between the codebooks, we show we can predict them in parallel, thus having only 50 auto-regressive steps per second of audio.
13
 
14
  MusicGen was published in [Simple and Controllable Music Generation](https://arxiv.org/abs/2306.05284) by *Jade Copet, Felix Kreuk, Itai Gat, Tal Remez, David Kant, Gabriel Synnaeve, Yossi Adi, Alexandre Défossez*.
@@ -21,48 +21,64 @@ Four checkpoints are released:
21
 
22
  ## Example
23
 
24
- Try out MusicGen yourself!
25
 
26
- - <a target="_blank" href="https://colab.research.google.com/drive/1fxGqfg96RBUvGxZ1XXN07s3DthrKUl4-?usp=sharing">
27
- <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
28
  </a>
29
 
30
- - <a target="_blank" href="https://huggingface.co/spaces/facebook/MusicGen">
31
- <img src="https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-sm.svg" alt="Open in HugginFace"/>
32
  </a>
33
 
34
- - You can run MusicGen locally as well:
35
 
36
- 1. First install the [`audiocraft` library](https://github.com/facebookresearch/audiocraft)
37
- ```
38
- pip install git+https://github.com/facebookresearch/audiocraft.git
39
- ```
40
 
41
- 2. Make sure to have [`ffmpeg`](https://ffmpeg.org/download.html) installed:
42
  ```
43
- apt get install ffmpeg
44
  ```
45
 
46
- 3. Run the following Python code:
47
 
48
  ```py
49
- import torchaudio
50
 
51
- from audiocraft.models import MusicGen
52
- from audiocraft.data.audio import audio_write
53
 
54
- model = MusicGen.get_pretrained('small')
55
- model.set_generation_params(duration=8) # generate 8 seconds.
56
 
57
- descriptions = ['happy rock', 'energetic EDM', 'sad jazz']
 
 
 
 
58
 
59
- wav = model.generate(descriptions) # generates 3 samples.
 
 
 
 
 
 
60
 
61
- for idx, one_wav in enumerate(wav):
62
- # Will save under {idx}.wav, with loudness normalization at -14 db LUFS.
63
- audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness")
64
  ```
65
 
 
 
 
 
 
 
 
 
 
 
 
66
  ## Model details
67
 
68
  **Organization developing the model:** The FAIR team of Meta AI.
 
6
 
7
  # MusicGen - Small - 300M
8
 
9
+ MusicGen is a text-to-music model capable of genreating high-quality music samples conditioned on text descriptions or audio prompts.
10
+ It is is a single stage auto-regressive Transformer model trained over a 32kHz EnCodec tokenizer with 4 codebooks sampled at 50 Hz.
11
+ Unlike existing methods, like MusicLM, MusicGen doesn't not require a self-supervised semantic representation, and it generates all 4 codebooks in one pass.
12
  By introducing a small delay between the codebooks, we show we can predict them in parallel, thus having only 50 auto-regressive steps per second of audio.
13
 
14
  MusicGen was published in [Simple and Controllable Music Generation](https://arxiv.org/abs/2306.05284) by *Jade Copet, Felix Kreuk, Itai Gat, Tal Remez, David Kant, Gabriel Synnaeve, Yossi Adi, Alexandre Défossez*.
 
21
 
22
  ## Example
23
 
24
+ Try out MusicGen yourself!
25
 
26
+ <a target="_blank" href="https://colab.research.google.com/github/sanchit-gandhi/notebooks/blob/main/MusicGen.ipynb">
27
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
28
  </a>
29
 
30
+ <a target="_blank" href="https://huggingface.co/spaces/facebook/MusicGen">
31
+ <img src="https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-sm.svg" alt="Open in HuggingFace"/>
32
  </a>
33
 
34
+ ### Usage
35
 
36
+ You can run MusicGen locally with the 🤗 Transformers library from version 4.31.0 onwards.
37
+
38
+ 1. First install the 🤗 [Transformers library](https://github.com/huggingface/transformers) from main:
 
39
 
 
40
  ```
41
+ pip install git+https://github.com/huggingface/transformers.git
42
  ```
43
 
44
+ 2. Run the following Python code to generate text-conditional audio samples:
45
 
46
  ```py
47
+ from transformers import AutoProcessor, MusicgenForConditionalGeneration
48
 
 
 
49
 
50
+ processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
51
+ model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
52
 
53
+ inputs = processor(
54
+ text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"],
55
+ padding=True,
56
+ return_tensors="pt",
57
+ )
58
 
59
+ audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
60
+ ```
61
+
62
+ 3. Listen to the audio samples either in an ipynb notebook:
63
+
64
+ ```py
65
+ from IPython.display import Audio
66
 
67
+ sampling_rate = model.config.audio_encoder.sampling_rate
68
+ Audio(audio_values[0].numpy(), rate=sampling_rate)
 
69
  ```
70
 
71
+ Or save them as a `.wav` file using a third-party library, e.g. `scipy`:
72
+
73
+ ```py
74
+ import scipy
75
+
76
+ sampling_rate = model.config.audio_encoder.sampling_rate
77
+ scipy.io.wavfile.write("musicgen_out.wav", rate=sampling_rate, data=audio_values[0, 0].numpy())
78
+ ```
79
+
80
+ For more details on using the MusicGen model for inference using the 🤗 Transformers library, refer to the [MusicGen docs](https://huggingface.co/docs/transformers/model_doc/musicgen).
81
+
82
  ## Model details
83
 
84
  **Organization developing the model:** The FAIR team of Meta AI.