blackshadow1 commited on
Commit
bd077ae
·
verified ·
1 Parent(s): b02b09a

updated the code ✅

Browse files
Files changed (1) hide show
  1. mediSync/app.py +78 -122
mediSync/app.py CHANGED
@@ -404,9 +404,52 @@ class MediSyncApp:
404
  # import gradio as gr
405
  # from mediSync.app import MediSyncApp
406
 
407
- def create_interface():
408
- """Create and launch the Gradio interface with all fixes implemented."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
409
 
 
 
410
  app = MediSyncApp()
411
 
412
  # Example medical report for demo
@@ -428,53 +471,24 @@ def create_interface():
428
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
429
  """
430
 
431
- # Get sample image path with robust error handling
432
  sample_image_path = None
433
  try:
434
- sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
435
- os.makedirs(sample_images_dir, exist_ok=True)
436
-
437
- # Check for existing images first
438
- sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
439
-
440
- if not sample_images:
441
- # Download fallback sample image if none exist
442
- fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
443
- sample_path = sample_images_dir / "sample_xray.jpg"
444
-
445
- try:
446
- response = requests.get(fallback_url, timeout=10)
447
- if response.status_code == 200:
448
- with open(sample_path, 'wb') as f:
449
- f.write(response.content)
450
- sample_image_path = str(sample_path)
451
- logging.info("Successfully downloaded fallback sample image")
452
- else:
453
- logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
454
- except Exception as download_error:
455
- logging.warning(f"Could not download sample image: {str(download_error)}")
456
- else:
457
- sample_image_path = str(sample_images[0])
458
  except Exception as e:
459
- logging.error(f"Error setting up sample images: {str(e)}")
460
 
461
- # Define interface with robust parameter handling
462
  with gr.Blocks(
463
  title="MediSync: Multi-Modal Medical Analysis System",
464
- theme=gr.themes.Soft()
 
465
  ) as interface:
466
- # Get appointment ID from URL parameters using JavaScript
467
- appointment_id = gr.Textbox(
468
- visible=False,
469
- value="",
470
- _js="""
471
- function() {
472
- const params = new URLSearchParams(window.location.search);
473
- return params.get('appointment_id') || '';
474
- }
475
- """
476
- )
477
-
478
  gr.Markdown("""
479
  # MediSync: Multi-Modal Medical Analysis System
480
 
@@ -485,22 +499,20 @@ def create_interface():
485
  1. Upload a chest X-ray image
486
  2. Enter the corresponding medical report text
487
  3. Choose the analysis type: image-only, text-only, or multimodal (combined)
488
- 4. Click "End Consultation" when finished to complete your appointment
489
  """)
490
 
491
  with gr.Tab("Multimodal Analysis"):
492
  with gr.Row():
493
  with gr.Column():
494
  multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
495
- multi_img_enhance = gr.Button("Enhance Image")
496
-
497
  multi_text_input = gr.Textbox(
498
  label="Enter Medical Report Text",
499
  placeholder="Enter the radiologist's report text here...",
500
  lines=10,
501
- value=example_report if not sample_image_path else None,
502
  )
503
-
504
  multi_analyze_btn = gr.Button(
505
  "Analyze Image & Text", variant="primary"
506
  )
@@ -520,7 +532,7 @@ def create_interface():
520
  with gr.Row():
521
  with gr.Column():
522
  img_input = gr.Image(label="Upload X-ray Image", type="pil")
523
- img_enhance = gr.Button("Enhance Image")
524
  img_analyze_btn = gr.Button("Analyze Image", variant="primary")
525
 
526
  with gr.Column():
@@ -547,7 +559,7 @@ def create_interface():
547
  text_analyze_btn = gr.Button("Analyze Text", variant="primary")
548
 
549
  with gr.Column():
550
- text_output = gr.Textbox(label="Processed Text")
551
  text_results = gr.HTML(label="Analysis Results")
552
  text_plot = gr.HTML(label="Entity Visualization")
553
 
@@ -565,33 +577,20 @@ def create_interface():
565
 
566
  ### Key Features
567
 
