feiyang-cai commited on
Commit
30479e2
·
verified ·
1 Parent(s): dc6ff35

Reset downstream UI on requirement changes

Browse files
Files changed (1) hide show
  1. app.py +116 -13
app.py CHANGED
@@ -70,6 +70,8 @@ DEFAULT_TEST_INDEX = int(os.environ.get("MG_DEFAULT_TEST_INDEX", "512"))
70
  THERMO_N_RESTARTS = int(os.environ.get("MG_THERMO_RESTARTS", "6"))
71
  THERMO_EPOCHS = int(os.environ.get("MG_THERMO_EPOCHS", "300"))
72
  MATERIAL_TYPE_NAMES = {0: "CPP", 1: "CHDPE", 2: "GPP", 3: "GHDPE"}
 
 
73
 
74
 
75
  REQUIREMENTS: List[Dict[str, Any]] = [
@@ -440,22 +442,54 @@ def ui_disable_all_buttons():
440
  return ui_set_buttons(False, False, False, False)
441
 
442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443
  def ui_input_mode_changed(mode: str):
444
  manual = mode == "Define by Input Values"
445
  status = ""
446
  if manual:
447
- status = "Manual requirement mode is active."
448
  else:
449
- status = "Load-from-test mode is active."
450
  return (
451
  gr.update(visible=manual),
452
  gr.update(visible=not manual),
453
  "",
454
  status,
455
- gr.update(interactive=manual),
456
- gr.update(interactive=not manual),
457
- gr.update(interactive=False),
458
- gr.update(interactive=False),
459
  )
460
 
461
 
@@ -681,7 +715,7 @@ def build_app():
681
  gr.Markdown("### Load Requirement Curves from Saved Test Data")
682
  with gr.Row():
683
  load_test_idx = gr.Number(value=DEFAULT_TEST_INDEX, precision=0, label="Load v5 test condition index")
684
- load_btn = gr.Button("Load Test Condition")
685
 
686
  n_generate = gr.Slider(
687
  minimum=1,
@@ -690,7 +724,7 @@ def build_app():
690
  value=DEFAULT_N_GENERATE,
691
  label="Number of generated candidates",
692
  )
693
- design_btn = gr.Button("Generate Parameter Sets")
694
 
695
  load_status = gr.Markdown("")
696
  req_plot = gr.Plot(label="Required Stress-Strain Curves")
@@ -703,7 +737,7 @@ def build_app():
703
 
704
  gr.Markdown("## 3. Choose the Closest Generated Design")
705
  design_choice = gr.Radio(choices=[], label="Generated designs")
706
- selected_design_md = gr.Markdown("No selected design is available.")
707
  design_payload = gr.State("")
708
 
709
  gr.Markdown("## 4. Thermoforming Requirements")
@@ -717,14 +751,75 @@ def build_app():
717
  angle_b = gr.Number(value=0.0, label="Maximum warpage angle B (degree)", precision=3)
718
  angle_c = gr.Number(value=0.0, label="Maximum warpage angle C (degree)", precision=3)
719
  max_stress = gr.Number(value=0.0, label="Maximum residual stress (MPa)", precision=3)
720
- thermo_btn = gr.Button("Thermoforming Process Design")
721
- thermo_selected_md = gr.Markdown("No selected design is available.")
722
- thermo_result_md = gr.Markdown("Thermoforming has not run yet.")
723
 
724
  input_mode.change(
725
  fn=ui_input_mode_changed,
726
  inputs=[input_mode],
727
- outputs=[manual_panel, test_panel, exact_test_coeffs, load_status, preview_btn, load_btn, design_btn, thermo_btn],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  show_progress=False,
729
  )
730
  load_btn.click(fn=ui_disable_all_buttons, outputs=[preview_btn, load_btn, design_btn, thermo_btn], show_progress=False).then(
@@ -732,6 +827,10 @@ def build_app():
732
  inputs=[load_test_idx],
733
  outputs=input_components + [load_status, req_plot, exact_test_coeffs],
734
  show_progress=False,
 
 
 
 
735
  ).then(
736
  fn=ui_after_requirement_ready,
737
  inputs=[input_mode],
@@ -750,6 +849,10 @@ def build_app():
750
  fn=ui_clear_exact_test_coeffs,
751
  outputs=[exact_test_coeffs],
752
  show_progress=False,
 
 
 
 
753
  ).then(
754
  fn=ui_after_requirement_ready,
755
  inputs=[input_mode],
 
70
  THERMO_N_RESTARTS = int(os.environ.get("MG_THERMO_RESTARTS", "6"))
71
  THERMO_EPOCHS = int(os.environ.get("MG_THERMO_EPOCHS", "300"))
72
  MATERIAL_TYPE_NAMES = {0: "CPP", 1: "CHDPE", 2: "GPP", 3: "GHDPE"}
73
+ NO_SELECTED_DESIGN = "No selected design is available."
74
+ THERMO_NOT_RUN = "Thermoforming has not run yet."
75
 
76
 
77
  REQUIREMENTS: List[Dict[str, Any]] = [
 
442
  return ui_set_buttons(False, False, False, False)
443
 
444
 
445
+ def ui_clear_design_outputs():
446
+ return (
447
+ gr.update(value=None),
448
+ "",
449
+ "",
450
+ gr.update(choices=[], value=None),
451
+ NO_SELECTED_DESIGN,
452
+ "",
453
+ NO_SELECTED_DESIGN,
454
+ THERMO_NOT_RUN,
455
+ )
456
+
457
+
458
+ def ui_manual_requirements_changed():
459
+ return (
460
+ "",
461
+ "Requirements changed. Generate curves before inverse design.",
462
+ gr.update(value=None),
463
+ *ui_clear_design_outputs(),
464
+ *ui_set_buttons(True, False, False, False),
465
+ )
466
+
467
+
468
+ def ui_test_index_changed():
469
+ return (
470
+ "",
471
+ "Test index changed. Load the test condition before inverse design.",
472
+ gr.update(value=None),
473
+ *ui_clear_design_outputs(),
474
+ *ui_set_buttons(False, True, False, False),
475
+ )
476
+
477
+
478
  def ui_input_mode_changed(mode: str):
479
  manual = mode == "Define by Input Values"
480
  status = ""
481
  if manual:
482
+ status = "Manual requirement mode is active. Generate curves before inverse design."
483
  else:
484
+ status = "Load-from-test mode is active. Load a test condition before inverse design."
485
  return (
486
  gr.update(visible=manual),
487
  gr.update(visible=not manual),
488
  "",
489
  status,
490
+ gr.update(value=None),
491
+ *ui_clear_design_outputs(),
492
+ *ui_set_buttons(manual, not manual, False, False),
 
493
  )
494
 
495
 
 
715
  gr.Markdown("### Load Requirement Curves from Saved Test Data")
716
  with gr.Row():
717
  load_test_idx = gr.Number(value=DEFAULT_TEST_INDEX, precision=0, label="Load v5 test condition index")
718
+ load_btn = gr.Button("Load Test Condition", interactive=False)
719
 
720
  n_generate = gr.Slider(
721
  minimum=1,
 
724
  value=DEFAULT_N_GENERATE,
725
  label="Number of generated candidates",
726
  )
727
+ design_btn = gr.Button("Generate Parameter Sets", interactive=False)
728
 
729
  load_status = gr.Markdown("")
730
  req_plot = gr.Plot(label="Required Stress-Strain Curves")
 
737
 
738
  gr.Markdown("## 3. Choose the Closest Generated Design")
739
  design_choice = gr.Radio(choices=[], label="Generated designs")
740
+ selected_design_md = gr.Markdown(NO_SELECTED_DESIGN)
741
  design_payload = gr.State("")
742
 
743
  gr.Markdown("## 4. Thermoforming Requirements")
 
751
  angle_b = gr.Number(value=0.0, label="Maximum warpage angle B (degree)", precision=3)
752
  angle_c = gr.Number(value=0.0, label="Maximum warpage angle C (degree)", precision=3)
753
  max_stress = gr.Number(value=0.0, label="Maximum residual stress (MPa)", precision=3)
754
+ thermo_btn = gr.Button("Thermoforming Process Design", interactive=False)
755
+ thermo_selected_md = gr.Markdown(NO_SELECTED_DESIGN)
756
+ thermo_result_md = gr.Markdown(THERMO_NOT_RUN)
757
 
758
  input_mode.change(
759
  fn=ui_input_mode_changed,
760
  inputs=[input_mode],
761
+ outputs=[
762
+ manual_panel,
763
+ test_panel,
764
+ exact_test_coeffs,
765
+ load_status,
766
+ req_plot,
767
+ design_plot,
768
+ design_summary,
769
+ run_log,
770
+ design_choice,
771
+ selected_design_md,
772
+ design_payload,
773
+ thermo_selected_md,
774
+ thermo_result_md,
775
+ preview_btn,
776
+ load_btn,
777
+ design_btn,
778
+ thermo_btn,
779
+ ],
780
+ show_progress=False,
781
+ )
782
+ for component in input_components:
783
+ component.input(
784
+ fn=ui_manual_requirements_changed,
785
+ outputs=[
786
+ exact_test_coeffs,
787
+ load_status,
788
+ req_plot,
789
+ design_plot,
790
+ design_summary,
791
+ run_log,
792
+ design_choice,
793
+ selected_design_md,
794
+ design_payload,
795
+ thermo_selected_md,
796
+ thermo_result_md,
797
+ preview_btn,
798
+ load_btn,
799
+ design_btn,
800
+ thermo_btn,
801
+ ],
802
+ show_progress=False,
803
+ )
804
+ load_test_idx.input(
805
+ fn=ui_test_index_changed,
806
+ outputs=[
807
+ exact_test_coeffs,
808
+ load_status,
809
+ req_plot,
810
+ design_plot,
811
+ design_summary,
812
+ run_log,
813
+ design_choice,
814
+ selected_design_md,
815
+ design_payload,
816
+ thermo_selected_md,
817
+ thermo_result_md,
818
+ preview_btn,
819
+ load_btn,
820
+ design_btn,
821
+ thermo_btn,
822
+ ],
823
  show_progress=False,
824
  )
825
  load_btn.click(fn=ui_disable_all_buttons, outputs=[preview_btn, load_btn, design_btn, thermo_btn], show_progress=False).then(
 
827
  inputs=[load_test_idx],
828
  outputs=input_components + [load_status, req_plot, exact_test_coeffs],
829
  show_progress=False,
830
+ ).then(
831
+ fn=ui_clear_design_outputs,
832
+ outputs=[design_plot, design_summary, run_log, design_choice, selected_design_md, design_payload, thermo_selected_md, thermo_result_md],
833
+ show_progress=False,
834
  ).then(
835
  fn=ui_after_requirement_ready,
836
  inputs=[input_mode],
 
849
  fn=ui_clear_exact_test_coeffs,
850
  outputs=[exact_test_coeffs],
851
  show_progress=False,
852
+ ).then(
853
+ fn=ui_clear_design_outputs,
854
+ outputs=[design_plot, design_summary, run_log, design_choice, selected_design_md, design_payload, thermo_selected_md, thermo_result_md],
855
+ show_progress=False,
856
  ).then(
857
  fn=ui_after_requirement_ready,
858
  inputs=[input_mode],