blackshadow1 commited on
Commit
4c90d09
·
verified ·
1 Parent(s): bec3819

updated the code ✅✅ will see tomorrow

Browse files
Files changed (1) hide show
  1. mediSync/app.py +197 -116
mediSync/app.py CHANGED
@@ -399,9 +399,11 @@ class MediSyncApp:
399
 
400
 
401
  def create_interface():
402
- """Create and launch the Gradio interface with all fixes."""
 
403
  app = MediSyncApp()
404
 
 
405
  example_report = """
406
  CHEST X-RAY EXAMINATION
407
 
@@ -420,74 +422,52 @@ def create_interface():
420
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
421
  """
422
 
423
- # Sample image handling with local fallback
424
  sample_image_path = None
425
  try:
426
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
427
  os.makedirs(sample_images_dir, exist_ok=True)
428
 
429
- # First check for existing local images
430
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
431
 
432
  if not sample_images:
433
- # Use local fallback if no images exist
434
- local_fallback = Path(__file__).parent.parent / "static" / "fallback_xray.jpg"
435
- if local_fallback.exists():
436
- sample_image_path = str(local_fallback)
437
- logger.info("Using local fallback sample image")
438
- else:
439
- logger.warning("No local fallback image found")
 
 
 
 
 
 
 
 
440
  else:
441
  sample_image_path = str(sample_images[0])
442
  except Exception as e:
443
- logger.error(f"Error handling sample images: {str(e)}")
444
-
445
- custom_css = """
446
- .alert-box {
447
- padding: 15px;
448
- margin: 10px 0;
449
- border-radius: 5px;
450
- font-family: sans-serif;
451
- }
452
- .alert-error {
453
- background-color: #ffebee;
454
- color: #b71c1c;
455
- border-left: 5px solid #b71c1c;
456
- }
457
- .alert-success {
458
- background-color: #e8f5e9;
459
- color: #1b5e20;
460
- border-left: 5px solid #1b5e20;
461
- }
462
- """
463
 
 
464
  with gr.Blocks(
465
- title="MediSync: Multi-Modal Medical Analysis System",
466
- theme=gr.themes.Soft(),
467
- css=custom_css
468
  ) as interface:
469
-
470
- # Session state to track if consultation was properly recorded
471
- session_state = gr.State({
472
- 'recorded': False,
473
- 'appointment_id': ''
474
- })
475
-
476
- # Initialize appointment ID from URL
477
- def get_appointment_id():
478
- try:
479
- from gradio.context import Context
480
- if hasattr(Context, 'request') and hasattr(Context.request, 'query_params'):
481
- appointment_id = Context.request.query_params.get("appointment_id", "")
482
- if appointment_id:
483
- return {'recorded': False, 'appointment_id': appointment_id}
484
- except Exception as e:
485
- logger.warning(f"Could not get URL parameters: {str(e)}")
486
- return {'recorded': False, 'appointment_id': ''}
487
 
488
- interface.load(
489
- fn=get_appointment_id,
490
- outputs=[session_state]
491
  )
492
 
493
  gr.Markdown("""
@@ -495,94 +475,195 @@ def create_interface():
495
 
496
  This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
497
  to provide comprehensive medical insights.
 
 
 
 
 
 
498
  """)
499
 
500
- # ... (keep all your existing tabs and components)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
  with gr.Row():
503
  with gr.Column():
504
- end_consultation_btn = gr.Button("End Consultation", variant="stop", size="lg")
 
 
 
 
505
  completion_status = gr.HTML()
506
 
507
- def complete_consultation(session_state):
508
- """Handle consultation completion with proper session recording."""
509
- if not session_state['appointment_id']:
510
- error_html = """
511
- <div class="alert-box alert-error">
512
- <strong>No Appointment ID Found</strong>
513
- <p>Please contact support to ensure your session is recorded.</p>
514
- </div>
515
- """
516
- logger.error("Completion attempted without appointment ID")
517
- return {'recorded': False, 'appointment_id': ''}, error_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
 
519
  try:
520
- # First verify if already recorded
521
- if session_state['recorded']:
522
- success_html = """
523
- <div class="alert-box alert-success">
524
- <strong>Consultation Already Completed</strong>
525
- <p>You will be redirected shortly.</p>
526
- <script>
527
- setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 1000);
528
- </script>
529
- </div>
530
- """
531
- return session_state, success_html
532
-
533
- # Call your Flask endpoint to record completion
534
  response = requests.post(
535
- "http://127.0.0.1:600/complete_consultation",
536
- json={"appointment_id": session_state['appointment_id']},
537
  timeout=10
538
  )
539
-
540
- if response.ok:
541
- success_html = """
542
- <div class="alert-box alert-success">
543
- <strong>Consultation Successfully Recorded</strong>
544
- <p>Your session has been properly recorded.</p>
545
- <p>You will be redirected shortly.</p>
546
  <script>
547
- setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 2000);
 
 
548
  </script>
549
  </div>
550
  """
