Upload 3 files
Browse files- app.py +68 -0
- midi_utils.py +20 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from midi_utils import process_midi # 我们重用之前的核心处理逻辑
|
| 5 |
+
|
| 6 |
+
def process_midi_and_provide_download(input_midi_file):
|
| 7 |
+
"""
|
| 8 |
+
这个函数会被Gradio调用。
|
| 9 |
+
它接收一个上传的文件,调用处理函数,然后返回一个可供下载的新文件路径。
|
| 10 |
+
"""
|
| 11 |
+
# 1. 检查用户是否上传了文件
|
| 12 |
+
if input_midi_file is None:
|
| 13 |
+
raise gr.Error("错误:请先上传一个MIDI文件!")
|
| 14 |
+
|
| 15 |
+
print(f"收到的临时文件路径: {input_midi_file.name}")
|
| 16 |
+
|
| 17 |
+
# 2. 从Gradio提供的临时文件中读取二进制数据
|
| 18 |
+
with open(input_midi_file.name, 'rb') as f:
|
| 19 |
+
midi_bytes = f.read()
|
| 20 |
+
|
| 21 |
+
# 3. 使用我们之前写好的核心MIDI处理函数
|
| 22 |
+
# 这个函数会返回一个包含已处理MIDI数据的内存缓冲区 (BytesIO)
|
| 23 |
+
processed_midi_buffer = process_midi(midi_bytes)
|
| 24 |
+
|
| 25 |
+
# 4. 将处理后的数据保存到一个新的临时文件中
|
| 26 |
+
# 这是必要的,因为Gradio需要一个文件路径来创建下载链接
|
| 27 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mid", prefix="processed-") as temp_output_file:
|
| 28 |
+
temp_output_file.write(processed_midi_buffer.getvalue())
|
| 29 |
+
output_file_path = temp_output_file.name
|
| 30 |
+
print(f"已处理的文件已保存到: {output_file_path}")
|
| 31 |
+
|
| 32 |
+
# 5. 返回这个新文件的路径,Gradio会自动生成一个下载链接
|
| 33 |
+
return output_file_path
|
| 34 |
+
|
| 35 |
+
# --- 使用Gradio Blocks创建用户界面 ---
|
| 36 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
+
# 标题和描述
|
| 38 |
+
gr.Markdown(
|
| 39 |
+
"""
|
| 40 |
+
# 🎵 MIDI 处理器 (Gradio版)
|
| 41 |
+
这是一个可以直接在Hugging Face Space中交互的版本。
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
gr.Markdown("---")
|
| 45 |
+
gr.Markdown("### 步骤 1: 上传你的MIDI文件")
|
| 46 |
+
|
| 47 |
+
# 文件上传组件
|
| 48 |
+
midi_input = gr.File(label="点击或拖拽MIDI文件到这里", file_types=[".mid", ".midi"])
|
| 49 |
+
|
| 50 |
+
gr.Markdown("### 步骤 2: 点击按钮进行处理")
|
| 51 |
+
|
| 52 |
+
# 处理按钮
|
| 53 |
+
process_button = gr.Button("处理MIDI文件 (所有音符升一个半音)", variant="primary")
|
| 54 |
+
|
| 55 |
+
gr.Markdown("### 步骤 3: 下载处理后的文件")
|
| 56 |
+
|
| 57 |
+
# 文件输出/下载组件
|
| 58 |
+
midi_output = gr.File(label="处理结果将出现在这里,点击即可下载")
|
| 59 |
+
|
| 60 |
+
# 将按钮的点击事件连接到我们的处理函数
|
| 61 |
+
process_button.click(
|
| 62 |
+
fn=process_midi_and_provide_download, # 要调用的函数
|
| 63 |
+
inputs=midi_input, # 函数的输入来自哪里
|
| 64 |
+
outputs=midi_output # 函数的输出更新哪个组件
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# 启动Gradio应用
|
| 68 |
+
demo.launch()
|
midi_utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import mido
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
|
| 4 |
+
def process_midi(midi_bytes: bytes) -> BytesIO:
|
| 5 |
+
"""处理MIDI文件:将所有音符提高一个半音"""
|
| 6 |
+
with BytesIO(midi_bytes) as input_buffer:
|
| 7 |
+
mid = mido.MidiFile(file=input_buffer)
|
| 8 |
+
|
| 9 |
+
for track in mid.tracks:
|
| 10 |
+
for msg in track:
|
| 11 |
+
if msg.type in ['note_on', 'note_off']:
|
| 12 |
+
# 确保音符不会超过MIDI的最大值127
|
| 13 |
+
if msg.note < 127:
|
| 14 |
+
msg.note += 1
|
| 15 |
+
|
| 16 |
+
output_buffer = BytesIO()
|
| 17 |
+
mid.save(file=output_buffer)
|
| 18 |
+
output_buffer.seek(0)
|
| 19 |
+
|
| 20 |
+
return output_buffer
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
mido==1.2.10
|