blackshadow1 commited on
Commit
b9d8320
·
verified ·
1 Parent(s): bf6b28e

added the new js impl.✅✅

Browse files
Files changed (1) hide show
  1. mediSync/app.py +129 -94
mediSync/app.py CHANGED
@@ -10,11 +10,16 @@ import gradio as gr
10
  from PIL import Image
11
  import requests
12
  import tempfile
13
-
14
-
15
  import gradio as gr
16
  import matplotlib.pyplot as plt
17
  from PIL import Image
 
 
 
 
 
 
 
18
 
19
  # Add parent directory to path
20
  parent_dir = os.path.dirname(os.path.abspath(__file__))
@@ -394,11 +399,10 @@ class MediSyncApp:
394
 
395
 
396
  def create_interface():
397
- """Create and launch the Gradio interface with all fixes implemented."""
398
-
399
  app = MediSyncApp()
400
 
401
- # Example medical report for demo
402
  example_report = """
403
  CHEST X-RAY EXAMINATION
404
 
@@ -417,54 +421,73 @@ def create_interface():
417
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
418
  """
419
 
420
- # Get sample image path with robust error handling
421
  sample_image_path = None
422
  try:
423
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
424
  os.makedirs(sample_images_dir, exist_ok=True)
425
-
426
- # Check for existing images first
427
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
428
-
429
- if not sample_images:
430
- # Download fallback sample image if none exist
431
- fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
432
- sample_path = sample_images_dir / "sample_xray.jpg"
433
-
434
- try:
435
- response = requests.get(fallback_url, timeout=10)
436
- if response.status_code == 200:
437
- with open(sample_path, 'wb') as f:
438
- f.write(response.content)
439
- sample_image_path = str(sample_path)
440
- logging.info("Successfully downloaded fallback sample image")
441
- else:
442
- logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
443
- except Exception as download_error:
444
- logging.warning(f"Could not download sample image: {str(download_error)}")
445
- else:
446
  sample_image_path = str(sample_images[0])
447
  except Exception as e:
