Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,68 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
print("
|
| 6 |
-
os.system("pip install --no-cache-dir
|
| 7 |
-
print("
|
| 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("
|
| 19 |
-
# 刷新 Python 的环境变量路径,确保刚安装的库立刻生效
|
| 20 |
import site
|
| 21 |
from importlib import reload
|
| 22 |
reload(site)
|
| 23 |
from legato.models import LegatoModel
|
| 24 |
|
| 25 |
-
#
|
| 26 |
hf_token = os.environ.get("HF_TOKEN")
|
| 27 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 28 |
|
| 29 |
-
print("
|
| 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, #
|
| 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 |
-
#
|
| 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
|
| 54 |
except Exception as e:
|
| 55 |
-
return f"
|
| 56 |
|
| 57 |
-
#
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=run_legato_omr,
|
| 60 |
-
inputs=gr.Image(type="pil", label="1.
|
| 61 |
-
outputs=gr.Textbox(label="2. LEGATO
|
| 62 |
-
title="🎼 LEGATO
|
| 63 |
-
description="
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
demo.launch()
|
| 68 |
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
|
| 4 |
+
# Force pip to download the code package directly via raw zip archive instead of git
|
| 5 |
+
print("Downloading and compiling LEGATO codebase via archive fallback...")
|
| 6 |
+
os.system("pip install --no-cache-dir https://github.com")
|
| 7 |
+
print("Installation completed.")
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import torch
|
| 11 |
from PIL import Image
|
| 12 |
from transformers import AutoProcessor
|
| 13 |
|
| 14 |
+
# Attempt immediate import
|
| 15 |
try:
|
| 16 |
from legato.models import LegatoModel
|
| 17 |
except ImportError:
|
| 18 |
+
print("Refreshing local environment site paths...")
|
|
|
|
| 19 |
import site
|
| 20 |
from importlib import reload
|
| 21 |
reload(site)
|
| 22 |
from legato.models import LegatoModel
|
| 23 |
|
| 24 |
+
# Extract your secure secret token
|
| 25 |
hf_token = os.environ.get("HF_TOKEN")
|
| 26 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 27 |
|
| 28 |
+
print("Loading LEGATO structural weights (bfloat16 low-memory profile)...")
|
| 29 |
processor = AutoProcessor.from_pretrained(MODEL_ID, token=hf_token)
|
| 30 |
model = LegatoModel.from_pretrained(
|
| 31 |
MODEL_ID,
|
| 32 |
token=hf_token,
|
| 33 |
+
torch_dtype=torch.bfloat16, # Compresses internal matrices to fit 16GB free tier
|
| 34 |
low_cpu_mem_usage=True
|
| 35 |
)
|
| 36 |
model.eval()
|
| 37 |
+
print("🎉 Model successfully mounted into runtime memory!")
|
| 38 |
|
| 39 |
def run_legato_omr(image):
|
| 40 |
if image is None:
|
| 41 |
+
return "Please upload a sheet music image first!"
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
# Preprocess the incoming image snippet
|
| 45 |
inputs = processor(images=image, return_tensors="pt")
|
| 46 |
|
| 47 |
+
# Execute CPU safe token inference
|
| 48 |
with torch.no_grad():
|
| 49 |
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
| 50 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 51 |
|
| 52 |
+
return generated_text if isinstance(generated_text, list) else generated_text
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"Inference execution aborted: {str(e)}"
|
| 55 |
|
| 56 |
+
# Declare Gradio UI block
|
| 57 |
demo = gr.Interface(
|
| 58 |
fn=run_legato_omr,
|
| 59 |
+
inputs=gr.Image(type="pil", label="1. Upload Sheet Music Snippet"),
|
| 60 |
+
outputs=gr.Textbox(label="2. LEGATO Output (ABC Notation Text)", show_copy_button=True),
|
| 61 |
+
title="🎼 LEGATO End-to-End OMR Engine",
|
| 62 |
+
description="Running on standard CPU space. Conversion may take 10-15 seconds per image."
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
demo.launch()
|
| 67 |
|
| 68 |
+
|