blackshadow1 commited on
Commit
eb97f40
·
verified ·
1 Parent(s): d8534c2

updated the code ✅✅

Browse files
Files changed (1) hide show
  1. mediSync/app.py +22 -178
mediSync/app.py CHANGED
@@ -3,23 +3,10 @@ import os
3
  import sys
4
  import tempfile
5
  from pathlib import Path
6
- import os
7
- import logging
8
- from pathlib import Path
9
- import gradio as gr
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__))
@@ -397,59 +384,9 @@ class MediSyncApp:
397
  self.logger.error(f"Error converting figure to HTML: {e}")
398
  return "<p>Error displaying visualization.</p>"
399
 
400
- # import logging
401
- # import requests
402
- # from pathlib import Path
403
- # import os
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 with all fixes implemented."""
453
 
454
  app = MediSyncApp()
455
 
@@ -472,53 +409,20 @@ def create_interface():
472
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
473
  """
474
 
475
- # Get sample image path with robust error handling
 
 
 
 
 
476
  sample_image_path = None
477
- try:
478
- sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
479
- os.makedirs(sample_images_dir, exist_ok=True)
480
-
481
- # Check for existing images first
482
- sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
483
-
484
- if not sample_images:
485
- # Download fallback sample image if none exist
486
- fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
487
- sample_path = sample_images_dir / "sample_xray.jpg"
488
-
489
- try:
490
- response = requests.get(fallback_url, timeout=10)
491
- if response.status_code == 200:
492
- with open(sample_path, 'wb') as f:
493
- f.write(response.content)
494
- sample_image_path = str(sample_path)
495
- logging.info("Successfully downloaded fallback sample image")
496
- else:
497
- logging.warning(f"Failed to download sample image. Status code: {response.status_code}")
498
- except Exception as download_error:
499
- logging.warning(f"Could not download sample image: {str(download_error)}")
500
- else:
501
- sample_image_path = str(sample_images[0])
502
- except Exception as e:
503
- logging.error(f"Error setting up sample images: {str(e)}")
504
-
505
- # Define interface with robust parameter handling
506
  with gr.Blocks(
507
- title="MediSync: Multi-Modal Medical Analysis System",
508
- theme=gr.themes.Soft()
509
  ) as interface:
510
- # Get appointment ID from URL parameters using JavaScript
511
- appointment_id = gr.Textbox(
512
- visible=False,
513
- value="",
514
- _js="""
515
- function() {
516
- const params = new URLSearchParams(window.location.search);
517
- return params.get('appointment_id') || '';
518
- }
519
- """
520
- )
521
-
522
  gr.Markdown("""
523
  # MediSync: Multi-Modal Medical Analysis System
524
 
@@ -529,7 +433,6 @@ def create_interface():
529
  1. Upload a chest X-ray image
530
  2. Enter the corresponding medical report text
531
  3. Choose the analysis type: image-only, text-only, or multimodal (combined)
532
- 4. Click "End Consultation" when finished to complete your appointment
533
  """)
534
 
535
  with gr.Tab("Multimodal Analysis"):
@@ -542,7 +445,7 @@ def create_interface():
542
  label="Enter Medical Report Text",
543
  placeholder="Enter the radiologist's report text here...",
544
  lines=10,
545
- value=example_report if not sample_image_path else None,
546
  )
547
 
