File size: 10,643 Bytes
b7ed90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

from inference import (
    predict_tx,
    plot_3d_tx_ego,
    neighbor_risk_distribution,
    overall_risk_distribution,
    ego_risk_distribution,
)

# ----------------------------------------------------------
# PAGE SETUP
# ----------------------------------------------------------
st.set_page_config(page_title="Bitcoin Transaction Risk Explorer", layout="wide")

# ----------------------------------------------------------
# BEAUTIFUL UI CSS (animations + modern visuals)
# ----------------------------------------------------------
st.markdown("""

<style>

body {

    font-family: 'Inter', sans-serif;

}

/* MAIN TITLE */

h1 {

    background: linear-gradient(90deg, #00eaff, #6f00ff);

    -webkit-background-clip: text;

    -webkit-text-fill-color: transparent;

    font-weight: 800 !important;

    letter-spacing: -1px;

}

/* SECTION HEADERS */

h3 {

    color: #dff6ff;

    font-weight: 700;

    margin-top: 30px;

}

/* Metric Cards */

.metric-card {

    background: rgba(255, 255, 255, 0.03);

    padding: 16px;

    border-radius: 14px;

    border: 1px solid rgba(0, 200, 255, 0.15);

    backdrop-filter: blur(10px);

    transition: 0.25s ease;

}

.metric-card:hover {

    transform: translateY(-4px);

    box-shadow: 0 8px 25px rgba(0, 200, 255, 0.12);

}

/* Analyze button */

.stButton > button {

    background: linear-gradient(90deg, #00c6ff, #7d2cff);

    color: white;

    padding: 10px 26px;

    border-radius: 12px;

    font-size: 16px;

    font-weight: 700;

    border: none;

    transition: 0.22s ease-in-out;

    box-shadow: 0 0 12px rgba(0, 200, 255, 0.3);

}

.stButton > button:hover {

    transform: scale(1.07);

    box-shadow: 0 0 22px rgba(0, 200, 255, 0.45);

}

/* Sidebar styling */

[data-testid="stSidebar"] {

    background: rgba(255,255,255,0.02);

    border-right: 1px solid rgba(0,200,255,0.1);

}

.sidebar-link {

    color: #4db8ff !important;

    font-weight: 700;

}

/* Text fade-in animation */

@keyframes fadeIn {

    from {opacity: 0; transform: translateY(8px);}

    to {opacity: 1; transform: translateY(0);}

}

.fade {

    animation: fadeIn 0.6s ease forwards;

}

/* Slider */

.stSlider > div > div > div {

    background: linear-gradient(90deg, #00d4ff, #6f00ff) !important;

}

</style>

""", unsafe_allow_html=True)

# ----------------------------------------------------------
# HEADER
# ----------------------------------------------------------
st.markdown("<h1 class='fade'>Bitcoin Transaction Risk Explorer</h1>", unsafe_allow_html=True)

st.markdown("""

<div class="fade" style='color:#b9d6e8; font-size:16px; margin-bottom:25px;'>

AI-powered forensic intelligence. Predict illicit risk, analyze network behavior, and visualize suspicious transaction activity in 3D.

</div>

""", unsafe_allow_html=True)

# ----------------------------------------------------------
# Sidebar helper: attractive bar chart renderer
# ----------------------------------------------------------
def render_sidebar_bar_chart(counts: dict):
    """Render an attractive Plotly bar chart in the sidebar with labels outside (avoid clipping)."""
    if not counts:
        st.sidebar.write("No risk data available.")
        return

    df = pd.DataFrame({
        "Risk": list(counts.keys()),
        "Count": list(counts.values())
    })

    # keep consistent order: Safe, High, Low, Medium (if present)
    desired_order = ["Safe", "High", "Low", "Medium"]
    df['Risk'] = pd.Categorical(df['Risk'], categories=[r for r in desired_order if r in df['Risk'].unique()], ordered=True)
    df = df.sort_values('Risk')

    fig = px.bar(
        df,
        x="Risk",
        y="Count",
        text="Count",
        color="Risk",
        color_discrete_map={
            "Safe": "#4da3ff",
            "High": "#ff2e63",
            "Low": "#00e0c7",
            "Medium": "#ffa41b"
        }
    )

    fig.update_traces(
        texttemplate="%{text:,}",       # format with commas
        textposition="outside",        # move label outside bar to prevent clipping
        cliponaxis=False,
        marker=dict(line=dict(color="rgba(255,255,255,0.55)", width=1.5))
    )

    fig.update_layout(
        height=340,
        margin=dict(l=10, r=10, t=8, b=8),
        yaxis_title="Count",
        xaxis_title="Risk",
        plot_bgcolor="rgba(0,0,0,0)",
        paper_bgcolor="rgba(0,0,0,0)",
        font=dict(color="white"),
    )

    st.sidebar.plotly_chart(fig, use_container_width=True)

# ----------------------------------------------------------
# SIDEBAR — Global Risk Distribution
# ----------------------------------------------------------
st.sidebar.header("📊 Global Risk Overview")

overall_counts = overall_risk_distribution()

