deeploy-adubowski commited on
Commit
0be1b9d
·
1 Parent(s): 9dfefb7

Fix session_state issues

Browse files
Files changed (1) hide show
  1. app.py +34 -72
app.py CHANGED
@@ -37,27 +37,6 @@ st.markdown(
37
  )
38
 
39
 
40
- def send_evaluation(
41
- client, deployment_id, request_log_id, prediction_log_id, evaluation_input
42
- ):
43
- """Send evaluation to Deeploy."""
44
- try:
45
- with st.spinner("Submitting evaluation..."):
46
- # Call the explain endpoint as it also includes the prediction
47
- client.evaluate(
48
- deployment_id, request_log_id, prediction_log_id, evaluation_input
49
- )
50
- return True
51
- except Exception as e:
52
- logging.error(e)
53
- st.error(
54
- "Failed to submit feedback."
55
- + "Check whether you are using the right model URL and token for evaluations. "
56
- + "Contact Deeploy if the problem persists."
57
- )
58
- st.write(f"Error message: {e}")
59
-
60
-
61
  def get_model_url():
62
  model_url = st.text_area(
63
  "Model URL (without the /explain endpoint, default is the demo deployment)",
@@ -112,10 +91,27 @@ with st.sidebar:
112
  }
113
  client = Client(**client_options)
114
 
115
- # with tabs[0] as loan_application:
116
- if "selected" not in st.session_state:
117
- st.session_state.selected = "Loan Application"
118
- # if selected == "Loan Application":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  # Attributes
120
  st.subheader("Loan Application")
121
  with st.expander("Application form", expanded=False):
@@ -202,18 +198,17 @@ if deployment_token != "my-secret-token":
202
  )
203
  if predict_button:
204
  st.session_state.predict_button_clicked = True
205
- st.session_state.selected = "Loan Decision"
206
-
207
-
208
- if st.session_state.selected == "Loan Decision":
209
- # If no prediction, show "loading..."
210
- # FIXME: Cleanup session state so that it's not sending a request to the explain endpoint each time
211
- try:
212
  with st.spinner("Loading prediction and explanation..."):
213
  # Call the explain endpoint as it also includes the prediction
214
  exp = client.explain(
215
  request_body=request_body, deployment_id=deployment_id
216
  )
 
 
 
 
 
217
  # Read explanation to dataframe from json
218
  predictions = exp["predictions"]
219
  request_log_id = exp["requestLogId"]
@@ -362,13 +357,14 @@ if st.session_state.selected == "Loan Decision":
362
  )
363
  ChangeButtonColour("No, I disagree", "white", "#DD360C")
364
  # ChangeButtonColour("No, I disagree", "#DD360C", "#F0F0F0")
365
- st.session_state["eval_selected"] = False
 
366
  if yes_button:
367
  st.session_state.eval_selected = True
368
  st.session_state.evaluation_input = {
369
  "result": 0 # Agree with the prediction
370
  }
371
- placeholder = "Income is sufficient, given applicant's background"
372
  if no_button:
373
  st.session_state.eval_selected = True
374
  desired_output = not predictions[0]
@@ -376,47 +372,14 @@ if st.session_state.selected == "Loan Decision":
376
  "result": 1, # Disagree with the prediction
377
  "value": {"predictions": [desired_output]},
378
  }
379
- placeholder = "Income is too low, given applicant's background"
380
 
381
- if 'success' not in st.session_state:
382
- st.session_state['success'] = False
383
  if st.session_state.eval_selected:
384
- comment = st.text_input("Would you like to add a comment?", placeholder=placeholder)
385
  if comment:
386
  st.session_state.evaluation_input["explanation"] = comment
