blackshadow1 commited on
Commit
75243ff
·
verified ·
1 Parent(s): ccb45f8

updated code ✅✅

Browse files
Files changed (1) hide show
  1. mediSync/app.py +69 -31
mediSync/app.py CHANGED
@@ -399,7 +399,7 @@ class MediSyncApp:
399
 
400
 
401
  def create_interface():
402
- """Create and launch the Gradio interface."""
403
  app = MediSyncApp()
404
 
405
  example_report = """
@@ -420,24 +420,32 @@ 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_path = None
 
 
 
 
 
 
424
  try:
425
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
426
  os.makedirs(sample_images_dir, exist_ok=True)
427
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
428
 
429
  if not sample_images:
430
- fallback_url = "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg"
431
- sample_path = sample_images_dir / "sample_xray.jpg"
432
- try:
433
- response = requests.get(fallback_url, timeout=10)
434
- if response.status_code == 200:
435
- with open(sample_path, 'wb') as f:
436
- f.write(response.content)
437
- sample_image_path = str(sample_path)
438
- logger.info("Downloaded fallback sample image")
439
- except Exception as e:
440
- logger.warning(f"Could not download sample image: {str(e)}")
 
441
  else:
442
  sample_image_path = str(sample_images[0])
443
  except Exception as e:
@@ -448,6 +456,7 @@ def create_interface():
448
  padding: 15px;
449
  margin: 10px 0;
450
  border-radius: 5px;
 
451
  }
452
  .alert-error {
453
  background-color: #ffebee;
@@ -467,19 +476,24 @@ def create_interface():
467
  css=custom_css
468
  ) as interface:
469
 
470
- appointment_id = gr.Textbox(visible=False)
471
-
 
 
 
 
 
 
 
 
 
 
 
472
  interface.load(
473
  None,
474
  None,
475
  None,
476
- _js="""
477
- function getAppointmentId() {
478
- const urlParams = new URLSearchParams(window.location.search);
479
- return urlParams.get('appointment_id') || '';
480
- }
481
- document.getElementById('appointment-id').value = getAppointmentId();
482
- """
483
  )
484
 
485
  gr.Markdown("""
@@ -563,6 +577,7 @@ def create_interface():
563
  end_consultation_btn = gr.Button("End Consultation", variant="stop", size="lg")
564
  completion_status = gr.HTML()
565
 
 
566
  multi_img_enhance.click(app.enhance_image, inputs=multi_img_input, outputs=multi_img_input)
567
  multi_analyze_btn.click(
568
  app.analyze_multimodal,
@@ -582,36 +597,55 @@ def create_interface():
582
  )
583
 
584
  def complete_consultation(appointment_id):
 
585
  if not appointment_id:
586
- return """
587
  <div class="alert-box alert-error">
588
- No appointment ID found. Please contact support.
 
 
 
 
589
  </div>
590
  """
 
 
 
591
  try:
592
  response = requests.post(
593
  "http://127.0.0.1:600/complete_consultation",
594
  json={"appointment_id": appointment_id},
595
  timeout=10
596
  )
597
- if response.status_code == 200:
598
- return """
 
599
  <div class="alert-box alert-success">
600
- Consultation completed successfully. Redirecting...
 
601
  <script>
602
- setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 2000);
603
  </script>
604
  </div>
605
  """
 
 
 
 
 
 
606
  return f"""
607
  <div class="alert-box alert-error">
608
- Error completing appointment (Status: {response.status_code}).
 
609
  </div>
610
  """
611
  except Exception as e:
 
612
  return f"""
613
  <div class="alert-box alert-error">
614
- Error: {str(e)}
 
615
  </div>
616
  """
617
 
@@ -621,7 +655,11 @@ def create_interface():
621
  outputs=[completion_status]
622
  )
623
 
624
- interface.launch()
 
 
 
 
625
 
626
  if __name__ == "__main__":
627
- create_interface()
 
399
 
400
 
401
  def create_interface():
402
+ """Create and launch the Gradio interface with all fixes."""
403
  app = MediSyncApp()
404
 
405
  example_report = """
 
420
  RECOMMENDATIONS: Follow-up chest CT to further characterize the nodular opacity in the right lower lobe.
421
  """
422
 
423
+ # Sample image handling with multiple fallback options
424
  sample_image_path = None
425
+ fallback_urls = [
426
+ "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr2_lrg-a.jpg",
427
+ "https://raw.githubusercontent.com/ieee8023/covid-chestxray-dataset/master/images/1-s2.0-S0929664620300449-gr1_lrg-a.jpg",
428
+ "https://github.com/ieee8023/covid-chestxray-dataset/raw/master/images/1-s2.0-S0929664620300449-gr3_lrg-a.jpg"
429
+ ]
430
+
431
  try:
