import gradio as gr import pandas as pd import random # ----------------------------- # Deterministic Seed # ----------------------------- SEED = 42 # ----------------------------- # Core Simulation # ----------------------------- def generate_demo(n): random.seed(SEED) age_groups = ["young", "middle", "elderly"] ethnicities = ["African", "South Asian", "Caucasian"] comorbidity_sets = [ "none", "diabetes", "diabetes+hypertension", "diabetes+hypertension+ckd" ] data = [] for i in range(n): age_group = random.choice(age_groups) ethnicity = random.choice(ethnicities) comorbidity = random.choice(comorbidity_sets) # ----------------------------- # Simulated Model Behavior # ----------------------------- base_score = 0.9 # Degrade based on real-world signals if age_group == "elderly": base_score -= 0.15 if "ckd" in comorbidity: base_score -= 0.2 elif "hypertension" in comorbidity: base_score -= 0.1 if ethnicity == "African": base_score -= 0.05 score = max(0.3, min(0.95, base_score)) failure = 1 if score < 0.7 else 0 data.append({ "patient_id": i, "age_group": age_group, "ethnicity": ethnicity, "comorbidity": comorbidity, "model_score": round(score, 2), "failure": failure }) df = pd.DataFrame(data) # ----------------------------- # Failure Summary # ----------------------------- summary = df.groupby( ["age_group", "ethnicity", "comorbidity"] ).agg( patients=("patient_id", "count"), failures=("failure", "sum") ).reset_index() summary["failure_rate"] = (summary["failures"] / summary["patients"]).round(2) return df, summary # ----------------------------- # UI Logic # ----------------------------- def run_demo(n): df, summary = generate_demo(n) return df, summary # ----------------------------- # Interface # ----------------------------- with gr.Blocks(title="HipAAsynth Lab") as demo: gr.Markdown(""" # HipAAsynth Lab ### Simulating real-world conditions to expose model failure This lab demonstrates how model performance degrades across: - patient populations - demographic variation - comorbidity complexity Models that perform well in controlled testing often fail under these conditions. """) n = gr.Slider(50, 300, value=100, step=10, label="Number of Patients") run = gr.Button("Run Validation Simulation") gr.Markdown("## Patient-Level Output") table = gr.Dataframe() gr.Markdown("## Failure Breakdown (Where Models Break)") summary = gr.Dataframe() run.click(fn=run_demo, inputs=n, outputs=[table, summary]) demo.launch()