blackshadow1 commited on
Commit
82dc143
·
verified ·
1 Parent(s): 293537d

updated code ✅✅

Browse files
Files changed (1) hide show
  1. mediSync/app.py +161 -68
mediSync/app.py CHANGED
@@ -426,8 +426,26 @@ def create_interface():
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
  logging.error(f"Error setting up sample images: {str(e)}")
@@ -436,142 +454,217 @@ def create_interface():
436
  title="MediSync: Multi-Modal Medical Analysis System",
437
  theme=gr.themes.Soft()
438
  ) as interface:
439
- # JavaScript to extract appointment ID from URL
440
- js_extract_id = """
441
- function() {
442
- const params = new URLSearchParams(window.location.search);
443
- return params.get('appointment_id') || '';
444
- }
445
- """
446
-
447
- # Hidden appointment ID component
448
- appointment_id = gr.Textbox(visible=False)
449
 
450
  # Load appointment ID from URL when interface loads
451
  interface.load(
452
- fn=None,
453
- _js=js_extract_id,
454
- outputs=appointment_id
 
 
 
 
 
455
  )
456
 
457
  gr.Markdown("""
458
  # MediSync: Multi-Modal Medical Analysis System
 
 
 
 
459
  ## How to Use
460
  1. Upload a chest X-ray image
461
- 2. Enter medical report text
462
- 3. Choose analysis type
463
- 4. Click "End Consultation" when finished
464
  """)
465
 
466
  with gr.Tab("Multimodal Analysis"):
467
  with gr.Row():
468
  with gr.Column():
469
  multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
 
 
470
  multi_text_input = gr.Textbox(
471
- label="Medical Report Text",
472
- value=example_report,
473
- lines=10
 
 
 
 
 
474
  )
475
- multi_analyze_btn = gr.Button("Analyze", variant="primary")
476
  with gr.Column():
477
- multi_results = gr.HTML(label="Results")
478
  multi_plot = gr.HTML(label="Visualization")
479
 
 
 
 
 
 
 
 
480
  with gr.Tab("Image Analysis"):
481
  with gr.Row():
482
  with gr.Column():
483
  img_input = gr.Image(label="Upload X-ray Image", type="pil")
484
- img_analyze_btn = gr.Button("Analyze", variant="primary")
 
 
485
  with gr.Column():
486
  img_output = gr.Image(label="Processed Image")
487
- img_results = gr.HTML(label="Results")
 
 
 
 
 
 
 
 
488
 
489
  with gr.Tab("Text Analysis"):
490
  with gr.Row():
491
  with gr.Column():
492
  text_input = gr.Textbox(
493
- label="Medical Report Text",
 
 
494
  value=example_report,
495
- lines=10
496
  )
497
- text_analyze_btn = gr.Button("Analyze", variant="primary")
 
498
  with gr.Column():
499
- text_results = gr.HTML(label="Results")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500
 
501
  # Consultation completion section
502
  with gr.Row():
503
- end_consultation_btn = gr.Button(
504
- "End Consultation",
505
- variant="stop",
506
- size="lg"
507
- )
508
- completion_status = gr.HTML()
 
 
 
 
 
 
 
 
 
 
 
509
 