551
- logger.info(f"Successfully recorded appointment {session_state['appointment_id']}")
552
- return {'recorded': True, 'appointment_id': session_state['appointment_id']}, success_html
553
-
554
- error_msg = f"Server error: {response.status_code}"
555
- if response.text:
556
- error_msg += f" - {response.text[:200]}"
557
- logger.error(error_msg)
558
- return session_state, f"""
559
- <div class="alert-box alert-error">
560
- <strong>Recording Failed</strong>
561
- <p>{error_msg}</p>
562
- <p>Please try again or contact support.</p>
563
- </div>
564
- """
565
  except Exception as e:
566
- logger.error(f"Completion error: {str(e)}")
567
- return session_state, f"""
568
- <div class="alert-box alert-error">
569
- <strong>Connection Error</strong>
570
- <p>{str(e)}</p>
571
- <p>Please check your connection and try again.</p>
572
  </div>
573
  """
574
 
575
  end_consultation_btn.click(
576
  fn=complete_consultation,
577
- inputs=[session_state],
578
- outputs=[session_state, completion_status]
579
  )
580
-
581
  try:
582
  interface.launch()
583
  except Exception as e:
584
- logger.error(f"Failed to launch interface: {str(e)}")
585
- raise
 
586
 
587
  if __name__ == "__main__":
 
 
 
 
588
  create_interface()
 
399
 
400
 
401
  def create_interface():
402
+ """Create and launch the Gradio interface with all fixes implemented."""
403
+
404
  app = MediSyncApp()
405
 
406
+ # Example medical report for demo
407
  example_report = """
408
  CHEST X-RAY EXAMINATION
409
 
 
422
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
423
  """
424
 
425
+ # Get sample image path with robust error handling
426
  sample_image_path = None
427
  try:
428
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
429
  os.makedirs(sample_images_dir, exist_ok=True)
430
 
431
+ # Check for existing images first
432
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
433
 
434
  if not sample_images:
435
+ # Download fallback sample image if none exist
436
+ fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
437
+ sample_path = sample_images_dir / "sample_xray.jpg"
438
+
439
+ try:
440
+ response = requests.get(fallback_url, timeout=10)
441
+ if response.status_code == 200:
442
+ with open(sample_path, 'wb') as f:
443
+ f.write(response.content)
444
+ sample_image_path = str(sample_path)
445
+ logging.info("Successfully downloaded fallback sample image")
446
+ else:
447
+ logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
448
+ except Exception as download_error:
449
+ logging.warning(f"Could not download sample image: {str(download_error)}")
450
  else:
451
  sample_image_path = str(sample_images[0])
452
  except Exception as e:
