|
|
import gradio as gr
|
|
|
import os
|
|
|
import tempfile
|
|
|
from midi_utils import process_midi
|
|
|
|
|
|
def process_midi_and_provide_download(input_midi_file):
|
|
|
"""
|
|
|
这个函数会被Gradio调用。
|
|
|
它接收一个上传的文件,调用处理函数,然后返回一个可供下载的新文件路径。
|
|
|
"""
|
|
|
|
|
|
if input_midi_file is None:
|
|
|
raise gr.Error("错误:请先上传一个MIDI文件!")
|
|
|
|
|
|
print(f"收到的临时文件路径: {input_midi_file.name}")
|
|
|
|
|
|
|
|
|
with open(input_midi_file.name, 'rb') as f:
|
|
|
midi_bytes = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
processed_midi_buffer = process_midi(midi_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".mid", prefix="processed-") as temp_output_file:
|
|
|
temp_output_file.write(processed_midi_buffer.getvalue())
|
|
|
output_file_path = temp_output_file.name
|
|
|
print(f"已处理的文件已保存到: {output_file_path}")
|
|
|
|
|
|
|
|
|
return output_file_path
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
gr.Markdown(
|
|
|
"""
|
|
|
# 🎵 MIDI 处理器 (Gradio版)
|
|
|
这是一个可以直接在Hugging Face Space中交互的版本。
|
|
|
"""
|
|
|
)
|
|
|
gr.Markdown("---")
|
|
|
gr.Markdown("### 步骤 1: 上传你的MIDI文件")
|
|
|
|
|
|
|
|
|
midi_input = gr.File(label="点击或拖拽MIDI文件到这里", file_types=[".mid", ".midi"])
|
|
|
|
|
|
gr.Markdown("### 步骤 2: 点击按钮进行处理")
|
|
|
|
|
|
|
|
|
process_button = gr.Button("处理MIDI文件 (所有音符升一个半音)", variant="primary")
|
|
|
|
|
|
gr.Markdown("### 步骤 3: 下载处理后的文件")
|
|
|
|
|
|
|
|
|
midi_output = gr.File(label="处理结果将出现在这里,点击即可下载")
|
|
|
|
|
|
|
|
|
process_button.click(
|
|
|
fn=process_midi_and_provide_download,
|
|
|
inputs=midi_input,
|
|
|
outputs=midi_output
|
|
|
)
|
|
|
|
|
|
|
|
|
demo.launch() |