432
  sample_images_dir = Path(__file__).parent.parent / "data" / "sample"
433
  os.makedirs(sample_images_dir, exist_ok=True)
434
  sample_images = list(sample_images_dir.glob("*.png")) + list(sample_images_dir.glob("*.jpg"))
435
 
436
  if not sample_images:
437
+ for i, url in enumerate(fallback_urls):
438
+ try:
439
+ sample_path = sample_images_dir / f"sample_xray_{i}.jpg"
440
+ response = requests.get(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
+ logger.info(f"Successfully downloaded fallback sample image from {url}")
446
+ break
447
+ except Exception as e:
448
+ logger.warning(f"Could not download sample image from {url}: {str(e)}")
449
  else:
450
  sample_image_path = str(sample_images[0])
451
  except Exception as e:
 
456
  padding: 15px;
457
  margin: 10px 0;
458
  border-radius: 5px;
459
+ font-family: sans-serif;
460
  }
461
  .alert-error {
462
  background-color: #ffebee;
 
476
  css=custom_css
477
  ) as interface:
478
 
479
+ # Appointment ID handling with both Python and JavaScript fallback
480
+ appointment_id = gr.Textbox(visible=False, elem_id="appointment-id")
481
+
482
+ # JavaScript to extract appointment ID from URL
483
+ js_code = """
484
+ function getAppointmentId() {
485
+ const urlParams = new URLSearchParams(window.location.search);
486
+ return urlParams.get('appointment_id') || '';
487
+ }
488
+ document.getElementById('appointment-id').value = getAppointmentId();
489
+ """
490
+
491
+ # Initialize appointment ID from URL
492
  interface.load(
493
  None,
494
  None,
495
  None,
496
+ _js=js_code
 
 
 
 
 
 
497
  )
498
 
499
  gr.Markdown("""
 
577
  end_consultation_btn = gr.Button("End Consultation", variant="stop", size="lg")
578
  completion_status = gr.HTML()
579
 
580
+ # Event handlers
581
  multi_img_enhance.click(app.enhance_image, inputs=multi_img_input, outputs=multi_img_input)
582
  multi_analyze_btn.click(
583
  app.analyze_multimodal,
 
597
  )
598
 
599
  def complete_consultation(appointment_id):
600
+ """Handle consultation completion with improved error handling."""
601
  if not appointment_id:
602
+ error_html = """
603
  <div class="alert-box alert-error">
604
+ <strong>No Appointment ID Found</strong>
605
+ <p>Your session may not be properly recorded.</p>
606
+ <script>
607
+ setTimeout(() => window.location.href = "http://127.0.0.1:600/doctors", 3000);
608
+ </script>
609
  </div>
610
  """
611
+ logger.error("Completion attempted without appointment ID")
612
+ return error_html
613
+
614
  try:
615
  response = requests.post(
616
  "http://127.0.0.1:600/complete_consultation",
617
  json={"appointment_id": appointment_id},
618
  timeout=10
619
  )
620
+
621
+ if response.ok:
622
+ success_html = """
623
  <div class="alert-box alert-success">
624
+ <strong>Consultation Completed</strong>
625
+ <p>You will be redirected shortly.</p>
626
  <script>
627
+ setTimeout(() => window.location.href = "http://127.0.0.1:600/complete_appointment", 2000);
628
  </script>
629
  </div>
630
  """
631
+ logger.info(f"Successfully completed appointment {appointment_id}")
632
+ return success_html
633
+ error_msg = f"Server error: {response.status_code}"
634
+ if response.text:
635
+ error_msg += f" - {response.text[:200]}"
636
+ logger.error(error_msg)
637
  return f"""
638
  <div class="alert-box alert-error">
639
+ <strong>Error Completing Appointment</strong>
640
+ <p>{error_msg}</p>
641
  </div>
642
  """
643
  except Exception as e:
644
+ logger.error(f"Completion error: {str(e)}")
645
  return f"""
646
  <div class="alert-box alert-error">
647
+ <strong>Connection Error</strong>
648
+ <p>{str(e)}</p>
649
  </div>
650
  """
651
 
 
655
  outputs=[completion_status]
656
  )
657
 
658
+ try:
659
+ interface.launch()
660
+ except Exception as e:
661
+ logger.error(f"Failed to launch interface: {str(e)}")
662
+ raise
663
 
664
  if __name__ == "__main__":
665
+ create_interface()