Spaces:
Sleeping
Sleeping
Commit
·
9cb79eb
1
Parent(s):
8870850
Remove submit button
Browse files
app.py
CHANGED
|
@@ -101,9 +101,6 @@ if "evaluation_submitted" not in st.session_state:
|
|
| 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 |
|
|
@@ -118,15 +115,19 @@ def hide_expander():
|
|
| 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()
|
|
@@ -138,6 +139,9 @@ def submit_and_clear():
|
|
| 138 |
+ "Contact Deeploy if the problem persists."
|
| 139 |
)
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
# Attributes
|
| 142 |
st.subheader("Loan Application")
|
| 143 |
with st.expander("Application form", expanded=st.session_state.expander_toggle):
|
|
@@ -230,8 +234,11 @@ if deployment_token != "my-secret-token" and st.session_state.exp is None:
|
|
| 230 |
)
|
| 231 |
st.session_state.exp = exp
|
| 232 |
|
|
|
|
|
|
|
|
|
|
| 233 |
# Show prediction and explanation after predict button is clicked
|
| 234 |
-
|
| 235 |
try:
|
| 236 |
exp = st.session_state.exp
|
| 237 |
# Read explanation to dataframe from json
|
|
@@ -283,90 +290,95 @@ if st.session_state.predict_button_clicked and st.session_state.exp is not None:
|
|
| 283 |
f"{feat}: {neg_exp_df_t.loc[feat, 'Feature value']}"
|
| 284 |
for feat in neg_feats
|
| 285 |
]
|
| 286 |
-
if
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
)
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 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]
|
| 372 |
with col_yes:
|
|
@@ -375,8 +387,11 @@ if st.session_state.predict_button_clicked and st.session_state.exp is not None:
|
|
| 375 |
key="yes_button",
|
| 376 |
use_container_width=True,
|
| 377 |
help="Click if you agree with the prediction",
|
|
|
|
|
|
|
| 378 |
)
|
| 379 |
ChangeButtonColour("Yes, I agree", "white", "green")
|
|
|
|
| 380 |
with col_no:
|
| 381 |
no_button = st.button(
|
| 382 |
"No, I disagree",
|
|
@@ -384,33 +399,10 @@ if st.session_state.predict_button_clicked and st.session_state.exp is not None:
|
|
| 384 |
use_container_width=True,
|
| 385 |
help="Click if you disagree with the prediction",
|
| 386 |
type="primary",
|
|
|
|
|
|
|
| 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 = {
|
| 394 |
-
"result": 0 # Agree with the prediction
|
| 395 |
-
}
|
| 396 |
-
st.session_state.placeholder = "Income is sufficient, given applicant's background"
|
| 397 |
-
if no_button:
|
| 398 |
-
st.session_state.eval_selected = True
|
| 399 |
-
desired_output = not predictions[0]
|
| 400 |
-
st.session_state.evaluation_input = {
|
| 401 |
-
"result": 1, # Disagree with the prediction
|
| 402 |
-
"value": {"predictions": [desired_output]},
|
| 403 |
-
}
|
| 404 |
-
st.session_state.placeholder = "Income is too low, given applicant's background"
|
| 405 |
-
|
| 406 |
-
if st.session_state.eval_selected:
|
| 407 |
-
comment = st.text_input("Would you like to add a comment?", placeholder="For example: '" + st.session_state.placeholder + "'")
|
| 408 |
-
if comment:
|
| 409 |
-
st.session_state.evaluation_input["explanation"] = comment
|
| 410 |
-
|
| 411 |
-
st.button("Submit", key="submit_button", on_click=submit_and_clear)
|
| 412 |
-
else:
|
| 413 |
-
st.success("Evaluation submitted successfully!")
|
| 414 |
|
| 415 |
except Exception as e:
|
| 416 |
logging.error(e)
|
|
|
|
| 101 |
if "predict_button_clicked" not in st.session_state:
|
| 102 |
st.session_state.predict_button_clicked = False
|
| 103 |
|
|
|
|
|
|
|
|
|
|
| 104 |
if "exp" not in st.session_state:
|
| 105 |
st.session_state.exp = None
|
| 106 |
|
|
|
|
| 115 |
def show_expander():
|
| 116 |
st.session_state.expander_toggle = True
|
| 117 |
|
| 118 |
+
def submit_and_clear(evaluation: str):
|
| 119 |
+
if evaluation == "yes":
|
| 120 |
+
st.session_state.evaluation_input["result"] = 0 # Agree with the prediction
|
| 121 |
+
else:
|
| 122 |
+
desired_output = not predictions[0]
|
| 123 |
+
st.session_state.evaluation_input["result"] = 1
|
| 124 |
+
st.session_state.evaluation_input["value"] = {"predictions": [desired_output]}
|
| 125 |
try:
|
| 126 |
# Call the explain endpoint as it also includes the prediction
|
| 127 |
client.evaluate(
|
| 128 |
deployment_id, request_log_id, prediction_log_id, st.session_state.evaluation_input
|
| 129 |
)
|
|
|
|
| 130 |
st.session_state.evaluation_submitted = True
|
|
|
|
| 131 |
st.session_state.predict_button_clicked = False
|
| 132 |
st.session_state.exp = None
|
| 133 |
show_expander()
|
|
|
|
| 139 |
+ "Contact Deeploy if the problem persists."
|
| 140 |
)
|
| 141 |
|
| 142 |
+
# with st.expander("Debug session state", expanded=False):
|
| 143 |
+
# st.write(st.session_state)
|
| 144 |
+
|
| 145 |
# Attributes
|
| 146 |
st.subheader("Loan Application")
|
| 147 |
with st.expander("Application form", expanded=st.session_state.expander_toggle):
|
|
|
|
| 234 |
)
|
| 235 |
st.session_state.exp = exp
|
| 236 |
|
| 237 |
+
if st.session_state.evaluation_submitted:
|
| 238 |
+
st.success("Evaluation submitted successfully!")
|
| 239 |
+
|
| 240 |
# Show prediction and explanation after predict button is clicked
|
| 241 |
+
elif st.session_state.predict_button_clicked and st.session_state.exp is not None:
|
| 242 |
try:
|
| 243 |
exp = st.session_state.exp
|
| 244 |
# Read explanation to dataframe from json
|
|
|
|
| 290 |
f"{feat}: {neg_exp_df_t.loc[feat, 'Feature value']}"
|
| 291 |
for feat in neg_feats
|
| 292 |
]
|
| 293 |
+
if predictions[0]:
|
| 294 |
+
# Show prediction
|
| 295 |
+
st.subheader("Loan Decision: :green[Approve]", divider="green")
|
| 296 |
+
# Format subheader to green
|
| 297 |
+
st.markdown(
|
| 298 |
+
"<style>.css-1v3fvcr{color: green;}</style>", unsafe_allow_html=True
|
| 299 |
+
)
|
| 300 |
+
col1, col2 = st.columns(2)
|
| 301 |
+
with col1:
|
| 302 |
+
# If prediction is positive, first show positive features, then negative features
|
| 303 |
+
st.success(
|
| 304 |
+
"The most important characteristics in favor of loan approval are: \n - "
|
| 305 |
+
+ " \n- ".join(pos_feats)
|
| 306 |
+
)
|
| 307 |
+
with col2:
|
| 308 |
+
st.warning(
|
| 309 |
+
"However, the following features weight against the loan applicant: \n - "
|
| 310 |
+
+ " \n- ".join(neg_feats)
|
| 311 |
+
# + " \n For more details, see full explanation of the credit assessment below.",
|
| 312 |
+
)
|
| 313 |
+
else:
|
| 314 |
+
st.subheader("Loan Decision: :red[Reject]", divider="red")
|
| 315 |
+
col1, col2 = st.columns(2)
|
| 316 |
+
with col1:
|
| 317 |
+
# If prediction is negative, first show negative features, then positive features
|
| 318 |
+
st.error(
|
| 319 |
+
"The most important characteristics in favor of loan rejection are: \n - "
|
| 320 |
+
+ " \n - ".join(neg_feats)
|
| 321 |
+
)
|
| 322 |
+
with col2:
|
| 323 |
+
st.warning(
|
| 324 |
+
"However, the following factors weigh in favor of the loan applicant: \n - "
|
| 325 |
+
+ " \n - ".join(pos_feats)
|
| 326 |
+
# + " \n For more details, see full explanation of the credit assessment below.",
|
| 327 |
)
|
| 328 |
+
explanation_expander = st.expander("Show explanation")
|
| 329 |
+
with explanation_expander:
|
| 330 |
+
# Show explanation
|
| 331 |
+
col_pos, col_neg = st.columns(2)
|
| 332 |
+
|
| 333 |
+
with col_pos:
|
| 334 |
+
st.subheader("Factors :green[in favor] of loan approval")
|
| 335 |
+
# st.success("**Factors in favor of loan approval**")
|
| 336 |
+
st.dataframe(
|
| 337 |
+
pos_exp_df_t,
|
| 338 |
+
hide_index=True,
|
| 339 |
+
width=600,
|
| 340 |
+
column_config={
|
| 341 |
+
"Weight": st.column_config.ProgressColumn(
|
| 342 |
+
"Weight",
|
| 343 |
+
width="small",
|
| 344 |
+
format=" ",
|
| 345 |
+
min_value=0,
|
| 346 |
+
max_value=1,
|
| 347 |
+
)
|
| 348 |
+
},
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
with col_neg:
|
| 352 |
+
st.subheader("Factors :red[against] loan approval")
|
| 353 |
+
# st.error("**Factors against loan approval**")
|
| 354 |
+
st.dataframe(
|
| 355 |
+
neg_exp_df_t,
|
| 356 |
+
hide_index=True,
|
| 357 |
+
width=600,
|
| 358 |
+
column_config={
|
| 359 |
+
"Weight": st.column_config.ProgressColumn(
|
| 360 |
+
"Weight",
|
| 361 |
+
width="small",
|
| 362 |
+
format=" ",
|
| 363 |
+
min_value=0,
|
| 364 |
+
max_value=1,
|
| 365 |
+
)
|
| 366 |
+
},
|
| 367 |
+
)
|
| 368 |
+
|
| 369 |
+
st.divider()
|
| 370 |
+
|
| 371 |
+
if not st.session_state.evaluation_submitted:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
# Add prediction evaluation
|
| 373 |
st.subheader("Evaluation: Do you agree with the loan assessment?")
|
| 374 |
st.write(
|
| 375 |
"AI model predictions always come with a certain level of uncertainty. Evaluate the correctness of the assessment based on your expertise and experience."
|
| 376 |
)
|
| 377 |
+
st.session_state.evaluation_input = {}
|
| 378 |
+
comment = st.text_input("Your assessment:", placeholder="For example: 'Income is too low, given applicant's background'")
|
| 379 |
+
if comment:
|
| 380 |
+
st.session_state.evaluation_input["explanation"] = comment
|
| 381 |
+
|
| 382 |
cols = st.columns(4)
|
| 383 |
col_yes, col_no = cols[:2]
|
| 384 |
with col_yes:
|
|
|
|
| 387 |
key="yes_button",
|
| 388 |
use_container_width=True,
|
| 389 |
help="Click if you agree with the prediction",
|
| 390 |
+
on_click=submit_and_clear,
|
| 391 |
+
args=["yes"]
|
| 392 |
)
|
| 393 |
ChangeButtonColour("Yes, I agree", "white", "green")
|
| 394 |
+
|
| 395 |
with col_no:
|
| 396 |
no_button = st.button(
|
| 397 |
"No, I disagree",
|
|
|
|
| 399 |
use_container_width=True,
|
| 400 |
help="Click if you disagree with the prediction",
|
| 401 |
type="primary",
|
| 402 |
+
on_click=submit_and_clear,
|
| 403 |
+
args=["no"]
|
| 404 |
)
|
| 405 |
+
ChangeButtonColour("No, I disagree", "white", "#DD360C") # Red color for disagree button
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
except Exception as e:
|
| 408 |
logging.error(e)
|