PrivGuard is a local, pre-LLM security and data privacy gateway designed to sanitize business evidence documents. It detects and redacts Personally Identifiable Information (PII) before allowing the text to be passed to any downstream AI or LLM process. It operates strictly on a fail-closed security model.
Business documents often contain sensitive PII. Passing these raw documents directly to an LLM creates a significant data exposure risk. A security layer is needed to identify, classify, and redact PII while preserving the original structure and semantic context of the document.
This application accepts PDF, DOCX, PPTX, and TXT files, extracts their text while retaining page/slide/paragraph location data, runs a hybrid PII detection engine (Regex, NER, Context Rules), redacts the findings using consistent semantic placeholders (e.g., [PERSON_001]), and strictly validates the output to ensure no PII remains.
User ↓ File Upload ↓ File Classification ↓ Document Extraction (PyMuPDF, docx, pptx) ↓ Normalized Document (DocumentBlock) ↓ Hybrid PII Detection ├── Regex ├── NER (spaCy) └── Context Rules ↓ PII Classification (Severity, Confidence) ↓ Exposure Report ↓ Redaction ↓ Post-Redaction Validation ↓ SAFE / BLOCKED ↓ Sanitized Content
- PDF: Uses PyMuPDF. Preserves page boundaries and detects scanned pages.
- DOCX: Uses python-docx. Preserves paragraphs and tables.
- PPTX: Uses python-pptx. Preserves slides, text boxes, and tables.
- TXT: Preserves paragraphs.
- Regex: Email, Phone, Credit Card, IP Address.
- NER: Uses spaCy
en_core_web_smto detect Person, Address, Date of Birth. - Context Rules: Identifies structured PII like Employee ID, Government ID by analyzing surrounding text.
Replaces PII with semantic placeholders (e.g., [EMAIL_001]). The application ensures the same entity receives the same placeholder consistently within a document to preserve relationships for the downstream LLM.
After redaction, the sanitized text is rescanned by the PII detection engine. If any high-confidence PII remains, the document is immediately marked as BLOCKED.
- Fail-Closed: If extraction, detection, redaction, or validation fails, or if PII remains, the document is BLOCKED.
- Local Processing: No data is sent to external APIs or LLMs.
app.py: Streamlit Dashboard.models/: Data schemas (DocumentBlock, PIIFinding, etc.).extractors/: File parsers for PDF, DOCX, PPTX, TXT.detection/: Hybrid PII engine (regex, NER, context).redaction/: Redactor and Post-redaction validator.reporting/: Summary metrics and JSON/CSV generation.tests/: Pytest suite.
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
python -m spacy download en_core_web_smpytesseract requires the Tesseract OCR engine binary on Windows:
- Download the Windows installer from UB-Mannheim Tesseract (e.g.
tesseract-ocr-w64-setup-5.x.x.exe). - Install to the standard path:
C:\Program Files\Tesseract-OCR\. - Ensure the installation folder is added to Windows PATH, or set the environment variable:
setx TESSERACT_CMD "C:\Program Files\Tesseract-OCR\tesseract.exe"
- Fail-Closed Security Note: If Tesseract is not installed or configured, the gateway does not assume documents are PII-free. Scanned pages and embedded images will generate explicit
OCR_UNAVAILABLEuncertainty blocks so downstream security controls never release uninspected evidence.
streamlit run app.pypython -m pytest tests/ -v- PDF: Dual-stream extraction (native text with bounding boxes + 300 DPI page rendering + OCR pipeline + embedded images + document metadata).
- DOCX: Body paragraphs + structured table cells + section headers/footers + embedded screenshot OCR + core properties metadata.
- PPTX: Text frames + recursive grouped shape traversal (
MSO_SHAPE_TYPE.GROUP) + structured tables + speaker notes + image OCR + presentation metadata. - TXT: Multi-encoding fallback (UTF-8, Latin-1, CP1252) with paragraph metadata.
- Traceability: Normalized
DocumentBlockpreserves spatial coordinates (bbox),is_ocr,ocr_confidence, and hierarchical location identifiers.