Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add music gen example #1028

Merged
merged 10 commits into from
Jan 17, 2025
Merged

Add music gen example #1028

merged 10 commits into from
Jan 17, 2025

Conversation

advay-modal
Copy link
Contributor

@advay-modal advay-modal commented Dec 30, 2024

2nd PR to split out boombot into 2 diff examples

Type of Change

  • New example

Checklist

  • Example is testable in synthetic monitoring system, or lambda-test: false is added to example frontmatter (---)
    • Example is tested by executing with modal run or an alternative cmd is provided in the example frontmatter (e.g. cmd: ["modal", "deploy"])
    • Example is tested by running with no arguments or the args are provided in the example frontmatter (e.g. args: ["--prompt", "Formula for room temperature superconductor:"]
  • Example is documented with comments throughout, in a Literate Programming style.
  • Example does not require third-party dependencies to be installed locally
  • Example pins its dependencies
    • Example pins container images to a stable tag, not a dynamic tag like latest
    • Example specifies a python_version for the base image, if it is used
    • Example pins all dependencies to at least minor version, ~=x.y.z or ==x.y
    • Example dependencies with version < 1 are pinned to patch version, ==0.y.z

Outside contributors

You're great! Thanks for your contribution.

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-2cda415.modal.run

@kristiankauffeld
Copy link

how would you expose this functionality through a web-API endpoint with a custom route like "/predict"?

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-64c8dc7.modal.run

@advay-modal
Copy link
Contributor Author

advay-modal commented Jan 2, 2025

you could do something like this:

@app.function(image=web_image)
@asgi_app()
def ui():
    import uuid
    from fastapi import FastAPI
    web_app = FastAPI()
    model = Audiocraft()
    # Create a temporary directory for audio files
    temp_dir = Path("/tmp/audiocraft")
    temp_dir.mkdir(exist_ok=True, parents=True)

    @web_app.post("/predict")
    def generate_music(
        prompt: str,
        duration: int = 10,
        format: str = "wav",
        melody_url: str = "",
    ):
        clip_audio_bytes, melody_clip_audio_bytes = model.generate.remote(
            prompt, duration, format, melody_url
        )

        # Create a unique filename for this generation
        clip_file = f"{temp_dir}/generated_music_{uuid.uuid4()}.{format}"
        # Save bytes to temporary file that Gradio can serve
        with open(clip_file, "wb") as f:
            f.write(clip_audio_bytes.read())

        melody_clip_file = None
        if melody_clip_audio_bytes is not None:
            melody_clip_file = f"{temp_dir}/melody_clip{uuid.uuid4()}.{format}"
            with open(melody_clip_file, "wb") as f:
                f.write(melody_clip_audio_bytes.read())

        return clip_file, melody_clip_file
    
    return web_app

@kristiankauffeld
Copy link

is it ok if I push a commit to this branch?

@kristiankauffeld
Copy link

made a PR towards this branch

Copy link
Collaborator

@charlesfrye charlesfrye left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly minor nits and comments.

I do think we want to avoid replicating the wav-handling code here, cf the comment on L70.

06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
06_gpu_and_ml/music_gen.py Outdated Show resolved Hide resolved
@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-aebb4a6.modal.run

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-6f563f8.modal.run

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-b5a546f.modal.run

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-8874c05.modal.run

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-a44f069.modal.run

@charlesfrye
Copy link
Collaborator

🚀 The docs preview is ready! Check it out here: https://modal-labs-examples--frontend-preview-eb883b3.modal.run

@charlesfrye charlesfrye merged commit 93ceb44 into main Jan 17, 2025
7 checks passed
@charlesfrye charlesfrye deleted the advay/music-gen branch January 17, 2025 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants