File size: 9,976 Bytes
650ba1c
6f9e0e8
 
650ba1c
6f9e0e8
 
650ba1c
 
 
 
6f9e0e8
650ba1c
 
6f9e0e8
 
 
 
 
 
 
 
 
 
650ba1c
 
6f9e0e8
650ba1c
6f9e0e8
 
 
 
650ba1c
 
 
 
 
 
 
 
6f9e0e8
 
 
 
 
 
 
 
 
 
 
650ba1c
5df0bd1
 
650ba1c
 
 
 
6f9e0e8
 
650ba1c
 
6f9e0e8
650ba1c
6f9e0e8
 
 
 
 
 
 
646fdea
650ba1c
 
6f9e0e8
650ba1c
 
 
6f9e0e8
 
650ba1c
 
 
 
 
 
646fdea
 
6f9e0e8
650ba1c
646fdea
 
 
 
650ba1c
 
 
 
6f9e0e8
646fdea
650ba1c
6f9e0e8
646fdea
 
 
6f9e0e8
646fdea
6f9e0e8
646fdea
 
 
 
 
 
650ba1c
6f9e0e8
650ba1c
 
 
 
 
6f9e0e8
646fdea
 
6f9e0e8
650ba1c
 
 
bb88d9a
650ba1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f9e0e8
650ba1c
 
3caf13c
 
 
 
 
 
 
 
 
 
 
650ba1c
 
 
3caf13c
650ba1c
 
5df0bd1
650ba1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f9e0e8
650ba1c
 
 
 
6f9e0e8
 
650ba1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f9e0e8
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
import os
import json
import joblib
import numpy as np
import pandas as pd
import gradio as gr

# Plotting backend for Spaces (prevents runtime plotting issues)
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

import shap
import dice_ml
from dice_ml import Dice

# Load artifacts
PIPELINE_PATH = "artifacts/pipeline.joblib"
SCHEMA_PATH = "artifacts/schema.json"

clf = joblib.load(PIPELINE_PATH)
schema = json.load(open(SCHEMA_PATH))

SKILLS_RAW = schema["skills"]  # like Skill_Python, Skill_SQL...
SKILLS_PRETTY = [s.replace("Skill_", "").replace("_", " ") for s in SKILLS_RAW]

# Your dataset choices
EDU_CHOICES = ["B.Sc", "B.Tech", "M.Tech", "MBA", "PhD"]
CERT_CHOICES = ["None", "AWS Certified", "Google ML", "Deep Learning Specialization"]
ROLE_CHOICES = ["AI Researcher", "Cybersecurity Analyst", "Data Scientist", "Software Engineer"]

# Helpers
def build_row(experience, salary, projects, education, certification, job_role, selected_skills_pretty):
    # Map pretty skills back to raw Skill_ columns
    selected_skills_raw = set("Skill_" + s.replace(" ", "_") for s in selected_skills_pretty)

    row = {k: 0 for k in SKILLS_RAW}
    for sk in SKILLS_RAW:
        row[sk] = 1 if sk in selected_skills_raw else 0

    row.update({
        "Experience (Years)": float(experience),
        "Salary Expectation ($)": float(salary),
        "Projects Count": int(projects),
        "Education": education,
        "Certifications": certification,
        "Job Role": job_role,
    })
    return pd.DataFrame([row])

def format_decision(proba: float):
    decision = "Hire ✅" if proba >= 0.75 else "Reject ❌"
    confidence = "High" if (proba >= 0.5 or proba <= 0.2) else "Medium"
    return decision, confidence

# Core functions
def predict_fn(experience, salary, projects, education, certification, job_role, selected_skills):
    X = build_row(experience, salary, projects, education, certification, job_role, selected_skills)
    proba = float(clf.predict_proba(X)[:, 1][0])
    decision, confidence = format_decision(proba)
    return decision, proba, confidence

