This repository was archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
AudioGen - Implemented Audio Generation [DRAFT PR] #117
Open
Nate8888
wants to merge
28
commits into
facebookresearch:main
Choose a base branch
from
Nate8888:audiocraft-cli-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b130194
Audiocraft CLI Directory & ReadME
Nate8888 fa305d4
initial files
Nate8888 3239fb8
Adds package setup & main driver code
Nate8888 3abca99
Adds initial testing framework pytest
Nate8888 c8d6876
Test initial workflow for AudioGen
Nate8888 5a7e045
improved setup + added requirements
Nate8888 d9b6c48
Implements --description & --duration for audiogen
Nate8888 a6eeb30
tests the creation of the audio file with desc
Nate8888 091c0a4
test workflow with torch install before audiocraft
Nate8888 c314e4f
test workflow with different distribution of torch
Nate8888 6a0ce73
[workflow] - try raw torch, vision, audio
Nate8888 3c32fda
[workflow] - Try downgrading Python
Nate8888 dc2419c
[workflow] - downgrade to match audiocraft + index
Nate8888 15bf85e
[Workflow] adds triple verbose to pytest
Nate8888 3e90fce
tries self-hosted runner on Google Colab
Nate8888 fbed041
test only file creation
Nate8888 4e4f97c
Refactors code, changes argparse to @click, Docstr
Nate8888 3532104
Changes entry point
Nate8888 f4e3ca6
adds batch functionality with file input
Nate8888 10cc1be
Checks if file was created
Nate8888 730579b
linting + consistency
Nate8888 acb9b64
README instructions
Nate8888 a11e54d
Switch from labgraph_audiogen to lg_audiogen
Nate8888 1317661
Add versions + Improve descriptions
Nate8888 9d5bebf
Adds ffmpeg to fix workflow
Nate8888 ecb2d04
fix package name to lg_audiogen
Nate8888 94aee5c
Adds O.S Support on ReadME
Nate8888 d5e347a
Improve ReadME with samples + batch instructions
Nate8888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 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.8' | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| cd extensions/labgraph_audiogen | ||
| python -m pip install --upgrade pip | ||
| 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 -vvv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # 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' | ||
|
Nate8888 marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| ## 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. | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import click | ||
| import torch | ||
| from audiocraft.models import AudioGen | ||
| from audiocraft.data.audio import audio_write | ||
|
|
||
| DEFAULT_AUDIOGEN_MODEL = 'facebook/audiogen-medium' | ||
| DEFAULT_AUDIO_DURATION = 5 | ||
|
|
||
| @click.command() | ||
| @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.') | ||
| @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, 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.") | ||
| 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 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}") | ||
|
|
||
| # 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 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. | ||
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from setuptools import setup, find_packages | ||
|
|
||
| setup( | ||
| name='labgraph_audiogen', | ||
| version='0.1', | ||
| description="Audio generation on labgraph", | ||
| packages=find_packages(), | ||
| install_requires=[ | ||
| 'Click', | ||
|
Nate8888 marked this conversation as resolved.
Outdated
|
||
| "torchaudio", | ||
| "audiocraft", | ||
| ], | ||
| entry_points=''' | ||
| [console_scripts] | ||
| labgraph_audiogen=labgraph_audiogen.main:parse_arguments | ||
| ''', | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import os | ||
| import subprocess | ||
|
|
||
| def test_single_description(): | ||
| ''' | ||
| Tests output with a single description | ||
| ''' | ||
| # Run the script with an example description | ||
| 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") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.