Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ from huggingface_hub import InferenceClient
|
|
| 7 |
# 1. Safely extract your secure secret token
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
-
# 2. Initialize the
|
| 11 |
MODEL_ID = "guangyangmusic/legato-small"
|
| 12 |
-
client = InferenceClient(model=MODEL_ID, token=hf_token)
|
| 13 |
|
| 14 |
print("Hugging Face API Pipeline active. Forwarding inference tasks...")
|
| 15 |
|
|
@@ -18,31 +18,33 @@ def run_legato_omr(image):
|
|
| 18 |
return "Please upload a sheet music image first!"
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
# Convert PIL Image to raw bytes for
|
| 22 |
buffered = io.BytesIO()
|
| 23 |
image.save(buffered, format="JPEG")
|
| 24 |
image_bytes = buffered.getvalue()
|
| 25 |
|
| 26 |
-
# Stream the image asset straight to the dedicated model architecture host
|
| 27 |
print("Sending payload to Hugging Face Inference clusters...")
|
| 28 |
-
response = client.image_to_text(image_bytes)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
return (
|
| 40 |
-
f"
|
| 41 |
-
"💡
|
| 42 |
-
"
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# 3. Present UI layout blocks
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=run_legato_omr,
|
| 48 |
inputs=gr.Image(type="pil", label="1. Upload Sheet Music Snippet"),
|
|
|
|
| 7 |
# 1. Safely extract your secure secret token
|
| 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 |
|
|
|
|
| 18 |
return "Please upload a sheet music image first!"
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# Convert PIL Image to raw bytes for streaming
|
| 22 |
buffered = io.BytesIO()
|
| 23 |
image.save(buffered, format="JPEG")
|
| 24 |
image_bytes = buffered.getvalue()
|
| 25 |
|
|
|
|
| 26 |
print("Sending payload to Hugging Face Inference clusters...")
|
|
|
|
| 27 |
|
| 28 |
+
# Explicit call mapping for Image-to-Text pipelines
|
| 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"Server Status Alert: The cluster is currently loading this model weights. Please try submitting again in 10 seconds.\n\nRaw Response: {result_text}"
|
| 36 |
+
|
| 37 |
+
return result_text
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
return (
|
| 41 |
+
f"Inference Route Exception: {str(e)}\n\n"
|
| 42 |
+
"💡 Troubleshooting Tips:\n"
|
| 43 |
+
"1. If a 'Timeout' occurred, the remote server is spinning up. Try again in a minute.\n"
|
| 44 |
+
"2. Double check that your Space Secret Key is exactly named HF_TOKEN and contains a valid 'Read' token."
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# 3. Present UI layout blocks
|
| 48 |
demo = gr.Interface(
|
| 49 |
fn=run_legato_omr,
|
| 50 |
inputs=gr.Image(type="pil", label="1. Upload Sheet Music Snippet"),
|