blackshadow1 commited on
Commit
2f24530
·
verified ·
1 Parent(s): 4b107df

updated code ✅✅

Browse files
Files changed (1) hide show
  1. mediSync/app.py +76 -60
mediSync/app.py CHANGED
@@ -397,6 +397,12 @@ class MediSyncApp:
397
  self.logger.error(f"Error converting figure to HTML: {e}")
398
  return "<p>Error displaying visualization.</p>"
399
 
 
 
 
 
 
 
400
 
401
  def create_interface():
402
  """Create and launch the Gradio interface with all fixes implemented."""
@@ -421,7 +427,7 @@ def create_interface():
421
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
422
  """
423
 
424
- # Updated image handling - only use local files
425
  sample_image_path = None
426
  try:
427
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
@@ -437,17 +443,31 @@ def create_interface():
437
  title="MediSync: Multi-Modal Medical Analysis System",
438
  theme=gr.themes.Soft()
439
  ) as interface:
440
- # Updated appointment ID handling
441
  appointment_id = gr.Textbox(visible=False, value="")
442
 
443
- # New initialization function for appointment ID
444
- @interface.load(inputs=[], outputs=[appointment_id])
445
- def load_id():
446
- try:
447
- from flask import request
448
- return request.args.get('appointment_id', '')
449
- except:
450
- return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
 
452
  # Unchanged interface sections
453
  gr.Markdown("""
@@ -463,7 +483,7 @@ def create_interface():
463
  4. Click "End Consultation" when finished to complete your appointment
464
  """)
465
 
466
- # Unchanged tab definitions
467
  with gr.Tab("Multimodal Analysis"):
468
  with gr.Row():
469
  with gr.Column():
@@ -487,14 +507,13 @@ def create_interface():
487
  label="Example X-ray and Report",
488
  )
489
 
490
-
491
  with gr.Tab("Image Analysis"):
492
  with gr.Row():
493
  with gr.Column():
494
  img_input = gr.Image(label="Upload X-ray Image", type="pil")
495
  img_enhance = gr.Button("Enhance Image")
496
  img_analyze_btn = gr.Button("Analyze Image", variant="primary")
497
-
498
  with gr.Column():
499
  img_output = gr.Image(label="Processed Image")
500
  img_results = gr.HTML(label="Analysis Results")
@@ -507,6 +526,7 @@ def create_interface():
507
  label="Example X-ray Image",
508
  )
509
 
 
510
  with gr.Tab("Text Analysis"):
511
  with gr.Row():
512
  with gr.Column():
@@ -517,7 +537,6 @@ def create_interface():
517
  value=example_report,
518
  )
519
  text_analyze_btn = gr.Button("Analyze Text", variant="primary")
520
-
521
  with gr.Column():
522
  text_output = gr.Textbox(label="Processed Text")
523
  text_results = gr.HTML(label="Analysis Results")
@@ -529,6 +548,7 @@ def create_interface():
529
  label="Example Medical Report",
530
  )
531
 
 
532
  with gr.Tab("About"):
533
  gr.Markdown("""
534
  ## About MediSync
@@ -551,7 +571,7 @@ def create_interface():
551
  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.
