Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,63 +4,65 @@ import gradio as gr
|
|
| 4 |
from PIL import Image
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
# 1.
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
-
|
| 10 |
-
# 2. Initialize the client with explicit server-side timeouts
|
| 11 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 12 |
-
client = InferenceClient(model=MODEL_ID, token=hf_token, timeout=30)
|
| 13 |
|
| 14 |
print("Hugging Face API Pipeline active. Forwarding inference tasks...")
|
| 15 |
|
|
|
|
| 16 |
def run_legato_omr(image):
|
| 17 |
if image is None:
|
| 18 |
return "Please upload a sheet music image first!"
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
# Convert
|
| 22 |
buffered = io.BytesIO()
|
| 23 |
image.save(buffered, format="JPEG")
|
| 24 |
image_bytes = buffered.getvalue()
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
response = client.post(data=image_bytes, model=MODEL_ID)
|
| 30 |
-
|
| 31 |
-
# Decode the raw server response text
|
| 32 |
result_text = response.decode("utf-8")
|
| 33 |
|
| 34 |
if not result_text or "error" in result_text.lower():
|
| 35 |
-
return f"
|
| 36 |
|
| 37 |
return result_text
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
return (
|
| 41 |
-
f"
|
| 42 |
"💡 Troubleshooting Tips:\n"
|
| 43 |
"1. If a 'Timeout' occurred, the remote server is spinning up. Try again in a minute.\n"
|
| 44 |
-
"2.
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# 3.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
if __name__ == "__main__":
|
| 57 |
-
demo
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
| 64 |
|
| 65 |
|
| 66 |
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# 1. Access secure system keys
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
| 9 |
MODEL_ID = "guangyangmusic/legato-small"
|
|
|
|
| 10 |
|
| 11 |
print("Hugging Face API Pipeline active. Forwarding inference tasks...")
|
| 12 |
|
| 13 |
+
# 2. Build the explicit serverless inference router
|
| 14 |
def run_legato_omr(image):
|
| 15 |
if image is None:
|
| 16 |
return "Please upload a sheet music image first!"
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
# Convert incoming canvas/image assets to generic bytes array
|
| 20 |
buffered = io.BytesIO()
|
| 21 |
image.save(buffered, format="JPEG")
|
| 22 |
image_bytes = buffered.getvalue()
|
| 23 |
|
| 24 |
+
# Instantiate localized pipeline execution clients
|
| 25 |
+
client = InferenceClient(model=MODEL_ID, token=hf_token, timeout=30)
|
| 26 |
|
| 27 |
+
print("Streaming asset matrices to Hugging Face cluster...")
|
| 28 |
response = client.post(data=image_bytes, model=MODEL_ID)
|
|
|
|
|
|
|
| 29 |
result_text = response.decode("utf-8")
|
| 30 |
|
| 31 |
if not result_text or "error" in result_text.lower():
|
| 32 |
+
return f"Cluster Warmup Alert: The remote model is currently spin up its parameters. Please resubmit this image in 10-15 seconds.\n\nDetails: {result_text}"
|
| 33 |
|
| 34 |
return result_text
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
return (
|
| 38 |
+
f"API Communication Exception: {str(e)}\n\n"
|
| 39 |
"💡 Troubleshooting Tips:\n"
|
| 40 |
"1. If a 'Timeout' occurred, the remote server is spinning up. Try again in a minute.\n"
|
| 41 |
+
"2. Ensure your Space Secret Key is exactly named HF_TOKEN and contains a valid 'Read' token."
|
| 42 |
)
|
| 43 |
|
| 44 |
+
# 3. Formulate the visual block interface
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("# 🎼 LEGATO End-to-End OMR Engine (API Cluster Layer)")
|
| 47 |
+
gr.Markdown("Proxied via serverless API hooks to bypass local 16GB CPU limits. Conversions take 2-5 seconds per line snippet.")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
input_image = gr.Image(type="pil", label="1. Upload Sheet Music Snippet")
|
| 52 |
+
submit_btn = gr.Button("Analyze Layout & Transcribe", variant="primary")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
output_text = gr.Textbox(label="2. LEGATO Output (ABC Notation)", lines=12)
|
| 55 |
+
|
| 56 |
+
submit_btn.click(fn=run_legato_omr, inputs=input_image, outputs=output_text)
|
| 57 |
|
| 58 |
+
# 4. CRITICAL REPAIR: Enforce port tracking to prevent premature file close execution
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
demo.queue().launch(
|
| 61 |
+
server_name="0.0.0.0",
|
| 62 |
+
server_port=7860,
|
| 63 |
+
ssr=False
|
| 64 |
+
)
|
| 65 |
+
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
|