File size: 4,963 Bytes
6ca4b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "marimo",
#     "matplotlib==3.10.1",
#     "numpy==2.2.4",
# ]
# ///

import marimo

__generated_with = "0.14.10"
app = marimo.App(css_file="theme.marimo.css", html_head_file="")


@app.cell
def _():
    import marimo as mo
    return (mo,)


@app.cell
def _(mo):
    get_applicant_count, set_applicant_count = mo.state(500)
    return get_applicant_count, set_applicant_count


@app.cell
def _(get_applicant_count, mo, set_applicant_count):
    applicant_count = mo.ui.number(
        start=1,
        step=1,
        value=get_applicant_count(),
        full_width=True,
        label="Number of applicants",
        on_change=set_applicant_count,
    )
    return (applicant_count,)


@app.cell
def _(get_applicant_count, mo):
    hire_count = mo.ui.number(
        start=1,
        stop=get_applicant_count() - 1,
        step=1,
        value=75,
        full_width=True,
        label="Hire count",
    )
    return (hire_count,)


@app.cell
def _(mo):
    base_rate = mo.ui.slider(
        value=40, start=1, stop=100, step=1, full_width=True, label="Base rate"
    )
    return (base_rate,)


@app.cell
def _(mo):
    validity = mo.ui.slider(
        value=0.18, start=0.01, stop=1.00, step=0.01, full_width=True, label="Validity"
    )
    return (validity,)


@app.cell
def _(applicant_count, base_rate, hire_count, validity):
    import requests

    url = "http://localhost:7860/api/v1/demo/r/demo"
    params = {
        "applicant_count": applicant_count.value,
        "hire_count": hire_count.value,
        "base_rate": base_rate.value,
        "validity": validity.value,
    }
    headers = {"accept": "application/json"}

    response = requests.post(url, params=params, headers=headers)

    return (response,)


@app.cell
def _(response):
    import pandas as pd
    import plotly.graph_objects as go
    from plotly.subplots import make_subplots

    json_data = response.json()

    data = pd.DataFrame(
        {
            "var": list(json_data.keys()),
            "label": [
                "Rightfully hired",
                "Incorrectly hired",
                "Rightfully rejected",
                "Incorrectly rejected",
            ],
            "value": list(json_data.values()),
        }
    )

    data["label_with_value"] = data["label"] + " (" + data["value"].astype(str) + ")"

    fig = make_subplots(
        rows=1,
        cols=2,
        specs=[[{"type": "pie"}, {"type": "pie"}]],
        subplot_titles=("Hiring Outcome", "Rejection Outcome"),
        horizontal_spacing=0.1,
        vertical_spacing=0.15,
    )

    hired_data = data[data["var"].isin(["true_positives", "false_positives"])]
    hired_values = hired_data["value"].tolist()
    hired_labels = hired_data["label_with_value"].tolist()

    rejected_data = data[data["var"].isin(["true_negatives", "false_negatives"])]
    rejected_values = rejected_data["value"].tolist()
    rejected_labels = rejected_data["label_with_value"].tolist()

    fig.add_trace(
        go.Pie(
            values=hired_values,
            labels=hired_labels,
            hole=0.5,
            marker_colors=["#6b71ed", "#403c5d"],
            name="Hiring",
            legendgroup="hiring",
            showlegend=True,
            hoverinfo="none",
        ),
        row=1,
        col=1,
    )

    fig.add_trace(
        go.Pie(
            values=rejected_values,
            labels=rejected_labels,
            hole=0.5,
            marker_colors=["#6b71ed", "#403c5d"],
            name="Rejection",
            legendgroup="rejection",
            showlegend=True,
            hoverinfo="none",
        ),
        row=1,
        col=2,
    )

    fig.update_layout(
        autosize=True,
        margin=dict(l=0, r=0, t=60, b=20),
        legend=dict(
            orientation="h",
            yanchor="top",
            y=-0.1,
            xanchor="center",
            x=0.5,
            font=dict(size=12),
        ),
        title=dict(text=None, x=0.5, font=dict(size=16)),
        height=400,
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
    )

    fig.update_annotations(font_size=14, y=1.08)

    return (fig,)


@app.cell
def _(applicant_count, base_rate, hire_count, mo, validity):
    with mo.status.spinner(subtitle="Loading data ...") as _spinner:
        mo.vstack([applicant_count, hire_count, base_rate, validity])
    return


@app.cell
def _(applicant_count, base_rate, hire_count, mo, validity):
    mo.vstack([applicant_count, hire_count, base_rate, validity])
    return


@app.cell
def _(fig, mo, response):
    if response.status_code == 200:
        fig.show(
            config={"displayModeBar": False, "staticPlot": True, "responsive": True}
        )
    else:
        error_msg = mo.md(f"{mo.icon('lucide:ban')} Error in server response")
        mo.output.append(error_msg)
    return


if __name__ == "__main__":
    app.run()