judith.ibanez commited on
Commit
3723edd
·
1 Parent(s): 11a8c1f

add some modifications

Browse files
Files changed (1) hide show
  1. app.py +23 -20
app.py CHANGED
@@ -3,7 +3,6 @@ import subprocess
3
  import os
4
  import base64
5
  from typing import List, Dict, Any
6
- import threading
7
  from pathlib import Path
8
  from datetime import datetime
9
 
@@ -226,7 +225,7 @@ class MusicRecognitionMCPServer:
226
  audiveris = "/opt/audiveris/bin/Audiveris"
227
 
228
  if output_dir is None:
229
- output_dir = "/tmp/output"
230
 
231
  # Ensure output directory exists with proper permissions
232
  os.makedirs(output_dir, exist_ok=True)
@@ -309,8 +308,8 @@ class MusicRecognitionMCPServer:
309
  print(f"⚠️ Warning: Could not test Audiveris: {e}")
310
  return False
311
 
312
- # Initialize MCP Server
313
- mcp_server = MusicRecognitionMCPServer(["/tmp", "uploads", "output"])
314
 
315
  def recognize_music_gradio(pdf_file):
316
  """Gradio wrapper for music recognition"""
@@ -345,9 +344,9 @@ def recognize_music_gradio(pdf_file):
345
  # If the above doesn't work, try to find the file directly
346
  pdf_basename = os.path.splitext(os.path.basename(pdf_file.name))[0]
347
  possible_files = [
348
- f"/tmp/output/{pdf_basename}.mxl",
349
- f"/tmp/output/{pdf_basename}.xml",
350
- f"/tmp/output/{pdf_basename}.musicxml"
351
  ]
352
 
353
  for file_path in possible_files:
@@ -357,7 +356,7 @@ def recognize_music_gradio(pdf_file):
357
  return file_path
358
 
359
  print("❌ No output file found in any expected location")
360
- print(f"Files in /tmp/output: {os.listdir('/tmp/output') if os.path.exists('/tmp/output') else 'Directory not found'}")
361
  return None
362
 
363
  except Exception as e:
@@ -375,20 +374,24 @@ gradio_interface = gr.Interface(
375
  description="Upload a PDF music score and create a MusicXML file from it.",
376
  )
377
 
378
- def run_gradio():
379
- """Run Gradio in a separate thread"""
380
- gradio_interface.launch(
381
- server_name="0.0.0.0",
382
- server_port=7860,
383
- share=True,
384
- mcp_server=True,
385
- prevent_thread_lock=True
386
- )
387
 
388
  if __name__ == "__main__":
389
- # Start Gradio in a separate thread
390
- gradio_thread = threading.Thread(target=run_gradio, daemon=True)
391
- gradio_thread.start()
392
 
393
  print("🎵 MCP-Compliant Music Recognition Service Starting...")
394
  print("📱 Gradio UI: http://localhost:7860")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
  import base64
5
  from typing import List, Dict, Any
 
6
  from pathlib import Path
7
  from datetime import datetime
8
 
 
225
  audiveris = "/opt/audiveris/bin/Audiveris"
226
 
227
  if output_dir is None:
228
+ output_dir = "/tmp"
229
 
230
  # Ensure output directory exists with proper permissions
231
  os.makedirs(output_dir, exist_ok=True)
 
308
  print(f"⚠️ Warning: Could not test Audiveris: {e}")
309
  return False
310
 
311
+ # Initialize MCP Server with proper absolute paths
312
+ mcp_server = MusicRecognitionMCPServer(["/tmp", "/app/uploads", "/app/output"])
313
 
314
  def recognize_music_gradio(pdf_file):
315
  """Gradio wrapper for music recognition"""
 
344
  # If the above doesn't work, try to find the file directly
345
  pdf_basename = os.path.splitext(os.path.basename(pdf_file.name))[0]
346
  possible_files = [
347
+ f"/tmp/{pdf_basename}.mxl",
348
+ f"/tmp/{pdf_basename}.xml",
349
+ f"/tmp/{pdf_basename}.musicxml"
350
  ]
351
 
352
  for file_path in possible_files:
 
356
  return file_path
357
 
358
  print("❌ No output file found in any expected location")
359
+ print(f"Files in /tmp: {os.listdir('/tmp') if os.path.exists('/tmp') else 'Directory not found'}")
360
  return None
361
 
362
  except Exception as e:
 
374
  description="Upload a PDF music score and create a MusicXML file from it.",
375
  )
376
 
377
+ # Removed run_gradio function - now running directly in main thread
 
 
 
 
 
 
 
 
378
 
379
  if __name__ == "__main__":
380
+ print("===== Application Startup at {} =====".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
381
+ print()
 
382
 
383
  print("🎵 MCP-Compliant Music Recognition Service Starting...")
384
  print("📱 Gradio UI: http://localhost:7860")
385
+
386
+ # Run Gradio directly in the main thread to keep the application alive
387
+ try:
388
+ gradio_interface.launch(
389
+ server_name="0.0.0.0",
390
+ server_port=7860,
391
+ share=False, # Set to False for container deployment
392
+ prevent_thread_lock=False # Allow blocking to keep main thread alive
393
+ )
394
+ except Exception as e:
395
+ print(f"❌ Failed to start Gradio interface: {e}")
396
+ import traceback
397
+ traceback.print_exc()