From 66d95413f5f2c496b6d73229709b867b72eab989 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Mon, 23 Oct 2023 15:04:07 -0600 Subject: [PATCH 01/17] Add audiogen extension --- extensions/labgraph_audiogen/README.md | 3 +++ .../labgraph_audiogen/labgraph_audiogen/__init__.py | 0 extensions/labgraph_audiogen/labgraph_audiogen/main.py | 3 +++ extensions/labgraph_audiogen/setup.py | 9 +++++++++ 4 files changed, 15 insertions(+) create mode 100644 extensions/labgraph_audiogen/README.md create mode 100644 extensions/labgraph_audiogen/labgraph_audiogen/__init__.py create mode 100644 extensions/labgraph_audiogen/labgraph_audiogen/main.py create mode 100644 extensions/labgraph_audiogen/setup.py diff --git a/extensions/labgraph_audiogen/README.md b/extensions/labgraph_audiogen/README.md new file mode 100644 index 000000000..7104035e0 --- /dev/null +++ b/extensions/labgraph_audiogen/README.md @@ -0,0 +1,3 @@ +# LabGraph AudioGen Extension + +A powerful Python command-line tool for Windows. This tool facilitates the creation, processing, and autocompletion high-quality audio and music, using the AudioCraft models, AudioGen, and MusicGen. diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/__init__.py b/extensions/labgraph_audiogen/labgraph_audiogen/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py new file mode 100644 index 000000000..ce6c35773 --- /dev/null +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -0,0 +1,3 @@ +import sys + +print(sys.argv) \ No newline at end of file diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py new file mode 100644 index 000000000..7e8e4354e --- /dev/null +++ b/extensions/labgraph_audiogen/setup.py @@ -0,0 +1,9 @@ +from setuptools import setup, find_packages + + +setup( + name="labgraph_audiogen", + version="0.0.1", + description="A command-line interface for the usage of AudioCraft for labgraph", + long_description="A command-line interface to facilitate the usage of the models of Audiocraft to generate and process audio on labgraph", +) \ No newline at end of file From 21baef47d8f866c0f72cef22319107e10a500bc9 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Fri, 27 Oct 2023 13:22:08 -0600 Subject: [PATCH 02/17] feat(audiocraft CLI): cli command now receives arguments to test --- extensions/labgraph_audiogen/labgraph_audiogen/main.py | 7 ++++++- extensions/labgraph_audiogen/setup.py | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index ce6c35773..3770ea8cc 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -1,3 +1,8 @@ import sys -print(sys.argv) \ No newline at end of file + +def main(): + print("Hello World from labgraph_audiogen!") + args = sys.argv + if args: + print(f"Args are: {' '.join(args[1:])}") \ No newline at end of file diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index 7e8e4354e..c460fb3cf 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup, find_packages +from setuptools import setup setup( @@ -6,4 +6,12 @@ version="0.0.1", description="A command-line interface for the usage of AudioCraft for labgraph", long_description="A command-line interface to facilitate the usage of the models of Audiocraft to generate and process audio on labgraph", + packages=["labgraph_audiogen"], + install_requires=[ + "Click", + ], + entry_points=""" + [console_scripts] + labgraph_audiogen=labgraph_audiogen.main:main + """ ) \ No newline at end of file From d23074f8d2437daeef1c2273da362f8d4764c083 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Tue, 31 Oct 2023 10:44:58 -0600 Subject: [PATCH 03/17] enhance(audiocraft CLI): cli command now receives arguments to test and generates music --- .../labgraph_audiogen/main.py | 47 +++++++++++++++++-- extensions/labgraph_audiogen/setup.py | 3 ++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index 3770ea8cc..c84c68e0e 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -1,8 +1,45 @@ -import sys +import click -def main(): +def generate_text_music(text): + from audiocraft.models import MusicGen + from audiocraft.data.audio import audio_write + print(f"Generating music from text '{text}'") + model = MusicGen.get_pretrained("facebook/musicgen-small") + model.set_generation_params( + use_sampling=True, + top_k=250, + duration=8 + ) + music = model.generate(text, progress=True) + for output in music: + output_cpu = output.cpu() + file_name = f"output/1.wav" + audio_write(file_name, output_cpu, 32000) + + +def generate_text_audio(text): + print(f"Generating audio from text '{text}'") + return text + + +@click.command(context_settings={"ignore_unknown_options": True}) +@click.version_option() +@click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run")#, required=True) +@click.option("-t", "--text", prompt="Enter the text to generate audio from", prompt_required=False, help="Text to generate audio from") +@click.option("-d", "--duration", prompt="Enter the duration of the audio", prompt_required=False, help="Duration of the audio to generate") +@click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") +def main(model, text, duration, output): print("Hello World from labgraph_audiogen!") - args = sys.argv - if args: - print(f"Args are: {' '.join(args[1:])}") \ No newline at end of file + if not model: + print("No model provided, set your model with the -m or --model flag (e.g. labgraph_audiogen -m audiogen)") + return + if not text: + print("No text provided, set your text with the -t or --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')") + return + if model == "audiogen" and len(text) < 100: + generate_text_audio(text) + elif model == "musicgen" and len(text) < 100: + generate_text_music(text) + else: + print(f"Model {model} not found") \ No newline at end of file diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index c460fb3cf..21560f4ca 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -9,6 +9,9 @@ packages=["labgraph_audiogen"], install_requires=[ "Click", + "torch>=2.0.0", + "torchaudio>=2.0.0", + "audiocraft" ], entry_points=""" [console_scripts] From c48c395ccdaca38e3c2d45cbe548b57b4e06ca9d Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Tue, 31 Oct 2023 11:55:42 -0600 Subject: [PATCH 04/17] enhance(audiocraft CLI): propperly working of music generation from text and validations improved --- .../labgraph_audiogen/main.py | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index c84c68e0e..3be477158 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -1,45 +1,57 @@ +import sys import click -def generate_text_music(text): +def generate_text_music(text, duration, output): + click.echo(f"\nGenerating music from text '{text}' with a duration of {duration}s and written on the {output}.wav") from audiocraft.models import MusicGen from audiocraft.data.audio import audio_write - print(f"Generating music from text '{text}'") - model = MusicGen.get_pretrained("facebook/musicgen-small") + model = MusicGen.get_pretrained("facebook/musicgen-medium") model.set_generation_params( use_sampling=True, top_k=250, - duration=8 + duration=duration, ) music = model.generate(text, progress=True) - for output in music: - output_cpu = output.cpu() - file_name = f"output/1.wav" - audio_write(file_name, output_cpu, 32000) + for generation in music: + generation_cpu = generation.cpu() + audio_write(f"outputs/{output}", generation_cpu, 32000) -def generate_text_audio(text): - print(f"Generating audio from text '{text}'") +def generate_text_audio(text, duration, output): + click.echo(f"\nGenerating audio from text '{text}' with a duration of {duration}s and written on the {output}.wav") return text @click.command(context_settings={"ignore_unknown_options": True}) @click.version_option() -@click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run")#, required=True) +@click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run") @click.option("-t", "--text", prompt="Enter the text to generate audio from", prompt_required=False, help="Text to generate audio from") @click.option("-d", "--duration", prompt="Enter the duration of the audio", prompt_required=False, help="Duration of the audio to generate") @click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") def main(model, text, duration, output): - print("Hello World from labgraph_audiogen!") if not model: - print("No model provided, set your model with the -m or --model flag (e.g. labgraph_audiogen -m audiogen)") - return + click.echo("No model provided, set your model with the -m / --model flag (e.g. labgraph_audiogen -m audiogen)") + return sys.exit(1) if not text: - print("No text provided, set your text with the -t or --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')") - return + click.echo("No text provided, set your text with the -t / --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')") + return sys.exit(1) + if not duration: + click.echo("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)") + duration = 10 + else: + duration = int(duration) + if not output: + click.echo("No output file name provided, set your output file name using -o / --output, default will be used (first 10 characters of the text)") + output = text[:10] if len(text) >= 10 else text + if len(output) > 35: + click.echo("Output file name too long, please use a name with less than 35 characters") + return sys.exit(1) + if len(text) > 100: + click.echo("Text too long, please use a text with less than 100 characters") + return sys.exit(1) + if model == "audiogen" and len(text) < 100: - generate_text_audio(text) + generate_text_audio(text, duration, output) elif model == "musicgen" and len(text) < 100: - generate_text_music(text) - else: - print(f"Model {model} not found") \ No newline at end of file + generate_text_music(text, duration, output) From af8df3d2d58a099abc75ff523f73a177c25bf816 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Tue, 7 Nov 2023 10:00:55 -0600 Subject: [PATCH 05/17] enhance(audiocraft CLI): musicgen style and validations improved --- .../labgraph_audiogen/main.py | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index 3be477158..dda53e38f 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -1,29 +1,28 @@ -import sys import click -def generate_text_music(text, duration, output): - click.echo(f"\nGenerating music from text '{text}' with a duration of {duration}s and written on the {output}.wav") +DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' +DEFAULT_MUSICGEN_MODEL = "facebook/musicgen-medium" +DEFAULT_AUDIO_DURATION = 10 + + +def generate_text_music(text, duration, output, musicgen_model): from audiocraft.models import MusicGen from audiocraft.data.audio import audio_write - model = MusicGen.get_pretrained("facebook/musicgen-medium") - model.set_generation_params( - use_sampling=True, - top_k=250, - duration=duration, - ) + click.echo(click.style(f"\nGenerating music from text '{text}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green")) + model = MusicGen.get_pretrained(musicgen_model) + model.set_generation_params(duration=duration) music = model.generate(text, progress=True) for generation in music: - generation_cpu = generation.cpu() - audio_write(f"outputs/{output}", generation_cpu, 32000) + audio_write(f"outputs/{output}", generation.cpu(), model.sample_rate) def generate_text_audio(text, duration, output): - click.echo(f"\nGenerating audio from text '{text}' with a duration of {duration}s and written on the {output}.wav") + click.echo(click.style(f"\nGenerating audio from text '{text}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green")) return text -@click.command(context_settings={"ignore_unknown_options": True}) +@click.command() @click.version_option() @click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run") @click.option("-t", "--text", prompt="Enter the text to generate audio from", prompt_required=False, help="Text to generate audio from") @@ -31,27 +30,25 @@ def generate_text_audio(text, duration, output): @click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") def main(model, text, duration, output): if not model: - click.echo("No model provided, set your model with the -m / --model flag (e.g. labgraph_audiogen -m audiogen)") - return sys.exit(1) + raise click.BadParameter(click.style("No model provided, set your model with the -m / --model flag (e.g. labgraph_audiogen -m audiogen)", fg="bright_red")) if not text: - click.echo("No text provided, set your text with the -t / --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')") - return sys.exit(1) + raise click.BadParameter(click.style("No text provided, set your text with the -t / --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')", fg="bright_red")) if not duration: - click.echo("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)") + click.echo(click.style("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)", fg="yellow")) duration = 10 else: duration = int(duration) if not output: - click.echo("No output file name provided, set your output file name using -o / --output, default will be used (first 10 characters of the text)") + click.echo(click.style("No output file name provided, set your output file name using -o / --output, default will be used (first 10 characters of the text)", fg="yellow")) output = text[:10] if len(text) >= 10 else text if len(output) > 35: - click.echo("Output file name too long, please use a name with less than 35 characters") - return sys.exit(1) + raise click.BadParameter(click.style("Output file name too long, please use a name with less than 35 characters", fg="bright_red")) if len(text) > 100: - click.echo("Text too long, please use a text with less than 100 characters") - return sys.exit(1) + raise click.BadParameter(click.style("Text too long, please use a text with less than 100 characters", fg="bright_red")) - if model == "audiogen" and len(text) < 100: - generate_text_audio(text, duration, output) - elif model == "musicgen" and len(text) < 100: - generate_text_music(text, duration, output) + if model == "audiogen": + audiogen_model = DEFAULT_AUDIOGEN_MODEL + generate_text_audio(text, duration, output, audiogen_model) + elif model == "musicgen": + musicgen_model = DEFAULT_MUSICGEN_MODEL + generate_text_music(text, duration, output, musicgen_model) From 89f32040efccd212b66e1b7294c53998df7dea94 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Wed, 8 Nov 2023 13:26:26 -0600 Subject: [PATCH 06/17] Fix(MusicGen CLI): Changed some generation parameters enabling to generate larger music --- .../labgraph_audiogen/main.py | 54 ----------------- .../__init__.py | 0 .../labgraph_audiogen/lg_audiogen/main.py | 58 +++++++++++++++++++ extensions/labgraph_audiogen/setup.py | 6 +- 4 files changed, 61 insertions(+), 57 deletions(-) delete mode 100644 extensions/labgraph_audiogen/labgraph_audiogen/main.py rename extensions/labgraph_audiogen/{labgraph_audiogen => lg_audiogen}/__init__.py (100%) create mode 100644 extensions/labgraph_audiogen/lg_audiogen/main.py diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py deleted file mode 100644 index dda53e38f..000000000 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ /dev/null @@ -1,54 +0,0 @@ -import click - - -DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' -DEFAULT_MUSICGEN_MODEL = "facebook/musicgen-medium" -DEFAULT_AUDIO_DURATION = 10 - - -def generate_text_music(text, duration, output, musicgen_model): - from audiocraft.models import MusicGen - from audiocraft.data.audio import audio_write - click.echo(click.style(f"\nGenerating music from text '{text}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green")) - model = MusicGen.get_pretrained(musicgen_model) - model.set_generation_params(duration=duration) - music = model.generate(text, progress=True) - for generation in music: - audio_write(f"outputs/{output}", generation.cpu(), model.sample_rate) - - -def generate_text_audio(text, duration, output): - click.echo(click.style(f"\nGenerating audio from text '{text}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green")) - return text - - -@click.command() -@click.version_option() -@click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run") -@click.option("-t", "--text", prompt="Enter the text to generate audio from", prompt_required=False, help="Text to generate audio from") -@click.option("-d", "--duration", prompt="Enter the duration of the audio", prompt_required=False, help="Duration of the audio to generate") -@click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") -def main(model, text, duration, output): - if not model: - raise click.BadParameter(click.style("No model provided, set your model with the -m / --model flag (e.g. labgraph_audiogen -m audiogen)", fg="bright_red")) - if not text: - raise click.BadParameter(click.style("No text provided, set your text with the -t / --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')", fg="bright_red")) - if not duration: - click.echo(click.style("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)", fg="yellow")) - duration = 10 - else: - duration = int(duration) - if not output: - click.echo(click.style("No output file name provided, set your output file name using -o / --output, default will be used (first 10 characters of the text)", fg="yellow")) - output = text[:10] if len(text) >= 10 else text - if len(output) > 35: - raise click.BadParameter(click.style("Output file name too long, please use a name with less than 35 characters", fg="bright_red")) - if len(text) > 100: - raise click.BadParameter(click.style("Text too long, please use a text with less than 100 characters", fg="bright_red")) - - if model == "audiogen": - audiogen_model = DEFAULT_AUDIOGEN_MODEL - generate_text_audio(text, duration, output, audiogen_model) - elif model == "musicgen": - musicgen_model = DEFAULT_MUSICGEN_MODEL - generate_text_music(text, duration, output, musicgen_model) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/__init__.py b/extensions/labgraph_audiogen/lg_audiogen/__init__.py similarity index 100% rename from extensions/labgraph_audiogen/labgraph_audiogen/__init__.py rename to extensions/labgraph_audiogen/lg_audiogen/__init__.py diff --git a/extensions/labgraph_audiogen/lg_audiogen/main.py b/extensions/labgraph_audiogen/lg_audiogen/main.py new file mode 100644 index 000000000..0b9896919 --- /dev/null +++ b/extensions/labgraph_audiogen/lg_audiogen/main.py @@ -0,0 +1,58 @@ +import click + + +DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' +DEFAULT_MUSICGEN_MODEL = "facebook/musicgen-medium" +DEFAULT_AUDIO_DURATION = 10 + + +def generate_text_music(description, duration, output, musicgen_model): + from audiocraft.models import MusicGen + from audiocraft.data.audio import audio_write + model = MusicGen.get_pretrained(musicgen_model) + model.set_generation_params(duration=duration) + outputs = [] + for i, text in enumerate(description): + if not output: + output = text[:10] if len(text) >= 10 else text + if len(text) > 100: + raise click.BadParameter(click.style(f"Description too long for {text}, please use a description of lees than 100 characters", fg="bright_red")) + if len(text) < 1: + raise click.BadParameter(click.style(f"Description too short for {text}, please use a description of more than 1 character", fg="bright_red")) + output = output.replace(" ", "_") + click.secho(f"Generating music from '{text}' written on the '{output}{i}.wav' file\n", fg="bright_green") + outputs.append(f"{output}{i}") + music = model.generate(description, progress=True) + for i, generation in enumerate(music): + audio_write(f"outputs/{outputs[i]}", generation.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) + click.secho(f"Audio generated and saved on the 'outputs/{outputs[i]}.wav' file", bg="green", fg="black") + + +def generate_text_audio(text, duration, output): + click.secho(f"\nGenerating audio from text '{text}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green") + return text + + +@click.command() +@click.argument('description', nargs=-1, required=False) +@click.option("-d", "--duration", prompt="Enter the duration of the audio", prompt_required=False, help="Duration of the audio to generate") +@click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run") +@click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") +def main(description, duration, model, output): + if not model: + raise click.BadParameter(click.style("No model provided, set your model with the -m / --model flag (e.g. labgraph_audiogen -m audiogen)", fg="bright_red")) + if not description: + raise click.BadParameter(click.style("No text provided, set your text with the -t / --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')", fg="bright_red")) + if not duration: + click.secho("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)", fg="yellow") + duration = 10 + else: + duration = int(duration) + + descriptions = [' '.join(description)] + if model == "audiogen": + audiogen_model = DEFAULT_AUDIOGEN_MODEL + generate_text_audio(descriptions, duration, output, audiogen_model) + elif model == "musicgen": + musicgen_model = DEFAULT_MUSICGEN_MODEL + generate_text_music(descriptions, duration, output, musicgen_model) \ No newline at end of file diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index 21560f4ca..33f0adf26 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -2,11 +2,11 @@ setup( - name="labgraph_audiogen", + name="lg_audiogen", version="0.0.1", description="A command-line interface for the usage of AudioCraft for labgraph", long_description="A command-line interface to facilitate the usage of the models of Audiocraft to generate and process audio on labgraph", - packages=["labgraph_audiogen"], + packages=["lg_audiogen"], install_requires=[ "Click", "torch>=2.0.0", @@ -15,6 +15,6 @@ ], entry_points=""" [console_scripts] - labgraph_audiogen=labgraph_audiogen.main:main + lg_audiogen=lg_audiogen.main:main """ ) \ No newline at end of file From 417799ac903dae124092c05ca05bc87ec043f90f Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Thu, 9 Nov 2023 12:10:59 -0600 Subject: [PATCH 07/17] Enchance(MusicGen CLI): Added functions docstring and corrected some variable naming --- .../labgraph_audiogen/lg_audiogen/main.py | 74 +++++++++++++------ 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/extensions/labgraph_audiogen/lg_audiogen/main.py b/extensions/labgraph_audiogen/lg_audiogen/main.py index 0b9896919..8c97863d7 100644 --- a/extensions/labgraph_audiogen/lg_audiogen/main.py +++ b/extensions/labgraph_audiogen/lg_audiogen/main.py @@ -1,48 +1,78 @@ import click +SUPPORTED_MODELS = ["audiogen-medium", "musicgen-small", "musicgen-medium", "musicgen-melody", "musicgen-large"] DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' DEFAULT_MUSICGEN_MODEL = "facebook/musicgen-medium" DEFAULT_AUDIO_DURATION = 10 -def generate_text_music(description, duration, output, musicgen_model): +def generate_text_music(descriptions, duration, output, musicgen_model): + """ + Generate music from the given descritptions + + @param descriptions: list of descriptions to generate music from + @param duration: duration of the audio to generate + @param output: name of the output file + @param musicgen_model: name of the musicgen model to use + """ from audiocraft.models import MusicGen from audiocraft.data.audio import audio_write model = MusicGen.get_pretrained(musicgen_model) model.set_generation_params(duration=duration) outputs = [] - for i, text in enumerate(description): + for i, description in enumerate(descriptions): if not output: - output = text[:10] if len(text) >= 10 else text - if len(text) > 100: - raise click.BadParameter(click.style(f"Description too long for {text}, please use a description of lees than 100 characters", fg="bright_red")) - if len(text) < 1: - raise click.BadParameter(click.style(f"Description too short for {text}, please use a description of more than 1 character", fg="bright_red")) + output = description[:10] if len(description) >= 10 else description + if len(description) > 100: + raise click.BadParameter(click.style(f"Description too long for {description}, please use a description of lees than 100 characters", fg="bright_red")) + if len(description) < 1: + raise click.BadParameter(click.style(f"Description too short for {description}, please use a description of more than 1 character", fg="bright_red")) output = output.replace(" ", "_") - click.secho(f"Generating music from '{text}' written on the '{output}{i}.wav' file\n", fg="bright_green") + click.secho(f"Generating music from '{description}' written on the '{output}{i}.wav' file\n", fg="bright_green") outputs.append(f"{output}{i}") - music = model.generate(description, progress=True) + music = model.generate(descriptions, progress=True) for i, generation in enumerate(music): audio_write(f"outputs/{outputs[i]}", generation.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) click.secho(f"Audio generated and saved on the 'outputs/{outputs[i]}.wav' file", bg="green", fg="black") -def generate_text_audio(text, duration, output): - click.secho(f"\nGenerating audio from text '{text}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green") - return text +def generate_text_audio(descriptions, duration, output, audiogen_model): + """ + Generate audio from the given descritptions + + @param descriptions: list of descriptions to generate audio from + @param duration: duration of the audio to generate + @param output: name of the output file + @param audiogen_model: name of the audiogen model to use + """ + click.secho(f"\nGenerating audio from text '{descriptions}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green") + return descriptions @click.command() @click.argument('description', nargs=-1, required=False) @click.option("-d", "--duration", prompt="Enter the duration of the audio", prompt_required=False, help="Duration of the audio to generate") -@click.option("-m", "--model", type=click.Choice(["audiogen", "musicgen"]), help="Select the model to run") +@click.option("-m", "--model", type=click.Choice(SUPPORTED_MODELS), help="Name of the model to use") @click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") -def main(description, duration, model, output): +@click.option('--batch', '-b', type=click.Path(), help='File name for batch audio description.') +def main(description, duration, model, output, batch): + """ + A command-line interface to manage the usage of the AudioCraft models + """ + if batch: + try: + with open(batch, mode='r', encoding='utf-8') as f: + descriptions = [line.strip() for line in f.readlines()] + except FileNotFoundError: + raise click.BadParameter(click.style(f"File {batch} not found. Please check the file path and try again.", fg="bright_red")) + else: + if not description: + raise click.BadParameter(click.style("Description argument is required when not using --batch.", fg="bright_red")) if not model: - raise click.BadParameter(click.style("No model provided, set your model with the -m / --model flag (e.g. labgraph_audiogen -m audiogen)", fg="bright_red")) - if not description: - raise click.BadParameter(click.style("No text provided, set your text with the -t / --text flag (e.g. labgraph_audiogen -m audiogen -t 'Hello World')", fg="bright_red")) + raise click.BadParameter(click.style("No model provided, set your model with the -m / --model flag (eg. lg_audiogen -m audiogen-medium)", fg="bright_red")) + else: + model = "facebook/" + model if not duration: click.secho("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)", fg="yellow") duration = 10 @@ -50,9 +80,7 @@ def main(description, duration, model, output): duration = int(duration) descriptions = [' '.join(description)] - if model == "audiogen": - audiogen_model = DEFAULT_AUDIOGEN_MODEL - generate_text_audio(descriptions, duration, output, audiogen_model) - elif model == "musicgen": - musicgen_model = DEFAULT_MUSICGEN_MODEL - generate_text_music(descriptions, duration, output, musicgen_model) \ No newline at end of file + if model == "facebook/audiogen-medium": + generate_text_audio(descriptions, duration, output, model) + else: + generate_text_music(descriptions, duration, output, model) \ No newline at end of file From 4b4671f1f56f5bb5d42b8811f0c326c8bd3643a4 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Fri, 10 Nov 2023 10:58:16 -0600 Subject: [PATCH 08/17] Testing(MusicGen CLI): Created a test for the generation with the model and its workflow --- .github/workflows/labgraph_audiogen.yml | 28 +++++++++++++++++++ .../tests/test_audiocraft_cli.py | 17 +++++++++++ 2 files changed, 45 insertions(+) create mode 100644 .github/workflows/labgraph_audiogen.yml create mode 100644 extensions/labgraph_audiogen/tests/test_audiocraft_cli.py diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml new file mode 100644 index 000000000..465937ec7 --- /dev/null +++ b/.github/workflows/labgraph_audiogen.yml @@ -0,0 +1,28 @@ +name: AudioCraft Tests + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' + + - name: Install dependencies + run: | + cd extensions/labgraph_audiogen + python -m pip install --upgrade pip + pip install -e . + pip install pytest + + - name: Run Tests + run: | + cd extensions/labgraph_audiogen + pytest -vvv \ No newline at end of file diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py new file mode 100644 index 000000000..2e5a92f18 --- /dev/null +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -0,0 +1,17 @@ +import os +import subprocess + + +def test_text_to_smallmusicgen(): + """ + Test the music generation from text with the musicgen-small model + """ + # Run the lg_audiogen -m musicgen-small -d 3 -o test_audio.wav "A happy song" + subprocess.run(["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], + capture_output=True, text=True) + # Assert file exists and size + assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" + assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" + # Remove the file + os.remove("outputs/test_audio0.wav") + os.rmdir("outputs") From 6b9ed4ebf3e516fe5c2db00bdb7d7516cdecc892 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Fri, 10 Nov 2023 11:06:50 -0600 Subject: [PATCH 09/17] Testing(MusicGen CLI workflow): Correction within the test file --- extensions/labgraph_audiogen/tests/test_audiocraft_cli.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py index 2e5a92f18..aa1cd7a77 100644 --- a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -7,9 +7,11 @@ def test_text_to_smallmusicgen(): Test the music generation from text with the musicgen-small model """ # Run the lg_audiogen -m musicgen-small -d 3 -o test_audio.wav "A happy song" - subprocess.run(["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], - capture_output=True, text=True) + print(subprocess.run(["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], + capture_output=True, text=True)) # Assert file exists and size + print(subprocess.run(["ls", "-l"], capture_output=True, text=True)) + print(subprocess.run(["pwd"], capture_output=True, text=True)) assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" # Remove the file From 99206968fadee7928b49a8124cf8ababef3e6a34 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Fri, 10 Nov 2023 11:24:30 -0600 Subject: [PATCH 10/17] Fix(MusicGen CLI workflow): Needed dependency added to the workflow --- .github/workflows/labgraph_audiogen.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 465937ec7..2cb309096 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -21,6 +21,7 @@ jobs: python -m pip install --upgrade pip pip install -e . pip install pytest + sudo apt-get install ffmpeg - name: Run Tests run: | From a29ab37279ef00e7bc7c3783fd23ab429971ed91 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Tue, 14 Nov 2023 11:40:03 -0600 Subject: [PATCH 11/17] QA(MusicGen CLI): Code improved, linting and consistency --- .github/workflows/labgraph_audiogen.yml | 40 +++--- .../labgraph_audiogen/lg_audiogen/main.py | 134 +++++++++++------- extensions/labgraph_audiogen/setup.py | 27 ++-- .../tests/test_audiocraft_cli.py | 10 +- 4 files changed, 127 insertions(+), 84 deletions(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 2cb309096..3fb7a960e 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -3,27 +3,27 @@ name: AudioCraft Tests on: [push] jobs: - build: - runs-on: ubuntu-latest + build: + runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v2 + steps: + - name: Checkout code + uses: actions/checkout@v2 - - name: Setup Python - uses: actions/setup-python@v2 - with: - python-version: '3.8' + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' - - name: Install dependencies - run: | - cd extensions/labgraph_audiogen - python -m pip install --upgrade pip - pip install -e . - pip install pytest - sudo apt-get install ffmpeg + - name: Install dependencies + run: | + cd extensions/labgraph_audiogen + python -m pip install --upgrade pip + pip install -e . + pip install pytest + sudo apt-get install ffmpeg - - name: Run Tests - run: | - cd extensions/labgraph_audiogen - pytest -vvv \ No newline at end of file + - name: Run Tests + run: | + cd extensions/labgraph_audiogen + pytest -vvv \ No newline at end of file diff --git a/extensions/labgraph_audiogen/lg_audiogen/main.py b/extensions/labgraph_audiogen/lg_audiogen/main.py index 8c97863d7..09255f783 100644 --- a/extensions/labgraph_audiogen/lg_audiogen/main.py +++ b/extensions/labgraph_audiogen/lg_audiogen/main.py @@ -1,86 +1,122 @@ import click -SUPPORTED_MODELS = ["audiogen-medium", "musicgen-small", "musicgen-medium", "musicgen-melody", "musicgen-large"] -DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' -DEFAULT_MUSICGEN_MODEL = "facebook/musicgen-medium" -DEFAULT_AUDIO_DURATION = 10 +SUPPORTED_MODELS = [ + "audiogen-medium", + "musicgen-small", + "musicgen-medium", + "musicgen-melody", + "musicgen-large" +] +MUSICGEN_MODELS = [ + "musicgen-small", + "musicgen-medium", + "musicgen-melody", + "musicgen-large" +] def generate_text_music(descriptions, duration, output, musicgen_model): """ - Generate music from the given descritptions + Generate music from the given descritptions and save it on the outputs folder @param descriptions: list of descriptions to generate music from @param duration: duration of the audio to generate @param output: name of the output file @param musicgen_model: name of the musicgen model to use """ + click.secho("\nStarting the music generation from the given descriptions", fg="bright_blue") + # Import MusicGen, audio_write in this function to avoid time consuming imports from audiocraft.models import MusicGen from audiocraft.data.audio import audio_write - model = MusicGen.get_pretrained(musicgen_model) + model = MusicGen.get_pretrained(f"facebook/{musicgen_model}") model.set_generation_params(duration=duration) - outputs = [] + filenames = [] for i, description in enumerate(descriptions): if not output: - output = description[:10] if len(description) >= 10 else description - if len(description) > 100: - raise click.BadParameter(click.style(f"Description too long for {description}, please use a description of lees than 100 characters", fg="bright_red")) - if len(description) < 1: - raise click.BadParameter(click.style(f"Description too short for {description}, please use a description of more than 1 character", fg="bright_red")) - output = output.replace(" ", "_") - click.secho(f"Generating music from '{description}' written on the '{output}{i}.wav' file\n", fg="bright_green") - outputs.append(f"{output}{i}") - music = model.generate(descriptions, progress=True) - for i, generation in enumerate(music): - audio_write(f"outputs/{outputs[i]}", generation.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) - click.secho(f"Audio generated and saved on the 'outputs/{outputs[i]}.wav' file", bg="green", fg="black") - - -def generate_text_audio(descriptions, duration, output, audiogen_model): - """ - Generate audio from the given descritptions + filenames.append(f"{description.replace(' ', '_')}") + else: + filenames.append(output.replace(' ', '_') + str(i)) - @param descriptions: list of descriptions to generate audio from - @param duration: duration of the audio to generate - @param output: name of the output file - @param audiogen_model: name of the audiogen model to use - """ - click.secho(f"\nGenerating audio from text '{descriptions}' with a duration of {duration}s and written on the {output}.wav", fg="bright_green") - return descriptions + if len(description) > 100: + raise click.BadParameter( + click.style( + f"Description too long for {description}, " + "please use a description of lees than 100 characters", + fg="bright_red" + ) + ) + if len(description) == 0: + raise click.BadParameter( + click.style( + f"Description too short for {description}, " + "please use a non-empty description", + fg="bright_red" + ) + ) + click.secho( + f"Generating music from '{description}' written on the '{filenames[i]}.wav' file", + fg="bright_green" + ) + try: + music = model.generate(descriptions, progress=True) + for i, generation in enumerate(music): + audio_write(f"outputs/{filenames[i]}", generation.cpu(), model.sample_rate, + strategy="loudness", loudness_compressor=True) + click.secho( + f"Audio generated and saved on the outputs/{filenames[i]}.wav file", + bg="green", fg="black" + ) + except Exception as music_error: + click.secho(f"Error generating music: {music_error}", bg="red", fg="white") @click.command() +@click.version_option() @click.argument('description', nargs=-1, required=False) -@click.option("-d", "--duration", prompt="Enter the duration of the audio", prompt_required=False, help="Duration of the audio to generate") +@click.option("-d", "--duration", type=int, prompt_required=False, help="Duration of the audio") @click.option("-m", "--model", type=click.Choice(SUPPORTED_MODELS), help="Name of the model to use") -@click.option("-o", "--output", prompt="Enter the desired output file name", prompt_required=False, help="Name of the output file") +@click.option("-o", "--output", prompt_required=False, help="Name of the output file") @click.option('--batch', '-b', type=click.Path(), help='File name for batch audio description.') -def main(description, duration, model, output, batch): +def parse_arguments(description, duration, model, output, batch): """ - A command-line interface to manage the usage of the AudioCraft models + A command-line command to facilitate the usage of the models of Audiocraft """ if batch: try: with open(batch, mode='r', encoding='utf-8') as f: descriptions = [line.strip() for line in f.readlines()] - except FileNotFoundError: - raise click.BadParameter(click.style(f"File {batch} not found. Please check the file path and try again.", fg="bright_red")) + except FileNotFoundError as file_error: + raise click.FileError(filename=batch, hint=click.style(file_error, fg="bright_red")) else: if not description: - raise click.BadParameter(click.style("Description argument is required when not using --batch.", fg="bright_red")) + raise click.BadParameter( + click.style( + "Description argument is required when not using --batch.", + fg="bright_red" + ) + ) + descriptions = [' '.join(description)] if not model: - raise click.BadParameter(click.style("No model provided, set your model with the -m / --model flag (eg. lg_audiogen -m audiogen-medium)", fg="bright_red")) - else: - model = "facebook/" + model + raise click.BadParameter( + click.style( + "No model provided, select a supported model with -m / --model " + f"(eg. -m musicgen-medium) between {', '.join(SUPPORTED_MODELS)}", + fg="bright_red" + ) + ) if not duration: - click.secho("No duration provided, set you duration using -d / --duration, default will be used (10 seconds)", fg="yellow") + click.secho( + "No duration provided, select a duration with -d / --duration, 10 seconds will be used", + fg="yellow" + ) duration = 10 - else: - duration = int(duration) + if duration <= 0: + raise click.BadParameter( + click.style( + "Duration must be greater than 0", fg="bright_red" + ) + ) - descriptions = [' '.join(description)] - if model == "facebook/audiogen-medium": - generate_text_audio(descriptions, duration, output, model) - else: - generate_text_music(descriptions, duration, output, model) \ No newline at end of file + if model in MUSICGEN_MODELS: + generate_text_music(descriptions, duration, output, model) diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index 33f0adf26..7b5e2d546 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -4,17 +4,22 @@ setup( name="lg_audiogen", version="0.0.1", - description="A command-line interface for the usage of AudioCraft for labgraph", - long_description="A command-line interface to facilitate the usage of the models of Audiocraft to generate and process audio on labgraph", + description="A command-line command for the usage of AudioCraft for labgraph", + long_description=""" + A command-line command to facilitate the usage of the models of Audiocraft + to generate and process audio on labgraph + """, packages=["lg_audiogen"], install_requires=[ - "Click", - "torch>=2.0.0", - "torchaudio>=2.0.0", - "audiocraft" + # Add versions + "Click>=8.1.7", + "torch>=2.1.0", + "torchaudio>=2.1.0", + "audiocraft>=1.0.0", ], - entry_points=""" - [console_scripts] - lg_audiogen=lg_audiogen.main:main - """ -) \ No newline at end of file + entry_points={ + 'console_scripts': [ + 'lg_audiogen=lg_audiogen.main:parse_arguments', + ], + }, +) diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py index aa1cd7a77..9b52b9bf6 100644 --- a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -5,13 +5,15 @@ def test_text_to_smallmusicgen(): """ Test the music generation from text with the musicgen-small model + + It requires the lg_audiogen command to be installed """ # Run the lg_audiogen -m musicgen-small -d 3 -o test_audio.wav "A happy song" - print(subprocess.run(["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], - capture_output=True, text=True)) + print(subprocess.run( + ["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], + capture_output=True, text=True, check=True)) # Assert file exists and size - print(subprocess.run(["ls", "-l"], capture_output=True, text=True)) - print(subprocess.run(["pwd"], capture_output=True, text=True)) + print(os.listdir("outputs")) assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" # Remove the file From eb80ca290d7176acc487e56077a26968ca497fdc Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Tue, 14 Nov 2023 12:05:06 -0600 Subject: [PATCH 12/17] Update(AudioGen): Updated README instructions --- extensions/labgraph_audiogen/README.md | 53 +++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/extensions/labgraph_audiogen/README.md b/extensions/labgraph_audiogen/README.md index 7104035e0..b21f88b7f 100644 --- a/extensions/labgraph_audiogen/README.md +++ b/extensions/labgraph_audiogen/README.md @@ -1,3 +1,54 @@ # LabGraph AudioGen Extension -A powerful Python command-line tool for Windows. This tool facilitates the creation, processing, and autocompletion high-quality audio and music, using the AudioCraft models, AudioGen, and MusicGen. +A powerful Python command-line command for Windows. This tool facilitates the creation, processing, and autocompletion high-quality audio and music, using the AudioCraft models, AudioGen, and MusicGen. + +The command is the `lg_audiogen` command, which can be installed with the `labgraph_audiogen` extension. + +## Features + +- Ability to specify duration of the generated audio. +- Ability to generate music based on a batch file. +- Ability to specify the model to be used for the audio generation. +- Ability to set the output file name. +- Ability to generate music within 4 different models. + +## Setup + +Audiocraft needs Python 3.8 or higher to run. If you have a suitable version of Python installed, you can install Audiogen with pip: + +```bash +cd extensions/labgraph_audiogen # Only if you are not already in this directory +pip install -e . +``` + +## Usage + +```bash +lg_audiogen --help +``` + +Usage: lg_audiogen [OPTIONS] [DESCRIPTION]... + +A command-line command to facilitate the usage of the models of Audiocraft + +Options: + +- `--version:` Show the version and exit. +- `-d, --duration:` INTEGER with the duration of the audio +- `-m, --model:` Name of the model to use between *[audiogen-medium | musicgen-small | musicgen-medium | musicgen-melody | musicgen-large]* +- `-o, --output:` TEXT with the name of the output file +- `-b, --batch:` PATH to file for batch audio description. +- `--help:` Show this message and exit. + +## Examples + +```bash +lg_audiogen -m musicgen-melody -d 15 -o "mariachi_rnb" "An rnb beat with some mariachi trumpets" + +lg_audiogen -m musicgen-small "A happy mood in a rainy day" +``` + +Outputs: + +- [mariachi_rnb.wav](https://drive.google.com/file/d/1OKXcZtRIqfL_dvfRFGtZV7VwC9CCUDX-/view) +- [A_happy_mood_in_a_rainy_day.wav](https://drive.google.com/file/d/1cCFhL7PP8FO0uzkEeRlhbDSGoflsBJ-G/view) From 6e31a19eb35a74f7ebff6dd8e51942458c4ff449 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Thu, 16 Nov 2023 14:51:52 -0600 Subject: [PATCH 13/17] Update(MusicGen): Updated comments for setup.py and main.py --- .../labgraph_audiogen/lg_audiogen/main.py | 35 ++++++++++++------- extensions/labgraph_audiogen/setup.py | 1 - 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/extensions/labgraph_audiogen/lg_audiogen/main.py b/extensions/labgraph_audiogen/lg_audiogen/main.py index 09255f783..8aeed5780 100644 --- a/extensions/labgraph_audiogen/lg_audiogen/main.py +++ b/extensions/labgraph_audiogen/lg_audiogen/main.py @@ -1,13 +1,16 @@ import click +# All models supported by the lg_audiogen command SUPPORTED_MODELS = [ - "audiogen-medium", + "audiogen-medium", "musicgen-small", "musicgen-medium", "musicgen-melody", "musicgen-large" ] + +# Musicgen models supported by the lg_audiogen command MUSICGEN_MODELS = [ "musicgen-small", "musicgen-medium", @@ -19,34 +22,31 @@ def generate_text_music(descriptions, duration, output, musicgen_model): """ Generate music from the given descritptions and save it on the outputs folder - + @param descriptions: list of descriptions to generate music from @param duration: duration of the audio to generate @param output: name of the output file @param musicgen_model: name of the musicgen model to use """ click.secho("\nStarting the music generation from the given descriptions", fg="bright_blue") + # Import MusicGen, audio_write in this function to avoid time consuming imports from audiocraft.models import MusicGen from audiocraft.data.audio import audio_write + + # Load the corresponding MusicGen model and set the generation parameters model = MusicGen.get_pretrained(f"facebook/{musicgen_model}") model.set_generation_params(duration=duration) + filenames = [] + # Validate the descriptions and filenames for i, description in enumerate(descriptions): if not output: filenames.append(f"{description.replace(' ', '_')}") else: filenames.append(output.replace(' ', '_') + str(i)) - if len(description) > 100: - raise click.BadParameter( - click.style( - f"Description too long for {description}, " - "please use a description of lees than 100 characters", - fg="bright_red" - ) - ) - if len(description) == 0: + if len(description) == 0: raise click.BadParameter( click.style( f"Description too short for {description}, " @@ -54,19 +54,25 @@ def generate_text_music(descriptions, duration, output, musicgen_model): fg="bright_red" ) ) + click.secho( - f"Generating music from '{description}' written on the '{filenames[i]}.wav' file", + f"Generating music from '{description}' written on the '{filenames[i]}.wav' file", fg="bright_green" - ) + ) + try: + # Generate the music from the descriptions music = model.generate(descriptions, progress=True) + # Save the music on the outputs folder with the corresponding filename for i, generation in enumerate(music): audio_write(f"outputs/{filenames[i]}", generation.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) + click.secho( f"Audio generated and saved on the outputs/{filenames[i]}.wav file", bg="green", fg="black" ) + except Exception as music_error: click.secho(f"Error generating music: {music_error}", bg="red", fg="white") @@ -82,6 +88,7 @@ def parse_arguments(description, duration, model, output, batch): """ A command-line command to facilitate the usage of the models of Audiocraft """ + # Validate batch and description if batch: try: with open(batch, mode='r', encoding='utf-8') as f: @@ -97,6 +104,8 @@ def parse_arguments(description, duration, model, output, batch): ) ) descriptions = [' '.join(description)] + + # Validate model and duration if not model: raise click.BadParameter( click.style( diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index 7b5e2d546..cb8f7227e 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -11,7 +11,6 @@ """, packages=["lg_audiogen"], install_requires=[ - # Add versions "Click>=8.1.7", "torch>=2.1.0", "torchaudio>=2.1.0", From 80b28578e98d99c776060e32acefb2a8d27b0d7e Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Thu, 16 Nov 2023 15:20:38 -0600 Subject: [PATCH 14/17] Fix(MusicGen CLI workflow): Extra line error fixed --- extensions/labgraph_audiogen/tests/test_audiocraft_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py index 9b52b9bf6..ced80d89f 100644 --- a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -13,7 +13,6 @@ def test_text_to_smallmusicgen(): ["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], capture_output=True, text=True, check=True)) # Assert file exists and size - print(os.listdir("outputs")) assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" # Remove the file From d0e5f37aba60bcbbbb1c2964dd69181fa4d482f6 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Thu, 16 Nov 2023 15:32:42 -0600 Subject: [PATCH 15/17] Tests(MusicGen CLI Testing): Debug of a test that worked --- extensions/labgraph_audiogen/tests/test_audiocraft_cli.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py index ced80d89f..bad9ea50c 100644 --- a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -12,6 +12,8 @@ def test_text_to_smallmusicgen(): print(subprocess.run( ["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], capture_output=True, text=True, check=True)) + print(subprocess.run(["ls"], capture_output=True, text=True, check=True)) + print(os.path.exists("test_audio0.wav")) # Assert file exists and size assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" From 4e48d1ce0a34f5452f1bdff593f220f967fde38d Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Thu, 16 Nov 2023 15:43:16 -0600 Subject: [PATCH 16/17] Tests(MusicGen CLI Testing): Debug of a test that was working previously --- extensions/labgraph_audiogen/tests/test_audiocraft_cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py index bad9ea50c..f085553ee 100644 --- a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -11,10 +11,10 @@ def test_text_to_smallmusicgen(): # Run the lg_audiogen -m musicgen-small -d 3 -o test_audio.wav "A happy song" print(subprocess.run( ["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], - capture_output=True, text=True, check=True)) - print(subprocess.run(["ls"], capture_output=True, text=True, check=True)) - print(os.path.exists("test_audio0.wav")) + capture_output=True, text=True)) # Assert file exists and size + print(subprocess.run(["ls", "-l"], capture_output=True, text=True)) + print(subprocess.run(["pwd"], capture_output=True, text=True)) assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" # Remove the file From e166f6784f3650d2093f770698e545675bb2e027 Mon Sep 17 00:00:00 2001 From: Omar Macias Date: Fri, 17 Nov 2023 09:36:43 -0600 Subject: [PATCH 17/17] Tests(MusicGen CLI Testing): Debug of a test that was working previously --- extensions/labgraph_audiogen/tests/test_audiocraft_cli.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py index f085553ee..6667cdc02 100644 --- a/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py +++ b/extensions/labgraph_audiogen/tests/test_audiocraft_cli.py @@ -13,8 +13,10 @@ def test_text_to_smallmusicgen(): ["lg_audiogen", "-m", "musicgen-small", "-d", "3", "-o", "test_audio", "A happy song"], capture_output=True, text=True)) # Assert file exists and size - print(subprocess.run(["ls", "-l"], capture_output=True, text=True)) + print(subprocess.run(["ls"], capture_output=True, text=True)) print(subprocess.run(["pwd"], capture_output=True, text=True)) + print(subprocess.run(["ls", ".."], capture_output=True, text=True)) + print(subprocess.run(["ls", "../.."], capture_output=True, text=True)) assert os.path.exists("outputs/test_audio0.wav"), "The file test_audio0.wav does not exist" assert os.path.getsize("outputs/test_audio0.wav") > 0, "The file test_audio0.wav is empty" # Remove the file