zulissimeta commited on
Commit
fa3477c
·
1 Parent(s): ced9fb3

add better validation

Browse files
Files changed (2) hide show
  1. app.py +17 -11
  2. simulation_scripts.py +52 -0
app.py CHANGED
@@ -12,7 +12,11 @@ from pathlib import Path
12
 
13
  import gradio as gr
14
 
15
- from simulation_scripts import run_md_simulation, run_relaxation_simulation
 
 
 
 
16
 
17
  DEFAULT_MOLECULAR_REPRESENTATIONS = [
18
  {
@@ -515,7 +519,8 @@ def main():
515
  label="Relaxation examples!",
516
  )
517
  with gr.Accordion(
518
- "UMA can use different output heads for the same structures", open=False
 
519
  ):
520
  gr.Examples(
521
  examples=[
@@ -674,7 +679,6 @@ def main():
674
  label="Molecular Dynamics Examples",
675
  )
676
 
677
-
678
  gr.Markdown(
679
  """
680
  Once you understand how the UMA model can be applied to different types of molecules and materials, the final tab above will help you try it out with your own structures!
@@ -777,7 +781,9 @@ def main():
777
  with gr.Column():
778
  input_structure.render()
779
 
780
- gr.LoginButton(size="large")
 
 
781
 
782
  gr.Markdown(
783
  """
@@ -973,15 +979,15 @@ def main():
973
  )
974
 
975
  input_structure.change(
976
- lambda file: gr.Button(interactive=bool(file)),
977
- inputs=[input_structure],
978
- outputs=[optimization_button],
979
  )
980
 
981
- input_structure.change(
982
- lambda file: gr.Button(interactive=bool(file)),
983
- inputs=[input_structure],
984
- outputs=[md_button],
985
  )
986
 
987
  demo.launch()
 
12
 
13
  import gradio as gr
14
 
15
+ from simulation_scripts import (
16
+ run_md_simulation,
17
+ run_relaxation_simulation,
18
+ validate_ase_atoms_and_login,
19
+ )
20
 
21
  DEFAULT_MOLECULAR_REPRESENTATIONS = [
22
  {
 
519
  label="Relaxation examples!",
520
  )
521
  with gr.Accordion(
522
+ "UMA can use different output heads for the same structures",
523
+ open=False,
524
  ):
525
  gr.Examples(
526
  examples=[
 
679
  label="Molecular Dynamics Examples",
680
  )
681
 
 
682
  gr.Markdown(
683
  """
684
  Once you understand how the UMA model can be applied to different types of molecules and materials, the final tab above will help you try it out with your own structures!
 
781
  with gr.Column():
782
  input_structure.render()
783
 
784
+ structure_validation = gr.Markdown()
785
+
786
+ login_button = gr.LoginButton(size="large")
787
 
788
  gr.Markdown(
789
  """
 
979
  )
980
 
981
  input_structure.change(
982
+ validate_ase_atoms_and_login,
983
+ inputs=[input_structure, login_button],
984
+ outputs=[optimization_button, md_button, structure_validation],
985
  )
986
 
987
+ login_button.change(
988
+ validate_ase_atoms_and_login,
989
+ inputs=[input_structure, login_button],
990
+ outputs=[optimization_button, md_button, structure_validation],
991
  )
992
 
993
  demo.launch()
simulation_scripts.py CHANGED
@@ -42,6 +42,58 @@ MAX_ATOMS = os.environ.get("MAX_ATOMS", 2000)
42
  INFERENCE_ENDPOINT_URL = os.environ["INFERENCE_ENDPOINT_URL"]
43
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def load_check_ase_atoms(structure_file):
46
  # Validate and write the uploaded file content
47
  if not structure_file:
 
42
  INFERENCE_ENDPOINT_URL = os.environ["INFERENCE_ENDPOINT_URL"]
43
 
44
 
45
+ def validate_ase_atoms_and_login(structure_file, login_button_value):
46
+ # Validate and write the uploaded file content
47
+ if not structure_file:
48
+ return (
49
+ gr.Button(interactive=False),
50
+ gr.Button(interactive=False),
51
+ "Missing input structure!",
52
+ )
53
+
54
+ try:
55
+ atoms = ase.io.read(structure_file)
56
+ except Exception as e:
57
+ return (
58
+ gr.Button(interactive=False),
59
+ gr.Button(interactive=False),
60
+ f"Failed to load ASE input: {str(e)}!",
61
+ )
62
+
63
+ if len(atoms) == 0:
64
+ return (
65
+ gr.Button(interactive=False),
66
+ gr.Button(interactive=False),
67
+ "No atoms in the structure file!",
68
+ )
69
+ elif not (all(atoms.pbc) or np.all(~np.array(atoms.pbc))):
70
+ return (
71
+ gr.Button(interactive=False),
72
+ gr.Button(interactive=False),
73
+ f"Your atoms has PBC {atoms.pbc}. Mixed PBC are not supported yet - please set PBC all True or False in your structure before uploading!",
74
+ )
75
+ elif len(atoms) > MAX_ATOMS:
76
+ return (
77
+ gr.Button(interactive=False),
78
+ gr.Button(interactive=False),
79
+ f"Structure file contains {len(atoms)}, which is more than {MAX_ATOMS} atoms. Please use a smaller structure for this demo, or run this on a local machine!",
80
+ )
81
+ elif (hash_file(structure_file) not in EXAMPLE_FILE_HASHES) and (
82
+ "login_button_value" not in login_button_value
83
+ ):
84
+ return (
85
+ gr.Button(interactive=False),
86
+ gr.Button(interactive=False),
87
+ "You must login to huggingface to use a custom structure!",
88
+ )
89
+ else:
90
+ return (
91
+ gr.Button(interactive=True),
92
+ gr.Button(interactive=True),
93
+ "Valid structure file and you are logged in!",
94
+ )
95
+
96
+
97
  def load_check_ase_atoms(structure_file):
98
  # Validate and write the uploaded file content
99
  if not structure_file: