Spaces:
Runtime error
Runtime error
File size: 4,864 Bytes
2f3ec08 22a273f 2f3ec08 22a273f 0e49377 22a273f 0e49377 22a273f 2f3ec08 22a273f 0e49377 22a273f fbf7ea2 0e49377 22a273f 0e49377 22a273f 0e49377 22a273f 25e7ad9 22a273f 25e7ad9 22a273f 0e49377 22a273f 25e7ad9 22a273f 0e49377 22a273f aae3141 2f3ec08 22a273f 2f3ec08 22a273f 2f3ec08 0986f57 2f3ec08 22a273f 2f3ec08 22a273f 2f3ec08 22a273f 2f3ec08 22a273f 2f3ec08 22a273f 2f3ec08 22a273f | 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 | import gradio as gr
import json
import os
import shutil
import subprocess
import sys
def install_and_import(package):
try:
__import__(package)
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
def generate_song(lyrics, audio_file):
if not lyrics.strip():
return None, "β Please enter some lyrics!"
if audio_file is None:
return None, "β Please upload an audio prompt file!"
try:
yield None, "π΅ Installing dependencies..."
# Install required packages on the fly
packages = ["torch", "torchaudio", "transformers", "accelerate", "omegaconf", "lightning", "einops", "numba", "librosa", "soundfile"]
for package in packages:
try:
install_and_import(package)
except:
pass
yield None, "π΅ Setting up SongBloom environment..."
# Create input files
audio_filename = "prompt.wav"
shutil.copy(audio_file, audio_filename)
jsonl_filename = "input.jsonl"
input_data = {
"idx": "1",
"lyrics": lyrics.strip(),
"prompt_wav": audio_filename
}
with open(jsonl_filename, 'w') as f:
json.dump(input_data, f)
yield None, "π΅ Downloading SongBloom model (first time only - 5-10 minutes)..."
# Download SongBloom if not present
if not os.path.exists("songbloom_repo"):
subprocess.run(["git", "clone", "https://github.com/cypress-yang/SongBloom.git", "songbloom_repo"], check=True)
yield None, "π΅ Running AI music generation (3-8 minutes)..."
# Run SongBloom
cmd = ["python3", "songbloom_repo/infer.py", "--input-jsonl", jsonl_filename, "--dtype", "bfloat16"]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1, universal_newlines=True)
for line in process.stdout:
if "it [" in line:
yield None, f"π΅ Generating music... {line.strip()}"
process.wait()
# Look for output files more thoroughly
output_dirs = ["output", "results", "generated", "songbloom_repo/output", "songbloom_repo"]
output_file = None
for output_dir in output_dirs:
if os.path.exists(output_dir):
for file in os.listdir(output_dir):
if file.endswith(('.wav', '.mp3', '.flac')) and not file.startswith('prompt'):
output_file = os.path.join(output_dir, file)
break
if output_file:
break
if output_file and os.path.exists(output_file):
yield output_file, "β
Your AI song is ready! π΅"
else:
yield None, "β Generation completed but output file not found. Check model setup."
except Exception as e:
yield None, f"β Error: {str(e)}"
# Create interface
with gr.Blocks(title="SongBloom AI Music Generator") as demo:
gr.Markdown("""
# π΅ SongBloom AI Music Generator
**Full AI Music Generation - From Lyrics to Complete Songs**
Upload an audio style prompt and enter lyrics to generate professional AI music!
""")
with gr.Row():
with gr.Column():
lyrics_input = gr.Textbox(
label="Song Lyrics",
placeholder="Enter your song lyrics here...\n\nExample:\nHere comes the sun, doo-doo-doo-doo\nHere comes the sun, and I say it's all right",
lines=8,
value="Here comes the sun, doo-doo-doo-doo\nHere comes the sun, and I say it's all right"
)
audio_input = gr.File(
label="Audio Style Prompt (Any Audio Format)",
type="filepath"
)
generate_btn = gr.Button("π΅ Generate Full Song", variant="primary", size="lg")
with gr.Column():
status_output = gr.Textbox(
label="Generation Progress",
lines=8,
interactive=False
)
audio_output = gr.Audio(label="π΅ Your AI-Generated Song")
generate_btn.click(
fn=generate_song,
inputs=[lyrics_input, audio_input],
outputs=[audio_output, status_output],
show_progress=True
)
gr.Markdown("""
---
**β‘ Powered by SongBloom AI - Professional Music Generation**
First generation may take 10-15 minutes (downloading models). Subsequent generations: 3-8 minutes.
""")
if __name__ == "__main__":
demo.launch()
|