Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
from transformers import AutoProcessor,
|
| 5 |
|
| 6 |
-
# 1.
|
| 7 |
-
# (确保你已经按照上一步在 Space Settings 的 Secrets 里添加 HF_TOKEN)
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
-
# 2.
|
| 11 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 12 |
|
| 13 |
-
print("
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def run_legato_omr(image):
|
| 20 |
if image is None:
|
| 21 |
-
return "
|
| 22 |
|
| 23 |
-
# 3. 图像预处理与模型推理
|
| 24 |
try:
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
return generated_text
|
| 32 |
except Exception as e:
|
| 33 |
-
return f"
|
| 34 |
|
| 35 |
-
#
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=run_legato_omr,
|
| 38 |
-
inputs=gr.Image(type="pil", label="1.
|
| 39 |
-
outputs=gr.Textbox(label="2. LEGATO
|
| 40 |
-
title="🎼 LEGATO
|
| 41 |
-
description="
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
demo.launch()
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
from PIL import Image
|
| 5 |
+
from transformers import AutoProcessor, AutoModel
|
| 6 |
|
| 7 |
+
# 1. Safely pull your secret token
|
|
|
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
+
# 2. Define the exact model identifier
|
| 11 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 12 |
|
| 13 |
+
print("Loading custom LEGATO OMR model architecture...")
|
| 14 |
+
|
| 15 |
+
# FIX: Use AutoProcessor and AutoModel with trust_remote_code=True
|
| 16 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID, token=hf_token, trust_remote_code=True)
|
| 17 |
+
model = AutoModel.from_pretrained(MODEL_ID, token=hf_token, trust_remote_code=True)
|
| 18 |
+
|
| 19 |
+
# Move model to CPU explicitly (HF Spaces default tier)
|
| 20 |
+
model.eval()
|
| 21 |
+
|
| 22 |
+
print("LEGATO Model loaded successfully!")
|
| 23 |
|
| 24 |
def run_legato_omr(image):
|
| 25 |
if image is None:
|
| 26 |
+
return "Please upload a sheet music image first!"
|
| 27 |
|
|
|
|
| 28 |
try:
|
| 29 |
+
# Preprocess the incoming image snippet
|
| 30 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 31 |
|
| 32 |
+
# Safe token inference execution
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
| 35 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 36 |
|
| 37 |
+
return generated_text[0] if isinstance(generated_text, list) else generated_text
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"Inference Pipeline Error: {str(e)}"
|
| 40 |
|
| 41 |
+
# 3. Gradio Interface Declaration
|
| 42 |
demo = gr.Interface(
|
| 43 |
fn=run_legato_omr,
|
| 44 |
+
inputs=gr.Image(type="pil", label="1. Upload Sheet Music Snippet"),
|
| 45 |
+
outputs=gr.Textbox(label="2. LEGATO Output (ABC Notation Text)", show_copy_button=True),
|
| 46 |
+
title="🎼 LEGATO End-to-End OMR Engine",
|
| 47 |
+
description="Running on standard CPU spaces. Generates machine-readable musical markup from raw notation images."
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
demo.launch()
|
| 52 |
+
|