File size: 1,239 Bytes
c2f178d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()