Spaces:
Sleeping
A newer version of the Gradio SDK is available: 6.25.0
title: BiasGuard Pro
emoji: π‘οΈ
colorFrom: purple
colorTo: pink
sdk: gradio
sdk_version: 6.2.0
python_version: '3.10'
app_file: app.py
pinned: false
An Explainable Text Bias Auditing & Counterfactual Suggestion Toolkit
π Live Demo | β¨ Features | π― Usage Guide | π§ Technical Details | π€ Contributing
π Overview
BiasXplainer (a.k.a. BiasGuard Pro) is an interactive and programmatic toolkit for:
- π Detecting potential bias (e.g. gendered language) in short text inputs
- π§ͺ Explaining classifier outputs via token-level SHAP attributions (with graceful fallbacks)
- π Generating neutral counterfactual rewrites while preserving semantics
- π Running batch analyses with aggregation statistics & comparative subgroup views
- π Exporting structured outputs (JSON/CSV) for downstream pipelines
This toolkit is designed as an exploratory auditing aidβideal for rapid experimentation, prototype fairness evaluation, educational demonstrations, and workflow integration proof-of-concepts.
β οΈ Important: This is not a definitive bias measurement instrument. For production or compliance use:
- Apply validated, domain-appropriate bias and fairness metrics
- Use diverse, representative corpora (not isolated sentences)
- Incorporate human review and domain expertise
- Follow established AI ethics & governance frameworks
π― Purpose & Scope
BiasXplainer helps you:
- Understand why a sentence may be flagged: token impacts clearly visualized
- Explore neutral alternatives via structured counterfactual suggestions
- Analyze variability across groups in batch mode (substring-based comparative lens)
- Export artifacts for integration with other auditing pipelines
π Why This Toolkit?
| Goal | How It's Achieved |
|---|---|
| Transparency | Token-level impact via SHAP + heuristic fallback |
| Counterfactual Exploration | Template + semantic replacements + FLANβT5 polish |
| Batch Insight | Aggregated stats: mean bias, class distribution, top impactful terms |
| Fairness Prototyping | Simple comparative view (e.g., βwomenβ vs βmenβ substring focus) |
| Extensibility | Modular architecture for adding models / exporters / fairness metrics |
| Developer Friendliness | Clean Python modules + minimal dependency surface |
β¨ Features
π¬ Core Capabilities
- Single Text Analysis: Bias score + classification + SHAP impact chart + highlighted tokens
- Counterfactual Suggestions: Structured rewrite candidates (neutralization focus)
- Batch Mode: Accepts
.txt,.csv,.jsonand runs background jobs - Comparative View: Substring-based group comparison (basic fairness proxy)
- Exports: JSON & CSV from UI or programmatic API
- Model Override: Auto-load local fine-tuned DistilBERT if present under
./models/ - Profiling Panel: Inline latency breakdown (tokenization, SHAP, rewrite phases)
π§© Extended Features
- β‘ Parallel bias + SHAP computation
- π§ͺ Resilient fallback keyword scoring if SHAP fails
- π FLANβT5-backed grammar polishing for counterfactuals
- π§± Batched inference APIs (
predict_batch_batched) - π§ͺ Hooks for future fairness metrics (equalized odds, subgroup performance gaps)
π Live Demo
Try it instantly on Hugging Face Spaces:
π Launch Interactive Demo
π Project Structure
BiasXplainer/
β
βββ main.py # Gradio entrypoint (BiasGuardDashboard)
βββ requirements.txt # Python dependencies
βββ README.md # This file
β
βββ core/
β βββ bias_detector.py # DistilBERT classifier logic
β βββ explainer.py # SHAP integration + fallback heuristic
β βββ counterfactuals.py # Rewrite engine + FLANβT5 polishing
β βββ utils.py # Helpers (token merging, formatting, etc.)
β
βββ export/
β βββ json_export.py # JSON serialization helper
β βββ csv_export.py # CSV serialization helper
β
βββ models/ # (Optional) Local fine-tuned model artifacts
βββ tests/ # Pytest suite (add fairness & stability tests)
βββ docs/
β βββ usage.md # Advanced usage patterns
β βββ api.md # Programmatic interface reference
β βββ roadmap.md # Extended roadmap details
β
βββ assets/ # (Optional) Screenshots / banners
βββ results/
β βββ latency_before_after.csv # Placeholder performance metrics file
βββ LICENSE # MIT license (add full text)
π― Usage Guide
Quick Start (Local)
git clone https://github.com/dyra-12/BiasXplainer.git
cd BiasXplainer
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python main.py
Open the printed local URL to start auditing text.
GUI Workflows
- Single Analysis β Paste text β Analyze β View bias score, class, SHAP token chart, highlighted impacts, and counterfactual suggestions.
- Batch & Compare β Paste multi-line text OR upload
.txt / .csv / .jsonβ Start job β Poll status β Download results. Optionally specify two substrings (e.g.,women,men). - Export β Use Export buttons (JSON/CSV) or programmatic API calls. Files saved under
./export/.
Supported Input Formats
| Format | Structure |
|---|---|
.txt |
Newline-separated sentences |
.csv |
Must include a text column (fallback to first column) |
.json |
List of strings OR list of objects with text field |
Minimal Programmatic API
from main import BiasGuardDashboard
dashboard = BiasGuardDashboard()
texts = [
"Women should be nurses because they are compassionate.",
"Men are naturally better at engineering roles.",
"This is a neutral sentence."
]
results = dashboard.analyzer.detector.predict_batch_batched(texts)
print(results) # [{'bias_probability': ..., 'classification': ..., 'confidence': ...}, ...]
π§ͺ Example Analysis Flow
1. User enters text
2. BiasDetector.predict_bias(text) β {probability, class, confidence}
3. Explainer.get_shap_values(text) β token-level impacts
4. CounterfactualGenerator.generate_counterfactuals(text, shap) β neutral rewrite candidates
5. UI renders gauge + impact bar + highlighted text + suggestions + profiling
Batch Flow
- Batched DistilBERT inference
- Async background job collects: mean bias probability, class distribution counts, top impactful tokens
- Optional simple substring comparison overlay
π§ Technical Details
Core Modules
| Module | Responsibility |
|---|---|
core/bias_detector.py |
DistilBERT-based classification (single + batched + efficient batching) |
core/explainer.py |
SHAP token attribution + fallback keyword heuristic |
core/counterfactuals.py |
Template-driven neutralization + FLANβT5 grammar refinement |
export/json_export.py |
Structured JSON serialization |
export/csv_export.py |
CSV export with impact flattening |
main.py |
Gradio composition, UI orchestration, profiling |
Models
- Classifier: DistilBERT (
distilbert-base-uncasedor local fine-tune under./models) - Polisher:
google/flan-t5-small(light rewrite improvements)
Performance Strategy
- Parallel futures for classifier + SHAP tasks
- Batching reduces tokenizer & forward overhead
- Token merging converts subword fragments into user-friendly units
- Inline profiling block surfaces latency bottlenecks
Counterfactual Strategy
- Extract high-impact tokens (SHAP or fallback keyword list)
- Apply neutral replacements or paraphrase templates
- Polish grammar & semantics via FLANβT5 Small
- Return ranked suggestions (preserving original intent)
π Performance
Record metrics in results/latency_before_after.csv (create if missing).
The project stores per-step latency measurements with the CSV schema: step,before_s,after_s,improvement_pct.
| Step | Before (s) | After (s) | Improvement (%) |
|---|---|---|---|
| predict_bias | 0.90 | 0.35 | 61.11 |
| get_shap_values | 24.00 | 6.60 | 72.50 |
| generate_counterfactuals | 2.50 | 1.00 | 60.00 |
| create_shap_chart | 0.80 | 0.50 | 37.50 |
| create_bias_meter | 0.20 | 0.15 | 25.00 |
| highlight_biased_words | 0.60 | 0.40 | 33.33 |
| total | 29.00 | 9.00 | 68.97 |
β Testing
pytest -q
Recommended test categories:
- Neutral vs biased fixtures (classification stability)
- SHAP fallback path when primary explainer errors
- Export correctness (headers + row counts)
- Deterministic counterfactual generation for controlled inputs
πΊ Roadmap
Short-Term:
- Live progress streaming (websocket / SSE)
- Extended fairness metrics (e.g., equalized odds, subgroup delta charts)
- Persistent job queue (Celery / RQ + Redis)
- Confidence calibration & uncertainty indicators
- Pluggable backbone registry (RoBERTa, DeBERTa, ALBERT)
Long-Term:
- Embedding-based semantic group comparison
- Multi-lingual model support
- Explanation fusion (SHAP + Integrated Gradients comparison)
- Audit session export bundles (results + metadata + configuration hash)
π Use Cases
Research
- Rapid prototyping of bias detection workflows
- Comparing attribution stability across variants
Industry
- Early-stage content moderation tool exploration
- Internal fairness experimentation sandbox
Education
- Teaching interpretability concepts interactively
- Student projects on ethical AI + XAI
π€ Contributing
We welcome improvements!
Ways to Contribute
- π Bug Reports: Open an issue with reproduction steps
- β¨ Feature Requests: Suggest metrics, exporters, or model options
- π Documentation: Improve guides, add examples
- π» Code: Submit well-scoped PRs with tests
- π Performance: Optimize latency & update benchmarking CSV
Development Setup
# Fork the repo, then:
git clone https://github.com/YOUR-USERNAME/BiasXplainer.git
cd BiasXplainer
# Create a branch
git checkout -b feat/your-feature-name
# Install & test
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
pytest -q
# Commit & push
git commit -m "Feat: descriptive summary"
git push origin feat/your-feature-name
Code Style
- Follow PEP 8
- Use type hints where practical
- Add docstrings to public functions
- Include tests for new logic paths
- Avoid large, multi-purpose PRs
βοΈ Ethical Use & Disclaimer
This toolkit provides heuristic insights into potential linguistic bias patterns. It does not guarantee:
- Fairness across real-world demographic groups
- Complete coverage of subtle stereotypes
- Context-aware ethical judgments
Always complement automated signals with:
- Human expert review
- Diverse, representative evaluation sets
- Formal fairness metrics and governance policies
π Security Notes
- Avoid submitting PII or confidential corpora to public hosted demos.
- For enterprise usage: run locally, restrict model paths, audit dependencies.
- Consider dependency pinning & vulnerability scanning (e.g.,
pip-audit,safety).
π License
This project is licensed under the MIT License β see the LICENSE file.
MIT License
Copyright (c) 2024 BiasXplainer Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction...
[Full license text here]
π Additional Resources
docs/usage.mdβ Advanced usage & troubleshootingdocs/api.mdβ Programmatic interface guidedocs/roadmap.mdβ Expanded roadmap details (optional)- Hugging Face Transformers Docs: https://huggingface.co/docs/transformers
- SHAP Documentation: https://shap.readthedocs.io/
π¬ Contact & Support
- Email: dyutidasmahaptra@gmail.com
- Hugging Face Space: https://huggingface.co/spaces/Dyra1204/BiasGuard-Pro
Built with β€οΈ by the community