From b13019491d0f91a9b932fde12955434c21ca63b9 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 20 Oct 2023 12:15:05 -0400 Subject: [PATCH 01/28] Audiocraft CLI Directory & ReadME --- extensions/labgraph_audiogen/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 extensions/labgraph_audiogen/README.md diff --git a/extensions/labgraph_audiogen/README.md b/extensions/labgraph_audiogen/README.md new file mode 100644 index 00000000..b287e7db --- /dev/null +++ b/extensions/labgraph_audiogen/README.md @@ -0,0 +1 @@ +# Audiocraft CLI ReadME \ No newline at end of file From fa305d454742ff68982f7296ac1d2ba48460b223 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 20 Oct 2023 12:24:09 -0400 Subject: [PATCH 02/28] initial files --- extensions/labgraph_audiogen/labgraph_audiogen/__init__.py | 0 extensions/labgraph_audiogen/labgraph_audiogen/main.py | 0 extensions/labgraph_audiogen/setup.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) 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/labgraph_audiogen/__init__.py b/extensions/labgraph_audiogen/labgraph_audiogen/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py new file mode 100644 index 00000000..e69de29b diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py new file mode 100644 index 00000000..e69de29b From 3239fb8ec05b99866e2c2cae8f5ed79f9d9234aa Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 20 Oct 2023 12:40:01 -0400 Subject: [PATCH 03/28] Adds package setup & main driver code --- .../labgraph_audiogen/labgraph_audiogen/main.py | 7 +++++++ extensions/labgraph_audiogen/setup.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index e69de29b..63a4b8a0 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -0,0 +1,7 @@ +import sys + +def main(args=None): + if args is None: + args = sys.argv[1:] + + print(f"Hello World from labgraph_audiogen with args: {' '.join(args)}") \ No newline at end of file diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index e69de29b..546f39d9 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup + +setup( + name='labgraph_audiogen', + version='0.1', + description="Audio generation 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 3abca9933373a11b3985042ca53321bd3768f6a5 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 20 Oct 2023 12:46:23 -0400 Subject: [PATCH 04/28] Adds initial testing framework pytest --- extensions/labgraph_audiogen/tests/test_main.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 extensions/labgraph_audiogen/tests/test_main.py diff --git a/extensions/labgraph_audiogen/tests/test_main.py b/extensions/labgraph_audiogen/tests/test_main.py new file mode 100644 index 00000000..2cbd1c8e --- /dev/null +++ b/extensions/labgraph_audiogen/tests/test_main.py @@ -0,0 +1,5 @@ +def test_main(): + import subprocess + process = subprocess.run(["labgraph_audiogen", "arg1", "arg2"], + capture_output=True, text=True) + assert process.stdout.strip() == "Hello World from labgraph_audiogen with args: arg1 arg2" \ No newline at end of file From c8d68760827fc4d127adc7a358168672e700d0f6 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 20 Oct 2023 13:09:46 -0400 Subject: [PATCH 05/28] Test initial workflow for AudioGen --- .github/workflows/labgraph_audiogen.yml | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/labgraph_audiogen.yml diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml new file mode 100644 index 00000000..b133d7ac --- /dev/null +++ b/.github/workflows/labgraph_audiogen.yml @@ -0,0 +1,28 @@ +name: AudioGen 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.x' + + - 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 \ No newline at end of file From 5a7e045e094822092277a28a4928ef6cc07e1119 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 22:35:25 -0400 Subject: [PATCH 06/28] improved setup + added requirements --- extensions/labgraph_audiogen/setup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index 546f39d9..35f3a842 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -1,12 +1,14 @@ -from setuptools import setup +from setuptools import setup, find_packages setup( name='labgraph_audiogen', version='0.1', description="Audio generation on labgraph", - packages=['labgraph_audiogen'], + packages=find_packages(), install_requires=[ 'Click', + "torchaudio", + "audiocraft", ], entry_points=''' [console_scripts] From d9b6c4853f99089a13f2623f1461c5b89702430c Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 22:36:34 -0400 Subject: [PATCH 07/28] Implements --description & --duration for audiogen --- .../labgraph_audiogen/main.py | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index 63a4b8a0..bc902e83 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -1,7 +1,32 @@ import sys +import argparse +import torchaudio +from audiocraft.models import AudioGen +from audiocraft.data.audio import audio_write def main(args=None): + # Parse arguments + parser = argparse.ArgumentParser(description='Generate audio from descriptions using Audiocraft\'s AudioGen.') + parser.add_argument('--description', nargs='+', type=str, help='Description of the generated audio.') + parser.add_argument('--duration', type=int, default=5, help='Duration of the generated audio.') if args is None: - args = sys.argv[1:] + args = parser.parse_args() + else: + args = parser.parse_args(args) - print(f"Hello World from labgraph_audiogen with args: {' '.join(args)}") \ No newline at end of file + print(f"Running labgraph_audiogen with description: {args.description}") + + # Load Audiocraft's AudioGen model and set generation params. + model = AudioGen.get_pretrained('facebook/audiogen-medium') + model.set_generation_params(duration=args.duration) + + # Generate audio from the description + wav = model.generate(args.description) + + # Save the generated audios. + for idx, one_wav in enumerate(wav): + # Will save under {idx}.wav, with loudness normalization at -14 db LUFS. + audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) + +if __name__ == "__main__": + main() \ No newline at end of file From a6eeb30a5a0e1de6d7545b10c8c050be3b6cd607 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 22:48:52 -0400 Subject: [PATCH 08/28] tests the creation of the audio file with desc --- extensions/labgraph_audiogen/tests/test_main.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/extensions/labgraph_audiogen/tests/test_main.py b/extensions/labgraph_audiogen/tests/test_main.py index 2cbd1c8e..f751f270 100644 --- a/extensions/labgraph_audiogen/tests/test_main.py +++ b/extensions/labgraph_audiogen/tests/test_main.py @@ -1,5 +1,13 @@ +import os +import subprocess + def test_main(): - import subprocess - process = subprocess.run(["labgraph_audiogen", "arg1", "arg2"], - capture_output=True, text=True) - assert process.stdout.strip() == "Hello World from labgraph_audiogen with args: arg1 arg2" \ No newline at end of file + # Run the script with an example description + process = subprocess.run(["labgraph_audiogen", "--description", "dog barking"], capture_output=True, text=True) + + # Assert that the script ran successfully + assert process.returncode == 0, f"Script returned {process.returncode}, expected 0. stdout: {process.stdout}, stderr: {process.stderr}" + + # Assert that the output file was created + assert os.path.exists("0.wav"), "Output file 0.wav was not created" + os.remove("0.wav") \ No newline at end of file From 091c0a4d77cde7a9127fa55c7790b06ea0773a86 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 22:58:22 -0400 Subject: [PATCH 09/28] test workflow with torch install before audiocraft --- .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 b133d7ac..916f23f2 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -19,6 +19,7 @@ jobs: run: | cd extensions/labgraph_audiogen python -m pip install --upgrade pip + pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 pip install -e . pip install pytest From c314e4f57fcbdce2f0cd001113e1c3489c6e9940 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 23:05:24 -0400 Subject: [PATCH 10/28] test workflow with different distribution of torch --- .github/workflows/labgraph_audiogen.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 916f23f2..40200f6a 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -19,7 +19,8 @@ jobs: run: | cd extensions/labgraph_audiogen python -m pip install --upgrade pip - pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 + pip install https://download.pytorch.org/whl/cu118/torch-2.0.1%2Bcu118-cp310-cp310-linux_x86_64.whl\#sha256\=a7a49d459bf4862f64f7bc1a68beccf8881c2fa9f3e0569608e16ba6f85ebf7b + pip install https://download.pytorch.org/whl/cu118/torchaudio-2.0.2%2Bcu118-cp310-cp310-linux_x86_64.whl\#sha256\=26692645ea061a005c57ec581a2d0425210ac6ba9f923edf11cc9b0ef3a111e9 pip install -e . pip install pytest From 6a0ce7362667eecba1934167324e6496cfc2045a Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 23:08:18 -0400 Subject: [PATCH 11/28] [workflow] - try raw torch, vision, audio --- .github/workflows/labgraph_audiogen.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 40200f6a..350862de 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -19,8 +19,7 @@ jobs: run: | cd extensions/labgraph_audiogen python -m pip install --upgrade pip - pip install https://download.pytorch.org/whl/cu118/torch-2.0.1%2Bcu118-cp310-cp310-linux_x86_64.whl\#sha256\=a7a49d459bf4862f64f7bc1a68beccf8881c2fa9f3e0569608e16ba6f85ebf7b - pip install https://download.pytorch.org/whl/cu118/torchaudio-2.0.2%2Bcu118-cp310-cp310-linux_x86_64.whl\#sha256\=26692645ea061a005c57ec581a2d0425210ac6ba9f923edf11cc9b0ef3a111e9 + pip install torch torchvision torchaudio pip install -e . pip install pytest From 3c32fdab13dda7686c1420f32c65c2256352fa23 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 23:13:41 -0400 Subject: [PATCH 12/28] [workflow] - Try downgrading Python --- .github/workflows/labgraph_audiogen.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 350862de..001d337c 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -13,7 +13,7 @@ jobs: - name: Setup Python uses: actions/setup-python@v2 with: - python-version: '3.x' + python-version: '3.10' - name: Install dependencies run: | From dc2419cf99e7f16db18f9ce8ff404a964228a8d5 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 23:39:31 -0400 Subject: [PATCH 13/28] [workflow] - downgrade to match audiocraft + index --- .github/workflows/labgraph_audiogen.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 001d337c..d48a8aed 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -13,17 +13,18 @@ jobs: - name: Setup Python uses: actions/setup-python@v2 with: - python-version: '3.10' + python-version: '3.8' - name: Install dependencies run: | cd extensions/labgraph_audiogen python -m pip install --upgrade pip - pip install torch torchvision torchaudio + pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu + pip install --pre xformers pip install -e . pip install pytest - name: Run tests run: | cd extensions/labgraph_audiogen - pytest \ No newline at end of file + pytest --verbose \ No newline at end of file From 15bf85e5468baed92dd9bb9aaf8a6ff0151e1c7c Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 26 Oct 2023 23:47:30 -0400 Subject: [PATCH 14/28] [Workflow] adds triple verbose to pytest --- .github/workflows/labgraph_audiogen.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index d48a8aed..aae373ba 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -27,4 +27,4 @@ jobs: - name: Run tests run: | cd extensions/labgraph_audiogen - pytest --verbose \ No newline at end of file + pytest -vvv \ No newline at end of file From 3e90fce510489586e186195471623c1f3e827773 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 27 Oct 2023 00:11:10 -0400 Subject: [PATCH 15/28] tries self-hosted runner on Google Colab --- .github/workflows/labgraph_audiogen.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index aae373ba..c9546ec4 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -4,7 +4,7 @@ on: [push] jobs: build: - runs-on: ubuntu-latest + runs-on: self-hosted steps: - name: Checkout code From fbed041d6e046cbd55a8265e47603e0a624d9922 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 27 Oct 2023 00:21:18 -0400 Subject: [PATCH 16/28] test only file creation --- extensions/labgraph_audiogen/tests/test_main.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/extensions/labgraph_audiogen/tests/test_main.py b/extensions/labgraph_audiogen/tests/test_main.py index f751f270..4b2dfe64 100644 --- a/extensions/labgraph_audiogen/tests/test_main.py +++ b/extensions/labgraph_audiogen/tests/test_main.py @@ -5,9 +5,6 @@ def test_main(): # Run the script with an example description process = subprocess.run(["labgraph_audiogen", "--description", "dog barking"], capture_output=True, text=True) - # Assert that the script ran successfully - assert process.returncode == 0, f"Script returned {process.returncode}, expected 0. stdout: {process.stdout}, stderr: {process.stderr}" - # Assert that the output file was created assert os.path.exists("0.wav"), "Output file 0.wav was not created" os.remove("0.wav") \ No newline at end of file From 4e4f97c5f52ff413a208c8832b92f200f63b838d Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 3 Nov 2023 12:07:10 -0400 Subject: [PATCH 17/28] Refactors code, changes argparse to @click, Docstr --- .../labgraph_audiogen/main.py | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index bc902e83..504fafc3 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -1,32 +1,46 @@ -import sys -import argparse -import torchaudio +import click +import torch from audiocraft.models import AudioGen from audiocraft.data.audio import audio_write -def main(args=None): - # Parse arguments - parser = argparse.ArgumentParser(description='Generate audio from descriptions using Audiocraft\'s AudioGen.') - parser.add_argument('--description', nargs='+', type=str, help='Description of the generated audio.') - parser.add_argument('--duration', type=int, default=5, help='Duration of the generated audio.') - if args is None: - args = parser.parse_args() - else: - args = parser.parse_args(args) +DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' +DEFAULT_AUDIO_DURATION = 5 - print(f"Running labgraph_audiogen with description: {args.description}") +@click.command() +@click.argument('description', nargs=-1, required=True) +@click.option('--duration', '-d', default=DEFAULT_AUDIO_DURATION, help='Duration of the generated audio.') +@click.option('--model', '-m', default=DEFAULT_AUDIOGEN_MODEL, help='Name of the Audiocraft AudioGen model to use.') +@click.option('--output', '-o', help='Name of the output file.') +def parse_arguments(description, duration, model, output): + """ + Generates audio from description using Audiocraft's AudioGen. + """ + description = ' '.join(description) + if output is None: + output = description[:10] + + run_audio_generation(description, duration, model, output) + + +def run_audio_generation(description, duration, model_name, output): + """ + Load Audiocraft's AudioGen model and generate audio from the description. + + :param description: The parsed arguments. + :param duration: Duration of the generated audio. + :param model_name: Name of the Audiocraft AudioGen model to use. + :param output: Name of the output file. + """ + print(f"Running labgraph_audiogen with description: {description}") # Load Audiocraft's AudioGen model and set generation params. - model = AudioGen.get_pretrained('facebook/audiogen-medium') - model.set_generation_params(duration=args.duration) + model = AudioGen.get_pretrained(model_name) + model.set_generation_params(duration=duration) # Generate audio from the description - wav = model.generate(args.description) + wav = model.generate([description]) # Save the generated audios. for idx, one_wav in enumerate(wav): - # Will save under {idx}.wav, with loudness normalization at -14 db LUFS. - audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) - -if __name__ == "__main__": - main() \ No newline at end of file + # Will save under {output}{idx}.wav, with loudness normalization at -14 db LUFS. + audio_write(f'{output}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) \ No newline at end of file From 3532104bb13f1d99973072ac3cd4c1e5f88d9b7d Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 3 Nov 2023 12:07:30 -0400 Subject: [PATCH 18/28] Changes entry point --- extensions/labgraph_audiogen/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/labgraph_audiogen/setup.py index 35f3a842..6e889587 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/labgraph_audiogen/setup.py @@ -12,6 +12,6 @@ ], entry_points=''' [console_scripts] - labgraph_audiogen=labgraph_audiogen.main:main + labgraph_audiogen=labgraph_audiogen.main:parse_arguments ''', ) \ No newline at end of file From f4e3ca6c46f74547267eb38fbe66f55ff6a1a400 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 3 Nov 2023 12:53:13 -0400 Subject: [PATCH 19/28] adds batch functionality with file input --- .../labgraph_audiogen/main.py | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index 504fafc3..cbf26c42 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -7,40 +7,49 @@ DEFAULT_AUDIO_DURATION = 5 @click.command() -@click.argument('description', nargs=-1, required=True) +@click.argument('description', nargs=-1, required=False) @click.option('--duration', '-d', default=DEFAULT_AUDIO_DURATION, help='Duration of the generated audio.') @click.option('--model', '-m', default=DEFAULT_AUDIOGEN_MODEL, help='Name of the Audiocraft AudioGen model to use.') @click.option('--output', '-o', help='Name of the output file.') -def parse_arguments(description, duration, model, output): +@click.option('--batch', type=click.Path(), help='File name for batch audio description.') +def parse_arguments(description, duration, model, output, batch): """ Generates audio from description using Audiocraft's AudioGen. """ - description = ' '.join(description) - if output is None: - output = description[:10] - - run_audio_generation(description, duration, model, output) - - -def run_audio_generation(description, duration, model_name, output): + if batch: + try: + with open(batch, 'r') as f: + descriptions = [line.strip() for line in f.readlines()] + except FileNotFoundError: + print(f"File {batch} not found. Please check the file path and try again.") + else: + if not description: + raise click.BadParameter("Description argument is required when not using --batch.") + descriptions = [' '.join(description)] + + run_audio_generation(descriptions, duration, model, output) + +def run_audio_generation(descriptions, duration, model_name, output): """ Load Audiocraft's AudioGen model and generate audio from the description. - :param description: The parsed arguments. + :param descriptions: The parsed arguments. :param duration: Duration of the generated audio. :param model_name: Name of the Audiocraft AudioGen model to use. :param output: Name of the output file. """ - print(f"Running labgraph_audiogen with description: {description}") + print(f"Running labgraph_audiogen with descriptions: {descriptions}") # Load Audiocraft's AudioGen model and set generation params. model = AudioGen.get_pretrained(model_name) model.set_generation_params(duration=duration) - # Generate audio from the description - wav = model.generate([description]) - + # Generate audio from the descriptions + wav = model.generate(descriptions) + batch_output = output # Save the generated audios. for idx, one_wav in enumerate(wav): # Will save under {output}{idx}.wav, with loudness normalization at -14 db LUFS. - audio_write(f'{output}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) \ No newline at end of file + if not output: + batch_output = descriptions[idx].replace(' ', '_') + audio_write(f'{batch_output}{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) \ No newline at end of file From 10cc1bebe94b39e08b3f96f40e0807b2c7457e1c Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 3 Nov 2023 13:01:39 -0400 Subject: [PATCH 20/28] Checks if file was created --- .github/workflows/labgraph_audiogen.yml | 2 +- extensions/labgraph_audiogen/tests/test_main.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index c9546ec4..aae373ba 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -4,7 +4,7 @@ on: [push] jobs: build: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - name: Checkout code diff --git a/extensions/labgraph_audiogen/tests/test_main.py b/extensions/labgraph_audiogen/tests/test_main.py index 4b2dfe64..16714bd5 100644 --- a/extensions/labgraph_audiogen/tests/test_main.py +++ b/extensions/labgraph_audiogen/tests/test_main.py @@ -1,10 +1,10 @@ import os import subprocess -def test_main(): +def test_single_description(): # Run the script with an example description - process = subprocess.run(["labgraph_audiogen", "--description", "dog barking"], capture_output=True, text=True) + process = subprocess.run(["labgraph_audiogen", "dog barking", "dog barking"], capture_output=True, text=True) # Assert that the output file was created - assert os.path.exists("0.wav"), "Output file 0.wav was not created" - os.remove("0.wav") \ No newline at end of file + assert os.path.exists("dog_barking0.wav"), "Output file dog_barking0.wav was not created" + os.remove("dog_barking0.wav") \ No newline at end of file From 730579ba69ca66965658c94d3908aa6476f4fd9b Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 3 Nov 2023 13:23:57 -0400 Subject: [PATCH 21/28] linting + consistency --- extensions/labgraph_audiogen/labgraph_audiogen/main.py | 8 ++++---- extensions/labgraph_audiogen/tests/test_main.py | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/labgraph_audiogen/labgraph_audiogen/main.py index cbf26c42..2e1bd8be 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/labgraph_audiogen/labgraph_audiogen/main.py @@ -11,14 +11,14 @@ @click.option('--duration', '-d', default=DEFAULT_AUDIO_DURATION, help='Duration of the generated audio.') @click.option('--model', '-m', default=DEFAULT_AUDIOGEN_MODEL, help='Name of the Audiocraft AudioGen model to use.') @click.option('--output', '-o', help='Name of the output file.') -@click.option('--batch', type=click.Path(), help='File name for batch audio description.') +@click.option('--batch', '-b', type=click.Path(), help='File name for batch audio description.') def parse_arguments(description, duration, model, output, batch): """ Generates audio from description using Audiocraft's AudioGen. """ if batch: try: - with open(batch, 'r') as f: + with open(batch, mode='r', encoding='utf-8') as f: descriptions = [line.strip() for line in f.readlines()] except FileNotFoundError: print(f"File {batch} not found. Please check the file path and try again.") @@ -26,7 +26,6 @@ def parse_arguments(description, duration, model, output, batch): if not description: raise click.BadParameter("Description argument is required when not using --batch.") descriptions = [' '.join(description)] - run_audio_generation(descriptions, duration, model, output) def run_audio_generation(descriptions, duration, model_name, output): @@ -52,4 +51,5 @@ def run_audio_generation(descriptions, duration, model_name, output): # Will save under {output}{idx}.wav, with loudness normalization at -14 db LUFS. if not output: batch_output = descriptions[idx].replace(' ', '_') - audio_write(f'{batch_output}{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) \ No newline at end of file + audio_write(f'{batch_output}{idx}', one_wav.cpu(), + model.sample_rate, strategy="loudness", loudness_compressor=True) diff --git a/extensions/labgraph_audiogen/tests/test_main.py b/extensions/labgraph_audiogen/tests/test_main.py index 16714bd5..1763c56d 100644 --- a/extensions/labgraph_audiogen/tests/test_main.py +++ b/extensions/labgraph_audiogen/tests/test_main.py @@ -2,9 +2,12 @@ import subprocess def test_single_description(): + ''' + Tests output with a single description + ''' # Run the script with an example description - process = subprocess.run(["labgraph_audiogen", "dog barking", "dog barking"], capture_output=True, text=True) - + subprocess.run(["labgraph_audiogen", "dog barking"], + capture_output=True, text=True, check=False) # Assert that the output file was created assert os.path.exists("dog_barking0.wav"), "Output file dog_barking0.wav was not created" - os.remove("dog_barking0.wav") \ No newline at end of file + os.remove("dog_barking0.wav") From acb9b644f2ae6d6b9176901237e4e42bff7f94cb Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Fri, 3 Nov 2023 13:24:23 -0400 Subject: [PATCH 22/28] README instructions --- extensions/labgraph_audiogen/README.md | 49 +++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/extensions/labgraph_audiogen/README.md b/extensions/labgraph_audiogen/README.md index b287e7db..91ab73c6 100644 --- a/extensions/labgraph_audiogen/README.md +++ b/extensions/labgraph_audiogen/README.md @@ -1 +1,48 @@ -# Audiocraft CLI ReadME \ No newline at end of file +# Audiogen + +Audiogen is a Python command-line tool that uses models from Audiocraft's AudioGen to generate audio from specified descriptions. This tool can generate a single piece of audio based on a specific description or multiple pieces of audio based on a batch file containing multiple descriptions. + +## Features + +* Ability to specify duration of the generated audio. +* Ability to generate audio based on a batch file. +* Ability to specify the model to be used for the audio generation. +* Ability to set the output file name. + +## 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: + +```shell +pip install -e . +``` + +## Usage + +### Command-line interface + +The CLI usage for Audiogen is `labgraph_audiogen [OPTIONS] [DESCRIPTION]...`. + +### Options + +* `description`: the description based on which the audio is to be generated. +* `duration, -d`: duration of the generated audio, default is 5. +* `model, -m`: name of the Audiocraft AudioGen model to use, default is 'facebook/audiogen-medium'. +* `output, -o`: name of the output file. +* `batch`: file name for batch audio description. + +### Example + +To generate an audio file you would use the following command: + +```shell +labgraph_audiogen -d 5 -m 'facebook/audiogen-medium' -o 'my_output' 'dog barking' + +labgraph_audiogen 'dog barking' + +labgraph_audiogen -b 'batch.txt' +``` + +## Error Handling + +If the batch file is not found, a notable error message will be presented. Moreover, if a description is not provided when not using a batch file, a misusage error will be raised. From a11e54dbc86a242ff86f195c160b03191e5627ce Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 16 Nov 2023 19:12:59 -0500 Subject: [PATCH 23/28] Switch from labgraph_audiogen to lg_audiogen --- .../{labgraph_audiogen => lg_audiogen}/README.md | 8 ++++---- .../lg_audiogen}/__init__.py | 0 .../lg_audiogen}/main.py | 10 +++++----- extensions/{labgraph_audiogen => lg_audiogen}/setup.py | 4 ++-- .../tests/test_main.py | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) rename extensions/{labgraph_audiogen => lg_audiogen}/README.md (86%) rename extensions/{labgraph_audiogen/labgraph_audiogen => lg_audiogen/lg_audiogen}/__init__.py (100%) rename extensions/{labgraph_audiogen/labgraph_audiogen => lg_audiogen/lg_audiogen}/main.py (88%) rename extensions/{labgraph_audiogen => lg_audiogen}/setup.py (75%) rename extensions/{labgraph_audiogen => lg_audiogen}/tests/test_main.py (87%) diff --git a/extensions/labgraph_audiogen/README.md b/extensions/lg_audiogen/README.md similarity index 86% rename from extensions/labgraph_audiogen/README.md rename to extensions/lg_audiogen/README.md index 91ab73c6..79b5badf 100644 --- a/extensions/labgraph_audiogen/README.md +++ b/extensions/lg_audiogen/README.md @@ -21,7 +21,7 @@ pip install -e . ### Command-line interface -The CLI usage for Audiogen is `labgraph_audiogen [OPTIONS] [DESCRIPTION]...`. +The CLI usage for Audiogen is `lg_audiogen [OPTIONS] [DESCRIPTION]...`. ### Options @@ -36,11 +36,11 @@ The CLI usage for Audiogen is `labgraph_audiogen [OPTIONS] [DESCRIPTION]...`. To generate an audio file you would use the following command: ```shell -labgraph_audiogen -d 5 -m 'facebook/audiogen-medium' -o 'my_output' 'dog barking' +lg_audiogen -d 5 -m 'facebook/audiogen-medium' -o 'my_output' 'dog barking' -labgraph_audiogen 'dog barking' +lg_audiogen 'dog barking' -labgraph_audiogen -b 'batch.txt' +lg_audiogen -b 'batch.txt' ``` ## Error Handling diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/__init__.py b/extensions/lg_audiogen/lg_audiogen/__init__.py similarity index 100% rename from extensions/labgraph_audiogen/labgraph_audiogen/__init__.py rename to extensions/lg_audiogen/lg_audiogen/__init__.py diff --git a/extensions/labgraph_audiogen/labgraph_audiogen/main.py b/extensions/lg_audiogen/lg_audiogen/main.py similarity index 88% rename from extensions/labgraph_audiogen/labgraph_audiogen/main.py rename to extensions/lg_audiogen/lg_audiogen/main.py index 2e1bd8be..6e738e23 100644 --- a/extensions/labgraph_audiogen/labgraph_audiogen/main.py +++ b/extensions/lg_audiogen/lg_audiogen/main.py @@ -32,12 +32,12 @@ def run_audio_generation(descriptions, duration, model_name, output): """ Load Audiocraft's AudioGen model and generate audio from the description. - :param descriptions: The parsed arguments. - :param duration: Duration of the generated audio. - :param model_name: Name of the Audiocraft AudioGen model to use. - :param output: Name of the output file. + @param descriptions: The parsed arguments. + @param duration: Duration of the generated audio. + @param model_name: Name of the Audiocraft AudioGen model to use. + @param output: Name of the output file. """ - print(f"Running labgraph_audiogen with descriptions: {descriptions}") + print(f"Running lg_audiogen with descriptions: {descriptions}") # Load Audiocraft's AudioGen model and set generation params. model = AudioGen.get_pretrained(model_name) diff --git a/extensions/labgraph_audiogen/setup.py b/extensions/lg_audiogen/setup.py similarity index 75% rename from extensions/labgraph_audiogen/setup.py rename to extensions/lg_audiogen/setup.py index 6e889587..f1e03431 100644 --- a/extensions/labgraph_audiogen/setup.py +++ b/extensions/lg_audiogen/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup( - name='labgraph_audiogen', + name='lg_audiogen', version='0.1', description="Audio generation on labgraph", packages=find_packages(), @@ -12,6 +12,6 @@ ], entry_points=''' [console_scripts] - labgraph_audiogen=labgraph_audiogen.main:parse_arguments + lg_audiogen=lg_audiogen.main:parse_arguments ''', ) \ No newline at end of file diff --git a/extensions/labgraph_audiogen/tests/test_main.py b/extensions/lg_audiogen/tests/test_main.py similarity index 87% rename from extensions/labgraph_audiogen/tests/test_main.py rename to extensions/lg_audiogen/tests/test_main.py index 1763c56d..c398fcaf 100644 --- a/extensions/labgraph_audiogen/tests/test_main.py +++ b/extensions/lg_audiogen/tests/test_main.py @@ -6,7 +6,7 @@ def test_single_description(): Tests output with a single description ''' # Run the script with an example description - subprocess.run(["labgraph_audiogen", "dog barking"], + subprocess.run(["lg_audiogen", "dog barking"], capture_output=True, text=True, check=False) # Assert that the output file was created assert os.path.exists("dog_barking0.wav"), "Output file dog_barking0.wav was not created" From 1317661db46fe1fcae00216bd2fcae9c048a5620 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 16 Nov 2023 19:44:08 -0500 Subject: [PATCH 24/28] Add versions + Improve descriptions --- extensions/lg_audiogen/setup.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/extensions/lg_audiogen/setup.py b/extensions/lg_audiogen/setup.py index f1e03431..220198d5 100644 --- a/extensions/lg_audiogen/setup.py +++ b/extensions/lg_audiogen/setup.py @@ -3,12 +3,17 @@ setup( name='lg_audiogen', version='0.1', - description="Audio generation on labgraph", + description="A Command-line interface to use Audiocraft for labgraph", + long_description=""" + A Command-line interface to facilitate the usage of Audiocraft's models + to generate and process audio on labgraph + """, packages=find_packages(), install_requires=[ - 'Click', - "torchaudio", - "audiocraft", + "Click>=8.1.7", + "torch>=2.1.0", + "torchaudio>=2.1.0", + "audiocraft==1.1.0", ], entry_points=''' [console_scripts] From 9d5bebfab7310ab27a370ccbc494867fa56feee3 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 16 Nov 2023 19:44:40 -0500 Subject: [PATCH 25/28] Adds ffmpeg to fix 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 aae373ba..3a68339d 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -19,6 +19,7 @@ jobs: run: | cd extensions/labgraph_audiogen python -m pip install --upgrade pip + sudo apt-get install ffmpeg pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu pip install --pre xformers pip install -e . From ecb2d04ede78276a2a5c2b185cc8cf15c516ca29 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 16 Nov 2023 19:48:34 -0500 Subject: [PATCH 26/28] fix package name to lg_audiogen --- .github/workflows/labgraph_audiogen.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/labgraph_audiogen.yml b/.github/workflows/labgraph_audiogen.yml index 3a68339d..57bc4372 100644 --- a/.github/workflows/labgraph_audiogen.yml +++ b/.github/workflows/labgraph_audiogen.yml @@ -17,7 +17,7 @@ jobs: - name: Install dependencies run: | - cd extensions/labgraph_audiogen + cd extensions/lg_audiogen python -m pip install --upgrade pip sudo apt-get install ffmpeg pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu @@ -27,5 +27,5 @@ jobs: - name: Run tests run: | - cd extensions/labgraph_audiogen + cd extensions/lg_audiogen pytest -vvv \ No newline at end of file From 94aee5c3fa3f1e2705af22844c165609a24fb9d6 Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 16 Nov 2023 20:18:47 -0500 Subject: [PATCH 27/28] Adds O.S Support on ReadME --- extensions/lg_audiogen/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/extensions/lg_audiogen/README.md b/extensions/lg_audiogen/README.md index 79b5badf..5a3ca7d9 100644 --- a/extensions/lg_audiogen/README.md +++ b/extensions/lg_audiogen/README.md @@ -43,6 +43,10 @@ lg_audiogen 'dog barking' lg_audiogen -b 'batch.txt' ``` +## O.S Support + +```Tested on Ubuntu 22.04 (Jammy) LTS``` + ## Error Handling If the batch file is not found, a notable error message will be presented. Moreover, if a description is not provided when not using a batch file, a misusage error will be raised. From d5e347ac8e9352eea517d14d8e3e4346d9dd3a4a Mon Sep 17 00:00:00 2001 From: Nate8888 Date: Thu, 16 Nov 2023 21:27:57 -0500 Subject: [PATCH 28/28] Improve ReadME with samples + batch instructions --- extensions/lg_audiogen/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/extensions/lg_audiogen/README.md b/extensions/lg_audiogen/README.md index 5a3ca7d9..dad65c03 100644 --- a/extensions/lg_audiogen/README.md +++ b/extensions/lg_audiogen/README.md @@ -43,6 +43,22 @@ lg_audiogen 'dog barking' lg_audiogen -b 'batch.txt' ``` +### Batch File Format + +The batch file should contain one description per line. The descriptions should be in the same format as the descriptions used in the command-line interface. + +Example: + +*batch.txt* +```txt +Natural sounds of a rainforest +Bird Chirping in the background +``` + +### Samples + +[Google Drive Folder](https://drive.google.com/drive/folders/1kdWB1CBog4NGVJ7jWddKLtBAuPm3gwDq?usp=drive_link) + ## O.S Support ```Tested on Ubuntu 22.04 (Jammy) LTS```