Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
Gradio app for classical music generation with a grid-based Transformer.
|
| 3 |
-
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
@@ -10,9 +10,8 @@ import tempfile
|
|
| 10 |
from typing import Tuple
|
| 11 |
import gradio as gr
|
| 12 |
import numpy as np
|
| 13 |
-
import fluidsynth
|
| 14 |
import pretty_midi
|
| 15 |
-
from
|
| 16 |
|
| 17 |
import config
|
| 18 |
from inference import generate_midi_file
|
|
@@ -135,7 +134,6 @@ function() {
|
|
| 135 |
let cellClass = `btn-${family}-${row}-${col}`;
|
| 136 |
let buttons = document.querySelectorAll('.' + cellClass);
|
| 137 |
|
| 138 |
-
// Flip internal visual arrays tracking memory
|
| 139 |
let currentVal = window.studioGrids[family][row][col];
|
| 140 |
let newVal = currentVal === 0 ? 1 : 0;
|
| 141 |
window.studioGrids[family][row][col] = newVal;
|
|
@@ -149,7 +147,6 @@ function() {
|
|
| 149 |
}
|
| 150 |
});
|
| 151 |
|
| 152 |
-
// Push the fully synchronized multi-dimensional array directly to hidden state modules
|
| 153 |
let wrapperId = `json_hidden_${family}`;
|
| 154 |
let container = document.getElementById(wrapperId);
|
| 155 |
if (container) {
|
|
@@ -194,12 +191,11 @@ def execute_pipeline(strings_json: str, woodwinds_json: str, brass_json: str, pe
|
|
| 194 |
midi_path = os.path.join(tmpdir, "generated.mid")
|
| 195 |
audio_path = os.path.join(tmpdir, "generated.wav")
|
| 196 |
|
| 197 |
-
# Safe parsing loops to guarantee structural grid sizing matching core layouts
|
| 198 |
def load_safe_grid(json_str, target_rows):
|
| 199 |
if not json_str or json_str.strip() == "":
|
| 200 |
-
return np.zeros((target_rows, config.INPUT_STEPS
|
| 201 |
try:
|
| 202 |
-
parsed = np.array(json.loads(json_str), dtype=np.float32)
|
| 203 |
if parsed.shape != (target_rows, config.INPUT_STEPS):
|
| 204 |
return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32)
|
| 205 |
return parsed
|
|
@@ -213,7 +209,7 @@ def execute_pipeline(strings_json: str, woodwinds_json: str, brass_json: str, pe
|
|
| 213 |
|
| 214 |
def to_backend_layout(numpy_grid, r_dims):
|
| 215 |
reshaped = numpy_grid.reshape(r_dims, config.INPUT_BARS, config.STEPS_PER_BAR)
|
| 216 |
-
return np.transpose(reshaped, (1, 2, 0))
|
| 217 |
|
| 218 |
s_seed = to_backend_layout(s_arr, config.PITCHES_MELODIC)
|
| 219 |
w_seed = to_backend_layout(w_arr, config.PITCHES_MELODIC)
|
|
@@ -226,35 +222,18 @@ def execute_pipeline(strings_json: str, woodwinds_json: str, brass_json: str, pe
|
|
| 226 |
midi_out_path=midi_path, device="cpu", primary_family=lead
|
| 227 |
)
|
| 228 |
|
| 229 |
-
#
|
| 230 |
try:
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
# Instantiate FluidSynth core instance context block
|
| 235 |
-
fs = fluidsynth.Synth()
|
| 236 |
-
sfid = fs.sfload("soundfont.sf2")
|
| 237 |
-
fs.program_select(0, sfid, 0, 0)
|
| 238 |
-
|
| 239 |
-
# Calculate maximum sample array size limits safely based on total generated track time
|
| 240 |
-
duration = pm.get_end_time()
|
| 241 |
-
total_samples = int(duration * sample_rate) + (sample_rate * 2)
|
| 242 |
-
|
| 243 |
-
# Allocate flat block arrays
|
| 244 |
-
synth_buffer = np.zeros(total_samples, dtype=np.float32)
|
| 245 |
-
|
| 246 |
-
# Instruct FluidSynth to process sequential MIDI events directly into data loops
|
| 247 |
-
# This bypasses all external platform sound card system layers
|
| 248 |
-
audio_data = pm.fluidsynth(fs=sample_rate)
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
wavfile.write(audio_path, sample_rate, (audio_data * 32767).astype(np.int16))
|
| 254 |
-
else:
|
| 255 |
-
wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16))
|
| 256 |
except Exception as e:
|
| 257 |
-
print(f"[
|
|
|
|
|
|
|
| 258 |
wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16))
|
| 259 |
|
| 260 |
return midi_path, (audio_path if os.path.exists(audio_path) else None)
|
|
@@ -265,6 +244,12 @@ def execute_pipeline(strings_json: str, woodwinds_json: str, brass_json: str, pe
|
|
| 265 |
|
| 266 |
with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as demo:
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
gr.HTML("""
|
| 269 |
<div style="text-align:center; padding:15px 0;">
|
| 270 |
<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;">
|
|
@@ -294,12 +279,6 @@ with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as de
|
|
| 294 |
</div>
|
| 295 |
""")
|
| 296 |
|
| 297 |
-
# Hidden textareas acting as continuous 2D structured JSON bridges to Python
|
| 298 |
-
strings_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_strings")
|
| 299 |
-
woodwinds_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_woodwinds")
|
| 300 |
-
brass_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_brass")
|
| 301 |
-
perc_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_perc")
|
| 302 |
-
|
| 303 |
# Interactive Grid Displays Mapping Sections
|
| 304 |
with gr.Row():
|
| 305 |
with gr.Column(scale=1):
|
|
@@ -312,11 +291,11 @@ with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as de
|
|
| 312 |
with gr.Row():
|
| 313 |
with gr.Column(scale=3):
|
| 314 |
with gr.Row():
|
| 315 |
-
target_minutes = gr.Slider(minimum=1.0, maximum=5.0, value=2.0, step=0.5, label="
|
| 316 |
-
temperature = gr.Slider(minimum=0.7, maximum=1.3, value=1.0, step=0.05, label="
|
| 317 |
with gr.Row():
|
| 318 |
-
top_k = gr.Slider(minimum=2, maximum=12, value=6, step=1, label="
|
| 319 |
-
primary_family = gr.Dropdown(choices=["strings", "woodwinds", "brass"], value="strings", label="
|
| 320 |
|
| 321 |
generate_button = gr.Button("🎵 Execute Transformer Music Generation Pipeline", variant="primary")
|
| 322 |
|
|
|
|
| 1 |
"""
|
| 2 |
Gradio app for classical music generation with a grid-based Transformer.
|
| 3 |
+
Utilizes a native HTML/JS matrix selection console coupled with a stable
|
| 4 |
+
midi2audio system path renderer to guarantee perfect playback on Hugging Face.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 10 |
from typing import Tuple
|
| 11 |
import gradio as gr
|
| 12 |
import numpy as np
|
|
|
|
| 13 |
import pretty_midi
|
| 14 |
+
from midi2audio import FluidSynth
|
| 15 |
|
| 16 |
import config
|
| 17 |
from inference import generate_midi_file
|
|
|
|
| 134 |
let cellClass = `btn-${family}-${row}-${col}`;
|
| 135 |
let buttons = document.querySelectorAll('.' + cellClass);
|
| 136 |
|
|
|
|
| 137 |
let currentVal = window.studioGrids[family][row][col];
|
| 138 |
let newVal = currentVal === 0 ? 1 : 0;
|
| 139 |
window.studioGrids[family][row][col] = newVal;
|
|
|
|
| 147 |
}
|
| 148 |
});
|
| 149 |
|
|
|
|
| 150 |
let wrapperId = `json_hidden_${family}`;
|
| 151 |
let container = document.getElementById(wrapperId);
|
| 152 |
if (container) {
|
|
|
|
| 191 |
midi_path = os.path.join(tmpdir, "generated.mid")
|
| 192 |
audio_path = os.path.join(tmpdir, "generated.wav")
|
| 193 |
|
|
|
|
| 194 |
def load_safe_grid(json_str, target_rows):
|
| 195 |
if not json_str or json_str.strip() == "":
|
| 196 |
+
return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32)
|
| 197 |
try:
|
| 198 |
+
parsed = np.array(json.loads(json_str), dtype=np.float32)
|
| 199 |
if parsed.shape != (target_rows, config.INPUT_STEPS):
|
| 200 |
return np.zeros((target_rows, config.INPUT_STEPS), dtype=np.float32)
|
| 201 |
return parsed
|
|
|
|
| 209 |
|
| 210 |
def to_backend_layout(numpy_grid, r_dims):
|
| 211 |
reshaped = numpy_grid.reshape(r_dims, config.INPUT_BARS, config.STEPS_PER_BAR)
|
| 212 |
+
return np.transpose(reshaped, (1, 2, 0))
|
| 213 |
|
| 214 |
s_seed = to_backend_layout(s_arr, config.PITCHES_MELODIC)
|
| 215 |
w_seed = to_backend_layout(w_arr, config.PITCHES_MELODIC)
|
|
|
|
| 222 |
midi_out_path=midi_path, device="cpu", primary_family=lead
|
| 223 |
)
|
| 224 |
|
| 225 |
+
# HIGH-PERFORMANCE LINUX CONTAINER SYNTHESIS PIPELINE
|
| 226 |
try:
|
| 227 |
+
# Absolute structural path pointing straight to the global packages.txt system soundfont
|
| 228 |
+
soundfont_path = "/usr/share/sounds/sf2/FluidR3_GM.sf2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
# Instantiate FluidSynth wrapper targeting the shared library context directly
|
| 231 |
+
fs = FluidSynth(soundfont_path=soundfont_path)
|
| 232 |
+
fs.midi_to_audio(midi_path, audio_path)
|
|
|
|
|
|
|
|
|
|
| 233 |
except Exception as e:
|
| 234 |
+
print(f"[FluidSynth Wrapper Failure Exception]: {e}")
|
| 235 |
+
# Build safety empty wave chunk if rendering fails
|
| 236 |
+
from scipy.io import wavfile
|
| 237 |
wavfile.write(audio_path, 44100, np.zeros(44100, dtype=np.int16))
|
| 238 |
|
| 239 |
return midi_path, (audio_path if os.path.exists(audio_path) else None)
|
|
|
|
| 244 |
|
| 245 |
with gr.Blocks(css=CUSTOM_DAW_CSS, title="Classical Music Grid Generator") as demo:
|
| 246 |
|
| 247 |
+
# Hidden textareas acting as continuous 2D structured JSON bridges to Python
|
| 248 |
+
strings_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_strings")
|
| 249 |
+
woodwinds_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_woodwinds")
|
| 250 |
+
brass_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_brass")
|
| 251 |
+
perc_json = gr.Textbox(value="", visible=False, elem_id="json_hidden_perc")
|
| 252 |
+
|
| 253 |
gr.HTML("""
|
| 254 |
<div style="text-align:center; padding:15px 0;">
|
| 255 |
<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;">
|
|
|
|
| 279 |
</div>
|
| 280 |
""")
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
# Interactive Grid Displays Mapping Sections
|
| 283 |
with gr.Row():
|
| 284 |
with gr.Column(scale=1):
|
|
|
|
| 291 |
with gr.Row():
|
| 292 |
with gr.Column(scale=3):
|
| 293 |
with gr.Row():
|
| 294 |
+
target_minutes = gr.Slider(minimum=1.0, maximum=5.0, value=2.0, step=0.5, label="Target Playback Length (Minutes)")
|
| 295 |
+
temperature = gr.Slider(minimum=0.7, maximum=1.3, value=1.0, step=0.05, label="Creativity Temperature")
|
| 296 |
with gr.Row():
|
| 297 |
+
top_k = gr.Slider(minimum=2, maximum=12, value=6, step=1, label="Top-K Distribution Filter")
|
| 298 |
+
primary_family = gr.Dropdown(choices=["strings", "woodwinds", "brass"], value="strings", label="Melodic Ensemble Driver Lead")
|
| 299 |
|
| 300 |
generate_button = gr.Button("🎵 Execute Transformer Music Generation Pipeline", variant="primary")
|
| 301 |
|