510
- def complete_consultation(app_id):
511
- print(f"Processing completion for appointment: {app_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
512
 
513
- if not app_id:
514
- return "<div class='alert alert-error'>No appointment ID found</div>"
515
 
516
  try:
 
 
 
517
  response = requests.post(
518
- "http://127.0.0.1:600/complete_appointment",
519
- json={"appointment_id": app_id},
520
  timeout=10
521
  )
522
 
523
  if response.status_code == 200:
524
  return """
525
  <div class='alert alert-success'>
526
- Completed! Redirecting...
527
  <script>
528
- setTimeout(() => {
529
  window.location.href = "http://127.0.0.1:600/doctors#consultations";
530
  }, 2000);
531
  </script>
532
  </div>
533
  """
534
- return f"""
535
- <div class='alert alert-error'>
536
- Error: {response.json().get('message', 'Unknown error')}
537
- </div>
538
- """
 
 
 
539
  except Exception as e:
540
  return f"""
541
  <div class='alert alert-error'>
542
- Connection error: {str(e)}
543
  </div>
544
  """
545
 
546
  end_consultation_btn.click(
547
  fn=complete_consultation,
548
- inputs=appointment_id,
549
  outputs=completion_status
550
  )
551
 
552
- # Analysis button handlers
553
- multi_analyze_btn.click(
554
- app.analyze_multimodal,
555
- inputs=[multi_img_input, multi_text_input],
556
- outputs=[multi_results, multi_plot]
557
- )
558
- img_analyze_btn.click(
559
- app.analyze_image,
560
- inputs=img_input,
561
- outputs=[img_output, img_results]
562
- )
563
- text_analyze_btn.click(
564
- app.analyze_text,
565
- inputs=text_input,
566
- outputs=text_results
567
- )
568
-
569
  try:
570
  interface.launch()
571
  except Exception as e:
572
  logging.error(f"Failed to launch interface: {str(e)}")
573
- raise
 
574
 
575
  if __name__ == "__main__":
576
- logging.basicConfig(level=logging.INFO)
 
 
 
577
  create_interface()
 
426
  try:
427
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
428
  os.makedirs(sample_images_dir, exist_ok=True)
429
+
430
+ # Check for existing images first
431
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
432
+
433
+ if not sample_images:
434
+ # Use reliable fallback image URL
435
+ fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
436
+ sample_path = sample_images_dir / "sample_xray.jpg"
437
+ try:
438
+ response = requests.get(fallback_url, timeout=10)
439
+ if response.status_code == 200:
440
+ with open(sample_path, 'wb') as f:
441
+ f.write(response.content)
442
+ sample_image_path = str(sample_path)
443
+ logging.info("Successfully downloaded fallback sample image")
444
+ else:
445
+ logging.warning("Failed to download sample image")
446
+ except Exception as e:
447
+ logging.warning(f"Could not download sample image: {str(e)}")
448
+ else:
449
  sample_image_path = str(sample_images[0])
450
  except Exception as e:
451
  logging.error(f"Error setting up sample images: {str(e)}")
 
454
  title="MediSync: Multi-Modal Medical Analysis System",
455
  theme=gr.themes.Soft()
456
  ) as interface:
457
+ # Appointment ID handling with JavaScript workaround
458
+ appointment_id = gr.Textbox(visible=False, value="")
 
 
 
 
 
 
 
 
459
 
460
  # Load appointment ID from URL when interface loads
461
  interface.load(
462
+ fn=lambda: "",
463
+ outputs=appointment_id,
464
+ _js="""
465
+ function() {
466
+ const params = new URLSearchParams(window.location.search);
467
+ return params.get('appointment_id') || '';
468
+ }
469
+ """
470
  )
471
 
472
  gr.Markdown("""
473
  # MediSync: Multi-Modal Medical Analysis System
474
+
475
+ This AI-powered healthcare solution combines X-ray image analysis with patient report text processing
476
+ to provide comprehensive medical insights.
477
+
478
  ## How to Use
479
  1. Upload a chest X-ray image
480
+ 2. Enter the corresponding medical report text
481
+ 3. Choose the analysis type: image-only, text-only, or multimodal (combined)
482
+ 4. Click "End Consultation" when finished to complete your appointment
483
  """)
484
 
485
  with gr.Tab("Multimodal Analysis"):
486
  with gr.Row():
487
  with gr.Column():
488
  multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
489
+ multi_img_enhance = gr.Button("Enhance Image")
490
+
491
  multi_text_input = gr.Textbox(
492
+ label="Enter Medical Report Text",
493
+ placeholder="Enter the radiologist's report text here...",
494
+ lines=10,
495
+ value=example_report if not sample_image_path else None,
496
+ )
497
+
498
+ multi_analyze_btn = gr.Button(
499
+ "Analyze Image & Text", variant="primary"
500
  )
501
+
502
  with gr.Column():
503
+ multi_results = gr.HTML(label="Analysis Results")
504
  multi_plot = gr.HTML(label="Visualization")
505
 
506
+ if sample_image_path:
507
+ gr.Examples(
508
+ examples=[[sample_image_path, example_report]],
509
+ inputs=[multi_img_input, multi_text_input],
510
+ label="Example X-ray and Report",
511
+ )
512
+
513
  with gr.Tab("Image Analysis"):
514
  with gr.Row():
515
  with gr.Column():
516
  img_input = gr.Image(label="Upload X-ray Image", type="pil")
517
+ img_enhance = gr.Button("Enhance Image")
518
+ img_analyze_btn = gr.Button("Analyze Image", variant="primary")
519
+
520
  with gr.Column():
521
  img_output = gr.Image(label="Processed Image")
522
+ img_results = gr.HTML(label="Analysis Results")
523
+ img_plot = gr.HTML(label="Visualization")
524
+
525
+ if sample_image_path:
526
+ gr.Examples(
527
+ examples=[[sample_image_path]],
528
+ inputs=[img_input],
529
+ label="Example X-ray Image",
530
+ )
531
 
