deeploy-adubowski commited on
Commit
8870850
1 Parent(s): aacd07c

Cleanup session state handling

Browse files
Files changed (1) hide show
  1. app.py +109 -95
app.py CHANGED
@@ -98,16 +98,38 @@ if "expander_toggle" not in st.session_state:
98
  if "evaluation_submitted" not in st.session_state:
99
  st.session_state.evaluation_submitted = False
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  def submit_and_clear():
102
  try:
103
  # Call the explain endpoint as it also includes the prediction
104
  client.evaluate(
105
  deployment_id, request_log_id, prediction_log_id, st.session_state.evaluation_input
106
  )
107
- # st.toast(":green-background[Feedback submitted successfully.]", icon="✅")
108
  st.session_state.eval_selected = False
109
  st.session_state.evaluation_submitted = True
110
  st.session_state.eval_selected = False
 
 
 
111
  except Exception as e:
112
  logging.error(e)
113
  st.error(
@@ -115,14 +137,6 @@ def submit_and_clear():
115
  + "Check whether you are using the right model URL and token for evaluations. "
116
  + "Contact Deeploy if the problem persists."
117
  )
118
- # st.toast(f"Failed to submit feedback: {e}")
119
-
120
-
121
- def hide_expander():
122
- st.session_state.expander_toggle = False
123
-
124
- def show_expander():
125
- st.session_state.expander_toggle = True
126
 
127
  # Attributes
128
  st.subheader("Loan Application")
@@ -202,17 +216,13 @@ with st.expander("Application form", expanded=st.session_state.expander_toggle):
202
  ]
203
  ]
204
  }
205
- if "predict_button_clicked" not in st.session_state:
206
- st.session_state.predict_button_clicked = False
207
 
208
- if deployment_token != "my-secret-token":
 
209
  predict_button = st.button(
210
- "Send loan application", key="predict_button", help="Click to get the AI prediction.", on_click=hide_expander
211
  )
212
  if predict_button:
213
- st.session_state.predict_button_clicked = True
214
- st.session_state.evaluation_submitted = False
215
- # st.session_state.selected = "Loan Decision"
216
  with st.spinner("Loading prediction and explanation..."):
217
  # Call the explain endpoint as it also includes the prediction
218
  exp = client.explain(
@@ -220,7 +230,8 @@ if deployment_token != "my-secret-token":
220
  )
221
  st.session_state.exp = exp
222
 
223
- if st.session_state.predict_button_clicked:
 
224
  try:
225
  exp = st.session_state.exp
226
  # Read explanation to dataframe from json
@@ -272,85 +283,89 @@ if st.session_state.predict_button_clicked:
272
  f"{feat}: {neg_exp_df_t.loc[feat, 'Feature value']}"
273
  for feat in neg_feats
274
  ]
275
-
276
- if predictions[0]:
277
- # Show prediction
278
- st.subheader("Loan Decision: :green[Approve]", divider="green")
279
- # Format subheader to green
280
- st.markdown(
281
- "<style>.css-1v3fvcr{color: green;}</style>", unsafe_allow_html=True
282
- )
283
-
284
- # If prediction is positive, first show positive features, then negative features
285
- st.success(
286
- "**Positive creditworthiness**. The most important characteristics in favor of loan approval are: \n - "
287
- + " \n- ".join(pos_feats)
288
- )
289
- st.warning(
290
- "However, the following features weight against the loan applicant: \n - "
291
- + " \n- ".join(neg_feats)
292
- + " \n For more details, see full explanation of the credit assessment below.",
293
- )
294
- else:
295
- st.subheader("Loan Decision: :red[Reject]", divider="red")
296
- # If prediction is negative, first show negative features, then positive features
297
- st.error(
298
- "**Negative creditworthiness**. The most important characteristics in favor of loan rejection are: \n - "
299
- + " \n - ".join(neg_feats)
300
- )
301
- st.warning(
302
- "However, the following factors weigh in favor of the loan applicant: \n - "
303
- + " \n - ".join(pos_feats)
304
- + " \n For more details, see full explanation of the credit assessment below.",
305
- )
306
- explanation_expander = st.expander("Show explanation")
307
- with explanation_expander:
308
- # Show explanation
309
- col_pos, col_neg = st.columns(2)
310
-
311
- with col_pos:
312
- st.subheader("Factors :green[in favor] of loan approval")
313
- # st.success("**Factors in favor of loan approval**")
314
- st.dataframe(
315
- pos_exp_df_t,
316
- hide_index=True,
317
- width=600,
318
- column_config={
319
- "Weight": st.column_config.ProgressColumn(
320
- "Weight",
321
- width="small",
322
- format=" ",
323
- min_value=0,
324
- max_value=1,
325
- )
326
- },
327
  )
