forked from facebookresearch/audiocraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Prediction interface for Cog ⚙️ | ||
# https://github.com/replicate/cog/blob/main/docs/python.md | ||
|
||
from cog import BasePredictor, Input, Path | ||
import torchaudio | ||
from audiocraft.models import MusicGen | ||
from audiocraft.data.audio import audio_write | ||
import soundfile as sf | ||
import time | ||
import uuid | ||
|
||
class Predictor(BasePredictor): | ||
def setup(self): | ||
"""Load the model into memory to make running multiple predictions efficient""" | ||
self.model = MusicGen.get_pretrained('large', device='cuda') | ||
self.model.set_generation_params(duration=12) # generate 8 seconds. | ||
# wav = model.generate_unconditional(4) # generates 4 unconditional audio samples | ||
|
||
|
||
self.model_melody = MusicGen.get_pretrained('melody', device='cuda') | ||
self.model_melody.set_generation_params(duration=16) # generate 8 seconds. | ||
# wav = model.generate_unconditional(4) # generates 4 unconditional audio samples | ||
|
||
def predict( | ||
self, | ||
text: str = Input(description="Text prompt", default="Music to watch girls go by"), | ||
# scale: float = Input( | ||
# description="Factor to scale image by", ge=0, le=10, default=1.5 | ||
# ), | ||
) -> Path: | ||
"""Run a single prediction on the model""" | ||
|
||
start = time.time() | ||
wav = self.model.generate([text], progress=True) # generates 3 samples. | ||
wav = wav[0] | ||
end = time.time() | ||
print(f"Generation took {end-start} seconds") | ||
|
||
save_path = f"/tmp/{uuid.uuid4()}" | ||
|
||
# Will save under {idx}.wav, with loudness normalization at -14 db LUFS. | ||
print(f"Saving {save_path}") | ||
audio_write(f'{save_path}', wav.cpu(), self.model.sample_rate, strategy="loudness") | ||
|
||
|
||
save_path = save_path + ".wav" | ||
# return path to wav file | ||
return Path(save_path) | ||
|
||
|