448
- logging.error(f"Error setting up sample images: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
 
450
- # Define interface with robust parameter handling
451
  with gr.Blocks(
452
- title="MediSync: Multi-Modal Medical Analysis System",
453
- theme=gr.themes.Soft()
 
454
  ) as interface:
455
- # Get appointment ID from URL parameters
456
- try:
457
- from gradio.context import Context
458
- appointment_id_value = Context.request.query_params.get("appointment_id", "") if hasattr(Context, 'request') else ""
459
- except Exception as e:
460
- logging.warning(f"Could not get URL parameters: {str(e)}")
461
- appointment_id_value = ""
 
 
 
 
 
 
 
 
 
462
 
 
463
  appointment_id = gr.Textbox(
 
464
  visible=False,
465
- value=appointment_id_value
466
  )
467
 
 
 
 
 
468
  gr.Markdown("""
469
  # MediSync: Multi-Modal Medical Analysis System
470
 
@@ -474,8 +497,8 @@ def create_interface():
474
  ## How to Use
475
  1. Upload a chest X-ray image
476
  2. Enter the corresponding medical report text
477
- 3. Choose the analysis type: image-only, text-only, or multimodal (combined)
478
- 4. Click "End Consultation" when finished to complete your appointment
479
  """)
480
 
481
  with gr.Tab("Multimodal Analysis"):
@@ -573,92 +596,104 @@ def create_interface():
573
  with gr.Row():
574
  with gr.Column():
575
  end_consultation_btn = gr.Button(
576
- "End Consultation",
577
  variant="stop",
578
  size="lg"
579
  )
580
  completion_status = gr.HTML()
581
 
582
- # Set up event handlers
583
- multi_img_enhance.click(
584
- app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
585
- )
586
- multi_analyze_btn.click(
587
- app.analyze_multimodal,
588
- inputs=[multi_img_input, multi_text_input],
589
- outputs=[multi_results, multi_plot],
590
- )
591
-
592
- img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
593
- img_analyze_btn.click(
594
- app.analyze_image,
595
- inputs=img_input,
596
- outputs=[img_output, img_results, img_plot],
597
- )
598
-
599
- text_analyze_btn.click(
600
- app.analyze_text,
601
- inputs=text_input,
602
- outputs=[text_output, text_results, text_plot],
603
- )
604
-
605
  def complete_consultation(appointment_id):
606
  """Handle consultation completion."""
607
  if not appointment_id:
608
- return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
 
 
 
 
 
 
 
 
 
 
 
 
609
 
610
  try:
611
- # Replace with your actual Flask app URL
612
- flask_app_url = "http://127.0.0.1:600/complete_consultation"
613
-
614
  response = requests.post(
615
- flask_app_url,
616
  json={"appointment_id": appointment_id},
617
  timeout=10
618
  )
619
-
620
- if response.status_code == 200:
621
- return """
622
- <div class='alert alert-success'>
623
- Consultation completed successfully. Redirecting...
624
- <script>
625
- setTimeout(function() {
626
- window.location.href = "http://127.0.0.1:600/doctors";
627
- }, 2000);
628
- </script>
629
  </div>
 
 
 
630
  """
 
 
631
  else:
 
 
 
 
632
  return f"""
633
- <div class='alert alert-error'>
634
- Error completing appointment (Status: {response.status_code}).
635
- Please contact support.
 
636
  </div>
637
  """
638
-
639
  except Exception as e:
 
640
  return f"""
641
- <div class='alert alert-error'>
642
- Error: {str(e)}
 
 
643
  </div>
644
  """
645
 
646
  end_consultation_btn.click(
647
  fn=complete_consultation,
648
  inputs=[appointment_id],
649
- outputs=completion_status
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650
  )
651
 
652
  try:
653
  interface.launch()
654
  except Exception as e:
655
- logging.error(f"Failed to launch interface: {str(e)}")
656
- raise RuntimeError("Failed to launch MediSync interface") from e
657
-
658
 
659
  if __name__ == "__main__":
660
- logging.basicConfig(
661
- level=logging.INFO,
662
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
663
- )
664
- create_interface()
 
10
  from PIL import Image
11
  import requests
12
  import tempfile
 
 
13
  import gradio as gr
14
  import matplotlib.pyplot as plt
15
  from PIL import Image
16
+ import os
17
+ import logging
18
+ import requests
19
+ from pathlib import Path
20
+ from typing import Optional
21
+ import gradio as gr
22
+ from PIL import Image
23
 
24
  # Add parent directory to path
25
  parent_dir = os.path.dirname(os.path.abspath(__file__))
 
399
 
400
 
401
  def create_interface():
402
+ """Create and launch the Gradio interface with all fixes."""
 
403
  app = MediSyncApp()
404
 
405
+ # Example medical report
406
  example_report = """
407
  CHEST X-RAY EXAMINATION
408
 
 
421
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
422
  """
423
 
424
+ # Sample image handling
425
  sample_image_path = None
426
  try:
427
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
428
  os.makedirs(sample_images_dir, exist_ok=True)
 
 
429
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
430
+ if sample_images:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
  sample_image_path = str(sample_images[0])
432
  except Exception as e:
433
+ logger.error(f"Error handling sample images: {e}")
434
+
435
+ # Custom CSS for styling
436
+ custom_css = """
437
+ .alert-box {
438
+ padding: 15px;
439
+ margin: 10px 0;
440
+ border-radius: 5px;
441
+ font-family: sans-serif;
442
+ }
443
+ .alert-error {
444
+ background-color: #ffebee;
445
+ color: #b71c1c;
446
+ border-left: 5px solid #b71c1c;
447
+ }
448
+ .alert-success {
449
+ background-color: #e8f5e9;
450
+ color: #1b5e20;
451
+ border-left: 5px solid #1b5e20;
452
+ }
453
+ .fa-icon {
454
+ margin-right: 8px;
455
+ }
456
+ """
457
 
 
458
  with gr.Blocks(
459
+ title="MediSync: Multi-Modal Medical Analysis System",
460
+ theme=gr.themes.Soft(),
461
+ css=custom_css
462
  ) as interface:
463
+
464
+ # JavaScript to handle URL parameters
465
+ js_code = """
466
+ function getAppointmentId() {
467
+ const urlParams = new URLSearchParams(window.location.search);
468
+ return urlParams.get('appointment_id') || '';
469
+ }
470
+ function setAppointmentId() {
471
+ const id = getAppointmentId();
472
+ if (id) {
473
+ document.getElementById('appointment-id').value = id;
474
+ console.log('Appointment ID set:', id);
475
+ }
476
+ }
477
+ window.onload = setAppointmentId;
478
+ """
479
 
480
+ # Hidden appointment ID field
481
  appointment_id = gr.Textbox(
482
+ elem_id="appointment-id",
483
  visible=False,
484
+ label="Appointment ID"
485
  )
486
 
487
+ # Initial JS to extract appointment ID
488
+ interface.load(None, None, None, _js=js_code)
489
+
490
+ # Main interface layout
491
  gr.Markdown("""
492
  # MediSync: Multi-Modal Medical Analysis System
493
 
 
497
  ## How to Use
498
  1. Upload a chest X-ray image
499
  2. Enter the corresponding medical report text
500
+ 3. Choose the analysis type
501
+ 4. Click "End Consultation" when finished
502
  """)
503
 
504
  with gr.Tab("Multimodal Analysis"):
 
596
  with gr.Row():
597
  with gr.Column():
598
  end_consultation_btn = gr.Button(
599
+ "End Consultation",
600
  variant="stop",
601
  size="lg"
602
  )
603
  completion_status = gr.HTML()
604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
605
  def complete_consultation(appointment_id):
606
  """Handle consultation completion."""
607
  if not appointment_id:
608
+ error_html = """
609
+ <div class="alert-box alert-error">
610
+ <i class="fas fa-exclamation-triangle fa-icon"></i>
611
+ <strong>No Appointment ID Found</strong>
612
+ <p>Your session may not be properly recorded.</p>
613
+ <p>You will be redirected back to the main page.</p>
614
+ </div>
615
+ <script>
616
+ setTimeout(() => window.location.href = "http://127.0.0.1:600/complete_appointment", 3000);
617
+ </script>
618
+ """
619
+ logger.error("Completion attempted without appointment ID")
620
+ return error_html
621
 
622
  try:
623
+ # Call your Flask endpoint
 
 
624
  response = requests.post(
625
+ "http://127.0.0.1:600/complete_consultation",
626
  json={"appointment_id": appointment_id},
627
  timeout=10
628
  )
629
+
630
+ if response.ok:
631
+ success_html = """
632
+ <div class="alert-box alert-success">
633
+ <i class="fas fa-check-circle fa-icon"></i>
634
+ <strong>Consultation Completed</strong>
635
+ <p>You will be redirected shortly.</p>
 
 
 
636
  </div>
637
+ <script>
638
+ setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 2000);
639
+ </script>
640
  """
641
+ logger.info(f"Successfully completed appointment {appointment_id}")
642
+ return success_html
643
  else:
644
+ error_msg = f"Server error: {response.status_code}"
645
+ if response.text:
646
+ error_msg += f" - {response.text[:200]}"
647
+ logger.error(error_msg)
648
  return f"""
649
+ <div class="alert-box alert-error">
650
+ <i class="fas fa-exclamation-triangle fa-icon"></i>
651
+ <strong>Error Completing Appointment</strong>
652
+ <p>{error_msg}</p>
653
  </div>
654
  """
 
655
  except Exception as e:
656
+ logger.error(f"Completion error: {str(e)}")
657
  return f"""
658
+ <div class="alert-box alert-error">
659
+ <i class="fas fa-exclamation-triangle fa-icon"></i>
660
+ <strong>Connection Error</strong>
661
+ <p>{str(e)}</p>
662
  </div>
663
  """
664
 
665
  end_consultation_btn.click(
666
  fn=complete_consultation,
667
  inputs=[appointment_id],
668
+ outputs=[completion_status]
669
+ )
670
+
671
+ # Set up all your existing analysis handlers here
672
+ multi_img_enhance.click(
673
+ app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
674
+ )
675
+ multi_analyze_btn.click(
676
+ app.analyze_multimodal,
677
+ inputs=[multi_img_input, multi_text_input],
678
+ outputs=[multi_results, multi_plot],
679
+ )
680
+ img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
681
+ img_analyze_btn.click(
682
+ app.analyze_image,
683
+ inputs=img_input,
684
+ outputs=[img_output, img_results, img_plot],
685
+ )
686
+ text_analyze_btn.click(
687
+ app.analyze_text,
688
+ inputs=text_input,
689
+ outputs=[text_output, text_results, text_plot],
690
  )
691
 
692
  try:
693
  interface.launch()
694
  except Exception as e:
695
+ logger.error(f"Failed to launch interface: {str(e)}")
696
+ raise
 
697
 
698
  if __name__ == "__main__":
699
+ create_interface()