A Flask web application for de-identifying Protected Health Information (PHI) and Personally Identifiable Information (PII) from clinical text using a fine-tuned ClinicalBERT model.
- Automatic detection of PHI/PII entities (names, dates, locations, etc.)
- Surrogate data generation for consistent de-identification
- Web-based interface for easy text processing
- Support for staff vs. patient name classification
- Date shifting for temporal anonymization
- Flask - Web framework
- PyTorch - Deep learning framework
- Transformers - Hugging Face library for BERT models
- ClinicalBERT - Fine-tuned model (
obi/deid_bert_i2b2) for clinical NER
- Clone the repository:
git clone https://github.com/rajellumen/pii-phi.git
cd pii-phi- Create a virtual environment:
python -m venv .venv
.venv\Scripts\activate # Windows
# or
source .venv/bin/activate # Linux/Mac- Install dependencies:
pip install -r requirements.txt- Set environment variables:
# Create a .env file or set environment variables
FLASK_SECRET_KEY=your-secret-key-here
DEID_API_KEY=your-api-key-here # Optional, for API endpoint protection- Run the application:
python app.pyThe application will be available at http://localhost:9000
- AWS EC2 instance (Ubuntu recommended)
- Python 3.10+ installed
- Git installed
- Connect to your EC2 instance:
ssh -i your-key.pem ubuntu@your-ec2-ip- Clone the repository:
git clone https://github.com/rajellumen/pii-phi.git
cd pii-phi- Set up Python virtual environment:
python3 -m venv venv
source venv/bin/activate- Install dependencies:
pip install --upgrade pip
pip install -r requirements.txt- Set environment variables:
export FLASK_SECRET_KEY="your-secret-key-here"
export DEID_API_KEY="your-api-key-here" # Optional
export PORT=9000 # Or your preferred port- Run with Gunicorn (Production):
gunicorn -w 2 -b 0.0.0.0:9000 app:app- Or run as a systemd service (Recommended):
- Create
/etc/systemd/system/pii-phi.service:
[Unit] Description=PII/PHI De-identification Service After=network.target [Service] User=ubuntu WorkingDirectory=/home/ubuntu/pii-phi Environment="PATH=/home/ubuntu/pii-phi/venv/bin" Environment="FLASK_SECRET_KEY=your-secret-key-here" Environment="DEID_API_KEY=your-api-key-here" ExecStart=/home/ubuntu/pii-phi/venv/bin/gunicorn -w 2 -b 0.0.0.0:9000 app:app [Install] WantedBy=multi-user.target
- Enable and start:
sudo systemctl enable pii-phi sudo systemctl start pii-phi - Create
- Security Groups: Ensure port 9000 (or your chosen port) is open in EC2 Security Groups
- Model Download: First run will download the BERT model (~500MB), which may take 5-10 minutes
- Memory Requirements: The model requires significant memory. Use at least 2GB RAM instance
- HTTPS: Use a reverse proxy (nginx) with SSL certificate for production
- Firewall: Configure ufw to allow your port:
sudo ufw allow 9000/tcp
| Variable | Required | Description |
|---|---|---|
FLASK_SECRET_KEY |
Yes | Secret key for Flask session management |
DEID_API_KEY |
No | API key for /deidentify endpoint protection |
PORT |
Auto | Port number (automatically set by Render) |
- Navigate to the web interface
- Paste or type clinical text containing PHI/PII
- Click "De-identify Text"
- View the de-identified text with highlighted changes
- Review the changes log showing all replacements
- Model:
obi/deid_bert_i2b2 - Purpose: Named Entity Recognition for clinical text
- Entity Types: PATIENT, STAFF, DATE, LOCATION, etc.
[Add your license here]
Raj Anantharaman
Email: ranantharaman@ellumen.com