Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,146 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""Music Transcriber
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
-
import spaces
|
| 6 |
from pathlib import Path
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
os.environ[
|
| 11 |
-
os.environ[
|
| 12 |
-
os.environ[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
@spaces.GPU(duration=600) # β THIS IS THE KEY FIX
|
| 15 |
-
def transcribe_audio(audio_file, model_size, instruments):
|
| 16 |
if audio_file is None:
|
| 17 |
-
return None, "β Please upload an audio file first.", 0
|
| 18 |
-
|
| 19 |
from muscriptor import TranscriptionModel
|
| 20 |
-
|
| 21 |
-
|
| 22 |
instrument_group = None
|
| 23 |
instrument_names = []
|
| 24 |
if instruments:
|
| 25 |
try:
|
| 26 |
from muscriptor.tokenizer.mt3 import MT3_FULL_PLUS_GROUP_NAMES
|
|
|
|
| 27 |
names = [n.strip().lower() for n in instruments.split(",")]
|
| 28 |
valid_names = [n for n in names if n in MT3_FULL_PLUS_GROUP_NAMES]
|
| 29 |
instrument_group = " ".join(str(MT3_FULL_PLUS_GROUP_NAMES[n]) for n in valid_names)
|
| 30 |
instrument_names = valid_names
|
| 31 |
except Exception:
|
| 32 |
pass
|
| 33 |
-
|
|
|
|
| 34 |
try:
|
| 35 |
model = TranscriptionModel.load_model(model_size)
|
| 36 |
except Exception as e:
|
| 37 |
-
return None, f"β Model load failed: {e}", 0
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
note_events = []
|
| 43 |
-
|
| 44 |
for event in model.transcribe(str(audio_file), instrument_group=instrument_group):
|
| 45 |
-
if hasattr(event,
|
| 46 |
-
pass # Progress tracked internally
|
| 47 |
-
elif hasattr(event, 'note_id'):
|
| 48 |
note_events.append(event)
|
| 49 |
-
|
| 50 |
-
midi_bytes = model.transcribe_to_midi(str(audio_file), instrument_group=instrument_group)
|
| 51 |
-
output_path.write_bytes(midi_bytes)
|
| 52 |
-
|
| 53 |
msg = f"β
Complete!\nπΌ {len(note_events)} notes detected"
|
| 54 |
if instrument_names:
|
| 55 |
msg += f"\nπ Filtered: {', '.join(instrument_names)}"
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
return None, f"β Error: {e}", 0
|
| 63 |
|
| 64 |
def list_instruments():
|
| 65 |
try:
|
| 66 |
from muscriptor.tokenizer.mt3 import MT3_FULL_PLUS_GROUP_NAMES
|
|
|
|
| 67 |
return "\n".join(f"β’ {name}" for name in sorted(MT3_FULL_PLUS_GROUP_NAMES.keys()))
|
| 68 |
except Exception:
|
| 69 |
return "Could not load instrument list."
|
| 70 |
|
|
|
|
|
|
|
| 71 |
with gr.Blocks(title="π΅ Music Transcriber") as demo:
|
| 72 |
-
|
| 73 |
-
gr.Markdown("# π΅ Music Transcriber\nTurn audio into MIDI using AI!")
|
| 74 |
-
|
| 75 |
with gr.Row():
|
|
|
|
| 76 |
with gr.Column(scale=1):
|
| 77 |
-
audio_input = gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
model_select = gr.Radio(
|
| 79 |
-
choices=[
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
-
instrument_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
transcribe_btn = gr.Button("πΉ Transcribe Audio", variant="primary", size="lg")
|
| 86 |
-
|
|
|
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
-
status_output = gr.Textbox(label="Status", lines=
|
| 89 |
-
note_count_output = gr.HTML(
|
|
|
|
|
|
|
| 90 |
midi_output = gr.File(label="Download MIDI", visible=False)
|
| 91 |
-
|
| 92 |
with gr.Accordion("π View Available Instruments", open=False):
|
| 93 |
gr.Markdown(list_instruments())
|
| 94 |
-
|
|
|
|
| 95 |
transcribe_btn.click(
|
| 96 |
fn=transcribe_audio,
|
| 97 |
-
inputs=[audio_input, model_select, instrument_input],
|
| 98 |
-
outputs=[midi_output, status_output, note_count_output]
|
| 99 |
).then(
|
| 100 |
-
lambda path,
|
| 101 |
-
f"<div style=\"font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;\">{notes}</div>"),
|
| 102 |
inputs=[midi_output, note_count_output],
|
| 103 |
-
outputs=[midi_output, note_count_output]
|
| 104 |
)
|
| 105 |
|
|
|
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch(theme=gr.themes.Soft(primary_hue="purple"), show_error=True)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Music Transcriber + Trap Mode for Hugging Face Spaces (ZeroGPU)"""
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
+
import spaces
|
| 6 |
from pathlib import Path
|
| 7 |
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 11 |
+
os.environ["TORCH_HOME"] = "/tmp/hf_cache"
|
| 12 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
| 13 |
+
|
| 14 |
+
from trap_transformer import TrapTransformer
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@spaces.GPU(duration=600)
|
| 18 |
+
def transcribe_audio(audio_file, model_size, instruments, trap_mode=False):
|
| 19 |
+
"""Transcribe audio β MIDI, optionally apply Trap transformation."""
|
| 20 |
|
|
|
|
|
|
|
| 21 |
if audio_file is None:
|
| 22 |
+
return None, "β Please upload an audio file first.", '<div style="font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;">0</div>'
|
| 23 |
+
|
| 24 |
from muscriptor import TranscriptionModel
|
| 25 |
+
|
| 26 |
+
# --- Parse instrument filter ---
|
| 27 |
instrument_group = None
|
| 28 |
instrument_names = []
|
| 29 |
if instruments:
|
| 30 |
try:
|
| 31 |
from muscriptor.tokenizer.mt3 import MT3_FULL_PLUS_GROUP_NAMES
|
| 32 |
+
|
| 33 |
names = [n.strip().lower() for n in instruments.split(",")]
|
| 34 |
valid_names = [n for n in names if n in MT3_FULL_PLUS_GROUP_NAMES]
|
| 35 |
instrument_group = " ".join(str(MT3_FULL_PLUS_GROUP_NAMES[n]) for n in valid_names)
|
| 36 |
instrument_names = valid_names
|
| 37 |
except Exception:
|
| 38 |
pass
|
| 39 |
+
|
| 40 |
+
# --- Load model ---
|
| 41 |
try:
|
| 42 |
model = TranscriptionModel.load_model(model_size)
|
| 43 |
except Exception as e:
|
| 44 |
+
return None, f"β Model load failed: {e}", '<div style="font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;">0</div>'
|
| 45 |
+
|
| 46 |
+
# --- Transcribe ---
|
|
|
|
| 47 |
try:
|
| 48 |
+
midi_bytes = model.transcribe_to_midi(str(audio_file), instrument_group=instrument_group)
|
| 49 |
+
|
| 50 |
+
# Save original MIDI
|
| 51 |
+
original_path = Path(tempfile.mktemp(suffix=".mid"))
|
| 52 |
+
original_path.write_bytes(midi_bytes)
|
| 53 |
+
|
| 54 |
+
# Count notes from streaming pass
|
| 55 |
note_events = []
|
|
|
|
| 56 |
for event in model.transcribe(str(audio_file), instrument_group=instrument_group):
|
| 57 |
+
if hasattr(event, "note_id"):
|
|
|
|
|
|
|
| 58 |
note_events.append(event)
|
| 59 |
+
|
|
|
|
|
|
|
|
|
|
| 60 |
msg = f"β
Complete!\nπΌ {len(note_events)} notes detected"
|
| 61 |
if instrument_names:
|
| 62 |
msg += f"\nπ Filtered: {', '.join(instrument_names)}"
|
| 63 |
+
|
| 64 |
+
# --- Trap transformation ---
|
| 65 |
+
if trap_mode:
|
| 66 |
+
trap_path = Path(tempfile.mktemp(suffix="_trap.mid"))
|
| 67 |
+
transformer = TrapTransformer(bpm=140)
|
| 68 |
+
transformer.transform_to_trap(original_path, str(trap_path))
|
| 69 |
+
original_path.unlink(missing_ok=True)
|
| 70 |
+
msg += "\nπ₯ Trap mode applied (808s, hi-hats, half-time drums)"
|
| 71 |
+
return str(trap_path), msg, f'<div style="font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;">{len(note_events)}</div>'
|
| 72 |
+
|
| 73 |
+
return str(original_path), msg, f'<div style="font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;">{len(note_events)}</div>'
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
+
return None, f"β Error: {e}", '<div style="font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;">0</div>'
|
| 77 |
+
|
|
|
|
| 78 |
|
| 79 |
def list_instruments():
|
| 80 |
try:
|
| 81 |
from muscriptor.tokenizer.mt3 import MT3_FULL_PLUS_GROUP_NAMES
|
| 82 |
+
|
| 83 |
return "\n".join(f"β’ {name}" for name in sorted(MT3_FULL_PLUS_GROUP_NAMES.keys()))
|
| 84 |
except Exception:
|
| 85 |
return "Could not load instrument list."
|
| 86 |
|
| 87 |
+
|
| 88 |
+
# ---------- UI ----------
|
| 89 |
with gr.Blocks(title="π΅ Music Transcriber") as demo:
|
| 90 |
+
|
| 91 |
+
gr.Markdown("# π΅ Music Transcriber\nTurn audio into MIDI using AI. Optionally transform to Trap style! π₯")
|
| 92 |
+
|
| 93 |
with gr.Row():
|
| 94 |
+
# LEFT COLUMN β Inputs
|
| 95 |
with gr.Column(scale=1):
|
| 96 |
+
audio_input = gr.Audio(
|
| 97 |
+
type="filepath",
|
| 98 |
+
label="Upload Audio (MP3, WAV, FLAC, OGG, M4A)",
|
| 99 |
+
interactive=True,
|
| 100 |
+
)
|
| 101 |
model_select = gr.Radio(
|
| 102 |
+
choices=[
|
| 103 |
+
("Small (~100M) - Fastest β‘", "small"),
|
| 104 |
+
("Medium (~300M) - Balanced π", "medium"),
|
| 105 |
+
("Large (~1.3B) - Best Quality π", "large"),
|
| 106 |
+
],
|
| 107 |
+
value="medium",
|
| 108 |
+
label="Model Size",
|
| 109 |
)
|
| 110 |
+
instrument_input = gr.Textbox(
|
| 111 |
+
label="Filter Instruments (optional)",
|
| 112 |
+
placeholder="e.g., acoustic_piano, acoustic_guitar",
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
trap_checkbox = gr.Checkbox(
|
| 116 |
+
label="π₯ Transform to Trap Style (808s, hi-hats, half-time drums)",
|
| 117 |
+
value=False,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
transcribe_btn = gr.Button("πΉ Transcribe Audio", variant="primary", size="lg")
|
| 121 |
+
|
| 122 |
+
# RIGHT COLUMN β Outputs
|
| 123 |
with gr.Column(scale=1):
|
| 124 |
+
status_output = gr.Textbox(label="Status", lines=5)
|
| 125 |
+
note_count_output = gr.HTML(
|
| 126 |
+
'<div style="font-size: 2rem; font-weight: bold; color: #8b5cf6; text-align: center; padding: 1rem;">0</div>'
|
| 127 |
+
)
|
| 128 |
midi_output = gr.File(label="Download MIDI", visible=False)
|
| 129 |
+
|
| 130 |
with gr.Accordion("π View Available Instruments", open=False):
|
| 131 |
gr.Markdown(list_instruments())
|
| 132 |
+
|
| 133 |
+
# --- Wire up the button ---
|
| 134 |
transcribe_btn.click(
|
| 135 |
fn=transcribe_audio,
|
| 136 |
+
inputs=[audio_input, model_select, instrument_input, trap_checkbox],
|
| 137 |
+
outputs=[midi_output, status_output, note_count_output],
|
| 138 |
).then(
|
| 139 |
+
lambda path, html: (gr.update(visible=bool(path)), html),
|
|
|
|
| 140 |
inputs=[midi_output, note_count_output],
|
| 141 |
+
outputs=[midi_output, note_count_output],
|
| 142 |
)
|
| 143 |
|
| 144 |
+
|
| 145 |
if __name__ == "__main__":
|
| 146 |
demo.launch(theme=gr.themes.Soft(primary_hue="purple"), show_error=True)
|