import os import subprocess import gradio as gr from pathlib import Path MSCORE_BIN = "/opt/squashfs-root/bin/mscore" APPIMAGE = "/opt/mscore.AppImage" EXTRACT_DIR = "/opt/squashfs-root" def extract_musescore(): if not Path(MSCORE_BIN).exists(): print("🔧 Extracting MuseScore AppImage...") subprocess.run([APPIMAGE, "--appimage-extract"], cwd="/opt", check=True) def convert_and_render(midi_path_str): midi_path = Path(midi_path_str) svg_path = midi_path.with_suffix(".svg") result = subprocess.run([ MSCORE_BIN, str(midi_path), "-o", str(svg_path) ], capture_output=True, text=True) if result.returncode != 0: return f"❌ MuseScore failed: {result.stderr}", None return "✅ Conversion successful", svg_path.read_text() iface = gr.Interface( fn=convert_and_render, inputs=gr.File(type="filepath", label="Upload MIDI file"), outputs=["text", gr.HTML(label="Rendered SVG")], title="MIDI to SVG Viewer via MuseScore" ) def launch_app(): if "HUGGINGFACE_SPACE" in os.environ: iface.launch(server_name="0.0.0.0", server_port=7860, share=True) else: iface.launch(server_name="0.0.0.0", server_port=7860) launch_app()