wxnono commited on
Commit
42c5bbc
·
verified ·
1 Parent(s): 0a760a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
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 optimized serverless client targeting LEGATO
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 standard network payload streaming
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
- # Handle different inference response formats
31
- if hasattr(response, 'generated_text'):
32
- return response.generated_text
33
- elif isinstance(response, dict) and "generated_text" in response:
34
- return response["generated_text"]
35
- else:
36
- return str(response)
 
 
 
37
 
38
  except Exception as e:
39
  return (
40
- f"API Inference Call Error: {str(e)}\n\n"
41
- "💡 Tip: Ensure your HF_TOKEN has 'Read' access, and that "
42
- "the model page doesn't require separate terms acceptance."
 
43
  )
44
 
45
- # 3. Present UI layout blocks - FIXED: Removed show_copy_button for backwards compatibility
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"),