548
  multi_analyze_btn = gr.Button(
@@ -553,6 +456,7 @@ def create_interface():
553
  multi_results = gr.HTML(label="Analysis Results")
554
  multi_plot = gr.HTML(label="Visualization")
555
 
 
556
  if sample_image_path:
557
  gr.Examples(
558
  examples=[[sample_image_path, example_report]],
@@ -572,6 +476,7 @@ def create_interface():
572
  img_results = gr.HTML(label="Analysis Results")
573
  img_plot = gr.HTML(label="Visualization")
574
 
 
575
  if sample_image_path:
576
  gr.Examples(
577
  examples=[[sample_image_path]],
@@ -595,6 +500,7 @@ def create_interface():
595
  text_results = gr.HTML(label="Analysis Results")
596
  text_plot = gr.HTML(label="Entity Visualization")
597
 
 
598
  gr.Examples(
599
  examples=[[example_report]],
600
  inputs=[text_input],
@@ -623,16 +529,6 @@ def create_interface():
623
  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.
624
  """)
625
 
626
- # Consultation completion section
627
- with gr.Row():
628
- with gr.Column():
629
- end_consultation_btn = gr.Button(
630
- "End Consultation",
631
- variant="stop",
632
- size="lg"
633
- )
634
- completion_status = gr.HTML()
635
-
636
  # Set up event handlers
637
  multi_img_enhance.click(
638
  app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
@@ -656,64 +552,12 @@ def create_interface():
656
  outputs=[text_output, text_results, text_plot],
657
  )
658
 
659
- def complete_consultation(appointment_id):
660
- """Handle consultation completion."""
661
- if not appointment_id:
662
- return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
663
-
664
- try:
665
- # Replace with your actual Flask app URL
666
- flask_app_url = "http://127.0.0.1:600/complete_consultation"
667
-
668
- response = requests.post(
669
- flask_app_url,
670
- json={"appointment_id": appointment_id},
671
- timeout=10
672
- )
673
-
674
- if response.status_code == 200:
675
- return """
676
- <div class='alert alert-success'>
677
- Consultation completed successfully. Redirecting...
678
- <script>
679
- setTimeout(function() {
680
- window.location.href = "http://127.0.0.1:600/doctors";
681
- }, 2000);
682
- </script>
683
- </div>
684
- """
685
- else:
686
- return f"""
687
- <div class='alert alert-error'>
688
- Error completing appointment (Status: {response.status_code}).
689
- Please contact support.
690
- </div>
691
- """
692
-
693
- except Exception as e:
694
- return f"""
695
- <div class='alert alert-error'>
696
- Error: {str(e)}
697
- </div>
698
- """
699
-
700
- end_consultation_btn.click(
701
- fn=complete_consultation,
702
- inputs=[appointment_id],
703
- outputs=completion_status
704
- )
705
-
706
- try:
707
- interface.launch()
708
- except Exception as e:
709
- logging.error(f"Failed to launch interface: {str(e)}")
710
- raise RuntimeError("Failed to launch MediSync interface") from e
711
 
712
 
713
  if __name__ == "__main__":
714
- logging.basicConfig(
715
- level=logging.INFO,
716
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
717
- )
718
  create_interface()
719
-
 
 
 
3
  import sys
4
  import tempfile
5
  from pathlib import Path
6
+
 
 
 
 
 
 
7
  import gradio as gr
8
  import matplotlib.pyplot as plt
9
  from PIL import Image
 
 
 
 
 
 
 
10
 
11
  # Add parent directory to path
12
  parent_dir = os.path.dirname(os.path.abspath(__file__))
 
384
  self.logger.error(f"Error converting figure to HTML: {e}")
385
  return "<p>Error displaying visualization.</p>"
386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
 
388
  def create_interface():
389
+ """Create and launch the Gradio interface."""
390
 
391
  app = MediSyncApp()
392
 
 
409
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
410
  """
411
 
412
+ # Get sample image path if available
413
+ sample_images_dir = Path(parent_dir) / "data" / "sample"
414
+ sample_images = list(sample_images_dir.glob("*.png")) + list(
415
+ sample_images_dir.glob("*.jpg")
416
+ )
417
+
418
  sample_image_path = None
419
+ if sample_images:
420
+ sample_image_path = str(sample_images[0])
421
+
422
+ # Define interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
  with gr.Blocks(
424
+ title="MediSync: Multi-Modal Medical Analysis System", theme=gr.themes.Soft()
 
425
  ) as interface:
 
 
 
 
 
 
 
 
 
 
 
 
426
  gr.Markdown("""
427
  # MediSync: Multi-Modal Medical Analysis System
428
 
 
433
  1. Upload a chest X-ray image
434
  2. Enter the corresponding medical report text
435
  3. Choose the analysis type: image-only, text-only, or multimodal (combined)
 
436
  """)
437
 
438
  with gr.Tab("Multimodal Analysis"):
 
445
  label="Enter Medical Report Text",
446
  placeholder="Enter the radiologist's report text here...",
447
  lines=10,
448
+ value=example_report if sample_image_path is None else None,
449
  )
450
 
451
  multi_analyze_btn = gr.Button(
 
456
  multi_results = gr.HTML(label="Analysis Results")
457
  multi_plot = gr.HTML(label="Visualization")
458
 
459
+ # Set up examples if sample image exists
460
  if sample_image_path:
461
  gr.Examples(
462
  examples=[[sample_image_path, example_report]],
 
476
  img_results = gr.HTML(label="Analysis Results")
477
  img_plot = gr.HTML(label="Visualization")
478
 
479
+ # Set up example if sample image exists
480
  if sample_image_path:
481
  gr.Examples(
482
  examples=[[sample_image_path]],
 
500
  text_results = gr.HTML(label="Analysis Results")
501
  text_plot = gr.HTML(label="Entity Visualization")
502
 
503
+ # Set up example
504
  gr.Examples(
505
  examples=[[example_report]],
506
  inputs=[text_input],
 
529
  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.
530
  """)
531
 
 
 
 
 
 
 
 
 
 
 
532
  # Set up event handlers
533
  multi_img_enhance.click(
534
  app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
 
552
  outputs=[text_output, text_results, text_plot],
553
  )
554
 
555
+ # Run the interface
556
+ interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
 
558
 
559
  if __name__ == "__main__":
 
 
 
 
560
  create_interface()
561
+
562
+
563
+ # Example Script