328
-
329
- with col_neg:
330
- st.subheader("Factors :red[against] loan approval")
331
- # st.error("**Factors against loan approval**")
332
- st.dataframe(
333
- neg_exp_df_t,
334
- hide_index=True,
335
- width=600,
336
- column_config={
337
- "Weight": st.column_config.ProgressColumn(
338
- "Weight",
339
- width="small",
340
- format=" ",
341
- min_value=0,
342
- max_value=1,
343
- )
344
- },
345
- )
346
-
347
- st.divider()
348
-
349
- if not st.session_state.evaluation_submitted:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
  # Add prediction evaluation
351
  st.subheader("Evaluation: Do you agree with the loan assessment?")
352
- st.info(
353
- "AI model predictions always come with a certain level of uncertainty. \nEvaluate the correctness of the prediction based on your expertise and experience."
354
  )
355
  cols = st.columns(4)
356
  col_yes, col_no = cols[:2]
@@ -372,8 +387,7 @@ if st.session_state.predict_button_clicked:
372
  )
373
  ChangeButtonColour("No, I disagree", "white", "#DD360C")
374
  # ChangeButtonColour("No, I disagree", "#DD360C", "#F0F0F0")
375
- if "eval_selected" not in st.session_state:
376
- st.session_state["eval_selected"] = False
377
  if yes_button:
378
  st.session_state.eval_selected = True
379
  st.session_state.evaluation_input = {
 
98
  if "evaluation_submitted" not in st.session_state:
99
  st.session_state.evaluation_submitted = False
100
 
101
+ if "predict_button_clicked" not in st.session_state:
102
+ st.session_state.predict_button_clicked = False
103
+
104
+ if "eval_selected" not in st.session_state:
105
+ st.session_state["eval_selected"] = False
106
+
107
+ if "exp" not in st.session_state:
108
+ st.session_state.exp = None
109
+
110
+ def set_prediction_view():
111
+ st.session_state.predict_button_clicked = True
112
+ st.session_state.evaluation_submitted = False
113
+ hide_expander()
114
+
115
+ def hide_expander():
116
+ st.session_state.expander_toggle = False
117
+
118
+ def show_expander():
119
+ st.session_state.expander_toggle = True
120
+
121
  def submit_and_clear():
122
  try:
123
  # Call the explain endpoint as it also includes the prediction
124
  client.evaluate(
125
  deployment_id, request_log_id, prediction_log_id, st.session_state.evaluation_input
126
  )
 
127
  st.session_state.eval_selected = False
128
  st.session_state.evaluation_submitted = True
129
  st.session_state.eval_selected = False
130
+ st.session_state.predict_button_clicked = False
131
+ st.session_state.exp = None
132
+ show_expander()
133
  except Exception as e:
134
  logging.error(e)
135
  st.error(
 
137
  + "Check whether you are using the right model URL and token for evaluations. "
138
  + "Contact Deeploy if the problem persists."
139
  )
 
 
 
 
 
 
 
 
140
 
141
  # Attributes
142
  st.subheader("Loan Application")
 
216
  ]
217
  ]
218
  }
 
 
219
 
220
+ # Show predict button if token is set
221
+ if deployment_token != "my-secret-token" and st.session_state.exp is None:
222
  predict_button = st.button(
223
+ "Send loan application", key="predict_button", help="Click to get the AI prediction.", on_click=set_prediction_view
224
  )
225
  if predict_button:
 
 
 