552
  """)
553
 
554
- # Consultation completion section
555
  with gr.Row():
556
  with gr.Column():
557
  end_consultation_btn = gr.Button(
@@ -561,69 +581,45 @@ def create_interface():
561
  )
562
  completion_status = gr.HTML()
563
 
564
- # Set up event handlers
565
- multi_img_enhance.click(
566
- app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
567
- )
568
- multi_analyze_btn.click(
569
- app.analyze_multimodal,
570
- inputs=[multi_img_input, multi_text_input],
571
- outputs=[multi_results, multi_plot],
572
- )
573
-
574
- img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
575
- img_analyze_btn.click(
576
- app.analyze_image,
577
- inputs=img_input,
578
- outputs=[img_output, img_results, img_plot],
579
- )
580
-
581
- text_analyze_btn.click(
582
- app.analyze_text,
583
- inputs=text_input,
584
- outputs=[text_output, text_results, text_plot],
585
- )
586
-
587
- def complete_consultation(appointment_id):
588
- """Handle consultation completion."""
589
- print(f"DEBUG - Appointment ID: {appointment_id}") # Debug output
590
 
591
- if not appointment_id:
592
- return "<div class='alert alert-error'>No appointment ID found. Please contact support.</div>"
 
 
 
 
 
 
593
 
594
  try:
595
- # Update to your actual Flask server URL
596
- flask_app_url = "http://127.0.0.1:600/complete_appointment"
597
-
598
  response = requests.post(
599
- flask_app_url,
600
- json={"appointment_id": appointment_id},
601
  timeout=10
602
  )
603
 
604
  if response.status_code == 200:
605
  return """
606
  <div class='alert alert-success'>
607
- Consultation completed successfully. Redirecting...
608
  <script>
609
- setTimeout(function() {
610
  window.location.href = "http://127.0.0.1:600/doctors#consultations";
611
  }, 2000);
612
  </script>
613
  </div>
614
  """
615
- else:
616
- return f"""
617
- <div class='alert alert-error'>
618
- Error completing appointment (Status: {response.status_code}).
619
- Please contact support.
620
- </div>
621
- """
622
-
623
  except Exception as e:
624
  return f"""
625
  <div class='alert alert-error'>
626
- Error: {str(e)}
627
  </div>
628
  """
629
 
@@ -633,13 +629,33 @@ def create_interface():
633
  outputs=completion_status
634
  )
635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
  try:
637
  interface.launch()
638
  except Exception as e:
639
  logging.error(f"Failed to launch interface: {str(e)}")
640
  raise RuntimeError("Failed to launch MediSync interface") from e
641
 
642
-
643
  if __name__ == "__main__":
644
  logging.basicConfig(
645
  level=logging.INFO,
 
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
  def create_interface():
408
  """Create and launch the Gradio interface with all fixes implemented."""
 
427
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
428
  """
429
 
430
+ # Get sample image path - using only local files (updated)
431
  sample_image_path = None
432
  try:
433
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
 
443
  title="MediSync: Multi-Modal Medical Analysis System",
444
  theme=gr.themes.Soft()
445
  ) as interface:
446
+ # Appointment ID handling (updated)
447
  appointment_id = gr.Textbox(visible=False, value="")
448
 
449
+ # JavaScript to capture appointment ID from URL
450
+ interface.load(
451
+ fn=None,
452
+ inputs=None,
453
+ outputs=appointment_id,
454
+ _js="""
455
+ function() {
456
+ const params = new URLSearchParams(window.location.search);
457
+ const id = params.get('appointment_id') || '';
458
+ console.log('Captured appointment ID:', id);
459
+ return id;
460
+ }
461
+ """
462
+ )
463
+
464
+ # Debug component to verify ID capture
465
+ debug_output = gr.Textbox(visible=False)
466
+ interface.load(
467
+ fn=lambda x: print(f"DEBUG - Captured Appointment ID: {x}") or x,
468
+ inputs=[appointment_id],
469
+ outputs=[debug_output]
470
+ )
471
 
472
  # Unchanged interface sections
473
  gr.Markdown("""
 
483
  4. Click "End Consultation" when finished to complete your appointment
484
  """)
485
 
486
+ # Unchanged Multimodal Analysis tab
487
  with gr.Tab("Multimodal Analysis"):
488
  with gr.Row():
489
  with gr.Column():
 
507
  label="Example X-ray and Report",
508
  )
509
 
510
+ # Unchanged Image Analysis tab
511
  with gr.Tab("Image Analysis"):
512
  with gr.Row():
513
  with gr.Column():
514
  img_input = gr.Image(label="Upload X-ray Image", type="pil")
