File size: 4,436 Bytes
a086192
 
4071be5
 
 
 
a086192
4071be5
 
 
 
 
bf55023
 
4071be5
 
 
 
bf55023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4071be5
 
bf55023
4071be5
bf55023
 
4071be5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf55023
4071be5
 
 
 
 
bf55023
 
 
 
 
 
 
 
4071be5
 
 
 
 
 
 
a086192
 
 
fd1241f
 
 
 
 
 
 
 
a086192
fd1241f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4071be5
fd1241f
a086192
 
fd1241f
a086192
 
 
4071be5
a086192
 
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
import streamlit as st
import pandas as pd
import logging
from deeploy import Client
from utils import ChangeButtonColour
from utils import get_input_values, get_texts, feature_texts, example_input, response, first_five_posneg_indices

logging.basicConfig(level=logging.INFO)

st.set_page_config(layout="wide")

st.title("Observing potential fraudulent transactions")
st.divider()

st.write(
    "Fill in left hand side and click on button to observe a potential fraudulent transaction"
)

def send_evaluation(client, deployment_id, request_log_id, prediction_log_id, evaluation_input):
    """Send evaluation to Deeploy."""
    try:
        with st.spinner("Submitting response..."):
            # Call the explain endpoint as it also includes the prediction
            client.evaluate(deployment_id, request_log_id, prediction_log_id, evaluation_input)
        return True
    except Exception as e:
        logging.error(e)
        st.error(
            "Failed to submit feedback."
            + "Check whether you are using the right model URL and Token. "
            + "Contact Deeploy if the problem persists."
        )
        st.write(f"Error message: {e}")


def get_model_url():
    """Get model url and retrieve workspace id and deployment id from it"""
    model_url = st.text_area(
        "Model URL (default is the demo deployment)",
        "https://api.app.deeploy.ml/workspaces/708b5808-27af-461a-8ee5-80add68384c7/deployments/1785f8b8-c5a6-4f55-9a83-df8bdb0b9977/",
        height=125,
    )
    elems = model_url.split("/")
    try:
        workspace_id = elems[4]
        deployment_id = elems[6]
    except IndexError:
        workspace_id = ""
        deployment_id = ""
    return model_url, workspace_id, deployment_id

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":
        button_clicked = st.button("Get suspicious transaction", key="get1", help="Click to get a suspicious transaction", use_container_width=True, on_click=lambda: st.experimental_rerun())

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


ChangeButtonColour("Get suspicious transaction", '#FFFFFF', "#00052D")#'#FFFFFF', "#00052D"


positive_and_negative_indices = first_five_posneg_indices(response)
positive_texts, negative_texts = get_texts(positive_and_negative_indices, feature_texts)
positive_vals, negative_vals = get_input_values(positive_and_negative_indices, example_input)

# Create a function to generate a table
def create_table(texts, values, title):
    # TODO: change color dataframe header -> tried many different options but none worked see below
    # header_style = '''
    # <style>
    #     th{
    #         background-color: #00052D;
    #     }
    # </style>
    # '''
    df = pd.DataFrame({"Feature Explanation": texts, 'Value': values})
    # df = df.style.set_properties(**{
    #        'selector': 'th',
    #        'props': [
    #            ('background-color', 'black'),
    #            ('color', 'cyan')]
    #    })
    # df = df.style.set_properties(**{'background-color': 'black',
    #                        'color': 'green'})

    # headers = {
    #     'selector': 'th',
    #     'props': [('background-color', '#67c5a4')]#'background-color: #000066; color: white;'
    #     }
    # df = df.style.set_table_styles([headers])
    st.markdown(f'#### {title}')  # Markdown for styling
    st.dataframe(df, hide_index=True)  # Display a simple table
    # st.markdown(header_style, unsafe_allow_html=True)

# Arrange tables horizontally using Streamlit columns
col1, col2 = st.columns(2,gap="small")

# Display tables in Streamlit columns
with col1:
    create_table(positive_texts, positive_vals, 'Important Suspicious Variables')

with col2:
    create_table(negative_texts, negative_vals, 'Important Unsuspicious Variables')