CoolFace
Apppublic

Raadhesh/GDPR-Data-Privacy-Sanitizer

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

๐Ÿ›ก๏ธ GDPR Data Privacy Sanitizer

An RL environment where an AI agent must mask Personally Identifiable Information (PII) in a relational SQLite database โ€” without breaking foreign-key integrity or distorting statistical distributions.

๐ŸŒ Real-World Value

GDPR, CCPA, and HIPAA regulations require organisations to protect personal data. Manual compliance is slow, error-prone, and does not scale.

This environment teaches RL agents to automate privacy-preserving data transformations โ€” a problem worth billions of dollars in compliance costs every year.

The agent must learn:

ChallengeWhat the agent must preserve
Column RedactionExact string replacement without data corruption
Referential HashingForeign-key joins must still work after hashing
Statistical JitteringAggregate statistics must remain within tolerance

๐Ÿ“ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       JSON Action        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   LLM Agent    โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ  โ”‚  DataPrivacyEnv  โ”‚
โ”‚  (Llama 3 /    โ”‚                           โ”‚   (SQLite DB)    โ”‚
โ”‚   Groq API)    โ”‚ โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     Observation + Reward   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Observation Space

FieldTypeDescription
schema_infodictTable schemas (columns, types, constraints)
sample_datadictUp to 3 sample rows per table
last_error`str \null`Error from the previous step
step_countintSteps taken so far

Action Space

FieldTypeValues
commandLiteralquery, mask_column, finish
table_namestrTarget table name
column_namestrTarget column (for mask_column)
mask_typeLiteralredact, hash, jitter
sqlstrSQL SELECT query (for query)

Reward Structure

EventReward
Valid SQL query+0.1
Successfully mask a column+0.5
SQL errorโˆ’0.2
Destructive SQL detectedโˆ’1.0 (episode ends)

๐ŸŽฏ Tasks

Easy โ€” Column Redaction

Redact email and phone in the Users table. Grading: 1.0 if both columns contain only [REDACTED]. Partial credit (0.5) for one column.

Medium โ€” Referential Hashing

Hash Users.id and Purchases.user_id with SHA-256. A JOIN between the tables must still return all 5 original rows. Grading: 1.0 if hashes are consistent and the JOIN succeeds. 0.0 otherwise.

Hard โ€” Statistical Jittering

Jitter Employees.salary so that every value changes, but AVG(salary) remains within 5% of the original. Grading: 1.0 if all salaries changed and the average is within tolerance. 0.0 otherwise.


๐Ÿš€ Quick Start

1. Install Dependencies

bash
pip install -r requirements.txt

2. Set Your API Key

We use Groq for free Meta Llama 3 inference (you can also use any OpenAI-compatible endpoint):

bash
export GROQ_API_KEY="gsk_your_key_here"

3. Run the Inference Script

bash
# Easy task (default)
python inference.py

# Medium task
TASK=medium python inference.py

# Hard task
TASK=hard python inference.py

4. Run the Server (for judge validation)

bash
uvicorn server:app --host 0.0.0.0 --port 7860

Then test:

bash
curl -X POST http://localhost:7860/reset

5. Docker

bash
docker build -t gdpr-sanitizer .
docker run -p 7860:7860 gdpr-sanitizer

โœ… Validation

The judges' validate-submission.sh checks:

  1. 1.Dockerfile exists โœ“
  2. 2.Container builds โœ“
  3. 3.`POST /reset` returns 200 OK โœ“

This submission passes all three checks.


๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ models.py          # Pydantic Observation & Action schemas
โ”œโ”€โ”€ db_utils.py        # SQLite database setup with mock data
โ”œโ”€โ”€ environment.py     # Core OpenEnv environment (step/reset/state)
โ”œโ”€โ”€ graders.py         # Programmatic graders (easy/medium/hard)
โ”œโ”€โ”€ server.py          # FastAPI HTTP wrapper (/reset, /step, /state)
โ”œโ”€โ”€ inference.py       # Async LLM agent with strict logging
โ”œโ”€โ”€ openenv.yaml       # OpenEnv configuration
โ”œโ”€โ”€ Dockerfile         # Container (python:3.10-slim, port 7860)
โ”œโ”€โ”€ requirements.txt   # Python dependencies
โ””โ”€โ”€ README.md          # This file

๐Ÿงช Technical Notes

  • โ€”LLM Backend: Uses the openai Python client pointed at Groq's free API (https://api.groq.com/openai/v1) with llama3-8b-8192. Swap by setting API_BASE_URL and MODEL_NAME environment variables.
  • โ€”Structured Output: The LLM is prompted to return raw JSON matching the Pydantic Action schema. response_format={"type": "json_object"} enforces valid JSON.
  • โ€”Dense Rewards: Every step provides immediate feedback, enabling faster RL convergence.
  • โ€”Safety: Destructive SQL keywords (DROP, DELETE, etc.) are detected and immediately terminate the episode with a โˆ’1.0 penalty.

๐Ÿ“œ License

MIT โ€” Built for the Meta PyTorch ร— Hugging Face OpenEnv Hackathon.