def explain_shap_fn(experience, salary, projects, education, certification, job_role, selected_skills):
    X = build_row(experience, salary, projects, education, certification, job_role, selected_skills)

    pre = clf.named_steps["preprocess"]
    model = clf.named_steps["model"]
    Xt = pre.transform(X)

    explainer = shap.TreeExplainer(model)

    # New SHAP API is more stable across versions
    sv = explainer(Xt)

    plt.figure(figsize=(9, 5))
    shap.plots.waterfall(sv[0], show=False)
    plt.tight_layout()
    return plt.gcf()

def dice_recourse_fn(experience, salary, projects, education, certification, job_role, selected_skills):
    """
    Lightweight demo-style DiCE:
    - Education & Job Role are immutable (ethical recourse)
    - Actionable: skills, projects, certifications, salary
    """
    try:
        X = build_row(experience, salary, projects, education, certification, job_role, selected_skills)

        # Create a tiny pool for DiCE to operate on
        df_pool = pd.concat([X] * 50, ignore_index=True)
        df_pool["target"] = (clf.predict_proba(df_pool)[:, 1] >= 0.5).astype(int)

        immutable = ["Education", "Job Role"]
        continuous = [
            c for c in df_pool.columns
            if df_pool[c].dtype != "object" and c not in immutable and c != "target"
        ]

        if len(continuous) == 0:
            return pd.DataFrame({"error": ["No continuous features detected for counterfactual search."]})

        d = dice_ml.Data(dataframe=df_pool, continuous_features=continuous, outcome_name="target")
        m = dice_ml.Model(model=clf, backend="sklearn")
        dice = Dice(d, m, method="random")

        actionable = [c for c in X.columns if c not in immutable]

        cfs = dice.generate_counterfactuals(
            query_instances=X,
            total_CFs=3,
            desired_class=1,
            features_to_vary=actionable
        )
        cf_df = cfs.cf_examples_list[0].final_cfs_df

        # Make it readable: remove Skill_ prefix
        rename_map = {c: c.replace("Skill_", "") for c in cf_df.columns if c.startswith("Skill_")}
        cf_df = cf_df.rename(columns=rename_map)

        return cf_df

    except Exception as e:
        return pd.DataFrame({"error": [str(e)]})

# Creative presets (presentation boost)
PRESETS = {
    "Strong Candidate (Hire)": dict(
        experience=5, salary=80000, projects=3, education="M.Tech",
        certification="AWS Certified", job_role="Data Scientist",
        skills=["Python", "SQL", "MachineLearning", "TensorFlow", "Pytorch"]
    ),
    "Borderline Candidate": dict(
        experience=2, salary=70000, projects=2, education="B.Sc",
        certification="None", job_role="Data Scientist",
        skills=["Python", "SQL"]
    ),
    "Weak Candidate (Reject)": dict(
        experience=0, salary=95000, projects=0, education="B.Sc",
        certification="None", job_role="Data Scientist",
        skills=[]
    ),
    "Cybersecurity Profile": dict(
        experience=4, salary=90000, projects=3, education="B.Tech",
        certification="None", job_role="Cybersecurity Analyst",
        skills=["Cybersecurity", "EthicalHacking", "Linux", "Networking"]
    ),
}

def load_preset(preset_name):
    p = PRESETS[preset_name]

    # Build a lookup that tolerates small formatting differences
    # e.g., "MachineLearning" -> "Machine Learning"
    pretty_lookup = {s.replace(" ", "").lower(): s for s in SKILLS_PRETTY}

    normalized = []
    for sk in p["skills"]:
        key = sk.replace(" ", "").replace("_", "").lower()
        if key in pretty_lookup:
            normalized.append(pretty_lookup[key])

    return (
        p["experience"], p["salary"], p["projects"],
        p["education"], p["certification"], p["job_role"],
        normalized
    )

