judith.ibanez commited on
Commit
1c5ed00
Β·
1 Parent(s): 15e4ee8

add some modifications

Browse files
Files changed (1) hide show
  1. app.py +304 -8
app.py CHANGED
@@ -365,14 +365,310 @@ def recognize_music_gradio(pdf_file):
365
  traceback.print_exc()
366
  return None
367
 
368
- # Create Gradio interface
369
- gradio_interface = gr.Interface(
370
- fn=recognize_music_gradio,
371
- inputs=gr.File(file_types=[".pdf"], label="Upload PDF music score"),
372
- outputs=gr.File(label="Download MusicXML file"),
373
- title="Music Score Recognition",
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
 
 
365
  traceback.print_exc()
366
  return None
367
 
368
+ # Create enhanced Gradio interface with custom CSS and better UX
369
+ custom_css = """
370
+ .gradio-container {
371
+ max-width: 1200px !important;
372
+ margin: auto !important;
373
+ }
374
+
375
+ .main-header {
376
+ text-align: center;
377
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
378
+ color: white;
379
+ padding: 2rem;
380
+ border-radius: 15px;
381
+ margin-bottom: 2rem;
382
+ box-shadow: 0 8px 32px rgba(0,0,0,0.1);
383
+ }
384
+
385
+ .feature-card {
386
+ background: white;
387
+ border-radius: 12px;
388
+ padding: 1.5rem;
389
+ margin: 1rem 0;
390
+ box-shadow: 0 4px 16px rgba(0,0,0,0.1);
391
+ border-left: 4px solid #667eea;
392
+ }
393
+
394
+ .upload-area {
395
+ border: 2px dashed #667eea;
396
+ border-radius: 12px;
397
+ padding: 2rem;
398
+ text-align: center;
399
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
400
+ transition: all 0.3s ease;
401
+ }
402
+
403
+ .upload-area:hover {
404
+ border-color: #764ba2;
405
+ background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
406
+ }
407
+
408
+ .status-success {
409
+ background: linear-gradient(135deg, #4caf50 0%, #45a049 100%);
410
+ color: white;
411
+ padding: 1rem;
412
+ border-radius: 8px;
413
+ margin: 1rem 0;
414
+ }
415
+
416
+ .status-error {
417
+ background: linear-gradient(135deg, #f44336 0%, #d32f2f 100%);
418
+ color: white;
419
+ padding: 1rem;
420
+ border-radius: 8px;
421
+ margin: 1rem 0;
422
+ }
423
+
424
+ .info-box {
425
+ background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
426
+ border-radius: 8px;
427
+ padding: 1rem;
428
+ margin: 1rem 0;
429
+ border-left: 4px solid #2196f3;
430
+ }
431
+ """
432
+
433
+ def create_gradio_interface():
434
+ with gr.Blocks(css=custom_css, title="🎼 Audiveris Music Score Recognition", theme=gr.themes.Soft()) as interface:
435
+ # Header
436
+ gr.HTML("""
437
+ <div class="main-header">
438
+ <h1>🎼 Audiveris Music Score Recognition</h1>
439
+ <p style="font-size: 1.2em; margin-top: 1rem; opacity: 0.9;">
440
+ Transform your PDF music scores into editable MusicXML files using advanced AI recognition
441
+ </p>
442
+ </div>
443
+ """)
444
+
445
+ # Main content area
446
+ with gr.Row():
447
+ with gr.Column(scale=1):
448
+ gr.HTML("""
449
+ <div class="feature-card">
450
+ <h3>✨ Features</h3>
451
+ <ul style="line-height: 1.8;">
452
+ <li>🎡 High-accuracy music notation recognition</li>
453
+ <li>πŸ“„ PDF to MusicXML conversion</li>
454
+ <li>🎹 Supports complex musical scores</li>
455
+ <li>⚑ Fast processing with Audiveris engine</li>
456
+ <li>πŸ’Ύ Downloadable results</li>
457
+ </ul>
458
+ </div>
459
+ """)
460
+
461
+ gr.HTML("""
462
+ <div class="info-box">
463
+ <h4>πŸ“‹ How to use:</h4>
464
+ <ol style="line-height: 1.6;">
465
+ <li>Upload your PDF music score</li>
466
+ <li>Click "🎡 Convert to MusicXML"</li>
467
+ <li>Wait for processing to complete</li>
468
+ <li>Download your MusicXML file</li>
469
+ </ol>
470
+ </div>
471
+ """)
472
+
473
+ with gr.Column(scale=2):
474
+ # File upload section
475
+ gr.HTML("<h3 style='text-align: center; color: #667eea;'>πŸ“ Upload Your Music Score</h3>")
476
+
477
+ pdf_input = gr.File(
478
+ file_types=[".pdf"],
479
+ label="Select PDF File",
480
+ file_count="single",
481
+ height=200,
482
+ elem_classes=["upload-area"]
483
+ )
484
+
485
+ # Processing button
486
+ convert_btn = gr.Button(
487
+ "🎡 Convert to MusicXML",
488
+ variant="primary",
489
+ size="lg",
490
+ scale=1
491
+ )
492
+
493
+ # Status and progress
494
+ status_display = gr.HTML(visible=False)
495
+ progress_bar = gr.Progress()
496
+
497
+ # Output section
498
+ gr.HTML("<h3 style='text-align: center; color: #667eea; margin-top: 2rem;'>πŸ“₯ Download Results</h3>")
499
+
500
+ output_file = gr.File(
501
+ label="MusicXML Output",
502
+ visible=False,
503
+ height=100
504
+ )
505
+
506
+ # Processing info
507
+ processing_info = gr.Textbox(
508
+ label="Processing Details",
509
+ lines=8,
510
+ visible=False,
511
+ interactive=False
512
+ )
513
+
514
+ # Footer
515
+ gr.HTML("""
516
+ <div style="text-align: center; margin-top: 3rem; padding: 2rem; background: #f8f9fa; border-radius: 12px;">
517
+ <p style="color: #666; margin: 0;">
518
+ Powered by <strong>Audiveris</strong> β€’ Built with ❀️ using Gradio
519
+ </p>
520
+ <p style="color: #888; font-size: 0.9em; margin-top: 0.5rem;">
521
+ For best results, use high-quality PDF scans with clear musical notation
522
+ </p>
523
+ </div>
524
+ """)
525
+
526
+ # Enhanced processing function with better feedback
527
+ def process_with_feedback(pdf_file, progress=gr.Progress()):
528
+ if pdf_file is None:
529
+ return (
530
+ gr.HTML("<div class='status-error'>❌ Please upload a PDF file first!</div>", visible=True),
531
+ None,
532
+ gr.Textbox(visible=False),
533
+ gr.File(visible=False)
534
+ )
535
+
536
+ try:
537
+ # Show processing status
538
+ progress(0.1, desc="πŸ“„ Analyzing PDF file...")
539
+
540
+ status_html = """
541
+ <div class='status-success'>
542
+ <h4>πŸ”„ Processing your music score...</h4>
543
+ <p>File: <strong>{}</strong></p>
544
+ <p>Size: <strong>{:.2f} MB</strong></p>
545
+ <p>Please wait while Audiveris analyzes your score...</p>
546
+ </div>
547
+ """.format(
548
+ pdf_file.name.split('/')[-1],
549
+ os.path.getsize(pdf_file.name) / (1024*1024)
550
+ )
551
+
552
+ progress(0.3, desc="🎡 Running Audiveris recognition...")
553
+
554
+ # Process the file
555
+ result_file = recognize_music_gradio(pdf_file)
556
+
557
+ progress(0.9, desc="βœ… Finalizing results...")
558
+
559
+ if result_file and os.path.exists(result_file):
560
+ # Success
561
+ success_html = """
562
+ <div class='status-success'>
563
+ <h4>βœ… Conversion completed successfully!</h4>
564
+ <p>πŸ“ Output: <strong>{}</strong></p>
565
+ <p>πŸ“Š Size: <strong>{:.2f} KB</strong></p>
566
+ <p>πŸŽ‰ Your MusicXML file is ready for download!</p>
567
+ </div>
568
+ """.format(
569
+ os.path.basename(result_file),
570
+ os.path.getsize(result_file) / 1024
571
+ )
572
+
573
+ # Processing details
574
+ details = f"""βœ… CONVERSION SUCCESSFUL
575
+
576
+ πŸ“„ Input File: {pdf_file.name.split('/')[-1]}
577
+ πŸ“Š Input Size: {os.path.getsize(pdf_file.name) / (1024*1024):.2f} MB
578
+
579
+ 🎡 Output File: {os.path.basename(result_file)}
580
+ πŸ“Š Output Size: {os.path.getsize(result_file) / 1024:.2f} KB
581
+
582
+ ⏱️ Processing completed at: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
583
+
584
+ 🎼 Your PDF music score has been successfully converted to MusicXML format!
585
+ You can now download the file and use it in music notation software like MuseScore, Finale, or Sibelius."""
586
+
587
+ progress(1.0, desc="πŸŽ‰ Complete!")
588
+
589
+ return (
590
+ gr.HTML(success_html, visible=True),
591
+ gr.File(result_file, visible=True),
592
+ gr.Textbox(details, visible=True),
593
+ gr.File(visible=True)
594
+ )
595
+ else:
596
+ # Failure
597
+ error_html = """
598
+ <div class='status-error'>
599
+ <h4>❌ Conversion failed</h4>
600
+ <p>The music recognition process encountered an error.</p>
601
+ <p>Please check that your PDF contains clear musical notation and try again.</p>
602
+ </div>
603
+ """
604
+
605
+ error_details = f"""❌ CONVERSION FAILED
606
+
607
+ πŸ“„ Input File: {pdf_file.name.split('/')[-1]}
608
+ πŸ“Š Input Size: {os.path.getsize(pdf_file.name) / (1024*1024):.2f} MB
609
+
610
+ ⚠️ Error: No output file was generated by Audiveris
611
+ ⏱️ Failed at: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
612
+
613
+ πŸ’‘ Troubleshooting tips:
614
+ β€’ Ensure your PDF contains clear, high-quality musical notation
615
+ β€’ Check that the PDF is not password-protected
616
+ β€’ Try with a different PDF file
617
+ β€’ Make sure the musical notation is not handwritten"""
618
+
619
+ return (
620
+ gr.HTML(error_html, visible=True),
621
+ None,
622
+ gr.Textbox(error_details, visible=True),
623
+ gr.File(visible=False)
624
+ )
625
+
626
+ except Exception as e:
627
+ # Exception handling
628
+ error_html = f"""
629
+ <div class='status-error'>
630
+ <h4>❌ Processing Error</h4>
631
+ <p>An unexpected error occurred: <code>{str(e)}</code></p>
632
+ <p>Please try again or contact support if the problem persists.</p>
633
+ </div>
634
+ """
635
+
636
+ error_details = f"""❌ PROCESSING ERROR
637
+
638
+ πŸ“„ Input File: {pdf_file.name.split('/')[-1] if pdf_file else 'Unknown'}
639
+ ⚠️ Error: {str(e)}
640
+ ⏱️ Failed at: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
641
+
642
+ πŸ”§ Technical Details:
643
+ {str(e)}
644
+
645
+ Please try again with a different file or contact support."""
646
+
647
+ return (
648
+ gr.HTML(error_html, visible=True),
649
+ None,
650
+ gr.Textbox(error_details, visible=True),
651
+ gr.File(visible=False)
652
+ )
653
+
654
+ # Connect the button to the processing function
655
+ convert_btn.click(
656
+ fn=process_with_feedback,
657
+ inputs=[pdf_input],
658
+ outputs=[status_display, output_file, processing_info, output_file],
659
+ show_progress=True
660
+ )
661
+
662
+ # Auto-hide status when new file is uploaded
663
+ pdf_input.change(
664
+ fn=lambda: (gr.HTML(visible=False), gr.Textbox(visible=False), gr.File(visible=False)),
665
+ outputs=[status_display, processing_info, output_file]
666
+ )
667
+
668
+ return interface
669
+
670
+ # Create the enhanced interface
671
+ gradio_interface = create_gradio_interface()
672
 
673
  # Removed run_gradio function - now running directly in main thread
674