decision-policy
import gradio as gr
from decision_policy import BinaryExpectedLossPolicy
def evaluate_transaction(fraud_probability, false_positive_cost, false_negative_cost):
cost_matrix = [
[0, false_positive_cost],
[false_negative_cost, 0]
]
policy = BinaryExpectedLossPolicy(cost_matrix)
decision = policy.predict([fraud_probability])[0]
expected_loss = policy.expected_loss([fraud_probability])[0]
action = "🛑 BLOCK TRANSACTION" if decision == 1 else "✅ ALLOW TRANSACTION"
return action, f"${expected_loss:,.2f}"
with gr.Blocks(title="Web3 Circuit Breaker") as demo:
gr.Markdown("# Web3 Smart Contract Circuit Breaker")
gr.Markdown("Adjust risk probability and financial penalties to see how `decision-policy` computes the optimal threshold.")
with gr.Row():
with gr.Column():
fraud_prob = gr.Slider(0.0, 1.0, value=0.15, label="Model Fraud Probability")
fp_cost = gr.Number(value=50, label="False Positive Cost ($)")
fn_cost = gr.Number(value=10000, label="False Negative Cost ($)")
analyze_btn = gr.Button("Evaluate Action", variant="primary")
with gr.Column():
decision_out = gr.Textbox(label="Optimal Decision")
loss_out = gr.Textbox(label="Expected Financial Loss")
analyze_btn.click(
fn=evaluate_transaction,
inputs=[fraud_prob, fp_cost, fn_cost],
outputs=[decision_out, loss_out]
)
demo.launch()