Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,66 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from transformers import AutoProcessor
|
| 6 |
-
# Import the custom model architecture directly from the installed legato package
|
| 7 |
-
from legato.models import LegatoModel
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# 2.
|
|
|
|
| 13 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 14 |
|
| 15 |
-
print("
|
| 16 |
-
|
| 17 |
-
# Initialize the processor and the specialized model class
|
| 18 |
processor = AutoProcessor.from_pretrained(MODEL_ID, token=hf_token)
|
| 19 |
model = LegatoModel.from_pretrained(
|
| 20 |
MODEL_ID,
|
| 21 |
token=hf_token,
|
| 22 |
-
torch_dtype=torch.bfloat16, #
|
| 23 |
-
low_cpu_mem_usage=True
|
| 24 |
)
|
| 25 |
-
|
| 26 |
model.eval()
|
| 27 |
-
print("
|
| 28 |
|
| 29 |
def run_legato_omr(image):
|
| 30 |
if image is None:
|
| 31 |
-
return "
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
#
|
| 35 |
inputs = processor(images=image, return_tensors="pt")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
with torch.no_grad():
|
| 39 |
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
| 40 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 41 |
|
| 42 |
return generated_text[0] if isinstance(generated_text, list) else generated_text
|
| 43 |
except Exception as e:
|
| 44 |
-
return f"
|
| 45 |
|
| 46 |
-
# 3. Gradio
|
| 47 |
demo = gr.Interface(
|
| 48 |
fn=run_legato_omr,
|
| 49 |
-
inputs=gr.Image(type="pil", label="1.
|
| 50 |
-
outputs=gr.Textbox(label="2. LEGATO
|
| 51 |
-
title="🎼 LEGATO
|
| 52 |
-
description="
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# 1. 核心修复:在代码运行最开始,强制通过命令行静默安装外部的 legato 依赖库
|
| 5 |
+
print("正在从 GitHub 动态构建并安装 LEGATO 核心组件...")
|
| 6 |
+
os.system("pip install --no-cache-dir git+https://github.com")
|
| 7 |
+
print("LEGATO 组件安装完毕!")
|
| 8 |
+
|
| 9 |
import gradio as gr
|
| 10 |
import torch
|
| 11 |
from PIL import Image
|
| 12 |
from transformers import AutoProcessor
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# 导入动态安装成功的核心模型类
|
| 15 |
+
try:
|
| 16 |
+
from legato.models import LegatoModel
|
| 17 |
+
except ImportError:
|
| 18 |
+
print("错误:Legato 库未能成功加载,正在尝试备用导入...")
|
| 19 |
+
# 刷新 Python 的环境变量路径,确保刚安装的库立刻生效
|
| 20 |
+
import site
|
| 21 |
+
from importlib import reload
|
| 22 |
+
reload(site)
|
| 23 |
+
from legato.models import LegatoModel
|
| 24 |
|
| 25 |
+
# 2. 提取安全凭证 Token
|
| 26 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 27 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 28 |
|
| 29 |
+
print("正在加载 LEGATO 权重结构 (采用 bfloat16 低内存模式)...")
|
|
|
|
|
|
|
| 30 |
processor = AutoProcessor.from_pretrained(MODEL_ID, token=hf_token)
|
| 31 |
model = LegatoModel.from_pretrained(
|
| 32 |
MODEL_ID,
|
| 33 |
token=hf_token,
|
| 34 |
+
torch_dtype=torch.bfloat16, # 极大程度压缩内存以适应 16GB 限制
|
| 35 |
+
low_cpu_mem_usage=True
|
| 36 |
)
|
|
|
|
| 37 |
model.eval()
|
| 38 |
+
print("🎉 全套模型已成功加载至内存!")
|
| 39 |
|
| 40 |
def run_legato_omr(image):
|
| 41 |
if image is None:
|
| 42 |
+
return "请先上传一张清晰的乐谱图片!"
|
| 43 |
|
| 44 |
try:
|
| 45 |
+
# 预处理输入的乐谱图像
|
| 46 |
inputs = processor(images=image, return_tensors="pt")
|
| 47 |
|
| 48 |
+
# 安全进行 CPU 推理
|
| 49 |
with torch.no_grad():
|
| 50 |
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
| 51 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 52 |
|
| 53 |
return generated_text[0] if isinstance(generated_text, list) else generated_text
|
| 54 |
except Exception as e:
|
| 55 |
+
return f"推理执行发生致命错误: {str(e)}"
|
| 56 |
|
| 57 |
+
# 3. 构造 Gradio 容器
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=run_legato_omr,
|
| 60 |
+
inputs=gr.Image(type="pil", label="1. 上传你的乐谱截图 (支持单行或片段)"),
|
| 61 |
+
outputs=gr.Textbox(label="2. LEGATO 转换结果 (ABC Notation 文本编码)", show_copy_button=True),
|
| 62 |
+
title="🎼 LEGATO 通用端到端乐谱识别系统",
|
| 63 |
+
description="该系统运行于 Hugging Face 免费 CPU 算力。由于无 GPU 硬件加速,单次乐谱解析预计需要 5~15 秒,请耐心等待。"
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|