kcelia commited on
Commit
bae49a6
β€’
1 Parent(s): ecd4b93

chore: version 6

Browse files
Files changed (1) hide show
  1. app.py +60 -62
app.py CHANGED
@@ -144,12 +144,10 @@ def get_features_fn(*checked_symptoms: Tuple[str]) -> Dict:
144
  return {
145
  error_box1: gr.update(visible=False),
146
  user_vect_box1: gr.update(
147
- visible=True, value=get_user_symptoms_from_checkboxgroup(pretty_print(checked_symptoms))
148
- ),
149
- recap_symptoms_box: gr.update(
150
- visible=True,
151
- value=pretty_print(checked_symptoms, case_conversion=str.capitalize, delimiter=", "),
152
  ),
 
153
  }
154
 
155
 
@@ -242,8 +240,7 @@ def encrypt_fn(user_symptoms: np.ndarray, user_id: str) -> None:
242
 
243
  return {
244
  error_box3: gr.update(visible=False),
245
- user_vect_box2: gr.update(visible=False, value=user_symptoms),
246
- quant_vect_box: gr.update(visible=False, value=quant_user_symptoms),
247
  enc_vect_box: gr.update(visible=True, value=encrypted_quantized_user_symptoms_shorten_hex),
248
  }
249
 
@@ -406,7 +403,9 @@ def get_output_fn(user_id: str, user_symptoms: np.ndarray) -> Dict:
406
  return {error_box6: gr.update(visible=False), srv_resp_retrieve_data_box: "Data received"}
407
 
408
 
409
- def decrypt_fn(user_id: str, user_symptoms: np.ndarray, threshold: int = 0.5) -> Dict:
 
 
410
  """Dencrypt the data on the `Client Side`.
411
 
412
  Args:
@@ -464,15 +463,15 @@ def decrypt_fn(user_id: str, user_symptoms: np.ndarray, threshold: int = 0.5) ->
464
  or (np.sum(top3_proba) < threshold)
465
  or (abs(top3_proba[0] - top3_proba[1]) < threshold)
466
  ):
467
- out = (
468
- "The prediction appears uncertain; including more symptoms may improve the results.\n\n"
469
- "Here are the top3 predictions:"
470
- )
471
  else:
472
- out = "Based on the information provided, here are the top3 predictions:"
473
 
474
  out = (
475
- f"{out}\n\n"
 
 
476
  f"1. Β« {get_disease_name(top3_diseases[0])} Β» with a probability of {top3_proba[0]:.2%}\n"
477
  f"2. Β« {get_disease_name(top3_diseases[1])} Β» with a probability of {top3_proba[1]:.2%}\n"
478
  f"3. Β« {get_disease_name(top3_diseases[2])} Β» with a probability of {top3_proba[2]:.2%}\n"
@@ -490,12 +489,13 @@ def reset_fn():
490
  clean_directory()
491
 
492
  return {
 
 
493
  user_id_box: gr.update(visible=False, value=None, interactive=False),
494
  user_vect_box1: None,
495
  recap_symptoms_box: gr.update(visible=False, value=None),
496
  default_symptoms: gr.update(visible=True, value=None),
497
  disease_box: gr.update(visible=True, value=None),
498
- user_vect_box2: gr.update(visible=False, value=None, interactive=False),
499
  quant_vect_box: gr.update(visible=False, value=None, interactive=False),
500
  enc_vect_box: gr.update(visible=True, value=None, interactive=False),
501
  key_box: gr.update(visible=True, value=None, interactive=False),
@@ -536,7 +536,7 @@ CSS = """
536
  </button>
537
 