# UI
CUSTOM_CSS = """
#title { font-size: 34px; font-weight: 800; margin-bottom: 0.2rem; }
#subtitle { font-size: 15px; opacity: 0.85; margin-bottom: 1rem; }
.badge { display:inline-block; padding: 6px 10px; border-radius: 999px; font-size: 12px; margin-right: 6px; }
.badge1 { background: rgba(0,255,150,0.12); border: 1px solid rgba(0,255,150,0.25); }
.badge2 { background: rgba(90,180,255,0.12); border: 1px solid rgba(90,180,255,0.25); }
.badge3 { background: rgba(255,200,90,0.12); border: 1px solid rgba(255,200,90,0.25); }
"""

# Build Gradio App
with gr.Blocks(css=CUSTOM_CSS, title="XAI Recruitment Screening (XGBoost + SHAP + DiCE)") as demo:
    gr.Markdown('<div id="title">XAI Recruitment Screening System</div>')
    gr.Markdown(
        '<div id="subtitle">'
        '<span class="badge badge1">Predict</span>'
        '<span class="badge badge2">Explain (SHAP)</span>'
        '<span class="badge badge3">Recourse (DiCE)</span>'
        '<br/>A transparent screening demo using XGBoost + SHAP + DiCE counterfactual recourse.'
        '</div>'
    )

    with gr.Accordion("📌 How to use this demo (for assessment)", open=True):
        gr.Markdown(
            """
**What you’ll see**
- **Predict:** Hire/Reject + probability  
- **Explain (SHAP):** Which features pushed the decision up/down  
- **Recourse (DiCE):** Actionable counterfactual suggestions  

**Ethical design**
- `Education` and `Job Role` are treated as **immutable** in counterfactuals (we don’t recommend changing identity/context).
- Dataset does not include protected attributes (e.g., gender/race), so fairness is audited by available groups (Education / Job Role).
            """
        )

    with gr.Row():
        with gr.Column(scale=1):
            preset = gr.Dropdown(
                choices=list(PRESETS.keys()),
                value="Strong Candidate (Hire)",
                label="🎛️ Quick Test Presets"
            )
            load_btn = gr.Button("Load Preset")

            exp = gr.Number(label="Experience (Years)", value=5)
            sal = gr.Number(label="Salary Expectation ($)", value=80000)
            proj = gr.Number(label="Projects Count", value=3)

            edu = gr.Dropdown(EDU_CHOICES, label="Education", value="B.Sc")
            cert = gr.Dropdown(CERT_CHOICES, label="Certifications", value="None")
            role = gr.Dropdown(ROLE_CHOICES, label="Job Role", value="Data Scientist")

            skills = gr.CheckboxGroup(
                choices=SKILLS_PRETTY,
                label="Skills (select all that apply)"
            )

        with gr.Column(scale=1):
            with gr.Tab("Predict"):
                run_btn = gr.Button("✅ Run Prediction", variant="primary")
                out_decision = gr.Text(label="Decision")
                out_prob = gr.Number(label="Hire Probability")
                out_conf = gr.Text(label="Confidence")

            with gr.Tab("Explain (SHAP)"):
                gr.Markdown("**Local explanation:** red features decrease hire probability, blue features increase it.")
                shap_plot = gr.Plot()
                shap_btn = gr.Button("🔎 Generate SHAP Explanation")

            with gr.Tab("Recourse (DiCE)"):
                gr.Markdown("**Recourse suggestions:** actionable changes that may flip the decision to *Hire*.")
                cf_table = gr.Dataframe()
                dice_btn = gr.Button("🧭 Generate Counterfactuals (DiCE)")

    # Preset loader
    load_btn.click(
        load_preset,
        inputs=[preset],
        outputs=[exp, sal, proj, edu, cert, role, skills]
    )

    # Wire buttons
    run_btn.click(
        predict_fn,
        inputs=[exp, sal, proj, edu, cert, role, skills],
        outputs=[out_decision, out_prob, out_conf]
    )

    shap_btn.click(
        explain_shap_fn,
        inputs=[exp, sal, proj, edu, cert, role, skills],
        outputs=[shap_plot]
    )

    dice_btn.click(
        dice_recourse_fn,
        inputs=[exp, sal, proj, edu, cert, role, skills],
        outputs=[cf_table]
    )

demo.launch()