Spaces:
Sleeping
Sleeping
Upload docs/usage.md with huggingface_hub
Browse files- docs/usage.md +88 -0
docs/usage.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# BiasXplainer — Usage Guide
|
| 2 |
+
|
| 3 |
+
This document covers how to use the BiasGuard Pro application for single analyses, batch processing, exports, and background jobs.
|
| 4 |
+
|
| 5 |
+
Requirements
|
| 6 |
+
|
| 7 |
+
- Python 3.10+ recommended
|
| 8 |
+
- Install dependencies from `requirements.txt`:
|
| 9 |
+
|
| 10 |
+
```bash
|
| 11 |
+
python -m venv .venv
|
| 12 |
+
source .venv/bin/activate
|
| 13 |
+
pip install -r requirements.txt
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
Launching the GUI (Gradio)
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
python main.py
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
The Gradio dashboard will open. Key areas:
|
| 23 |
+
|
| 24 |
+
- Input Text: analyze a single text with SHAP explanations and counterfactual suggestions.
|
| 25 |
+
- Quick Examples: sample prompts to populate the input box.
|
| 26 |
+
- Batch & Compare tab: paste multiple texts (one per line) or upload a file, then start a background batch job and refresh status.
|
| 27 |
+
|
| 28 |
+
Batch input formats
|
| 29 |
+
|
| 30 |
+
- Plain text (.txt): newline-separated texts.
|
| 31 |
+
- CSV (.csv): include a `text` column. If absent, the code will use the first available column per row.
|
| 32 |
+
- JSON (.json): either a list of strings, or a list of objects with a `text` field.
|
| 33 |
+
|
| 34 |
+
Background batch jobs
|
| 35 |
+
|
| 36 |
+
- Click "Start Background Batch" to create a background job. A Job ID is returned.
|
| 37 |
+
- Click "Refresh Job Status" and paste the Job ID to poll the job progress.
|
| 38 |
+
- When finished, the job record contains `results`, `summary`, and optional `comparison` (if you supplied group filters).
|
| 39 |
+
|
| 40 |
+
Exports
|
| 41 |
+
|
| 42 |
+
- When starting a background job you can provide a save path (e.g. `./export/results.json` or `./export/results.csv`) and the worker will attempt to write the results.
|
| 43 |
+
- After a run you can also use the Export JSON / Export CSV buttons to save the last-run results; files are written under `./export/` by default.
|
| 44 |
+
|
| 45 |
+
CLI-style batch example (programmatic)
|
| 46 |
+
|
| 47 |
+
You can import and use the dashboard classes in scripts. Example:
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
from main import BiasGuardDashboard
|
| 51 |
+
|
| 52 |
+
# Construct dashboard (this will initialize models)
|
| 53 |
+
d = BiasGuardDashboard()
|
| 54 |
+
|
| 55 |
+
texts = [
|
| 56 |
+
"Women should be nurses because they are compassionate.",
|
| 57 |
+
"Men are naturally better at engineering roles.",
|
| 58 |
+
"This is a neutral sentence."
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
# Run a blocking batch (not background)
|
| 62 |
+
results = d.analyze_batch(texts)
|
| 63 |
+
summary = d.summarize_batch(results)
|
| 64 |
+
print(summary)
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
Testing
|
| 68 |
+
|
| 69 |
+
Run unit tests with:
|
| 70 |
+
|
| 71 |
+
```bash
|
| 72 |
+
pytest -q
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
Notes and best practices
|
| 76 |
+
|
| 77 |
+
- For very large batches, prefer running scoring-only batches (you can adapt the code to call `detector.predict_batch_batched` directly) and run SHAP explanations only on a subset.
|
| 78 |
+
- The current background job runner is in-memory and suitable for single-machine development. For production, use a queue (e.g., Redis + RQ/Celery) and persistent job state.
|
| 79 |
+
- If you run into thread-safety issues with heavy models or SHAP in background threads, run the worker in a separate process or use a queue that launches worker processes.
|
| 80 |
+
|
| 81 |
+
Troubleshooting
|
| 82 |
+
|
| 83 |
+
- "Model load errors": ensure model files exist under `./models` or `model_path` points to a valid Hugging Face model.
|
| 84 |
+
- "Slow batches": reduce SHAP usage or increase batch_size in `predict_batch_batched`.
|
| 85 |
+
|
| 86 |
+
Contact
|
| 87 |
+
|
| 88 |
+
If you need additional features (streaming progress via websocket, Celery integration, or hosted deployment), open an issue or request the feature and I can implement it next.
|