538
  """
539
- back_to_top_btn_html = '''
540
 
541
  <button onclick="scrollToTop()" style="color:white; text-decoration:none;">
542
  Back to Top!
@@ -555,7 +555,7 @@ function scrollToTop() {
555
  </script>
556
 
557
 
558
- '''
559
 
560
  if __name__ == "__main__":
561
 
@@ -565,7 +565,7 @@ if __name__ == "__main__":
565
 
566
  (X_train, X_test), (y_train, y_test), valid_symptoms, diseases = load_data()
567
 
568
- with gr.Blocks(css="#box { height: 500px; overflow-y: scroll !important}") as demo:
569
 
570
  # Link + images
571
  gr.Markdown(
@@ -610,8 +610,9 @@ if __name__ == "__main__":
610
  with gr.Tabs(eelem_id="them") as tabs:
611
  with gr.TabItem("1. Chief Complaints", id=0):
612
  gr.Markdown("<span style='color:grey'>Client Side</span>")
613
- gr.Markdown("## Provide your chief complaints")
614
- gr.Markdown("Provide at least 5 chief complaints by filling in the boxes below. ")
 
615
 
616
  # Box symptoms
617
  check_boxes = []
@@ -633,7 +634,7 @@ if __name__ == "__main__":
633
 
634
  # Default disease, picked from the dataframe
635
  gr.Markdown(
636
- "You can choose an existing disease and explore its associated symptoms."
637
  )
638
 
639
  with gr.Row():
@@ -648,18 +649,24 @@ if __name__ == "__main__":
648
  fn=display_default_symptoms_fn, inputs=[disease_box], outputs=[default_symptoms]
649
  )
650
 
651
- # User symptom vector
652
- user_vect_box1 = gr.Textbox(label="User Symptoms Vector:", interactive=False)
 
 
 
 
 
653
 
654
- # Submit botton
655
- submit_button = gr.Button("Submit πŸ‘†")
 
 
 
 
 
656
 
657
  # Clear botton
658
  clear_button = gr.Button("Reset Space πŸ”")
659
- # Next tab
660
- gr.Markdown("")
661
- next_tab = gr.Button("Next Step πŸ‘‰")
662
- next_tab.click(lambda _: gr.Tabs.update(selected=1), None, tabs)
663
 
664
  with gr.TabItem("2. Data Encryption", id=1):
665
  gr.Markdown("<span style='color:grey'>Client Side</span>")
@@ -672,15 +679,10 @@ if __name__ == "__main__":
672
 
673
  gen_key_btn = gr.Button("Generate the evaluation key πŸ‘†")
674
  error_box2 = gr.Textbox(label="Error ❌", visible=False)
675
-
676
  user_id_box = gr.Textbox(label="User ID:", interactive=False, visible=True)
677
- # Evaluation key size
678
-
679
  key_len_box = gr.Textbox(
680
  label="Evaluation Key Size:", interactive=False, visible=False
681
  )
682
-
683
- # Evaluation key (truncated)
684
  key_box = gr.Textbox(
685
  label="Evaluation key (truncated):",
686
  max_lines=3,
@@ -688,41 +690,26 @@ if __name__ == "__main__":
688
  visible=False,
689
  )
690
 
691
- gen_key_btn.click(
692
- key_gen_fn,
693
- inputs=user_vect_box1,
694
- outputs=[
695
- key_box,
696
- user_id_box,
697
- key_len_box,
698
- error_box2,
699
- ],
700
- )
701
-
702
  gr.Markdown("## Encrypt the data")
703
 
704
  encrypt_btn = gr.Button("Encrypt the data using the πŸ”’ private secret key πŸ‘†")
705
  error_box3 = gr.Textbox(label="Error ❌", visible=False)
 
 
 
 
 
706
 
707
  with gr.Row():
708
- with gr.Column(scale=1, min_width=600):
709
  user_vect_box2 = gr.Textbox(
710
- label="User Symptoms Vector:",
711
- interactive=False,
712
- visible=False,
713
  )
714
 
715
- with gr.Column(scale=1, min_width=600):
716
- quant_vect_box = gr.Textbox(
717
- label="Quantized Vector:",
718
- interactive=False,
719
- visible=False,
720
- )
721
-
722
- with gr.Column(scale=1, min_width=600):
723
  enc_vect_box = gr.Textbox(
724
  label="Encrypted Vector:",
725
- max_lines=3,
726
  interactive=False,
727
  )
728
 
@@ -731,7 +718,6 @@ if __name__ == "__main__":
731
  inputs=[user_vect_box1, user_id_box],
732
  outputs=[
733
  user_vect_box2,
734
- quant_vect_box,
735
  enc_vect_box,
736
  error_box3,
737
  ],
@@ -832,7 +818,7 @@ if __name__ == "__main__":
832
 
833
  decrypt_target_btn.click(
834
  decrypt_fn,
835
- inputs=[user_id_box, user_vect_box1],
836
  outputs=[decrypt_target_box, error_box7],
837
  )
838
 
@@ -845,17 +831,29 @@ if __name__ == "__main__":
845
  next_tab = gr.Button("πŸ‘ˆ πŸ‘ˆ Go back to start")
846
  next_tab.click(lambda _: gr.Tabs.update(selected=0), None, tabs)
847
 
 
 
 
 
 
 
 
 
 
 
 
848
  submit_button.click(
849
  fn=get_features_fn,
850
  inputs=[*check_boxes],
851
- outputs=[user_vect_box1, error_box1, recap_symptoms_box],
852
  )
853
 
854
  clear_button.click(
855
  reset_fn,
856
  outputs=[
857
- user_vect_box1,
858
  user_vect_box2,
 
 
859
  # disease_box,
860
  error_box1,
861
  error_box2,
 
144
  return {
145
  error_box1: gr.update(visible=False),
146
  user_vect_box1: gr.update(
147
+ visible=False,
148
+ value=get_user_symptoms_from_checkboxgroup(pretty_print(checked_symptoms)),
 
 
 
149
  ),
150
+ submit_button: gr.update(value="Data Submitted βœ…"),
151
  }
152
 
153
 
 
240
 
241
  return {
242
  error_box3: gr.update(visible=False),
243
+ user_vect_box2: gr.update(visible=True, value=user_symptoms),
 
244
  enc_vect_box: gr.update(visible=True, value=encrypted_quantized_user_symptoms_shorten_hex),
245
  }
246
 
 
403
  return {error_box6: gr.update(visible=False), srv_resp_retrieve_data_box: "Data received"}
404
 
405
 
406
+ def decrypt_fn(
407
+ user_id: str, user_symptoms: np.ndarray, *checked_symptoms, threshold: int = 0.5
408
+ ) -> Dict:
409
  """Dencrypt the data on the `Client Side`.
