Course ES
← back to chapter

PII redactor

Paste text with names, emails, phones, addresses, or card numbers and get it back with each replaced by a typed tag. A guardrail that runs on the way out — before you log, store, or forward.

Showcase — PII redactor

Paste text with names, emails, phones, addresses, or card numbers and get it back with each replaced by a typed tag. A guardrail that runs on the way out — before you log, store, or forward.

Run

bash bootstrap-secrets.sh              # reads ../../../../.env, writes secrets/
docker compose up --build              # default: PROVIDER=openai

Open http://localhost:3000. Gemini: PROVIDER=gemini docker compose up --build.

What's where

  • backend/ai_openai.py / backend/ai_gemini.py — the redaction prompt.
  • frontend/app/page.tsx — text box, redacted text out.

Stop

docker compose down

Run locally

Download the project as a ZIP and run it with Docker. Brings up a FastAPI backend + Next.js frontend on localhost:3000.

Download pii-redactor.zip

unzip pii-redactor.zip
cd pii-redactor
bash bootstrap-secrets.sh   # one-time: pulls API keys into ./secrets
docker compose up --build   # default provider: openai
# or:  PROVIDER=gemini docker compose up --build

Type some input, pick a provider, and run the same code shown in Source against the live API. Sign-in required.


  

The same modules the Run button hits. The whole project (frontend, Dockerfile, compose) is in the ZIP under README.

backend/ai_openai.py

"""Showcase 2 (OpenAI): PII redaction.

Before you log a prompt, store a transcript, or forward text to another service,
strip the personal data. Paste text with names, emails, phone numbers, or card
numbers and get it back with each replaced by a typed tag. A guardrail that runs
on the way OUT of your system, not just in.
"""
from openai import OpenAI

_client = OpenAI()

_MODEL = "gpt-5.4-nano"

_PROMPT = (
    "Redact all personally identifiable information in the user's text: person "
    "names, email addresses, phone numbers, street addresses, credit-card numbers, "
    "and government ids. Replace each with a typed tag like [NAME], [EMAIL], "
    "[PHONE], [ADDRESS], [CARD], [ID]. Preserve everything else exactly. Return "
    "ONLY the redacted text."
)


def run(text: str) -> str:
    t = text.strip()
    if not t:
        return "Paste text containing PII to redact."
    return _client.responses.create(model=_MODEL, instructions=_PROMPT, input=[{"role": "user", "content": t}]).output_text

backend/ai_gemini.py

"""Showcase 2 (Gemini): PII redaction.

Same redaction task, on Gemini.
"""
import os

from google import genai
from google.genai import types

_client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

_MODEL = "gemini-3.1-flash-lite"

_PROMPT = (
    "Redact all personally identifiable information in the user's text: person "
    "names, email addresses, phone numbers, street addresses, credit-card numbers, "
    "and government ids. Replace each with a typed tag like [NAME], [EMAIL], "
    "[PHONE], [ADDRESS], [CARD], [ID]. Preserve everything else exactly. Return "
    "ONLY the redacted text."
)


def run(text: str) -> str:
    t = text.strip()
    if not t:
        return "Paste text containing PII to redact."
    r = _client.models.generate_content(
        model=_MODEL,
        contents=[types.Content(role="user", parts=[types.Part(text=t)])],
        config=types.GenerateContentConfig(system_instruction=_PROMPT),
    )
    return r.text or ""

Project files

  • .gitignore
  • README.es.md
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/main.py
  • backend/requirements.txt
  • bootstrap-secrets.sh
  • docker-compose.yml
  • frontend/Dockerfile
  • frontend/app/layout.tsx
  • frontend/app/page.tsx
  • frontend/next.config.ts
  • frontend/package.json
  • frontend/tsconfig.json