Spaces:
Sleeping
Sleeping
Dyuti Dasmahapatra
commited on
Commit
Β·
816d43f
1
Parent(s):
dd5a03c
enhanced UI with improved styling and user instructions
Browse files
app.py
CHANGED
|
@@ -240,29 +240,168 @@ def create_demo_image():
|
|
| 240 |
|
| 241 |
return img
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
# Create the Gradio interface
|
| 244 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
| 245 |
-
|
|
|
|
| 246 |
"""
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
"""
|
| 253 |
)
|
| 254 |
|
| 255 |
# Model selection (shared across all tabs)
|
| 256 |
with gr.Row():
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
load_btn.click(
|
| 268 |
fn=lambda model: load_selected_model(SUPPORTED_MODELS[model]),
|
|
@@ -274,12 +413,20 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
|
| 274 |
with gr.Tabs():
|
| 275 |
# Tab 1: Basic Explainability
|
| 276 |
with gr.TabItem("π Basic Explainability"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
with gr.Row():
|
| 278 |
with gr.Column(scale=1):
|
| 279 |
image_input = gr.Image(
|
| 280 |
label="π Upload Image",
|
| 281 |
type="pil",
|
| 282 |
-
|
|
|
|
| 283 |
)
|
| 284 |
|
| 285 |
with gr.Accordion("βοΈ Explanation Settings", open=False):
|
|
@@ -290,30 +437,39 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
|
| 290 |
"GradientSHAP"
|
| 291 |
],
|
| 292 |
value="Attention Visualization",
|
| 293 |
-
label="Explanation Method"
|
|
|
|
| 294 |
)
|
| 295 |
|
|
|
|
| 296 |
with gr.Row():
|
| 297 |
layer_index = gr.Slider(
|
| 298 |
minimum=0, maximum=11, value=6, step=1,
|
| 299 |
-
label="
|
|
|
|
| 300 |
)
|
| 301 |
head_index = gr.Slider(
|
| 302 |
minimum=0, maximum=11, value=0, step=1,
|
| 303 |
-
label="
|
|
|
|
| 304 |
)
|
| 305 |
|
| 306 |
-
analyze_btn = gr.Button("π Analyze Image", variant="primary")
|
| 307 |
-
status_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
with gr.Column(scale=2):
|
| 310 |
with gr.Row():
|
| 311 |
original_display = gr.Image(
|
| 312 |
label="πΈ Processed Image",
|
| 313 |
-
interactive=False
|
|
|
|
| 314 |
)
|
| 315 |
prediction_display = gr.Plot(
|
| 316 |
-
label="π
|
| 317 |
)
|
| 318 |
|
| 319 |
explanation_display = gr.Plot(
|
|
@@ -329,32 +485,61 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
|
| 329 |
|
| 330 |
# Tab 2: Counterfactual Analysis
|
| 331 |
with gr.TabItem("π Counterfactual Analysis"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
with gr.Row():
|
| 333 |
with gr.Column(scale=1):
|
| 334 |
cf_image_input = gr.Image(
|
| 335 |
label="π Upload Image",
|
| 336 |
type="pil",
|
| 337 |
-
|
|
|
|
| 338 |
)
|
| 339 |
|
| 340 |
with gr.Accordion("βοΈ Counterfactual Settings", open=True):
|
| 341 |
patch_size = gr.Slider(
|
| 342 |
minimum=16, maximum=64, value=32, step=16,
|
| 343 |
-
label="Patch Size"
|
|
|
|
| 344 |
)
|
| 345 |
perturbation_type = gr.Dropdown(
|
| 346 |
choices=["blur", "blackout", "gray", "noise"],
|
| 347 |
value="blur",
|
| 348 |
-
label="Perturbation Type"
|
|
|
|
| 349 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
-
cf_analyze_btn = gr.Button("π Run Counterfactual Analysis", variant="primary")
|
| 352 |
-
cf_status_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
with gr.Column(scale=2):
|
| 355 |
cf_explanation_display = gr.Plot(
|
| 356 |
label="π Counterfactual Analysis Results"
|
| 357 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
cf_analyze_btn.click(
|
| 360 |
fn=analyze_counterfactual,
|
|
@@ -364,27 +549,59 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
|
| 364 |
|
| 365 |
# Tab 3: Confidence Calibration
|
| 366 |
with gr.TabItem("π Confidence Calibration"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
with gr.Row():
|
| 368 |
with gr.Column(scale=1):
|
| 369 |
cal_image_input = gr.Image(
|
| 370 |
-
label="π Upload Sample Image
|
| 371 |
type="pil",
|
| 372 |
-
|
|
|
|
| 373 |
)
|
| 374 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
with gr.Accordion("βοΈ Calibration Settings", open=True):
|
| 376 |
n_bins = gr.Slider(
|
| 377 |
minimum=5, maximum=20, value=10, step=1,
|
| 378 |
-
label="Number of Bins"
|
|
|
|
| 379 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
-
cal_analyze_btn = gr.Button("π Analyze Calibration", variant="primary")
|
| 382 |
-
cal_status_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
with gr.Column(scale=2):
|
| 385 |
cal_explanation_display = gr.Plot(
|
| 386 |
label="π Calibration Analysis Results"
|
| 387 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
cal_analyze_btn.click(
|
| 390 |
fn=analyze_calibration,
|
|
@@ -394,21 +611,54 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
|
| 394 |
|
| 395 |
# Tab 4: Bias Detection
|
| 396 |
with gr.TabItem("βοΈ Bias Detection"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
with gr.Row():
|
| 398 |
with gr.Column(scale=1):
|
| 399 |
bias_image_input = gr.Image(
|
| 400 |
-
label="π Upload Sample Image
|
| 401 |
type="pil",
|
| 402 |
-
|
|
|
|
| 403 |
)
|
| 404 |
|
| 405 |
-
|
| 406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
with gr.Column(scale=2):
|
| 409 |
bias_explanation_display = gr.Plot(
|
| 410 |
label="βοΈ Bias Detection Results"
|
| 411 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
|
| 413 |
bias_analyze_btn.click(
|
| 414 |
fn=analyze_bias_detection,
|
|
@@ -417,26 +667,40 @@ with gr.Blocks(theme=gr.themes.Soft(), title="ViT Auditing Toolkit") as demo:
|
|
| 417 |
)
|
| 418 |
|
| 419 |
# Footer
|
| 420 |
-
gr.
|
| 421 |
"""
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
"""
|
| 434 |
)
|
| 435 |
|
| 436 |
# Launch the application
|
| 437 |
if __name__ == "__main__":
|
| 438 |
demo.launch(
|
| 439 |
-
server_name="localhost",
|
| 440 |
server_port=7860,
|
| 441 |
share=False,
|
| 442 |
show_error=True
|
|
|
|
| 240 |
|
| 241 |
return img
|
| 242 |
|
| 243 |
+
# Minimal CSS for basic styling without breaking functionality
|
| 244 |
+
custom_css = """
|
| 245 |
+
/* Basic styling without interfering with dropdowns */
|
| 246 |
+
.gradio-container {
|
| 247 |
+
background: linear-gradient(135deg, #0f1419 0%, #1a1f2e 50%, #0f1419 100%);
|
| 248 |
+
font-family: 'Inter', sans-serif;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
/* Header styling */
|
| 252 |
+
.main-header {
|
| 253 |
+
background: rgba(99, 102, 241, 0.05);
|
| 254 |
+
border-radius: 20px;
|
| 255 |
+
padding: 2.5rem;
|
| 256 |
+
margin-bottom: 2rem;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
/* Button styling */
|
| 260 |
+
button.primary {
|
| 261 |
+
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
|
| 262 |
+
border: none;
|
| 263 |
+
color: white;
|
| 264 |
+
font-weight: 600;
|
| 265 |
+
padding: 14px 32px;
|
| 266 |
+
border-radius: 12px;
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
button.primary:hover {
|
| 270 |
+
transform: translateY(-2px);
|
| 271 |
+
box-shadow: 0 6px 24px rgba(99, 102, 241, 0.6);
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
/* Block styling */
|
| 275 |
+
.block {
|
| 276 |
+
background: rgba(30, 41, 59, 0.4);
|
| 277 |
+
border-radius: 16px;
|
| 278 |
+
padding: 1.5rem;
|
| 279 |
+
border: 1px solid rgba(99, 102, 241, 0.15);
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
/* Tab styling */
|
| 283 |
+
.tab-nav button {
|
| 284 |
+
background: rgba(30, 41, 59, 0.5);
|
| 285 |
+
border: 1px solid rgba(99, 102, 241, 0.2);
|
| 286 |
+
border-radius: 12px;
|
| 287 |
+
padding: 14px 28px;
|
| 288 |
+
margin: 0 6px;
|
| 289 |
+
color: #94a3b8;
|
| 290 |
+
font-weight: 600;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
.tab-nav button.selected {
|
| 294 |
+
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
|
| 295 |
+
color: white;
|
| 296 |
+
}
|
| 297 |
+
"""
|
| 298 |
+
|
| 299 |
# Create the Gradio interface
|
| 300 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="ViT Auditing Toolkit") as demo:
|
| 301 |
+
# Main Header
|
| 302 |
+
gr.HTML(
|
| 303 |
"""
|
| 304 |
+
<div class="main-header">
|
| 305 |
+
<h1 style="
|
| 306 |
+
font-size: 3rem;
|
| 307 |
+
font-weight: 800;
|
| 308 |
+
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #ec4899 100%);
|
| 309 |
+
-webkit-background-clip: text;
|
| 310 |
+
-webkit-text-fill-color: transparent;
|
| 311 |
+
margin-bottom: 0.5rem;
|
| 312 |
+
text-align: center;
|
| 313 |
+
">
|
| 314 |
+
π― ViT Auditing Toolkit
|
| 315 |
+
</h1>
|
| 316 |
+
<p style="
|
| 317 |
+
font-size: 1.25rem;
|
| 318 |
+
color: #94a3b8;
|
| 319 |
+
text-align: center;
|
| 320 |
+
font-weight: 500;
|
| 321 |
+
margin-bottom: 0;
|
| 322 |
+
">
|
| 323 |
+
Comprehensive Model Explainability and Validation Dashboard
|
| 324 |
+
</p>
|
| 325 |
+
</div>
|
| 326 |
+
"""
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
# About Section
|
| 330 |
+
gr.HTML(
|
| 331 |
+
"""
|
| 332 |
+
<div style="
|
| 333 |
+
background: rgba(30, 41, 59, 0.4);
|
| 334 |
+
border-radius: 16px;
|
| 335 |
+
padding: 2rem;
|
| 336 |
+
margin-bottom: 2rem;
|
| 337 |
+
border: 1px solid rgba(99, 102, 241, 0.15);
|
| 338 |
+
">
|
| 339 |
+
<h2 style="font-size: 1.75rem; font-weight: 700; color: #e0e7ff; margin-bottom: 1rem;">
|
| 340 |
+
π οΈ About This Toolkit
|
| 341 |
+
</h2>
|
| 342 |
+
|
| 343 |
+
<p style="color: #94a3b8; line-height: 1.8; font-size: 1.05rem; margin-bottom: 1.5rem;">
|
| 344 |
+
This interactive dashboard provides comprehensive auditing capabilities for Vision Transformer models,
|
| 345 |
+
enabling researchers and practitioners to understand, validate, and improve their AI models through
|
| 346 |
+
multiple explainability techniques.
|
| 347 |
+
</p>
|
| 348 |
+
|
| 349 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem;">
|
| 350 |
+
<div style="background: rgba(99, 102, 241, 0.08); padding: 1.5rem; border-radius: 12px; border: 1px solid rgba(99, 102, 241, 0.2);">
|
| 351 |
+
<div style="font-size: 2rem; margin-bottom: 0.5rem;">π</div>
|
| 352 |
+
<strong style="color: #a5b4fc; font-size: 1.1rem;">Basic Explainability</strong>
|
| 353 |
+
<p style="margin-top: 0.5rem; font-size: 0.9rem; color: #94a3b8;">
|
| 354 |
+
Understand model predictions with attention maps, GradCAM, and SHAP visualizations
|
| 355 |
+
</p>
|
| 356 |
+
</div>
|
| 357 |
+
|
| 358 |
+
<div style="background: rgba(99, 102, 241, 0.08); padding: 1.5rem; border-radius: 12px; border: 1px solid rgba(99, 102, 241, 0.2);">
|
| 359 |
+
<div style="font-size: 2rem; margin-bottom: 0.5rem;">π</div>
|
| 360 |
+
<strong style="color: #c4b5fd; font-size: 1.1rem;">Counterfactual Analysis</strong>
|
| 361 |
+
<p style="margin-top: 0.5rem; font-size: 0.9rem; color: #94a3b8;">
|
| 362 |
+
Test prediction robustness by systematically perturbing image regions
|
| 363 |
+
</p>
|
| 364 |
+
</div>
|
| 365 |
+
|
| 366 |
+
<div style="background: rgba(99, 102, 241, 0.08); padding: 1.5rem; border-radius: 12px; border: 1px solid rgba(99, 102, 241, 0.2);">
|
| 367 |
+
<div style="font-size: 2rem; margin-bottom: 0.5rem;">π</div>
|
| 368 |
+
<strong style="color: #f9a8d4; font-size: 1.1rem;">Confidence Calibration</strong>
|
| 369 |
+
<p style="margin-top: 0.5rem; font-size: 0.9rem; color: #94a3b8;">
|
| 370 |
+
Evaluate whether model confidence scores accurately reflect prediction reliability
|
| 371 |
+
</p>
|
| 372 |
+
</div>
|
| 373 |
+
|
| 374 |
+
<div style="background: rgba(99, 102, 241, 0.08); padding: 1.5rem; border-radius: 12px; border: 1px solid rgba(99, 102, 241, 0.2);">
|
| 375 |
+
<div style="font-size: 2rem; margin-bottom: 0.5rem;">βοΈ</div>
|
| 376 |
+
<strong style="color: #93c5fd; font-size: 1.1rem;">Bias Detection</strong>
|
| 377 |
+
<p style="margin-top: 0.5rem; font-size: 0.9rem; color: #94a3b8;">
|
| 378 |
+
Identify performance variations across different demographic or data subgroups
|
| 379 |
+
</p>
|
| 380 |
+
</div>
|
| 381 |
+
</div>
|
| 382 |
+
</div>
|
| 383 |
"""
|
| 384 |
)
|
| 385 |
|
| 386 |
# Model selection (shared across all tabs)
|
| 387 |
with gr.Row():
|
| 388 |
+
with gr.Column(scale=3):
|
| 389 |
+
model_choice = gr.Dropdown(
|
| 390 |
+
choices=list(SUPPORTED_MODELS.keys()),
|
| 391 |
+
value="ViT-Base",
|
| 392 |
+
label="π― Select Model",
|
| 393 |
+
info="Choose which Vision Transformer model to use"
|
| 394 |
+
)
|
| 395 |
|
| 396 |
+
with gr.Column(scale=1):
|
| 397 |
+
load_btn = gr.Button("π Load Model", variant="primary", size="lg")
|
| 398 |
+
|
| 399 |
+
with gr.Row():
|
| 400 |
+
model_status = gr.Textbox(
|
| 401 |
+
label="π‘ Model Status",
|
| 402 |
+
interactive=False,
|
| 403 |
+
placeholder="Select a model and click 'Load Model' to begin..."
|
| 404 |
+
)
|
| 405 |
|
| 406 |
load_btn.click(
|
| 407 |
fn=lambda model: load_selected_model(SUPPORTED_MODELS[model]),
|
|
|
|
| 413 |
with gr.Tabs():
|
| 414 |
# Tab 1: Basic Explainability
|
| 415 |
with gr.TabItem("π Basic Explainability"):
|
| 416 |
+
gr.Markdown(
|
| 417 |
+
"""
|
| 418 |
+
### Understanding Model Predictions
|
| 419 |
+
Visualize what the model "sees" and understand which features influence its decisions.
|
| 420 |
+
"""
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
with gr.Row():
|
| 424 |
with gr.Column(scale=1):
|
| 425 |
image_input = gr.Image(
|
| 426 |
label="π Upload Image",
|
| 427 |
type="pil",
|
| 428 |
+
sources=["upload", "clipboard"],
|
| 429 |
+
height=350
|
| 430 |
)
|
| 431 |
|
| 432 |
with gr.Accordion("βοΈ Explanation Settings", open=False):
|
|
|
|
| 437 |
"GradientSHAP"
|
| 438 |
],
|
| 439 |
value="Attention Visualization",
|
| 440 |
+
label="π¬ Explanation Method",
|
| 441 |
+
info="Select the explainability technique to apply"
|
| 442 |
)
|
| 443 |
|
| 444 |
+
gr.Markdown("**Attention-specific Parameters:**")
|
| 445 |
with gr.Row():
|
| 446 |
layer_index = gr.Slider(
|
| 447 |
minimum=0, maximum=11, value=6, step=1,
|
| 448 |
+
label="Layer Index",
|
| 449 |
+
info="Which transformer layer to visualize"
|
| 450 |
)
|
| 451 |
head_index = gr.Slider(
|
| 452 |
minimum=0, maximum=11, value=0, step=1,
|
| 453 |
+
label="Head Index",
|
| 454 |
+
info="Which attention head to visualize"
|
| 455 |
)
|
| 456 |
|
| 457 |
+
analyze_btn = gr.Button("π Analyze Image", variant="primary", size="lg")
|
| 458 |
+
status_output = gr.Textbox(
|
| 459 |
+
label="π Analysis Status",
|
| 460 |
+
interactive=False,
|
| 461 |
+
placeholder="Upload an image and click 'Analyze Image' to start..."
|
| 462 |
+
)
|
| 463 |
|
| 464 |
with gr.Column(scale=2):
|
| 465 |
with gr.Row():
|
| 466 |
original_display = gr.Image(
|
| 467 |
label="πΈ Processed Image",
|
| 468 |
+
interactive=False,
|
| 469 |
+
height=300
|
| 470 |
)
|
| 471 |
prediction_display = gr.Plot(
|
| 472 |
+
label="π Top Predictions"
|
| 473 |
)
|
| 474 |
|
| 475 |
explanation_display = gr.Plot(
|
|
|
|
| 485 |
|
| 486 |
# Tab 2: Counterfactual Analysis
|
| 487 |
with gr.TabItem("π Counterfactual Analysis"):
|
| 488 |
+
gr.Markdown(
|
| 489 |
+
"""
|
| 490 |
+
### Testing Model Robustness
|
| 491 |
+
Systematically perturb image regions to understand which areas are most critical for predictions.
|
| 492 |
+
"""
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
with gr.Row():
|
| 496 |
with gr.Column(scale=1):
|
| 497 |
cf_image_input = gr.Image(
|
| 498 |
label="π Upload Image",
|
| 499 |
type="pil",
|
| 500 |
+
sources=["upload", "clipboard"],
|
| 501 |
+
height=350
|
| 502 |
)
|
| 503 |
|
| 504 |
with gr.Accordion("βοΈ Counterfactual Settings", open=True):
|
| 505 |
patch_size = gr.Slider(
|
| 506 |
minimum=16, maximum=64, value=32, step=16,
|
| 507 |
+
label="π² Patch Size",
|
| 508 |
+
info="Size of perturbation patches (larger = fewer patches)"
|
| 509 |
)
|
| 510 |
perturbation_type = gr.Dropdown(
|
| 511 |
choices=["blur", "blackout", "gray", "noise"],
|
| 512 |
value="blur",
|
| 513 |
+
label="π¨ Perturbation Type",
|
| 514 |
+
info="How to modify image patches"
|
| 515 |
)
|
| 516 |
+
|
| 517 |
+
gr.Markdown("""
|
| 518 |
+
**Perturbation Types:**
|
| 519 |
+
- **Blur**: Gaussian blur effect
|
| 520 |
+
- **Blackout**: Replace with black pixels
|
| 521 |
+
- **Gray**: Convert to grayscale
|
| 522 |
+
- **Noise**: Add random noise
|
| 523 |
+
""")
|
| 524 |
|
| 525 |
+
cf_analyze_btn = gr.Button("π Run Counterfactual Analysis", variant="primary", size="lg")
|
| 526 |
+
cf_status_output = gr.Textbox(
|
| 527 |
+
label="π Analysis Status",
|
| 528 |
+
interactive=False,
|
| 529 |
+
placeholder="Upload an image and click to start counterfactual analysis..."
|
| 530 |
+
)
|
| 531 |
|
| 532 |
with gr.Column(scale=2):
|
| 533 |
cf_explanation_display = gr.Plot(
|
| 534 |
label="π Counterfactual Analysis Results"
|
| 535 |
)
|
| 536 |
+
|
| 537 |
+
gr.Markdown("""
|
| 538 |
+
**Understanding Results:**
|
| 539 |
+
- **Confidence Change**: How much the model's certainty shifts
|
| 540 |
+
- **Prediction Flip Rate**: Percentage of patches causing misclassification
|
| 541 |
+
- **Sensitive Regions**: Areas most critical to the model's decision
|
| 542 |
+
""")
|
| 543 |
|
| 544 |
cf_analyze_btn.click(
|
| 545 |
fn=analyze_counterfactual,
|
|
|
|
| 549 |
|
| 550 |
# Tab 3: Confidence Calibration
|
| 551 |
with gr.TabItem("π Confidence Calibration"):
|
| 552 |
+
gr.Markdown(
|
| 553 |
+
"""
|
| 554 |
+
### Evaluating Prediction Reliability
|
| 555 |
+
Assess whether the model's confidence scores accurately reflect the likelihood of correct predictions.
|
| 556 |
+
"""
|
| 557 |
+
)
|
| 558 |
+
|
| 559 |
with gr.Row():
|
| 560 |
with gr.Column(scale=1):
|
| 561 |
cal_image_input = gr.Image(
|
| 562 |
+
label="π Upload Sample Image",
|
| 563 |
type="pil",
|
| 564 |
+
sources=["upload", "clipboard"],
|
| 565 |
+
height=350
|
| 566 |
)
|
| 567 |
|
| 568 |
+
gr.Markdown("""
|
| 569 |
+
βΉοΈ *Note: This demo uses the uploaded image to create a test set.
|
| 570 |
+
In production, use a proper validation dataset.*
|
| 571 |
+
""")
|
| 572 |
+
|
| 573 |
with gr.Accordion("βοΈ Calibration Settings", open=True):
|
| 574 |
n_bins = gr.Slider(
|
| 575 |
minimum=5, maximum=20, value=10, step=1,
|
| 576 |
+
label="π Number of Bins",
|
| 577 |
+
info="Granularity of calibration analysis"
|
| 578 |
)
|
| 579 |
+
|
| 580 |
+
gr.Markdown("""
|
| 581 |
+
**Calibration Metrics:**
|
| 582 |
+
- **Perfect calibration**: Confidence matches accuracy
|
| 583 |
+
- **Overconfident**: High confidence, low accuracy
|
| 584 |
+
- **Underconfident**: Low confidence, high accuracy
|
| 585 |
+
""")
|
| 586 |
|
| 587 |
+
cal_analyze_btn = gr.Button("π Analyze Calibration", variant="primary", size="lg")
|
| 588 |
+
cal_status_output = gr.Textbox(
|
| 589 |
+
label="π Analysis Status",
|
| 590 |
+
interactive=False,
|
| 591 |
+
placeholder="Upload an image and click to analyze calibration..."
|
| 592 |
+
)
|
| 593 |
|
| 594 |
with gr.Column(scale=2):
|
| 595 |
cal_explanation_display = gr.Plot(
|
| 596 |
label="π Calibration Analysis Results"
|
| 597 |
)
|
| 598 |
+
|
| 599 |
+
gr.Markdown("""
|
| 600 |
+
**Interpreting Calibration:**
|
| 601 |
+
- A well-calibrated model's confidence should match its accuracy
|
| 602 |
+
- If the model predicts 80% confidence, it should be correct 80% of the time
|
| 603 |
+
- Large deviations indicate calibration issues requiring attention
|
| 604 |
+
""")
|
| 605 |
|
| 606 |
cal_analyze_btn.click(
|
| 607 |
fn=analyze_calibration,
|
|
|
|
| 611 |
|
| 612 |
# Tab 4: Bias Detection
|
| 613 |
with gr.TabItem("βοΈ Bias Detection"):
|
| 614 |
+
gr.Markdown(
|
| 615 |
+
"""
|
| 616 |
+
### Identifying Performance Disparities
|
| 617 |
+
Detect potential biases by comparing model performance across different data subgroups.
|
| 618 |
+
"""
|
| 619 |
+
)
|
| 620 |
+
|
| 621 |
with gr.Row():
|
| 622 |
with gr.Column(scale=1):
|
| 623 |
bias_image_input = gr.Image(
|
| 624 |
+
label="π Upload Sample Image",
|
| 625 |
type="pil",
|
| 626 |
+
sources=["upload", "clipboard"],
|
| 627 |
+
height=350
|
| 628 |
)
|
| 629 |
|
| 630 |
+
gr.Markdown("""
|
| 631 |
+
βΉοΈ *Note: This demo creates synthetic subgroups from your image.
|
| 632 |
+
In production, use predefined demographic or data subgroups.*
|
| 633 |
+
""")
|
| 634 |
+
|
| 635 |
+
gr.Markdown("""
|
| 636 |
+
**Generated Subgroups:**
|
| 637 |
+
- Original image (baseline)
|
| 638 |
+
- Increased brightness
|
| 639 |
+
- Decreased brightness
|
| 640 |
+
- Enhanced contrast
|
| 641 |
+
""")
|
| 642 |
+
|
| 643 |
+
bias_analyze_btn = gr.Button("βοΈ Detect Bias", variant="primary", size="lg")
|
| 644 |
+
bias_status_output = gr.Textbox(
|
| 645 |
+
label="π Analysis Status",
|
| 646 |
+
interactive=False,
|
| 647 |
+
placeholder="Upload an image and click to detect potential biases..."
|
| 648 |
+
)
|
| 649 |
|
| 650 |
with gr.Column(scale=2):
|
| 651 |
bias_explanation_display = gr.Plot(
|
| 652 |
label="βοΈ Bias Detection Results"
|
| 653 |
)
|
| 654 |
+
|
| 655 |
+
gr.Markdown("""
|
| 656 |
+
**Understanding Bias Metrics:**
|
| 657 |
+
- Compare confidence scores across subgroups
|
| 658 |
+
- Large disparities may indicate systematic biases
|
| 659 |
+
- Consider demographic, environmental, and quality variations
|
| 660 |
+
- Use findings to improve data collection and model training
|
| 661 |
+
""")
|
| 662 |
|
| 663 |
bias_analyze_btn.click(
|
| 664 |
fn=analyze_bias_detection,
|
|
|
|
| 667 |
)
|
| 668 |
|
| 669 |
# Footer
|
| 670 |
+
gr.HTML(
|
| 671 |
"""
|
| 672 |
+
<div style="
|
| 673 |
+
margin-top: 3rem;
|
| 674 |
+
padding: 2rem;
|
| 675 |
+
background: rgba(30, 41, 59, 0.3);
|
| 676 |
+
border-top: 1px solid rgba(99, 102, 241, 0.2);
|
| 677 |
+
border-radius: 16px;
|
| 678 |
+
text-align: center;
|
| 679 |
+
">
|
| 680 |
+
<p style="
|
| 681 |
+
color: #64748b;
|
| 682 |
+
font-size: 0.95rem;
|
| 683 |
+
margin: 0;
|
| 684 |
+
">
|
| 685 |
+
Built with β€οΈ using <strong style="color: #a5b4fc;">Gradio</strong>,
|
| 686 |
+
<strong style="color: #c4b5fd;">Transformers</strong>, and
|
| 687 |
+
<strong style="color: #f9a8d4;">Captum</strong>
|
| 688 |
+
</p>
|
| 689 |
+
<p style="
|
| 690 |
+
color: #475569;
|
| 691 |
+
font-size: 0.85rem;
|
| 692 |
+
margin-top: 0.5rem;
|
| 693 |
+
">
|
| 694 |
+
Β© 2024 ViT Auditing Toolkit β’ For research and educational purposes
|
| 695 |
+
</p>
|
| 696 |
+
</div>
|
| 697 |
"""
|
| 698 |
)
|
| 699 |
|
| 700 |
# Launch the application
|
| 701 |
if __name__ == "__main__":
|
| 702 |
demo.launch(
|
| 703 |
+
server_name="localhost",
|
| 704 |
server_port=7860,
|
| 705 |
share=False,
|
| 706 |
show_error=True
|