Spaces:
Runtime error
Runtime error
File size: 16,467 Bytes
8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f 20bbae4 8ac663f e51a8ee 8ac663f 20bbae4 e51a8ee 8ac663f 20bbae4 e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f 20bbae4 f523d1c 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f e51a8ee 8ac663f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | import gradio as gr
import torch
import numpy as np
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
import os
import tempfile
from typing import Tuple, Optional
import soundfile as sf
# Global variables for model caching
_model = None
_tokenizer = None
_processor = None
def load_model():
"""Lazy load the SongGen model"""
global _model, _tokenizer, _processor
if _model is None or _tokenizer is None:
print("Loading SongGen model...")
# SongGen model from Hugging Face
# Using the open-source SongGen implementation
model_name = "songgen/songgen-base" # or alternative SongGen model
try:
# Try loading SongGen model
# SongGen uses a different architecture than MusicGen
_tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=True
)
_processor = AutoProcessor.from_pretrained(
model_name,
trust_remote_code=True
)
_model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float32,
low_cpu_mem_usage=True,
trust_remote_code=True
)
# Move to CPU
_model = _model.to("cpu")
_model.eval()
print("SongGen model loaded successfully!")
except Exception as e:
print(f"Primary model load failed: {e}")
print("Attempting fallback to alternative SongGen implementation...")
try:
# Alternative: Try loading from different repo or local path
# Some SongGen implementations use diffusers
from diffusers import DiffusionPipeline
import torch
# Try alternative SongGen loading
_model = DiffusionPipeline.from_pretrained(
"songgen/songgen-v1", # alternative name
torch_dtype=torch.float32,
use_safetensors=True,
variant="fp32"
)
_model = _model.to("cpu")
_tokenizer = None # Diffusers pipeline doesn't use tokenizer the same way
_processor = None
print("SongGen (diffusers) loaded successfully!")
except Exception as e2:
print(f"Alternative load also failed: {e2}")
# Final fallback: simulate with a message but don't crash
raise gr.Error(
f"Failed to load SongGen model. Please ensure you have access to the model "
f"or check your internet connection. Error: {str(e2)}"
)
return _model, _tokenizer, _processor
def generate_music(
prompt: str,
duration: float,
guidance_scale: float,
num_inference_steps: int,
temperature: float,
top_k: int,
top_p: float,
progress: gr.Progress = gr.Progress()
) -> Tuple[Optional[str], str]:
"""
Generate music based on text prompt using SongGen model.
Args:
prompt: Text description of the music to generate
duration: Duration of generated audio in seconds
guidance_scale: Controls adherence to prompt vs diversity
num_inference_steps: Number of denoising steps
temperature: Controls randomness in generation
top_k: Top-k sampling parameter
top_p: Nucleus sampling parameter
Returns:
Tuple of (audio_file_path, status_message)
"""
if not prompt or not prompt.strip():
return None, "β Please enter a prompt describing the music you want to generate."
try:
progress(0.1, desc="Loading SongGen model...")
model, tokenizer, processor = load_model()
progress(0.2, desc="Preparing inputs...")
# Calculate number of tokens based on duration
# SongGen typically uses ~50 tokens per second at 32kHz
sample_rate = 32000
num_tokens = int(duration * 50)
# Prepare prompt with SongGen-specific formatting
# SongGen often uses special tokens or formatting
formatted_prompt = f"Generate music: {prompt}. Duration: {duration}s."
progress(0.3, desc="Encoding prompt...")
# Tokenize input if using transformers-based model
if tokenizer is not None:
inputs = tokenizer(
formatted_prompt,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512
)
else:
# Diffusers pipeline uses different input format
inputs = {"prompt": formatted_prompt}
progress(0.4, desc="Generating music with SongGen...")
# Generate based on model type
with torch.no_grad():
if hasattr(model, 'generate'):
# Transformers-style generation
if tokenizer is not None:
outputs = model.generate(
**inputs,
max_new_tokens=num_tokens,
do_sample=True,
temperature=temperature,
top_k=top_k,
top_p=top_p,
guidance_scale=guidance_scale,
)
# Decode audio tokens to waveform
# SongGen specific decoding
if hasattr(model, 'decode_audio'):
audio = model.decode_audio(outputs)
else:
# Generic fallback - assume outputs are audio tokens
audio = outputs.float().cpu().numpy()
else:
# Diffusers pipeline
outputs = model(
prompt=formatted_prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
audio_length_in_s=duration,
).audios[0]
audio = outputs
else:
# Direct pipeline call for diffusers
result = model(
prompt=formatted_prompt,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
audio_length_in_s=duration,
)
audio = result.audios[0]
progress(0.8, desc="Processing audio...")
# Ensure audio is proper shape
if isinstance(audio, torch.Tensor):
audio = audio.cpu().numpy()
# Handle different output shapes
if audio.ndim == 1:
audio = audio[np.newaxis, :] # Add channel dimension
# Normalize audio
audio = audio / (np.max(np.abs(audio)) + 1e-8)
# Ensure correct sample rate
if audio.shape[-1] / sample_rate < duration * 0.5:
# If audio is too short, we might need to resample or pad
# This shouldn't happen with proper generation
pass
progress(0.9, desc="Saving audio...")
# Save to temporary file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
output_path = tmp_file.name
# Save using soundfile (more reliable than torchaudio for various formats)
sf.write(output_path, audio.T if audio.shape[0] == 1 else audio, sample_rate)
progress(1.0, desc="Complete!")
# Create success message with metadata
actual_duration = audio.shape[-1] / sample_rate if audio.ndim > 1 else len(audio) / sample_rate
info_msg = f"""β
Music generated successfully with SongGen!
π΅ **Prompt:** {prompt}
β±οΈ **Duration:** {actual_duration:.2f}s
ποΈ **Sample Rate:** {sample_rate}Hz
π§ **Settings:** guidance={guidance_scale}, steps={num_inference_steps}, temp={temperature}"""
return output_path, info_msg
except Exception as e:
import traceback
print(f"Error: {e}")
print(traceback.format_exc())
return None, f"β Error generating music: {str(e)}\n\nPlease check that the SongGen model is properly installed and accessible."
def create_examples():
"""Create example prompts for the UI"""
return [
["Upbeat electronic dance music with a strong bass line and energetic synths", 8.0, 3.0, 50, 1.0, 250, 0.99],
["Calm ambient piano music with soft strings, peaceful and relaxing", 10.0, 3.5, 50, 0.8, 250, 0.95],
["Epic orchestral soundtrack with brass and percussion, cinematic and dramatic", 10.0, 4.0, 50, 1.0, 250, 0.99],
["Lo-fi hip hop beats with jazzy chords, chill and study music", 8.0, 2.5, 50, 0.9, 250, 0.95],
["Acoustic guitar folk melody, warm and nostalgic", 6.0, 3.0, 50, 0.85, 250, 0.95],
["Cyberpunk synthwave with retro 80s vibes, driving and energetic", 8.0, 3.0, 50, 1.0, 250, 0.99],
]
# Custom theme for modern UI
custom_theme = gr.themes.Soft(
primary_hue="indigo",
secondary_hue="violet",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="lg"
).set(
button_primary_background_fill="*primary_600",
button_primary_background_fill_hover="*primary_700",
block_title_text_weight="600",
block_title_text_size="*text_lg",
block_background_fill="*neutral_50",
block_border_width="1px",
block_border_color="*neutral_200",
)
# Gradio 6: title goes in Blocks, NOT in launch()
with gr.Blocks(title="SongGen AI Music Generator") as demo:
# Header with branding
with gr.Row():
with gr.Column():
gr.Markdown("""
# π΅ SongGen AI Music Generator
Generate custom music from text descriptions using **SongGen** -
a state-of-the-art neural audio generation model.
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
""")
with gr.Row():
# Left panel - Controls
with gr.Column(scale=1):
gr.Markdown("### ποΈ Generation Settings")
prompt_input = gr.Textbox(
label="Music Description",
placeholder="Describe the music you want to generate...",
lines=3,
info="Be specific about genre, instruments, mood, and tempo",
value="Upbeat electronic dance music with energetic synths and strong bass"
)
with gr.Accordion("Advanced Settings", open=False):
duration_slider = gr.Slider(
minimum=3,
maximum=20,
value=8,
step=0.5,
label="Duration (seconds)",
info="Longer durations take more time to generate"
)
guidance_slider = gr.Slider(
minimum=1.0,
maximum=10.0,
value=3.0,
step=0.5,
label="Guidance Scale",
info="Higher = more prompt adherence, less diversity"
)
steps_slider = gr.Slider(
minimum=10,
maximum=100,
value=50,
step=5,
label="Inference Steps",
info="More steps = higher quality but slower"
)
temperature_slider = gr.Slider(
minimum=0.1,
maximum=2.0,
value=1.0,
step=0.05,
label="Temperature",
info="Higher = more random/creative"
)
topk_slider = gr.Slider(
minimum=1,
maximum=500,
value=250,
step=10,
label="Top-K",
info="Limits vocabulary for sampling"
)
topp_slider = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.99,
step=0.01,
label="Top-P (Nucleus)",
info="Cumulative probability threshold"
)
generate_btn = gr.Button(
"π΅ Generate Music with SongGen",
variant="primary",
size="lg"
)
# Status and info
status_output = gr.Textbox(
label="Status",
lines=6,
interactive=False
)
# Right panel - Output
with gr.Column(scale=1):
gr.Markdown("### π§ Generated Music")
audio_output = gr.Audio(
label="Generated Audio",
type="filepath",
autoplay=False,
buttons=["download"],
waveform_options=gr.WaveformOptions(
waveform_color="#4f46e5",
waveform_progress_color="#7c3aed",
show_recording_waveform=False,
sample_rate=32000
)
)
# Tips section
with gr.Accordion("π‘ Tips for Better Results", open=True):
gr.Markdown("""
**Prompt Engineering Tips for SongGen:**
1. **Be specific about genre:** "electronic", "classical", "jazz", "rock"
2. **Mention instruments:** "piano", "synthesizers", "drums", "strings"
3. **Describe the mood:** "upbeat", "melancholic", "energetic", "calm"
4. **Add tempo hints:** "fast tempo", "slow ballad", "medium groove"
5. **Use reference styles:** "like 80s synthwave", "cinematic soundtrack"
**SongGen-specific tips:**
- SongGen works best with clear, descriptive prompts
- The model understands musical terminology well
- Try mentioning specific artists or styles for guidance
**Example prompts:**
- "Upbeat pop with catchy synth melody and electronic drums"
- "Sad piano ballad with emotional strings, slow tempo"
- "Heavy metal with distorted guitars and aggressive drums"
""")
# Examples section
gr.Markdown("### π― Quick Examples")
examples = gr.Examples(
examples=create_examples(),
inputs=[prompt_input, duration_slider, guidance_slider, steps_slider,
temperature_slider, topk_slider, topp_slider],
label="Click to load example",
examples_per_page=3
)
# Footer
gr.Markdown("""
---
Made with β€οΈ using Gradio and SongGen |
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
""")
# Event handlers
generate_btn.click(
fn=generate_music,
inputs=[
prompt_input,
duration_slider,
guidance_slider,
steps_slider,
temperature_slider,
topk_slider,
topp_slider
],
outputs=[audio_output, status_output],
api_visibility="public"
)
# Launch with Gradio 6 syntax
demo.launch(
theme=custom_theme,
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
{"label": "Gradio", "url": "https://gradio.app"},
{"label": "Settings", "url": "#"}
],
show_error=True,
quiet=False
) |