Eric Mitchell commited on
Commit
b3740e7
·
1 Parent(s): cda8def

Switched to using st.session_state to manage state.

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -19,18 +19,21 @@ st.markdown("***")
19
  # https://discuss.streamlit.io/t/simple-example-of-persistence-and-waiting-for-input/2111
20
  @st.cache(allow_output_mutation=True)
21
  def Edits():
22
- return pd.DataFrame([], columns=["Edit input", "Edit label"])
23
 
24
  @st.cache(allow_output_mutation=True)
25
  def ModelOutput():
26
- return pd.DataFrame([], columns=["Input", "Generation", "Edits applied"])
27
 
28
- edits = Edits()
29
- model_outputs = ModelOutput()
 
 
 
30
 
31
  def reset():
32
- edits.drop(edits.index, inplace=True)
33
- model_outputs.drop(edits.index, inplace=True)
34
 
35
  selected_alg = st.session_state.alg_selector
36
  selected_alg_idx = EDIT_ALGS.index(selected_alg)
@@ -38,10 +41,10 @@ def reset():
38
  ############# Need to reset the model here (and maybe show progress spinner?)
39
 
40
  def apply_edit():
41
- edits.loc[len(edits)] = [str(edit_input), str(edit_label)]
42
 
43
  def sample_model():
44
- model_outputs.loc[len(model_outputs)] = [str(test_input), "blah blah blah", len(edits)]
45
 
46
  col1, col2 = st.columns([5,1])
47
  with col1:
@@ -51,7 +54,7 @@ with col2:
51
  st.button("Clear edits", on_click=reset)
52
 
53
  st.write("Edits applied so far:")
54
- st.table(edits)
55
 
56
  st.markdown("***")
57
 
@@ -66,7 +69,7 @@ with col3:
66
 
67
  st.markdown("***")
68
 
69
- if len(edits) == 0:
70
  title = "Input to sample from *unedited* model:"
71
  else:
72
  title = f"Input to sample from *edited* model:"
@@ -78,4 +81,4 @@ with col2:
78
  generate_button = st.button("Generate", on_click=sample_model)
79
 
80
  st.write("Model generation history:")
81
- st.table(model_outputs)
 
19
  # https://discuss.streamlit.io/t/simple-example-of-persistence-and-waiting-for-input/2111
20
  @st.cache(allow_output_mutation=True)
21
  def Edits():
22
+ return
23
 
24
  @st.cache(allow_output_mutation=True)
25
  def ModelOutput():
26
+ return
27
 
28
+ if "init" not in st.session_state:
29
+ # Perform first-time initialization
30
+ st.session_state.edits = pd.DataFrame([], columns=["Edit input", "Edit label"])
31
+ st.session_state.model_outputs = pd.DataFrame([], columns=["Input", "Generation", "Edits applied"])
32
+ st.session_state.init = True
33
 
34
  def reset():
35
+ st.session_state.edits.drop(st.session_state.edits.index, inplace=True)
36
+ st.session_state.model_outputs.drop(st.session_state.edits.index, inplace=True)
37
 
38
  selected_alg = st.session_state.alg_selector
39
  selected_alg_idx = EDIT_ALGS.index(selected_alg)
 
41
  ############# Need to reset the model here (and maybe show progress spinner?)
42
 
43
  def apply_edit():
44
+ st.session_state.edits.loc[len(st.session_state.edits)] = [str(edit_input), str(edit_label)]
45
 
46
  def sample_model():
47
+ st.session_state.model_outputs.loc[len(st.session_state.model_outputs)] = [str(test_input), "blah blah blah", len(st.session_state.edits)]
48
 
49
  col1, col2 = st.columns([5,1])
50
  with col1:
 
54
  st.button("Clear edits", on_click=reset)
55
 
56
  st.write("Edits applied so far:")
57
+ st.table(st.session_state.edits)
58
 
59
  st.markdown("***")
60
 
 
69
 
70
  st.markdown("***")
71
 
72
+ if len(st.session_state.edits) == 0:
73
  title = "Input to sample from *unedited* model:"
74
  else:
75
  title = f"Input to sample from *edited* model:"
 
81
  generate_button = st.button("Generate", on_click=sample_model)
82
 
83
  st.write("Model generation history:")
84
+ st.table(st.session_state.model_outputs)