453
+ logging.error(f"Error setting up sample images: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
 
455
+ # Define interface with robust parameter handling
456
  with gr.Blocks(
457
+ title="MediSync: Multi-Modal Medical Analysis System",
458
+ theme=gr.themes.Soft()
 
459
  ) as interface:
460
+ # Get appointment ID from URL parameters
461
+ try:
462
+ from gradio.context import Context
463
+ appointment_id_value = Context.request.query_params.get("appointment_id", "") if hasattr(Context, 'request') else ""
464
+ except Exception as e:
465
+ logging.warning(f"Could not get URL parameters: {str(e)}")
466
+ appointment_id_value = ""
 
 
 
 
 
 
 
 
 
 
 
467
 
468
+ appointment_id = gr.Textbox(
469
+ visible=False,
470
+ value=appointment_id_value
471
  )
472
 
473
  gr.Markdown("""
 
475
 
476
  This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
477
  to provide comprehensive medical insights.
478
+
479
+ ## How to Use
480
+ 1. Upload a chest X-ray image
481
+ 2. Enter the corresponding medical report text
482
+ 3. Choose the analysis type: image-only, text-only, or multimodal (combined)
483
+ 4. Click "End Consultation" when finished to complete your appointment
484
  """)
485
 
486
+ with gr.Tab("Multimodal Analysis"):
487
+ with gr.Row():
488
+ with gr.Column():
489
+ multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
490
+ multi_img_enhance = gr.Button("Enhance Image")
491
+
492
+ multi_text_input = gr.Textbox(
493
+ label="Enter Medical Report Text",
494
+ placeholder="Enter the radiologist's report text here...",
495
+ lines=10,
496
+ value=example_report if not sample_image_path else None,
497
+ )
498
+
499
+ multi_analyze_btn = gr.Button(
500
+ "Analyze Image & Text", variant="primary"
501
+ )
502
+
503
+ with gr.Column():
504
+ multi_results = gr.HTML(label="Analysis Results")
505
+ multi_plot = gr.HTML(label="Visualization")
506
+
507
+ if sample_image_path:
508
+ gr.Examples(
509
+ examples=[[sample_image_path, example_report]],
510
+ inputs=[multi_img_input, multi_text_input],
511
+ label="Example X-ray and Report",
512
+ )
513
 
514
+ with gr.Tab("Image Analysis"):
515
+ with gr.Row():
516
+ with gr.Column():
517
+ img_input = gr.Image(label="Upload X-ray Image", type="pil")
518
+ img_enhance = gr.Button("Enhance Image")
519
+ img_analyze_btn = gr.Button("Analyze Image", variant="primary")
520
+
521
+ with gr.Column():
522
+ img_output = gr.Image(label="Processed Image")
523
+ img_results = gr.HTML(label="Analysis Results")
524
+ img_plot = gr.HTML(label="Visualization")
525
+
526
+ if sample_image_path:
527
+ gr.Examples(
528
+ examples=[[sample_image_path]],
529
+ inputs=[img_input],
530
+ label="Example X-ray Image",
531
+ )
532
+
533
+ with gr.Tab("Text Analysis"):
534
+ with gr.Row():
535
+ with gr.Column():
536
+ text_input = gr.Textbox(
537
+ label="Enter Medical Report Text",
538
+ placeholder="Enter the radiologist's report text here...",
539
+ lines=10,
540
+ value=example_report,
541
+ )
542
+ text_analyze_btn = gr.Button("Analyze Text", variant="primary")
543
+
544
+ with gr.Column():
545
+ text_output = gr.Textbox(label="Processed Text")
546
+ text_results = gr.HTML(label="Analysis Results")
547
+ text_plot = gr.HTML(label="Entity Visualization")
548
+
549
+ gr.Examples(
550
+ examples=[[example_report]],
551
+ inputs=[text_input],
552
+ label="Example Medical Report",
553
+ )
554
+
555
+ with gr.Tab("About"):
556
+ gr.Markdown("""
557
+ ## About MediSync
558
+
559
+ MediSync is an AI-powered healthcare solution that uses multi-modal analysis to provide comprehensive insights from medical images and reports.
560
+
561
+ ### Key Features
562
+
563
+ - **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
564
+ - **Medical Report Processing**: Extracts key information from patient reports using NLP models
565
+ - **Multi-modal Integration**: Combines insights from both image and text data for more accurate analysis
566
+
567
+ ### Models Used
568
+
569
+ - **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
570
+ - **Medical Text Analysis**: medicalai/ClinicalBERT
571
+
572
+ ### Important Disclaimer
573
+
574
+ This tool is for educational and research purposes only. It is not intended to provide medical advice or replace professional healthcare. Always consult with qualified healthcare providers for medical decisions.
575
+ """)
576
+
577
+ # Consultation completion section
578
  with gr.Row():
579
  with gr.Column():
580
+ end_consultation_btn = gr.Button(
581
+ "End Consultation",
582
+ variant="stop",
583
+ size="lg"
584
+ )
585
  completion_status = gr.HTML()
586
 
587
+ # Set up event handlers
588
+ multi_img_enhance.click(
589
+ app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
590
+ )
591
+ multi_analyze_btn.click(
592
+ app.analyze_multimodal,
593
+ inputs=[multi_img_input, multi_text_input],
594
+ outputs=[multi_results, multi_plot],
595
+ )
596
+
597
+ img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
598
+ img_analyze_btn.click(
599
+ app.analyze_image,
600
+ inputs=img_input,
601
+ outputs=[img_output, img_results, img_plot],
602
+ )
603
+
604
+ text_analyze_btn.click(
605
+ app.analyze_text,
606
+ inputs=text_input,
607
+ outputs=[text_output, text_results, text_plot],
608
+ )
609
+
610
+ def complete_consultation(appointment_id):
611
+ """Handle consultation completion."""
612
+ if not appointment_id:
613
+ return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
614
 
615
  try:
616
+ # Replace with your actual Flask app URL
617
+ flask_app_url = "http://127.0.0.1:600/complete_consultation"
618
+
 
 
 
 
 
 
 
 
 
 
 
619
  response = requests.post(
620
+ flask_app_url,
621
+ json={"appointment_id": appointment_id},
622
  timeout=10
623
  )
624
+
625
+ if response.status_code == 200:
626
+ return """
627
+ <div class='alert alert-success'>
628
+ Consultation completed successfully. Redirecting...
 
 
629
  <script>
630
+ setTimeout(function() {
631
+ window.location.href = "http://127.0.0.1:600/doctors";
632
+ }, 2000);
633
  </script>
634
  </div>
635
  """
636
+ else:
637
+ return f"""
638
+ <div class='alert alert-error'>
639
+ Error completing appointment (Status: {response.status_code}).
640
+ Please contact support.
641
+ </div>
642
+ """
643
+
 
 
 
 
 
 
644
  except Exception as e:
645
+ return f"""
646
+ <div class='alert alert-error'>
647
+ Error: {str(e)}
 
 
 
648
  </div>
649
  """
650
 
651
  end_consultation_btn.click(
652
  fn=complete_consultation,
653
+ inputs=[appointment_id],
654
+ outputs=completion_status
655
  )
656
+
657
  try:
658
  interface.launch()
659
  except Exception as e:
660
+ logging.error(f"Failed to launch interface: {str(e)}")
661
+ raise RuntimeError("Failed to launch MediSync interface") from e
662
+
663
 
664
  if __name__ == "__main__":
665
+ logging.basicConfig(
666
+ level=logging.INFO,
667
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
668
+ )
669
  create_interface()