Spaces:
Sleeping
Sleeping
Download app.py from hephaestusonfire/Music_Generator: direct link, hf CLI and curl.
- Browser
- Download file 14.6 kB
-
https://huggingface.co/spaces/hephaestusonfire/Music_Generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/hephaestusonfire/Music_Generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hephaestusonfire/Music_Generator/resolve/main/app.py
14.6 kB
| """ | |
| Gradio app for classical music generation with a grid-based Transformer. | |
| Fully optimized with a client-side HTML/JS matrix, robust hidden state bridges, | |
| and an absolute xvfb-wrapped FluidSynth audio synthesis engine for Hugging Face Spaces. | |
| """ | |
| import os | |
| import json | |
| import tempfile | |
| import subprocess | |
| from typing import Tuple | |
| import gradio as gr | |
| import numpy as np | |
| import config | |
| from inference import generate_midi_file | |
| # ============================================================================== | |
| # UI Constants & Color Mappings | |
| # ============================================================================== | |
| _MELODIC_NOTE_NAMES = ["C4","C#4","D4","D#4","E4","F4","F#4","G4","G#4","A4","A#4","B4"] | |
| _PERC_NOTE_NAMES = ["Kick", "Snare", "Hi-hat"] | |
| # Custom CSS for high-fidelity dark studio layout highlights | |
| CUSTOM_DAW_CSS = """ | |
| .instrument-container { | |
| background-color: #151b26 !important; | |
| border: 1px solid rgba(255, 255, 255, 0.05) !important; | |
| padding: 14px !important; | |
| border-radius: 8px !important; | |
| margin-bottom: 15px; | |
| } | |
| .matrix-row-layout { | |
| display: flex; | |
| gap: 0px; | |
| margin-bottom: 2px; | |
| align-items: center; | |
| } | |
| .grid-label { | |
| font-family: monospace !important; | |
| font-size: 11px !important; | |
| font-weight: bold !important; | |
| color: #8892aa !important; | |
| width: 50px; | |
| text-align: right; | |
| padding-right: 8px; | |
| box-sizing: border-box; | |
| } | |
| .cells-container { | |
| display: flex; | |
| flex: 1; | |
| gap: 2px; | |
| } | |
| .matrix-btn { | |
| flex: 1; | |
| height: 24px; | |
| border: 1px solid rgba(255,255,255,0.03); | |
| border-radius: 2px; | |
| cursor: pointer; | |
| background: #111827; | |
| transition: all 0.1s; | |
| padding: 0 !important; | |
| } | |
| .matrix-btn:hover { | |
| background: rgba(255,255,255,0.08); | |
| } | |
| .matrix-btn.bar-line { | |
| border-left: 2px solid rgba(255, 255, 255, 0.25); | |
| } | |
| /* Explicit Color Highlights applied using the custom selection token classes */ | |
| .matrix-btn.selected.strings { background-color: #63b3ed !important; box-shadow: inset 0 0 4px #fff !important; } | |
| .matrix-btn.selected.woodwinds { background-color: #6ed29b !important; box-shadow: inset 0 0 4px #fff !important; } | |
| .matrix-btn.selected.brass { background-color: #fbbf24 !important; box-shadow: inset 0 0 4px #fff !important; } | |
| .matrix-btn.selected.perc { background-color: #fc814a !important; box-shadow: inset 0 0 4px #fff !important; } | |
| """ | |
| # Client-side Web Audio synthesis framework & matrix synchronizer state controller | |
| JS_AUDIO_SYNTH = """ | |
| function() { | |
| window.AudioContext = window.AudioContext || window.webkitAudioContext; | |
| let audioCtx = null; | |
| // In-memory JavaScript replication models mapping grid dimensional requirements | |
| window.studioGrids = { | |
| "strings": Array(12).fill(0).map(() => Array(24).fill(0)), | |
| "woodwinds": Array(12).fill(0).map(() => Array(24).fill(0)), | |
| "brass": Array(12).fill(0).map(() => Array(24).fill(0)), | |
| "perc": Array(3).fill(0).map(() => Array(24).fill(0)) | |
| }; | |
| function triggerTone(row, isPerc) { | |
| if (!audioCtx) audioCtx = new AudioContext(); | |
| if (audioCtx.state === 'suspended') audioCtx.resume(); | |
| let now = audioCtx.currentTime; | |
| let gainNode = audioCtx.createGain(); | |
| gainNode.connect(audioCtx.destination); | |
| if (isPerc) { | |
| let bufSize = Math.floor(audioCtx.sampleRate * 0.12); | |
| let buffer = audioCtx.createBuffer(1, bufSize, audioCtx.sampleRate); | |
| let data = buffer.getChannelData(0); | |
| for(let i=0; i<bufSize; i++) data[i] = Math.random() * 2 - 1; | |
| let src = audioCtx.createBufferSource(); src.buffer = buffer; | |
| let flt = audioCtx.createBiquadFilter(); flt.type = 'bandpass'; | |
| if (row === 0) { flt.frequency.setValueAtTime(60, now); gainNode.gain.setValueAtTime(0.7, now); } | |
| else if (row === 1) { flt.frequency.setValueAtTime(1000, now); gainNode.gain.setValueAtTime(0.4, now); } | |
| else { flt.frequency.setValueAtTime(7000, now); gainNode.gain.setValueAtTime(0.2, now); } | |
| src.connect(flt); flt.connect(gainNode); | |
| src.start(now); src.stop(now + 0.12); | |
| } else { | |
| let freq = 440 * Math.pow(2, ((60 + row) - 69) / 12); | |
| let osc = audioCtx.createOscillator(); osc.type = 'triangle'; | |
| osc.frequency.setValueAtTime(freq, now); | |
| gainNode.gain.setValueAtTime(0.2, now); | |
| gainNode.gain.exponentialRampToValueAtTime(0.001, now + 0.25); | |
| osc.connect(gainNode); | |
| osc.start(now); osc.stop(now + 0.25); | |
| } | |
| } | |
| window.toggleStudioCell = function(family, row, col, isPerc) { | |
| let cellClass = `btn-${family}-${row}-${col}`; | |
| let buttons = document.querySelectorAll('.' + cellClass); | |
| // Flip internal visual arrays tracking memory | |
| let currentVal = window.studioGrids[family][row][col]; | |
| let newVal = currentVal === 0 ? 1 : 0; | |
| window.studioGrids[family][row][col] = newVal; | |
| buttons.forEach(btn => { | |
| if (newVal === 1) { | |
| btn.classList.add('selected'); | |
| triggerTone(row, isPerc === 1); | |
| } else { | |
| btn.classList.remove('selected'); | |
| } | |
| }); | |
| // Push the fully synchronized multi-dimensional array directly to hidden state modules | |
| let wrapperId = `json_hidden_${family}`; | |
| let container = document.getElementById(wrapperId); | |
| if (container) { | |
| let textarea = container.querySelector('textarea'); | |
| if (textarea) { | |
| let nativeValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set; | |
| nativeValueSetter.call(textarea, JSON.stringify(window.studioGrids[family])); | |
| textarea.dispatchEvent(new Event('input', { bubbles: true })); | |
| } | |
| } | |
| }; | |
| } | |
| """ | |
| def generate_html_grid(family_id: str, max_rows: int, labels: list, is_perc: bool = False) -> str: | |
| """Generates an embedded raw grid block structure to preserve look and feel.""" | |
| html = f"<div class='instrument-container'>" | |
| html += f"<div style='color: #c9d1e8; font-weight: bold; margin-bottom: 8px;'>{family_id.upper()} TIMELINE</div>" | |
| for r in reversed(range(max_rows)): | |
| html += "<div class='matrix-row-layout'>" | |
| html += f"<div class='grid-label'>{labels[r]}</div>" | |
| html += "<div class='cells-container'>" | |
| for c in range(config.INPUT_STEPS): | |
| is_bar_start = (c > 0 and c % config.STEPS_PER_BAR == 0) | |
| cls = f"matrix-btn {family_id} btn-{family_id}-{r}-{c}" | |
| if is_bar_start: | |
| cls += " bar-line" | |
| html += f"<button class='{cls}' onclick='toggleStudioCell(\"{family_id}\", {r}, {c}, {1 if is_perc else 0})'></button>" | |
| html += "</div></div>" | |
| html += "</div>" | |
| return html | |
| # ============================================================================== | |
| # Pipeline Adapters & Audio Synthesis Verification Logic | |
| # ============================================================================== | |
| def execute_pipeline(strings_json: str, woodwinds_json: str, brass_json: str, perc_json: str, length: float, temp: float, tk: float, lead: str) -> Tuple[str, str]: | |
| """Compiles MIDI layout parameters and synthesizes a high-fidelity output wave track.""" | |
| tmpdir = tempfile.mkdtemp() | |
| midi_path = os.path.join(tmpdir, "generated.mid") | |
| audio_path = os.path.join(tmpdir, "generated.wav") | |
| # Safe parsing loops to guarantee structural grid sizing matching core layouts | |
| def load_safe_grid(json_str, target_rows): | |
| if not json_str or json_str.strip() == "": | |
| return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32) | |
| try: | |
| parsed = np.array(json.loads(json_str), dtype=np.float32) | |
| if parsed.shape != (target_rows, config.INPUT_STEPS): | |
| return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32) | |
| return parsed | |
| except Exception: | |
| return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32) | |
| s_arr = load_safe_grid(strings_json, config.PITCHES_MELODIC) | |
| w_arr = load_safe_grid(woodwinds_json, config.PITCHES_MELODIC) | |
| b_arr = load_safe_grid(brass_json, config.PITCHES_MELODIC) | |
| p_arr = load_safe_grid(perc_json, config.PITCHES_PERC) | |
| def to_backend_layout(numpy_grid, r_dims): | |
| reshaped = numpy_grid.reshape(r_dims, config.INPUT_BARS, config.STEPS_PER_BAR) | |
| return np.transpose(reshaped, (1, 2, 0)) | |
| s_seed = to_backend_layout(s_arr, config.PITCHES_MELODIC) | |
| w_seed = to_backend_layout(w_arr, config.PITCHES_MELODIC) | |
| b_seed = to_backend_layout(b_arr, config.PITCHES_MELODIC) | |
| p_seed = to_backend_layout(p_arr, config.PITCHES_PERC) | |
| midi_path = generate_midi_file( | |
| strings_seed=s_seed, woodwinds_seed=w_seed, brass_seed=b_seed, perc_seed=p_seed, | |
| target_minutes=length, temperature=temp, top_k=int(tk), | |
| midi_out_path=midi_path, device="cpu", primary_family=lead | |
| ) | |
| # HIGH-PERFORMANCE LINUX CONTAINER SYNTHESIS PIPELINE (HEADLESS SAFE) | |
| try: | |
| soundfont_path = "/usr/share/sounds/sf2/FluidR3_GM.sf2" | |
| # Wrap execution via an isolated virtual display framebuffer to intercept hardware driver errors | |
| if os.path.exists(midi_path) and os.path.getsize(midi_path) > 0: | |
| subprocess.run([ | |
| "xvfb-run", "fluidsynth", "-ni", soundfont_path, | |
| midi_path, "-F", audio_path, "-r", "44100" | |
| ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
| else: | |
| raise FileNotFoundError("MIDI engine returned empty array space outputs.") | |
| except Exception as e: | |
| print(f"[FluidSynth Subprocess Failure, writing blank signal map]: {e}") | |
| from scipy.io import wavfile | |
| wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16)) | |
| # CRITICAL TRACK ALIGNMENT: Returns parameters to perfectly match interface destinations | |
| return midi_path, audio_path | |
| # ============================================================================== | |
| # UI Window Context Orchestration | |
| # ============================================================================== | |
| with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as demo: | |
| # Hidden textareas acting as continuous 2D structured JSON bridges to Python | |
| strings_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_strings") | |
| woodwinds_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_woodwinds") | |
| brass_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_brass") | |
| perc_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_perc") | |
| gr.HTML(""" | |
| <div style="text-align:center; padding:15px 0;"> | |
| <h1 style="font-size:2.2rem; font-weight:800; background:linear-gradient(135deg, #63b3ed 0%, #9b5de5 50%, #fc814a 100%); -webkit-background-clip:text; -webkit-text-fill-color:transparent; margin:0 0 5px 0;"> | |
| 🎼 Classical Music Grid Generator | |
| </h1> | |
| <p style="color:#8892aa; font-size:14px; max-width:650px; margin:0 auto;"> | |
| Click cells inside the instrument matrix panels to assign note frameworks natively across all three bars. | |
| </p> | |
| </div> | |
| """) | |
| with gr.Accordion("📖 Setup Blueprint & Manual Guide", open=True): | |
| gr.HTML(""" | |
| <div style="display:grid; grid-template-columns:repeat(auto-fit,minmax(220px,1fr)); gap:12px; padding:6px 0;"> | |
| <div style="background:rgba(99,179,237,0.06); border:1px solid rgba(99,179,237,0.2); border-radius:8px; padding:12px;"> | |
| <strong style="color:#63b3ed;">1. Paint Framework Motifs</strong> | |
| <p style="color:#8892aa; font-size:12px; margin:4px 0 0; line-height:1.4;"> | |
| Click grid boxes to toggle note triggers across all 3 bars. Highlights display instantly and remain fixed when clicked. | |
| </p> | |
| </div> | |
| <div style="background:rgba(252,129,74,0.06); border:1px solid rgba(252,129,74,0.2); border-radius:8px; padding:12px;"> | |
| <strong style="color:#fc814a;">2. Compile & Stream Live</strong> | |
| <p style="color:#8892aa; font-size:12px; margin:4px 0 0; line-height:1.4;"> | |
| Adjust target duration parameters and lead options, then click generate. Use the inline media layout player to listen live. | |
| </p> | |
| </div> | |
| </div> | |
| """) | |
| # Interactive Grid Displays Mapping Sections | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.HTML(generate_html_grid("strings", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False)) | |
| gr.HTML(generate_html_grid("woodwinds", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False)) | |
| with gr.Column(scale=1): | |
| gr.HTML(generate_html_grid("brass", config.PITCHES_MELODIC, _MELODIC_NOTE_NAMES, is_perc=False)) | |
| gr.HTML(generate_html_grid("perc", config.PITCHES_PERC, _PERC_NOTE_NAMES, is_perc=True)) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| with gr.Row(): | |
| target_minutes = gr.Slider(minimum=1.0, maximum=5.0, value=2.0, step=0.5, label="Target Playback Length (Minutes)") | |
| temperature = gr.Slider(minimum=0.7, maximum=1.3, value=1.0, step=0.05, label="Creativity Temperature") | |
| with gr.Row(): | |
| top_k = gr.Slider(minimum=2, maximum=12, value=6, step=1, label="Top-K Distribution Filter") | |
| primary_family = gr.Dropdown(choices=["strings", "woodwinds", "brass"], value="strings", label="Melodic Ensemble Driver Lead") | |
| generate_button = gr.Button("🎵 Execute Transformer Music Generation Pipeline", variant="primary") | |
| with gr.Column(scale=2): | |
| # Target nodes matched exactly to avoid browser type serialization crashes | |
| midi_output = gr.File(label="⬇ Download Output Standard MIDI File (.mid)") | |
| audio_output = gr.Audio(label="🔊 Fluidsynth Generated Audio Player (Listen Live)", type="filepath") | |
| generate_button.click( | |
| fn=execute_pipeline, | |
| inputs=[strings_json, woodwinds_json, brass_json, perc_json, target_minutes, temperature, top_k, primary_family], | |
| outputs=[midi_output, audio_output] | |
| ) | |
| demo.load(None, js=JS_AUDIO_SYNTH) | |
| if __name__ == "__main__": | |
| demo.launch() |