Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,9 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
-
from transformers import AutoProcessor
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# 1. Safely pull your secret token
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
|
@@ -12,13 +14,16 @@ MODEL_ID = "guangyangmusic/legato-small"
|
|
| 12 |
|
| 13 |
print("Loading custom LEGATO OMR model architecture...")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
processor = AutoProcessor.from_pretrained(MODEL_ID, token=hf_token
|
| 17 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
|
@@ -29,7 +34,7 @@ def run_legato_omr(image):
|
|
| 29 |
# Preprocess the incoming image snippet
|
| 30 |
inputs = processor(images=image, return_tensors="pt")
|
| 31 |
|
| 32 |
-
#
|
| 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)
|
|
@@ -44,7 +49,7 @@ demo = gr.Interface(
|
|
| 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
|
| 48 |
)
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
|
|
|
| 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 |
# 1. Safely pull your secret token
|
| 10 |
hf_token = os.environ.get("HF_TOKEN")
|
|
|
|
| 14 |
|
| 15 |
print("Loading custom LEGATO OMR model architecture...")
|
| 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, # Drastically cuts RAM usage to fit 16GB CPU limits
|
| 23 |
+
low_cpu_mem_usage=True # Prevents system memory spikes during loading
|
| 24 |
+
)
|
| 25 |
|
|
|
|
| 26 |
model.eval()
|
|
|
|
| 27 |
print("LEGATO Model loaded successfully!")
|
| 28 |
|
| 29 |
def run_legato_omr(image):
|
|
|
|
| 34 |
# Preprocess the incoming image snippet
|
| 35 |
inputs = processor(images=image, return_tensors="pt")
|
| 36 |
|
| 37 |
+
# Execute token inference securely
|
| 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)
|
|
|
|
| 49 |
inputs=gr.Image(type="pil", label="1. Upload Sheet Music Snippet"),
|
| 50 |
outputs=gr.Textbox(label="2. LEGATO Output (ABC Notation Text)", show_copy_button=True),
|
| 51 |
title="🎼 LEGATO End-to-End OMR Engine",
|
| 52 |
+
description="Running on optimized CPU spaces. Generates machine-readable musical markup from raw notation images."
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|