File size: 1,213 Bytes
d3340ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3f6ff7
d3340ef
 
 
 
 
 
 
 
 
 
 
 
 
 
488f338
d3340ef
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
import gradio as gr
import subprocess
import os

def recognize_music(pdf_file):
    """Converts a PDF file to a Music score
    
    Args:
        pdf_file: the file with the PDF file
        
    Returns:
        the path were the MusicXML file is generated
    """
    audiveris = "/opt/audiveris/bin/Audiveris"
    output_dir = "/tmp/output"
    pdf_file_path = pdf_file.name
    pdf_file_name = os.path.basename(pdf_file.name)
    musicXml_file_name = os.path.splitext(pdf_file_name)[0]+".mxl"
    output_file = output_dir + "/" + musicXml_file_name

    cmd = [
        audiveris, "-batch", "--add-opens java.base/java.nio=ALL-UNNAMED", "-export", "-output", output_dir, pdf_file_path
    ]

    result = subprocess.run(cmd, capture_output=True, text=True)

    print (f"Done. Music score file saved to {output_file}\n\n{result.stdout}\n{result.stderr}")

    return output_file

audiveris = gr.Interface(
    fn=recognize_music,
    inputs=gr.File(file_types=[".pdf"], label="Upload PDF music score"),
    outputs=gr.File(label="Download MusicXML file"),
    description="Upload a PDF music socre and create a MusicXML file from it.",
)
audiveris.launch(server_name="0.0.0.0", mcp_server=True, share=True)