515
  img_enhance = gr.Button("Enhance Image")
516
  img_analyze_btn = gr.Button("Analyze Image", variant="primary")
 
517
  with gr.Column():
518
  img_output = gr.Image(label="Processed Image")
519
  img_results = gr.HTML(label="Analysis Results")
 
526
  label="Example X-ray Image",
527
  )
528
 
529
+ # Unchanged Text Analysis tab
530
  with gr.Tab("Text Analysis"):
531
  with gr.Row():
532
  with gr.Column():
 
537
  value=example_report,
538
  )
539
  text_analyze_btn = gr.Button("Analyze Text", variant="primary")
 
540
  with gr.Column():
541
  text_output = gr.Textbox(label="Processed Text")
542
  text_results = gr.HTML(label="Analysis Results")
 
548
  label="Example Medical Report",
549
  )
550
 
551
+ # Unchanged About tab
552
  with gr.Tab("About"):
553
  gr.Markdown("""
554
  ## About MediSync
 
571
  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.
572
  """)
573
 
574
+ # Consultation completion section (updated)
575
  with gr.Row():
576
  with gr.Column():
577
  end_consultation_btn = gr.Button(
 
581
  )
582
  completion_status = gr.HTML()
583
 
584
+ def complete_consultation(app_id):
585
+ print(f"FINAL VERIFICATION - Appointment ID: {app_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586
 
587
+ if not app_id:
588
+ return """
589
+ <div class='alert alert-error'>
590
+ <h3>No Appointment ID Found</h3>
591
+ <p>Please ensure you accessed this page through the proper booking system.</p>
592
+ <p>Current URL: <script>document.write(window.location.href)</script></p>
593
+ </div>
594
+ """
595
 
596
  try:
 
 
 
597
  response = requests.post(
598
+ "http://127.0.0.1:600/complete_appointment",
599
+ json={"appointment_id": app_id},
600
  timeout=10
601
  )
602
 
603
  if response.status_code == 200:
604
  return """
605
  <div class='alert alert-success'>
606
+ Consultation completed successfully! Redirecting...
607
  <script>
608
+ setTimeout(() => {
609
  window.location.href = "http://127.0.0.1:600/doctors#consultations";
610
  }, 2000);
611
  </script>
612
  </div>
613
  """
614
+ return f"""
615
+ <div class='alert alert-error'>
616
+ Error completing appointment (Status: {response.status_code})
617
+ </div>
618
+ """
 
 
 
619
  except Exception as e:
620
  return f"""
621
  <div class='alert alert-error'>
622
+ Connection error: {str(e)}
623
  </div>
624
  """
625
 
 
629
  outputs=completion_status
630
  )
631
 
632
+ # Unchanged event handlers
633
+ multi_img_enhance.click(
634
+ app.enhance_image, inputs=multi_img_input, outputs=multi_img_input
635
+ )
636
+ multi_analyze_btn.click(
637
+ app.analyze_multimodal,
638
+ inputs=[multi_img_input, multi_text_input],
639
+ outputs=[multi_results, multi_plot],
640
+ )
641
+ img_enhance.click(app.enhance_image, inputs=img_input, outputs=img_output)
642
+ img_analyze_btn.click(
643
+ app.analyze_image,
644
+ inputs=img_input,
645
+ outputs=[img_output, img_results, img_plot],
646
+ )
647
+ text_analyze_btn.click(
648
+ app.analyze_text,
649
+ inputs=text_input,
650
+ outputs=[text_output, text_results, text_plot],
651
+ )
652
+
653
  try:
654
  interface.launch()
655
  except Exception as e:
656
  logging.error(f"Failed to launch interface: {str(e)}")
657
  raise RuntimeError("Failed to launch MediSync interface") from e
658
 
 
659
  if __name__ == "__main__":
660
  logging.basicConfig(
661
  level=logging.INFO,