Spaces:
Running
Running
transkun
Browse files- app.py +20 -53
- models/2.0.conf +0 -37
- models/2.0.pt +0 -3
- models/__init__.py +0 -0
app.py
CHANGED
|
@@ -296,6 +296,8 @@ def trim_midi_silence(mid, debug=False):
|
|
| 296 |
if debug:
|
| 297 |
print(f"MIDI裁剪失败: {e}")
|
| 298 |
|
|
|
|
|
|
|
| 299 |
# 核心转换函数
|
| 300 |
def process_audio(input_file, use_cuda=True, use_quantize=True, progress=gr.Progress(), file_progress_offset=0.0, file_progress_scale=1.0):
|
| 301 |
"""
|
|
@@ -311,76 +313,42 @@ def process_audio(input_file, use_cuda=True, use_quantize=True, progress=gr.Prog
|
|
| 311 |
"""
|
| 312 |
temp_dir = None
|
| 313 |
try:
|
| 314 |
-
#
|
| 315 |
-
# 修复:创建一个临时目录来存储所有的输出文件
|
| 316 |
temp_dir = tempfile.mkdtemp()
|
| 317 |
|
| 318 |
-
# Get a meaningful filename from the input file
|
| 319 |
# 从输入文件中获取一个有意义的文件名
|
| 320 |
input_name = Path(input_file).stem
|
| 321 |
|
| 322 |
-
#
|
| 323 |
-
# 在临时目录中创建非量化MIDI文件的路径
|
| 324 |
output_file = Path(temp_dir) / f"{input_name}.mid"
|
| 325 |
|
| 326 |
quantized_output_file = None
|
| 327 |
|
| 328 |
-
|
|
|
|
| 329 |
|
| 330 |
start_time = time.time()
|
| 331 |
-
progress(file_progress_offset, desc="准备
|
| 332 |
-
|
| 333 |
-
#
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
#
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
)
|
| 344 |
-
|
| 345 |
-
# 加载配置
|
| 346 |
-
conf_manager = moduleconf.parseFromFile(default_conf)
|
| 347 |
-
TransKun = conf_manager["Model"].module.TransKun
|
| 348 |
-
conf = conf_manager["Model"].config
|
| 349 |
-
|
| 350 |
-
# 加载模型
|
| 351 |
-
checkpoint = torch.load(default_weight, map_location=device)
|
| 352 |
-
model = TransKun(conf=conf).to(device)
|
| 353 |
-
if "best_state_dict" not in checkpoint:
|
| 354 |
-
model.load_state_dict(checkpoint["state_dict"], strict=False)
|
| 355 |
-
else:
|
| 356 |
-
model.load_state_dict(checkpoint["best_state_dict"], strict=False)
|
| 357 |
-
model.eval()
|
| 358 |
-
|
| 359 |
-
progress(file_progress_offset + 0.2 * file_progress_scale, desc="读取音频...")
|
| 360 |
-
# 读取并处理音频
|
| 361 |
-
fs, audio = transkun.transcribe.readAudio(input_file)
|
| 362 |
-
if fs != model.fs:
|
| 363 |
-
import soxr
|
| 364 |
-
audio = soxr.resample(audio, fs, model.fs)
|
| 365 |
-
|
| 366 |
-
x = torch.from_numpy(audio).to(device)
|
| 367 |
-
|
| 368 |
-
progress(file_progress_offset + 0.4 * file_progress_scale, desc="转录中...")
|
| 369 |
-
# 转录
|
| 370 |
-
with torch.no_grad():
|
| 371 |
-
notes_est = model.transcribe(x)
|
| 372 |
|
| 373 |
progress(file_progress_offset + 0.7 * file_progress_scale, desc="保存MIDI...")
|
| 374 |
-
# 保存MIDI到临时目录,将 Path 对象转换为字符串
|
| 375 |
-
output_midi = transkun.transcribe.writeMidi(notes_est)
|
| 376 |
-
output_midi.write(str(output_file))
|
| 377 |
|
| 378 |
# 如果勾选了规整化选项,则进行MIDI规整化
|
| 379 |
if use_quantize:
|
| 380 |
progress(file_progress_offset + 0.8 * file_progress_scale, desc="规整化MIDI...")
|
| 381 |
try:
|
| 382 |
-
#
|
| 383 |
-
# midi_quantize函数现在将以预期的名称写入输出文件
|
| 384 |
quantized_output_file = midi_quantize(str(output_file), debug=False, optimize_bpm=True)
|
| 385 |
except Exception as e:
|
| 386 |
print(f"规整化处理失败: {str(e)}")
|
|
@@ -407,7 +375,6 @@ def process_audio(input_file, use_cuda=True, use_quantize=True, progress=gr.Prog
|
|
| 407 |
"output": f"转换失败: {str(e)}",
|
| 408 |
"files": []
|
| 409 |
}
|
| 410 |
-
# Removed the manual cleanup block, Gradio will handle this now.
|
| 411 |
# 删除了手动清理代码块,现在由 Gradio 来处理。
|
| 412 |
|
| 413 |
# 创建Gradio界面
|
|
|
|
| 296 |
if debug:
|
| 297 |
print(f"MIDI裁剪失败: {e}")
|
| 298 |
|
| 299 |
+
import subprocess
|
| 300 |
+
|
| 301 |
# 核心转换函数
|
| 302 |
def process_audio(input_file, use_cuda=True, use_quantize=True, progress=gr.Progress(), file_progress_offset=0.0, file_progress_scale=1.0):
|
| 303 |
"""
|
|
|
|
| 313 |
"""
|
| 314 |
temp_dir = None
|
| 315 |
try:
|
| 316 |
+
# 创建一个临时目录来存储所有的输出文件
|
|
|
|
| 317 |
temp_dir = tempfile.mkdtemp()
|
| 318 |
|
|
|
|
| 319 |
# 从输入文件中获取一个有意义的文件名
|
| 320 |
input_name = Path(input_file).stem
|
| 321 |
|
| 322 |
+
# 在临时目录中创建MIDI文件的路径
|
|
|
|
| 323 |
output_file = Path(temp_dir) / f"{input_name}.mid"
|
| 324 |
|
| 325 |
quantized_output_file = None
|
| 326 |
|
| 327 |
+
# 设置设备参数
|
| 328 |
+
device_param = "--cuda" if use_cuda and cuda_available else ""
|
| 329 |
|
| 330 |
start_time = time.time()
|
| 331 |
+
progress(file_progress_offset, desc="准备转录...")
|
| 332 |
+
|
| 333 |
+
# 使用命令行调用transkun
|
| 334 |
+
progress(file_progress_offset + 0.3 * file_progress_scale, desc="转录中...")
|
| 335 |
+
cmd = ["transkun", input_file, str(output_file), device_param]
|
| 336 |
+
|
| 337 |
+
# 执行命令
|
| 338 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
| 339 |
+
stdout, stderr = process.communicate()
|
| 340 |
+
|
| 341 |
+
# 检查命令是否成功执行
|
| 342 |
+
if process.returncode != 0:
|
| 343 |
+
raise Exception(f"transkun命令执行失败: {stderr}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
progress(file_progress_offset + 0.7 * file_progress_scale, desc="保存MIDI...")
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
# 如果勾选了规整化选项,则进行MIDI规整化
|
| 348 |
if use_quantize:
|
| 349 |
progress(file_progress_offset + 0.8 * file_progress_scale, desc="规整化MIDI...")
|
| 350 |
try:
|
| 351 |
+
# midi_quantize函数将以预期的名称写入输出文件
|
|
|
|
| 352 |
quantized_output_file = midi_quantize(str(output_file), debug=False, optimize_bpm=True)
|
| 353 |
except Exception as e:
|
| 354 |
print(f"规整化处理失败: {str(e)}")
|
|
|
|
| 375 |
"output": f"转换失败: {str(e)}",
|
| 376 |
"files": []
|
| 377 |
}
|
|
|
|
| 378 |
# 删除了手动清理代码块,现在由 Gradio 来处理。
|
| 379 |
|
| 380 |
# 创建Gradio界面
|
models/2.0.conf
DELETED
|
@@ -1,37 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"Model": {
|
| 3 |
-
"module": "transkun.ModelTransformer",
|
| 4 |
-
"configClassName": "Config",
|
| 5 |
-
"config": {
|
| 6 |
-
"f_min": 30,
|
| 7 |
-
"f_max": 8000,
|
| 8 |
-
"n_mels": 229,
|
| 9 |
-
"segmentHopSizeInSecond": 8,
|
| 10 |
-
"segmentSizeInSecond": 16,
|
| 11 |
-
"hopSize": 1024,
|
| 12 |
-
"windowSize": 4096,
|
| 13 |
-
"fs": 44100,
|
| 14 |
-
"nExtraWins": 5,
|
| 15 |
-
"baseSize": 64,
|
| 16 |
-
"downsampleF": true,
|
| 17 |
-
"posEmbedInitGamma": 1,
|
| 18 |
-
"nHead": 8,
|
| 19 |
-
"fourierSize": 64,
|
| 20 |
-
"nLayers": 6,
|
| 21 |
-
"enabledAttn": [
|
| 22 |
-
"F",
|
| 23 |
-
"T"
|
| 24 |
-
],
|
| 25 |
-
"hiddenFactorAttn": 1,
|
| 26 |
-
"hiddenFactor": 4,
|
| 27 |
-
"velocityPredictorHiddenSize": 512,
|
| 28 |
-
"refinedOFPredictorHiddenSize": 512,
|
| 29 |
-
"scoringExpansionFactor": 4,
|
| 30 |
-
"useInnerProductScorer": true,
|
| 31 |
-
"scoreDropoutProb": 0.1,
|
| 32 |
-
"contextDropoutProb": 0.0,
|
| 33 |
-
"velocityDropoutProb": 0.1,
|
| 34 |
-
"refinedOFDropoutProb": 0.1
|
| 35 |
-
}
|
| 36 |
-
}
|
| 37 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models/2.0.pt
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:50a80010effc2a59ffcd068a95cd2b29bd7f23a27a3515bc3ccd209c89a3d44c
|
| 3 |
-
size 56408978
|
|
|
|
|
|
|
|
|
|
|
|
models/__init__.py
DELETED
|
File without changes
|