568
- - **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
569
- - **Medical Report Processing**: Extracts key information from patient reports using NLP models
570
- - **Multi-modal Integration**: Combines insights from both image and text data for more accurate analysis
571
-
572
- ### Models Used
573
-
574
- - **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
575
- - **Medical Text Analysis**: medicalai/ClinicalBERT
576
 
577
  ### Important Disclaimer
578
 
579
  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.
580
  """)
581
 
582
- # Consultation completion section
583
- with gr.Row():
584
- with gr.Column():
585
- end_consultation_btn = gr.Button(
586
- "End Consultation",
587
- variant="stop",
588
- size="lg"
589
- )
590
- completion_status = gr.HTML()
591
-
592
  # Set up event handlers
593
  multi_img_enhance.click(
594
- app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
 
 
595
  )
596
  multi_analyze_btn.click(
597
  app.analyze_multimodal,
@@ -599,7 +598,11 @@ def create_interface():
599
  outputs=[multi_results, multi_plot],
600
  )
601
 
602
- img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
 
 
 
 
603
  img_analyze_btn.click(
604
  app.analyze_image,
605
  inputs=img_input,
@@ -612,59 +615,12 @@ def create_interface():
612
  outputs=[text_output, text_results, text_plot],
613
  )
614
 
615
- def complete_consultation(appointment_id):
616
- """Handle consultation completion."""
617
- if not appointment_id:
618
- return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
619
-
620
- try:
621
- # Replace with your actual Flask app URL
622
- flask_app_url = "http://127.0.0.1:600/complete_consultation"
623
-
624
- response = requests.post(
625
- flask_app_url,
626
- json={"appointment_id": appointment_id},
627
- timeout=10
628
- )
629
-
630
- if response.status_code == 200:
631
- return """
632
- <div class='alert alert-success'>
633
- Consultation completed successfully. Redirecting...
634
- <script>
635
- setTimeout(function() {
636
- window.location.href = "http://127.0.0.1:600/doctors";
637
- }, 2000);
638
- </script>
639
- </div>
640
- """
641
- else:
642
- return f"""
643
- <div class='alert alert-error'>
644
- Error completing appointment (Status: {response.status_code}).
645
- Please contact support.
646
- </div>
647
- """
648
-
649
- except Exception as e:
650
- return f"""
651
- <div class='alert alert-error'>
652
- Error: {str(e)}
653
- </div>
654
- """
655
-
656
- end_consultation_btn.click(
657
- fn=complete_consultation,
658
- inputs=[appointment_id],
659
- outputs=completion_status
660
- )
661
-
662
- try:
663
- interface.launch()
664
- except Exception as e:
665
- logging.error(f"Failed to launch interface: {str(e)}")
666
- raise RuntimeError("Failed to launch MediSync interface") from e
667
-
668
 
669
  if __name__ == "__main__":
670
  logging.basicConfig(
 
404
  # import gradio as gr
405
  # from mediSync.app import MediSyncApp
406
 
407
+ import os
408
+ import logging
409
+ from pathlib import Path
410
+ import requests
411
+ import gradio as gr
412
+ from PIL import Image
413
+ import io
414
+
415
+ class MediSyncApp:
416
+ """Mock application class for demonstration purposes."""
417
+
418
+ def enhance_image(self, image):
419
+ """Mock image enhancement function."""
420
+ if image is None:
421
+ return None
422
+ return image
423
+
424
+ def analyze_multimodal(self, image, text):
425
+ """Mock multimodal analysis function."""
426
+ return (
427
+ "<div style='color: green'>Multimodal analysis completed successfully.</div>",
428
+ "<div>Visualization placeholder</div>"
429
+ )
430
+
431
+ def analyze_image(self, image):
432
+ """Mock image analysis function."""
433
+ if image is None:
434
+ return None, "<div style='color: red'>No image provided</div>", ""
435
+ return (
436
+ image,
437
+ "<div style='color: green'>Image analysis completed successfully.</div>",
438
+ "<div>Image visualization placeholder</div>"
439
+ )
440
+
441
+ def analyze_text(self, text):
442
+ """Mock text analysis function."""
443
+ if not text.strip():
444
+ return "", "<div style='color: red'>No text provided</div>", ""
445
+ return (
446
+ text,
447
+ "<div style='color: green'>Text analysis completed successfully.</div>",
448
+ "<div>Text visualization placeholder</div>"
449
+ )
450
 
451
+ def create_interface():
452
+ """Create and launch the Gradio interface for Hugging Face Spaces."""
453
  app = MediSyncApp()
454
 
455
  # Example medical report for demo
 
471
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
472
  """