410
 
411
  Args:
 
463
  or (np.sum(top3_proba) < threshold)
464
  or (abs(top3_proba[0] - top3_proba[1]) < threshold)
465
  ):
466
+ out = "⚠️ The prediction appears uncertain; including more symptoms may improve the results.\n\n"
467
+
 
 
468
  else:
469
+ out = ""
470
 
471
  out = (
472
+ f"{out}"
473
+ f"Given the symptoms you provided: {pretty_print(checked_symptoms, case_conversion=str.capitalize, delimiter=', ')}\n\n"
474
+ "Here are the top3 predictions:\n\n"
475
  f"1. Β« {get_disease_name(top3_diseases[0])} Β» with a probability of {top3_proba[0]:.2%}\n"
476
  f"2. Β« {get_disease_name(top3_diseases[1])} Β» with a probability of {top3_proba[1]:.2%}\n"
477
  f"3. Β« {get_disease_name(top3_diseases[2])} Β» with a probability of {top3_proba[2]:.2%}\n"
 
489
  clean_directory()
490
 
491
  return {
492
+ user_vect_box2: None,
493
+ submit_button: gr.update(value="Confirm Symptoms"),
494
  user_id_box: gr.update(visible=False, value=None, interactive=False),
495
  user_vect_box1: None,
496
  recap_symptoms_box: gr.update(visible=False, value=None),
497
  default_symptoms: gr.update(visible=True, value=None),
498
  disease_box: gr.update(visible=True, value=None),
 
499
  quant_vect_box: gr.update(visible=False, value=None, interactive=False),
500
  enc_vect_box: gr.update(visible=True, value=None, interactive=False),
501
  key_box: gr.update(visible=True, value=None, interactive=False),
 
536
  </button>
537
 
538
  """
539
+ back_to_top_btn_html = """
540
 
541
  <button onclick="scrollToTop()" style="color:white; text-decoration:none;">
542
  Back to Top!
 
555
  </script>
556
 
557
 
558
+ """
559
 
560
  if __name__ == "__main__":
561
 
 
565
 
566
  (X_train, X_test), (y_train, y_test), valid_symptoms, diseases = load_data()
567
 
568
+ with gr.Blocks(css="them") as demo:
569
 
570
  # Link + images
571
  gr.Markdown(
 
610
  with gr.Tabs(eelem_id="them") as tabs:
611
  with gr.TabItem("1. Chief Complaints", id=0):
612
  gr.Markdown("<span style='color:grey'>Client Side</span>")
613
+ gr.Markdown(
614
+ "## Provide at least 5 chief complaints by filling in the boxes below. "
615
+ )
616
 
617
  # Box symptoms
618
  check_boxes = []
 
634
 
635
  # Default disease, picked from the dataframe
636
  gr.Markdown(
637
+ "## You can choose an **existing disease** and explore its associated symptoms."
638
  )
639
 
640
  with gr.Row():
 
649
  fn=display_default_symptoms_fn, inputs=[disease_box], outputs=[default_symptoms]
650
  )
651
 
652
+ gr.Markdown(
653
+ "#### Submit your chief complaints by clicking on **Confirm Symptoms πŸ‘†** then go to the **Next Step πŸ‘‰**"
654
+ )
655
+
656
+ user_vect_box1 = gr.Textbox(
657
+ visible=False,
658
+ )
659
 
660
+ with gr.Row():
661
+ with gr.Column():
662
+ # Submit botton
663
+ submit_button = gr.Button("Confirm Symptoms πŸ‘†")
664
+ with gr.Column():
665
+ next_tab = gr.Button("Next Step πŸ‘‰")
666
+ next_tab.click(lambda _: gr.Tabs.update(selected=1), None, tabs)
667
 
668
  # Clear botton
669
  clear_button = gr.Button("Reset Space πŸ”")
 
 
 
 
670
 
671
  with gr.TabItem("2. Data Encryption", id=1):
672
  gr.Markdown("<span style='color:grey'>Client Side</span>")
 
679
 
680
  gen_key_btn = gr.Button("Generate the evaluation key πŸ‘†")
681
  error_box2 = gr.Textbox(label="Error ❌", visible=False)
 
682
  user_id_box = gr.Textbox(label="User ID:", interactive=False, visible=True)
 
 
683
  key_len_box = gr.Textbox(
684
  label="Evaluation Key Size:", interactive=False, visible=False
685
  )
 
 
686
  key_box = gr.Textbox(
687
  label="Evaluation key (truncated):",
688
  max_lines=3,
 
690
  visible=False,
691
  )
692
 
 
 
 
 
 
 
 
 
 
 
 
693
  gr.Markdown("## Encrypt the data")
694
 
695
  encrypt_btn = gr.Button("Encrypt the data using the πŸ”’ private secret key πŸ‘†")
696
  error_box3 = gr.Textbox(label="Error ❌", visible=False)
697
+ quant_vect_box = gr.Textbox(
698
+ label="Quantized Vector:",
699
+ interactive=False,
700
+ visible=False,
701
+ )
702
 
703
  with gr.Row():
704
+ with gr.Column():
705
  user_vect_box2 = gr.Textbox(
706
+ label="User Symptoms Vector:", interactive=False, max_lines=10
 
 
707
  )
708
 
709
+ with gr.Column():
 
 
 
 
 
 
 
710
  enc_vect_box = gr.Textbox(
711
  label="Encrypted Vector:",
712
+ max_lines=10,
713
  interactive=False,
714
  )
715
 
 
718
  inputs=[user_vect_box1, user_id_box],
719
  outputs=[
720
  user_vect_box2,
 
721
  enc_vect_box,
722
  error_box3,
723
  ],
 
818
 
819
  decrypt_target_btn.click(
820
  decrypt_fn,
821
+ inputs=[user_id_box, user_vect_box1, *check_boxes],
822
  outputs=[decrypt_target_box, error_box7],
823
  )
824
 
 
831
  next_tab = gr.Button("πŸ‘ˆ πŸ‘ˆ Go back to start")
832
  next_tab.click(lambda _: gr.Tabs.update(selected=0), None, tabs)
833
 
834
+ gen_key_btn.click(
835
+ key_gen_fn,
836
+ inputs=user_vect_box1,
837
+ outputs=[
838
+ key_box,
839
+ user_id_box,
840
+ key_len_box,
841
+ error_box2,
842
+ ],
843
+ )
844
+
845
  submit_button.click(
846
  fn=get_features_fn,
847
  inputs=[*check_boxes],
848
+ outputs=[user_vect_box1, error_box1, submit_button],
849
  )
850
 
851
  clear_button.click(
852
  reset_fn,
853
  outputs=[
 
854
  user_vect_box2,
855
+ user_vect_box1,
856
+ submit_button,
857
  # disease_box,
858
  error_box1,
859
  error_box2,