import gradio as gr import plotly.graph_objects as go import pandas as pd import numpy as np import json import spaces # Load the two exported dashboard files spend = pd.read_csv("spend.csv").squeeze("columns") headline = json.load(open("headline.json")) def gini(x): x = np.sort(np.asarray(x, dtype=float)) n = len(x) if n == 0 or x.sum() == 0: return float("nan") i = np.arange(1, n + 1) return (2 * np.sum(i * x) / (n * x.sum())) - (n + 1) / n def lorenz(x): x = np.sort(np.asarray(x, dtype=float)) cum = np.cumsum(x) cum_share = np.insert(cum / cum[-1], 0, 0.0) cum_pop = np.insert( np.arange(1, len(x) + 1) / len(x), 0, 0.0 ) return cum_pop, cum_share G = gini(spend) def lorenz_fig(): pop, share = lorenz(spend) figure = go.Figure() figure.add_trace( go.Scatter( x=pop, y=share, name="Actual", fill="tozeroy" ) ) figure.add_trace( go.Scatter( x=[0, 1], y=[0, 1], name="Equality", line=dict(dash="dash") ) ) figure.update_layout( title=f"Spend concentration (Gini {G:.2f})", xaxis_title="Share of patients", yaxis_title="Share of dollars", margin=dict(l=10, r=10, t=40, b=10), height=320 ) return figure def kpis(): ratio = ( headline["mean_spend"] / max(headline["median_spend"], 1) ) return ( f"Median patient: USD {headline['median_spend']:,}\n" f"Mean patient: USD {headline['mean_spend']:,} " f"({ratio:.1f}x median)\n" f"Top 10% drive: " f"{headline['top10_pct_of_dollars']}% of all dollars\n" f"Top-decile line: USD " f"{headline['top_decile_cutoff']:,}\n" f"This week's exemplar: USD " f"{headline['patient_spend']:,} " f"({headline['patient_percentile']}th percentile)" ) @spaces.GPU def refresh_dashboard(): return kpis(), lorenz_fig() with gr.Blocks() as demo: gr.Markdown("# Healthcare Spend - 30-Second Decision View") with gr.Row(): numbers = gr.Textbox( value=kpis(), label="The numbers that change the decision", lines=6 ) concentration = gr.Plot( value=lorenz_fig(), label="Concentration" ) refresh = gr.Button("Refresh dashboard") refresh.click( fn=refresh_dashboard, outputs=[numbers, concentration] ) gr.Markdown( "*Design rule: if it is not on this screen, " "it is not a headline. Detail lives one click deeper.*" ) demo.launch()