473
 
474
+ # Get sample image - simplified for Hugging Face Spaces
475
  sample_image_path = None
476
  try:
477
+ # Try to use a sample image from the web
478
+ sample_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
479
+ response = requests.get(sample_url, timeout=10)
480
+ if response.status_code == 200:
481
+ sample_image = Image.open(io.BytesIO(response.content))
482
+ sample_image_path = sample_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  except Exception as e:
484
+ logging.warning(f"Could not load sample image: {str(e)}")
485
 
486
+ # Define interface
487
  with gr.Blocks(
488
  title="MediSync: Multi-Modal Medical Analysis System",
489
+ theme=gr.themes.Soft(),
490
+ css=".alert {padding: 10px; border-radius: 5px;} .alert-error {background-color: #ffebee;} .alert-success {background-color: #e8f5e9;}"
491
  ) as interface:
 
 
 
 
 
 
 
 
 
 
 
 
492
  gr.Markdown("""
493
  # MediSync: Multi-Modal Medical Analysis System
494
 
 
499
  1. Upload a chest X-ray image
500
  2. Enter the corresponding medical report text
501
  3. Choose the analysis type: image-only, text-only, or multimodal (combined)
502
+ 4. View the analysis results
503
  """)
504
 
505
  with gr.Tab("Multimodal Analysis"):
506
  with gr.Row():
507
  with gr.Column():
508
  multi_img_input = gr.Image(label="Upload X-ray Image", type="pil")
509
+ multi_img_enhance = gr.Button("Enhance Image", variant="secondary")
 
510
  multi_text_input = gr.Textbox(
511
  label="Enter Medical Report Text",
512
  placeholder="Enter the radiologist's report text here...",
513
  lines=10,
514
+ value=example_report,
515
  )
 
516
  multi_analyze_btn = gr.Button(
517
  "Analyze Image & Text", variant="primary"
518
  )
 
532
  with gr.Row():
533
  with gr.Column():
534
  img_input = gr.Image(label="Upload X-ray Image", type="pil")
535
+ img_enhance = gr.Button("Enhance Image", variant="secondary")
536
  img_analyze_btn = gr.Button("Analyze Image", variant="primary")
537
 
538
  with gr.Column():
 
559
  text_analyze_btn = gr.Button("Analyze Text", variant="primary")
560
 
561
  with gr.Column():
562
+ text_output = gr.Textbox(label="Processed Text", interactive=False)
563
  text_results = gr.HTML(label="Analysis Results")
564
  text_plot = gr.HTML(label="Entity Visualization")
565
 
 
577
 
578
  ### Key Features
579
 
580
+ - **X-ray Image Analysis**: Detects abnormalities in chest X-rays
581
+ - **Medical Report Processing**: Extracts key information from patient reports
582
+ - **Multi-modal Integration**: Combines insights from both image and text data
 
 
 
 
 
583
 
584
  ### Important Disclaimer
585
 
586
  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.
587
  """)
588
 
 
 
 
 
 
 
 
 
 
 
589
  # Set up event handlers
590
  multi_img_enhance.click(
591
+ app.enhance_image,
592
+ inputs=multi_img_input,
593
+ outputs=multi_img_input
594
  )
595
  multi_analyze_btn.click(
596
  app.analyze_multimodal,
 
598
  outputs=[multi_results, multi_plot],
599
  )
600
 
601
+ img_enhance.click(
602
+ app.enhance_image,
603
+ inputs=img_input,
604
+ outputs=img_output
605
+ )
606
  img_analyze_btn.click(
607
  app.analyze_image,
608
  inputs=img_input,
 
615
  outputs=[text_output, text_results, text_plot],
616
  )
617
 
618
+ # Launch configuration for Hugging Face Spaces
619
+ interface.launch(
620
+ server_name="0.0.0.0",
621
+ server_port=int(os.getenv("PORT", "7860")),
622
+ share=False
623
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
 
625
  if __name__ == "__main__":
626
  logging.basicConfig(