wxnono commited on
Commit
146b933
·
verified ·
1 Parent(s): d6e0fa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -27
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. 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
 
 
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 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"),
51
- outputs=gr.Textbox(label="2. LEGATO Output (ABC Notation Text)"),
52
- title="🎼 LEGATO End-to-End OMR Engine (API Optimized)",
53
- description="Proxied through Hugging Face's serverless pipeline to bypass local 16GB CPU limits. Transcriptions finish in seconds."
54
- )
 
 
 
 
 
55
 
 
56
  if __name__ == "__main__":
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)"),
61
- title="🎼 LEGATO End-to-End OMR Engine (API Optimized)",
62
- description="Proxied through Hugging Face's serverless pipeline to bypass local 16GB CPU limits. Transcriptions finish in seconds."
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