| | import gradio as gr |
| | import torch |
| | import numpy as np |
| | from transformers import AutoProcessor, MusicgenForConditionalGeneration |
| |
|
| | |
| | processor = AutoProcessor.from_pretrained("facebook/musicgen-small") |
| | model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small") |
| |
|
| | device = "cuda" if torch.cuda.is_available() else "cpu" |
| | model.to(device) |
| |
|
| | def generate_music(prompt, duration): |
| | inputs = processor( |
| | text=[prompt], |
| | padding=True, |
| | return_tensors="pt" |
| | ).to(device) |
| |
|
| | |
| | |
| | generated_audio = model.generate( |
| | **inputs, |
| | do_sample=True, |
| | guidance_scale=3, |
| | max_new_tokens=duration * 50 |
| | ) |
| |
|
| | |
| | audio = generated_audio[0].cpu().numpy() |
| | sampling_rate = 32000 |
| |
|
| | return (audio, sampling_rate) |
| |
|
| | |
| | demo = gr.Interface( |
| | fn=generate_music, |
| | inputs=[ |
| | gr.Textbox(lines=2, label="Describe your vibe (e.g., 'lofi chill with rain and piano')"), |
| | gr.Slider(5, 30, value=10, step=5, label="Duration (seconds)") |
| | ], |
| | outputs=gr.Audio(label="Generated Music"), |
| | title="🎷 LoFiJazz Agent", |
| | description="AI agent that composes lo-fi and jazz-style music based on your vibe prompt." |
| | ) |
| |
|
| | demo.launch() |
| |
|