File size: 5,306 Bytes
a086192
 
4071be5
 
0df0d2a
 
 
 
a086192
4071be5
 
 
 
0df0d2a
 
bf55023
0df0d2a
 
 
4071be5
0df0d2a
 
 
4071be5
 
 
 
 
 
 
 
 
 
 
 
 
bf55023
4071be5
 
 
0df0d2a
 
 
 
 
 
 
 
4071be5
0df0d2a
bf55023
 
 
 
 
 
 
0df0d2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4071be5
0df0d2a
4071be5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import streamlit as st
import pandas as pd
import logging
from deeploy import Client
from utils import get_request_body, get_fake_certainty, get_model_url, get_random_suspicious_transaction
from utils import get_explainability_texts, get_explainability_values
from utils import COL_NAMES, feature_texts
from visual_components import create_data_input_table, create_table, ChangeButtonColour

logging.basicConfig(level=logging.INFO)

st.set_page_config(layout="wide")

data = pd.read_pickle("data/preprocessed_data.pkl")
# data = data.drop('isFraud', axis=1)

    # Disable the submit button after it is clicked
def disable():
    st.session_state.disabled = True

# Initialize disabled for form_submit_button to False
if "disabled" not in st.session_state:
    st.session_state.disabled = False

st.markdown("""
    <style>
        [data-testid=stSidebar] {
            background-color: #E0E0E0; ##E5E6EA
        }
    </style>
    """, unsafe_allow_html=True)

with st.sidebar:
    # Add deeploy logo
    st.image("deeploy_logo.png", width=270)
    # Ask for model URL and token
    host = st.text_input("Host (changing is optional)", "app.deeploy.ml")
    model_url, workspace_id, deployment_id = get_model_url()
    deployment_token = st.text_input("Deeploy Model Token", "my-secret-token")
    if deployment_token == "my-secret-token":
        st.warning(
            "Please enter Deeploy API token."
        )
    else:
        st.button("Get suspicious transaction", key="predict_button", help="Click to get a suspicious transaction", use_container_width=True,  on_click=disable, disabled=st.session_state.disabled
    ) #on_click=lambda: st.experimental_rerun()
        ChangeButtonColour("Get suspicious transaction", '#FFFFFF', "#00052D")#'#FFFFFF', "#00052D"
    

    # define client optsions and instantiate client
    client_options = {
    "host": host,
    "deployment_token": deployment_token,
    "workspace_id": workspace_id,
    }
    client = Client(**client_options)

    # st.text(client_options)
    # st.text(deployment_id)

if "predict_button" not in st.session_state:
    st.session_state.predict_button = False
    st.title("Money Laundering System")
    st.divider()
    st.info(
    "Fill in left hand side and click on button to observe a potential fraudulent transaction"
    )
if st.session_state.predict_button:
    try:
        with st.spinner("Loading..."):
            datapoint_pd = get_random_suspicious_transaction(data)
            request_body = get_request_body(datapoint_pd)
        # Call the explain endpoint as it also includes the prediction
            exp = client.explain(request_body=request_body, deployment_id=deployment_id)
            shap_values = exp['explanations'][0]['shap_values']
          
        
        col1, col2 = st.columns(2)

        with col1:

            create_data_input_table(datapoint_pd, COL_NAMES)
        with col2:
            certainty = get_fake_certainty()
            st.metric(label='#### Model Certainty', value=certainty)

            explainability_texts, sorted_indices = get_explainability_texts(shap_values, feature_texts)
            explainability_values = get_explainability_values(sorted_indices, datapoint_pd)
            create_table(explainability_texts, explainability_values, 'Important Suspicious Variables: ')

        
        st.divider()

    
                # Add prediction evaluation
        st.subheader("Prediction Evaluation")
        st.write("Do you agree with the prediction?")

        yes_button = st.button("Yes :thumbsup:", key="yes_button")
        if 'eval_selected' not in st.session_state:
            st.session_state['eval_selected'] = False
        if yes_button:
            st.session_state.eval_selected = True
            st.session_state.evaluation_input = {
                "result": 0 # Agree with the prediction
            }
        no_button = st.button("No :thumbsdown:", key="no_button")
        if no_button:
            st.session_state.eval_selected = True
            # desired_output = not predictions[0]
            # st.session_state.evaluation_input = {
            #     "result": 1, # Disagree with the prediction
            #     "value": {"predictions": [desired_output]},
            # }

        success = False
        if st.session_state.eval_selected:
            comment = st.text_input("Would you like to add a comment?")
            if comment:
                st.session_state.evaluation_input["explanation"] = comment
            logging.debug("Selected feedback:" + str(st.session_state.evaluation_input))
            if st.button("Submit", key="submit_button"):
                st.session_state.eval_selected = False
                # success = send_evaluation(client, deployment_id, request_log_id, prediction_log_id, st.session_state.evaluation_input)
        if success:
            st.session_state.eval_selected = False
            st.success("Feedback submitted successfully.")
        
   


    
    except Exception as e:
        logging.error(e)
        st.error(
            "Failed to retrieve the prediction or explanation."
            + "Check whether you are using the right model URL and Token. "
            + "Contact Deeploy if the problem persists."
        )





st.session_state.successful_call = False