mahynski commited on
Commit
ca4b289
·
verified ·
1 Parent(s): 6aeacd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -17,14 +17,23 @@ from streamlit_extras.add_vertical_space import add_vertical_space
17
 
18
  UPLOAD_FOLDER = "uploaded_nmr"
19
 
 
 
 
 
20
  # ----------------------------- CACHED FUNCTIONS -----------------------------
21
  @st.cache_data
22
  def build_library():
23
  """Build NMR library from HF."""
 
 
 
 
 
24
  nmr_dataset = load_dataset(
25
  "mahynski/bmrb-hsqc-nmr-1H13C",
26
  split="train",
27
- token=os.getenv("HF_TOKEN"),
28
  trust_remote_code=True,
29
  )
30
  substances = [
@@ -36,8 +45,6 @@ def build_library():
36
  lib = finchnmr.library.Library(substances)
37
  return lib
38
 
39
-
40
- # @st.cache_data
41
  def build_model(_target, _lib, _param_grid, _nmr_model, _model_kw):
42
  """Build model for target."""
43
  optimized_models, analyses = finchnmr.model.optimize_models(
@@ -49,7 +56,6 @@ def build_model(_target, _lib, _param_grid, _nmr_model, _model_kw):
49
  )
50
  return optimized_models, analyses
51
 
52
-
53
  # --------------------------------- SIDEBAR ----------------------------------
54
  st.set_page_config(layout="wide")
55
  st.header("Analyze an HSQC NMR Spectra with FINCHnmr")
@@ -112,8 +118,6 @@ if uploaded_file is not None:
112
  warning="ignore",
113
  )
114
 
115
- # optimized_models = []
116
-
117
  tab1_, tab2_ = st.tabs(["Configure Model", "Analyze Results"])
118
  with tab1_:
119
  st.subheader("Configure Model")
@@ -258,6 +262,8 @@ if uploaded_file is not None:
258
  model_ = st.session_state['optimized_models'][0] # We only fit one model
259
  analysis_ = st.session_state['analyses'][0]
260
 
 
 
261
  # Plot original vs. reconstructed and residual
262
  col3_, col4_ = st.columns(2)
263
  with col3_:
@@ -317,7 +323,6 @@ if uploaded_file is not None:
317
  max_n_ = len(analysis_._model.importances())
318
  default_n_ = np.min([10, max_n_])
319
  with col6_:
320
- st.metric(label="Model's score (R^2)", value=model_.score())
321
  n_imp_ = st.slider(
322
  label="Visualize the most important N spectra in the library",
323
  value=default_n_,
 
17
 
18
  UPLOAD_FOLDER = "uploaded_nmr"
19
 
20
+ def is_running_on_huggingface():
21
+ """Checks if the app is running on Hugging Face Spaces."""
22
+ return "HF_SPACE_ID" in os.environ
23
+
24
  # ----------------------------- CACHED FUNCTIONS -----------------------------
25
  @st.cache_data
26
  def build_library():
27
  """Build NMR library from HF."""
28
+ if is_running_on_huggingface():
29
+ token = os.getenv("HF_TOKEN") # If on huggingface
30
+ else:
31
+ token = st.secrets["HF_TOKEN"] # If on streamlit community cloud
32
+
33
  nmr_dataset = load_dataset(
34
  "mahynski/bmrb-hsqc-nmr-1H13C",
35
  split="train",
36
+ token=token,
37
  trust_remote_code=True,
38
  )
39
  substances = [
 
45
  lib = finchnmr.library.Library(substances)
46
  return lib
47
 
 
 
48
  def build_model(_target, _lib, _param_grid, _nmr_model, _model_kw):
49
  """Build model for target."""
50
  optimized_models, analyses = finchnmr.model.optimize_models(
 
56
  )
57
  return optimized_models, analyses
58
 
 
59
  # --------------------------------- SIDEBAR ----------------------------------
60
  st.set_page_config(layout="wide")
61
  st.header("Analyze an HSQC NMR Spectra with FINCHnmr")
 
118
  warning="ignore",
119
  )
120
 
 
 
121
  tab1_, tab2_ = st.tabs(["Configure Model", "Analyze Results"])
122
  with tab1_:
123
  st.subheader("Configure Model")
 
262
  model_ = st.session_state['optimized_models'][0] # We only fit one model
263
  analysis_ = st.session_state['analyses'][0]
264
 
265
+ st.metric(label="Model's score (R^2)", value=model_.score())
266
+
267
  # Plot original vs. reconstructed and residual
268
  col3_, col4_ = st.columns(2)
269
  with col3_:
 
323
  max_n_ = len(analysis_._model.importances())
324
  default_n_ = np.min([10, max_n_])
325
  with col6_:
 
326
  n_imp_ = st.slider(
327
  label="Visualize the most important N spectra in the library",
328
  value=default_n_,