226
  with st.spinner("Loading prediction and explanation..."):
227
  # Call the explain endpoint as it also includes the prediction
228
  exp = client.explain(
 
230
  )
231
  st.session_state.exp = exp
232
 
233
+ # Show prediction and explanation after predict button is clicked
234
+ if st.session_state.predict_button_clicked and st.session_state.exp is not None:
235
  try:
236
  exp = st.session_state.exp
237
  # Read explanation to dataframe from json
 
283
  f"{feat}: {neg_exp_df_t.loc[feat, 'Feature value']}"
284
  for feat in neg_feats
285
  ]
286
+ if not st.session_state.evaluation_submitted:
287
+ if predictions[0]:
288
+ # Show prediction
289
+ st.subheader("Loan Decision: :green[Approve]", divider="green")
290
+ # Format subheader to green
291
+ st.markdown(
292
+ "<style>.css-1v3fvcr{color: green;}</style>", unsafe_allow_html=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  )
294
+ col1, col2 = st.columns(2)
295
+ with col1:
296
+ # If prediction is positive, first show positive features, then negative features
297
+ st.success(
298
+ "The most important characteristics in favor of loan approval are: \n - "
299
+ + " \n- ".join(pos_feats)
300
+ )
301
+ with col2:
302
+ st.warning(
303
+ "However, the following features weight against the loan applicant: \n - "
304
+ + " \n- ".join(neg_feats)
305
+ # + " \n For more details, see full explanation of the credit assessment below.",
306
+ )
307
+ else:
308
+ st.subheader("Loan Decision: :red[Reject]", divider="red")
309
+ col1, col2 = st.columns(2)
310
+ with col1:
311
+ # If prediction is negative, first show negative features, then positive features
312
+ st.error(
313
+ "The most important characteristics in favor of loan rejection are: \n - "
314
+ + " \n - ".join(neg_feats)
315
+ )
316
+ with col2:
317
+ st.warning(
318
+ "However, the following factors weigh in favor of the loan applicant: \n - "
319
+ + " \n - ".join(pos_feats)
320
+ # + " \n For more details, see full explanation of the credit assessment below.",
321
+ )
322
+ explanation_expander = st.expander("Show explanation")
323
+ with explanation_expander:
324
+ # Show explanation
325
+ col_pos, col_neg = st.columns(2)
326
+
327
+ with col_pos:
328
+ st.subheader("Factors :green[in favor] of loan approval")
329
+ # st.success("**Factors in favor of loan approval**")
330
+ st.dataframe(
331
+ pos_exp_df_t,
332
+ hide_index=True,
333
+ width=600,
334
+ column_config={
335
+ "Weight": st.column_config.ProgressColumn(
336
+ "Weight",
337
+ width="small",
338
+ format=" ",
339
+ min_value=0,
340
+ max_value=1,
341
+ )
342
+ },
343
+ )
344
+
345
+ with col_neg:
346
+ st.subheader("Factors :red[against] loan approval")
347
+ # st.error("**Factors against loan approval**")
348
+ st.dataframe(
349
+ neg_exp_df_t,
350
+ hide_index=True,
351
+ width=600,
352
+ column_config={
353
+ "Weight": st.column_config.ProgressColumn(
354
+ "Weight",
355
+ width="small",
356
+ format=" ",
357
+ min_value=0,
358
+ max_value=1,
359
+ )
360
+ },
361
+ )
362
+
363
+ st.divider()
364
+
365
  # Add prediction evaluation
366
  st.subheader("Evaluation: Do you agree with the loan assessment?")
367
+ st.write(
368
+ "AI model predictions always come with a certain level of uncertainty. Evaluate the correctness of the assessment based on your expertise and experience."
369
  )
370
  cols = st.columns(4)
371
  col_yes, col_no = cols[:2]
 
387
  )
388
  ChangeButtonColour("No, I disagree", "white", "#DD360C")
389
  # ChangeButtonColour("No, I disagree", "#DD360C", "#F0F0F0")
390
+
 
391
  if yes_button:
392
  st.session_state.eval_selected = True
393
  st.session_state.evaluation_input = {