532
  with gr.Tab("Text Analysis"):
533
  with gr.Row():
534
  with gr.Column():
535
  text_input = gr.Textbox(
536
+ label="Enter Medical Report Text",
537
+ placeholder="Enter the radiologist's report text here...",
538
+ lines=10,
539
  value=example_report,
 
540
  )
541
+ text_analyze_btn = gr.Button("Analyze Text", variant="primary")
542
+
543
  with gr.Column():
544
+ text_output = gr.Textbox(label="Processed Text")
545
+ text_results = gr.HTML(label="Analysis Results")
546
+ text_plot = gr.HTML(label="Entity Visualization")
547
+
548
+ gr.Examples(
549
+ examples=[[example_report]],
550
+ inputs=[text_input],
551
+ label="Example Medical Report",
552
+ )
553
+
554
+ with gr.Tab("About"):
555
+ gr.Markdown("""
556
+ ## About MediSync
557
+
558
+ MediSync is an AI-powered healthcare solution that uses multi-modal analysis to provide comprehensive insights from medical images and reports.
559
+
560
+ ### Key Features
561
+
562
+ - **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
563
+ - **Medical Report Processing**: Extracts key information from patient reports using NLP models
564
+ - **Multi-modal Integration**: Combines insights from both image and text data for more accurate analysis
565
+
566
+ ### Models Used
567
+
568
+ - **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
569
+ - **Medical Text Analysis**: medicalai/ClinicalBERT
570
+
571
+ ### Important Disclaimer
572
+
573
+ 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.
574
+ """)
575
 
576
  # Consultation completion section
577
  with gr.Row():
578
+ with gr.Column():
579
+ end_consultation_btn = gr.Button(
580
+ "End Consultation",
581
+ variant="stop",
582
+ size="lg"
583
+ )
584
+ completion_status = gr.HTML()
585
+
586
+ # Set up event handlers
587
+ multi_img_enhance.click(
588
+ app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
589
+ )
590
+ multi_analyze_btn.click(
591
+ app.analyze_multimodal,
592
+ inputs=[multi_img_input, multi_text_input],
593
+ outputs=[multi_results, multi_plot],
594
+ )
595
 
596
+ img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
597
+ img_analyze_btn.click(
598
+ app.analyze_image,
599
+ inputs=img_input,
600
+ outputs=[img_output, img_results, img_plot],
601
+ )
602
+
603
+ text_analyze_btn.click(
604
+ app.analyze_text,
605
+ inputs=text_input,
606
+ outputs=[text_output, text_results, text_plot],
607
+ )
608
+
609
+ def complete_consultation(appointment_id):
610
+ """Handle consultation completion."""
611
+ print(f"DEBUG - Appointment ID: {appointment_id}") # Debug output
612
 
613
+ if not appointment_id:
614
+ return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
615
 
616
  try:
617
+ # Update to your actual Flask server URL
618
+ flask_app_url = "http://127.0.0.1:600/complete_appointment"
619
+
620
  response = requests.post(
621
+ flask_app_url,
622
+ json={"appointment_id": appointment_id},
623
  timeout=10
624
  )
625
 
626
  if response.status_code == 200:
627
  return """
628
  <div class='alert alert-success'>
629
+ Consultation completed successfully. Redirecting...
630
  <script>
631
+ setTimeout(function() {
632
  window.location.href = "http://127.0.0.1:600/doctors#consultations";
633
  }, 2000);
634
  </script>
635
  </div>
636
  """
637
+ else:
638
+ return f"""
639
+ <div class='alert alert-error'>
640
+ Error completing appointment (Status: {response.status_code}).
641
+ Please contact support.
642
+ </div>
643
+ """
644
+
645
  except Exception as e:
646
  return f"""
647
  <div class='alert alert-error'>
648
+ Error: {str(e)}
649
  </div>
650
  """
651
 
652
  end_consultation_btn.click(
653
  fn=complete_consultation,
654
+ inputs=[appointment_id],
655
  outputs=completion_status
656
  )
657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
658
  try:
659
  interface.launch()
660
  except Exception as e:
661
  logging.error(f"Failed to launch interface: {str(e)}")
662
+ raise RuntimeError("Failed to launch MediSync interface") from e
663
+
664
 
665
  if __name__ == "__main__":
666
+ logging.basicConfig(
667
+ level=logging.INFO,
668
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
669
+ )
670
  create_interface()