# metric cards
if overall_counts:
    total = sum(overall_counts.values())
    for k, v in overall_counts.items():
        st.sidebar.markdown(
            f"""

            <div class='metric-card'>

                <div style="font-size:16px; font-weight:700">{k}</div>

                <div style="font-size:22px; margin-top:6px">

                    {v:,} <span style='color:#9fbcd6; font-size:13px;'>({v/total*100:.1f}%)</span>

                </div>

            </div>

            """,
            unsafe_allow_html=True,
        )

    # Render improved Plotly bar chart (fixes large-label clipping)
    render_sidebar_bar_chart(overall_counts)

else:
    st.sidebar.write("No risk data available.")

st.sidebar.markdown("---")

# Dataset Link
st.sidebar.markdown("""

🔗 **Dataset Source:**  

[Open Bitcoin Fraud Dataset →](hhttps://huggingface.co/datasets/vansh0003/Bitcoin_Dataset/tree/main)

""")

# ----------------------------------------------------------
# INPUTS
# ----------------------------------------------------------
st.subheader("🔍 Analyze a Transaction")

tx_input = st.text_input("Enter transaction ID (txId)", value="78144215")

col_h1, col_h2 = st.columns(2)
with col_h1:
    hops = st.slider("Ego-graph hops", 1, 4, 2)
with col_h2:
    max_nodes = st.slider("Max nodes in ego graph", 50, 800, 400, step=50)

# ----------------------------------------------------------
# ANALYSIS LOGIC
# ----------------------------------------------------------
if st.button("Analyze"):
    try:
        try:
            txid = int(tx_input)
        except:
            txid = tx_input

        info = predict_tx(txid)

        st.markdown("### Prediction Summary")

        c1, c2, c3, c4, c5 = st.columns(5)

        # Metric function
        def card(col, title, value):
            col.markdown(
                f"""

                <div class="metric-card fade">

                    <div style='color:#9fbcd6; font-size:13px;'>{title}</div>

                    <div style='font-size:22px; font-weight:700; margin-top:6px'>{value}</div>

                </div>

                """,
                unsafe_allow_html=True
            )

        card(c1, "Transaction ID", info["txId"])
        card(c2, "Illicit Probability", f"{info['proba_illicit']:.6f}")
        card(c3, "Risk Bucket", info["risk_bucket"])
        card(c4, "Degree", info["degree"])
        card(c5, "Original Class", info["class"])

        st.markdown("---")

        # ----------------------------------------------------
        # Neon Donut Chart (Ego Risk)
        # ----------------------------------------------------
        st.markdown("### 🥧 Ego-Network Risk Composition")

        ego_counts = ego_risk_distribution(txid, hops=hops, max_nodes=max_nodes)

        if ego_counts:
            labels = list(ego_counts.keys())
            values = list(ego_counts.values())

            color_map = {
                "High": "#ff2e63",
                "Medium": "#ffa41b",
                "Low": "#00e0c7",
                "Safe": "#5596ff"
            }
            colors = [color_map.get(r, "#9fbcd6") for r in labels]

            pie_fig = go.Figure(
                data=[go.Pie(
                    labels=labels,
                    values=values,
                    hole=0.55,
                    hoverinfo="label+percent+value",
                    textinfo="percent",
                    textfont=dict(size=14, color="white"),
                    marker=dict(
                        colors=colors,
                        line=dict(color="rgba(255,255,255,0.18)", width=2)
                    ),
                    sort=False
                )]
            )

            pie_fig.update_layout(
                title="Risk Distribution (Ego Network)",
                title_font=dict(color="white"),
                paper_bgcolor="rgba(0,0,0,0)",
                plot_bgcolor="rgba(0,0,0,0)",
                annotations=[
                    dict(
                        text="EGO",
                        x=0.5, y=0.5,
                        font=dict(size=22, color="white"),
                        showarrow=False
                    )
                ],
                margin=dict(l=10, r=10, t=60, b=10),
                showlegend=True,
                legend=dict(
                    orientation="h",
                    yanchor="bottom",
                    y=-0.15,
                    xanchor="center",
                    x=0.5,
                    font=dict(color="#dff8ff")
                )
            )

            st.plotly_chart(pie_fig, use_container_width=True)

        else:
            st.info("No ego-network risk data available.")

        st.markdown("---")

        # ------------------------------------------------------
        # NEIGHBOR RISK
        # ------------------------------------------------------
        st.markdown("### 📌 Neighbor Risk Distribution")
        nb_counts = neighbor_risk_distribution(txid)

        if nb_counts:
            st.bar_chart(pd.Series(nb_counts))
        else:
            st.info("No neighbors found for this transaction.")

        st.markdown("---")

        # ------------------------------------------------------
        # 3D NETWORK GRAPH
        # ------------------------------------------------------
        st.markdown("### 🌐 3D Ego Graph")
        fig = plot_3d_tx_ego(txid, hops=hops, max_nodes=max_nodes)
        st.plotly_chart(fig, use_container_width=True)

    except Exception as e:
        st.error(f"Unexpected error: {e}")