from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse from pydantic import BaseModel from transformers import pipeline import uvicorn app = FastAPI( title="PII Warden Cloud AI Endpoint", description="Tier 2 Cloud AI Inference service for the PII Warden browser extension." ) # Enable CORS (Cross-Origin Resource Sharing) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) class AnalyzeRequest(BaseModel): text: str # Load the standard PyTorch model from Hugging Face on startup print("Loading DistilBERT PII model into memory...") try: nlp_pipeline = pipeline( "token-classification", model="samuelolubukun/pii-ner-finetuned-distilbert", aggregation_strategy="simple" ) print("Model loaded successfully!") except Exception as e: print(f"Error loading model: {e}") nlp_pipeline = None @app.get("/", response_class=HTMLResponse) async def read_root(): return """
Your hosted PII inference endpoint is live. Configure your browser extension to query the endpoint below for Tier 2 context analysis.