Spaces:
Running
Running
admin commited on
Commit ·
cbce35a
1
Parent(s): c0495bf
Auto sync at 2026-05-03 16:27:53
Browse files- app.py +3 -23
- convert.py +16 -23
app.py
CHANGED
|
@@ -5,27 +5,7 @@ import gradio as gr
|
|
| 5 |
from mutagen.mp3 import MP3
|
| 6 |
from mutagen.flac import FLAC
|
| 7 |
from piano_transcription_inference import PianoTranscription, load_audio, sample_rate
|
| 8 |
-
from convert import midi2xml, xml2abc, xml2mxl, xml2jpg
|
| 9 |
-
|
| 10 |
-
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 11 |
-
TMP_DIR = "./__pycache__"
|
| 12 |
-
|
| 13 |
-
if EN_US:
|
| 14 |
-
import huggingface_hub
|
| 15 |
-
|
| 16 |
-
MODEL_PATH = huggingface_hub.snapshot_download(
|
| 17 |
-
"Genius-Society/piano_trans",
|
| 18 |
-
cache_dir=TMP_DIR,
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
else:
|
| 22 |
-
import modelscope
|
| 23 |
-
|
| 24 |
-
MODEL_PATH = modelscope.snapshot_download(
|
| 25 |
-
"Genius-Society/piano_trans",
|
| 26 |
-
cache_dir=TMP_DIR,
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
|
| 30 |
ZH2EN = {
|
| 31 |
"五线谱": "Staff",
|
|
@@ -88,7 +68,7 @@ def audio2midi(audio_path: str, cache_dir: str):
|
|
| 88 |
audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)
|
| 89 |
transcriptor = PianoTranscription(
|
| 90 |
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 91 |
-
checkpoint_path=f"{
|
| 92 |
)
|
| 93 |
midi_path = f"{cache_dir}/output.mid"
|
| 94 |
transcriptor.transcribe(audio, midi_path)
|
|
@@ -112,7 +92,7 @@ def upl_infer(audio_path: str, cache_dir=f"{TMP_DIR}/cache"):
|
|
| 112 |
return status, midi, pdf, xml, mxl, abc, jpg
|
| 113 |
|
| 114 |
|
| 115 |
-
def find_audio_files(folder_path=f"{
|
| 116 |
wav_files = []
|
| 117 |
for root, _, files in os.walk(folder_path):
|
| 118 |
for file in files:
|
|
|
|
| 5 |
from mutagen.mp3 import MP3
|
| 6 |
from mutagen.flac import FLAC
|
| 7 |
from piano_transcription_inference import PianoTranscription, load_audio, sample_rate
|
| 8 |
+
from convert import midi2xml, xml2abc, xml2mxl, xml2jpg, EN_US, TMP_DIR, MODEL_DIR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
ZH2EN = {
|
| 11 |
"五线谱": "Staff",
|
|
|
|
| 68 |
audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)
|
| 69 |
transcriptor = PianoTranscription(
|
| 70 |
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 71 |
+
checkpoint_path=f"{MODEL_DIR}/CRNN_note_F1=0.9677_pedal_F1=0.9186.pth",
|
| 72 |
)
|
| 73 |
midi_path = f"{cache_dir}/output.mid"
|
| 74 |
transcriptor.transcribe(audio, midi_path)
|
|
|
|
| 92 |
return status, midi, pdf, xml, mxl, abc, jpg
|
| 93 |
|
| 94 |
|
| 95 |
+
def find_audio_files(folder_path=f"{MODEL_DIR}/examples"):
|
| 96 |
wav_files = []
|
| 97 |
for root, _, files in os.walk(folder_path):
|
| 98 |
for file in files:
|
convert.py
CHANGED
|
@@ -1,42 +1,35 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import fitz
|
| 4 |
-
import
|
| 5 |
import subprocess
|
| 6 |
from PIL import Image
|
| 7 |
from music21 import converter
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
os.makedirs(directory)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
for chunk in response.iter_content(chunk_size=1024):
|
| 19 |
-
if chunk:
|
| 20 |
-
file.write(chunk)
|
| 21 |
-
|
| 22 |
-
print(f"文件已下载并保存到 {file_path}")
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
if sys.platform.startswith("linux"):
|
| 31 |
apkname = "MuseScore.AppImage"
|
|
|
|
| 32 |
extra_dir = "squashfs-root"
|
| 33 |
-
if not os.path.exists(apkname):
|
| 34 |
-
download(
|
| 35 |
-
url="https://www.modelscope.cn/studio/Genius-Society/piano_trans/resolve/master/MuseScore.AppImage",
|
| 36 |
-
directory="./",
|
| 37 |
-
filename=apkname,
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
if not os.path.exists(extra_dir):
|
| 41 |
subprocess.run(["chmod", "+x", f"./{apkname}"])
|
| 42 |
subprocess.run([f"./{apkname}", "--appimage-extract"])
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import fitz
|
| 4 |
+
import shutil
|
| 5 |
import subprocess
|
| 6 |
from PIL import Image
|
| 7 |
from music21 import converter
|
| 8 |
|
| 9 |
+
EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
|
| 10 |
+
TMP_DIR = "./__pycache__"
|
| 11 |
|
| 12 |
+
if EN_US:
|
| 13 |
+
import huggingface_hub
|
|
|
|
| 14 |
|
| 15 |
+
MODEL_DIR = huggingface_hub.snapshot_download(
|
| 16 |
+
"Genius-Society/piano_trans",
|
| 17 |
+
cache_dir=TMP_DIR,
|
| 18 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
else:
|
| 21 |
+
import modelscope
|
| 22 |
|
| 23 |
+
MODEL_DIR = modelscope.snapshot_download(
|
| 24 |
+
"Genius-Society/piano_trans",
|
| 25 |
+
cache_dir=TMP_DIR,
|
| 26 |
+
)
|
| 27 |
|
| 28 |
|
| 29 |
if sys.platform.startswith("linux"):
|
| 30 |
apkname = "MuseScore.AppImage"
|
| 31 |
+
shutil.move(os.path.realpath(f"{MODEL_DIR}/{apkname}"), f"./{apkname}")
|
| 32 |
extra_dir = "squashfs-root"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if not os.path.exists(extra_dir):
|
| 34 |
subprocess.run(["chmod", "+x", f"./{apkname}"])
|
| 35 |
subprocess.run([f"./{apkname}", "--appimage-extract"])
|