387
- logging.debug(
388
- "Selected feedback:" + str(st.session_state.evaluation_input)
389
- )
390
- success = True
391
- print(success)
392
- if st.button("Submit", key="submit_button"):
393
- with st.spinner("Submitting evaluation..."):
394
- try:
395
- # Call the explain endpoint as it also includes the prediction
396
- client.evaluate(
397
- deployment_id, request_log_id, prediction_log_id, evaluation_input
398
- )
399
- except Exception as e:
400
- success = False
401
- logging.error(e)
402
- st.error(
403
- "Failed to submit feedback."
404
- + "Check whether you are using the right model URL and token for evaluations. "
405
- + "Contact Deeploy if the problem persists."
406
- )
407
- st.write(f"Error message: {e}")
408
- print(success)
409
- if success:
410
- st.session_state.success = True
411
- else:
412
- st.error("Evaluation failed. Check whether your token allows for evaluations.")
413
- # st.session_state.eval_selected = False
414
- # yes_button = False
415
- # no_button = False
416
- # print(st.session_state.success)
417
- # print(st.session_state.success)
418
- if st.session_state.success:
419
- st.success("Feedback submitted successfully.")
420
 
421
  except Exception as e:
422
  logging.error(e)
@@ -425,4 +388,3 @@ if st.session_state.selected == "Loan Decision":
425
  + "Check whether you are using the right model URL and Token. "
426
  + "Contact Deeploy if the problem persists."
427
  )
428
- # st.write(f"Error message: {e}")
 
37
  )
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def get_model_url():
41
  model_url = st.text_area(
42
  "Model URL (without the /explain endpoint, default is the demo deployment)",
 
91
  }
92
  client = Client(**client_options)
93
 
94
+
95
+ def submit_and_clear():
96
+ try:
97
+ # Call the explain endpoint as it also includes the prediction
98
+ client.evaluate(
99
+ deployment_id, request_log_id, prediction_log_id, st.session_state.evaluation_input
100
+ )
101
+ st.toast(":green[Feedback submitted successfully.]", icon="✅")
102
+ st.session_state.eval_selected = False
103
+ st.session_state.predict_button_clicked = False
104
+ st.session_state.eval_selected = False
105
+ except Exception as e:
106
+ logging.error(e)
107
+ st.error(
108
+ "Failed to submit feedback."
109
+ + "Check whether you are using the right model URL and token for evaluations. "
110
+ + "Contact Deeploy if the problem persists."
111
+ )
112
+ st.toast(f"Failed to submit feedback: {e}")
113
+
114
+
115
  # Attributes
116
  st.subheader("Loan Application")
117
  with st.expander("Application form", expanded=False):
 
198
  )
199
  if predict_button:
200
  st.session_state.predict_button_clicked = True
201
+ # st.session_state.selected = "Loan Decision"
 
 
 
 
 
 
202
  with st.spinner("Loading prediction and explanation..."):
203
  # Call the explain endpoint as it also includes the prediction
204
  exp = client.explain(
205
  request_body=request_body, deployment_id=deployment_id
206
  )
207
+ st.session_state.exp = exp
208
+
209
+ if st.session_state.predict_button_clicked:
210
+ try:
211
+ exp = st.session_state.exp
212
  # Read explanation to dataframe from json
213
  predictions = exp["predictions"]
214
  request_log_id = exp["requestLogId"]
 
357
  )
358
  ChangeButtonColour("No, I disagree", "white", "#DD360C")
359
  # ChangeButtonColour("No, I disagree", "#DD360C", "#F0F0F0")
360
+ if "eval_selected" not in st.session_state:
361
+ st.session_state["eval_selected"] = False
362
  if yes_button:
363
  st.session_state.eval_selected = True
364
  st.session_state.evaluation_input = {
365
  "result": 0 # Agree with the prediction
366
  }
367
+ st.session_state.placeholder = "Income is sufficient, given applicant's background"
368
  if no_button:
369
  st.session_state.eval_selected = True
370
  desired_output = not predictions[0]
 
372
  "result": 1, # Disagree with the prediction
373
  "value": {"predictions": [desired_output]},
374
  }
375
+ st.session_state.placeholder = "Income is too low, given applicant's background"
376
 
 
 
377
  if st.session_state.eval_selected:
378
+ comment = st.text_input("Would you like to add a comment?", placeholder=st.session_state.placeholder)
379
  if comment:
380
  st.session_state.evaluation_input["explanation"] = comment
381
+
382
+ st.button("Submit", key="submit_button", on_click=submit_and_clear)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
 
384
  except Exception as e:
385
  logging.error(e)
 
388
  + "Check whether you are using the right model URL and Token. "
389
  + "Contact Deeploy if the problem persists."
390
  )