Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +2 -2
- app.py +10 -18
- requirements.txt +0 -2
Dockerfile
CHANGED
|
@@ -19,9 +19,9 @@ COPY --chown=user requirements.txt .
|
|
| 19 |
# Install dependencies using CPU-only extra index to prevent massive GPU CUDA installations
|
| 20 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu
|
| 21 |
|
| 22 |
-
# Pre-cache the
|
| 23 |
# This guarantees instant startup times when the Space boots up.
|
| 24 |
-
RUN python -c "from
|
| 25 |
|
| 26 |
# Copy the rest of the application files
|
| 27 |
COPY --chown=user . .
|
|
|
|
| 19 |
# Install dependencies using CPU-only extra index to prevent massive GPU CUDA installations
|
| 20 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu
|
| 21 |
|
| 22 |
+
# Pre-cache the standard PyTorch DistilBERT model during the image build process
|
| 23 |
# This guarantees instant startup times when the Space boots up.
|
| 24 |
+
RUN python -c "from transformers import pipeline; pipeline('token-classification', model='samuelolubukun/pii-ner-finetuned-distilbert')"
|
| 25 |
|
| 26 |
# Copy the rest of the application files
|
| 27 |
COPY --chown=user . .
|
app.py
CHANGED
|
@@ -2,8 +2,7 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from transformers import
|
| 6 |
-
from optimum.onnxruntime import ORTModelForTokenClassification
|
| 7 |
import uvicorn
|
| 8 |
|
| 9 |
app = FastAPI(
|
|
@@ -23,24 +22,17 @@ app.add_middleware(
|
|
| 23 |
class AnalyzeRequest(BaseModel):
|
| 24 |
text: str
|
| 25 |
|
| 26 |
-
# Load the
|
| 27 |
-
print("Loading
|
| 28 |
try:
|
| 29 |
-
model = ORTModelForTokenClassification.from_pretrained(
|
| 30 |
-
"samuelolubukun/pii-ner-edge-optimized",
|
| 31 |
-
file_name="model_quantized.onnx"
|
| 32 |
-
)
|
| 33 |
-
tokenizer = AutoTokenizer.from_pretrained("samuelolubukun/pii-ner-edge-optimized")
|
| 34 |
-
|
| 35 |
nlp_pipeline = pipeline(
|
| 36 |
"token-classification",
|
| 37 |
-
model=
|
| 38 |
-
tokenizer=tokenizer,
|
| 39 |
aggregation_strategy="simple"
|
| 40 |
)
|
| 41 |
-
print("
|
| 42 |
except Exception as e:
|
| 43 |
-
print(f"Error loading
|
| 44 |
nlp_pipeline = None
|
| 45 |
|
| 46 |
@app.get("/", response_class=HTMLResponse)
|
|
@@ -114,8 +106,8 @@ async def read_root():
|
|
| 114 |
</head>
|
| 115 |
<body>
|
| 116 |
<div class="card">
|
| 117 |
-
<h1>🛡️ PII Warden AI
|
| 118 |
-
<p>Your hosted
|
| 119 |
<div class="endpoint">POST /analyze</div>
|
| 120 |
<div class="badge">Online & Active</div>
|
| 121 |
</div>
|
|
@@ -126,7 +118,7 @@ async def read_root():
|
|
| 126 |
@app.post("/analyze")
|
| 127 |
async def analyze_text(request: AnalyzeRequest):
|
| 128 |
if nlp_pipeline is None:
|
| 129 |
-
raise HTTPException(status_code=503, detail="
|
| 130 |
|
| 131 |
try:
|
| 132 |
text = request.text
|
|
@@ -148,7 +140,7 @@ async def analyze_text(request: AnalyzeRequest):
|
|
| 148 |
return formatted_results
|
| 149 |
|
| 150 |
except Exception as e:
|
| 151 |
-
raise HTTPException(status_code=500, detail=f"
|
| 152 |
|
| 153 |
@app.get("/health")
|
| 154 |
async def health_check():
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from transformers import pipeline
|
|
|
|
| 6 |
import uvicorn
|
| 7 |
|
| 8 |
app = FastAPI(
|
|
|
|
| 22 |
class AnalyzeRequest(BaseModel):
|
| 23 |
text: str
|
| 24 |
|
| 25 |
+
# Load the standard PyTorch model from Hugging Face on startup
|
| 26 |
+
print("Loading DistilBERT PII model into memory...")
|
| 27 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
nlp_pipeline = pipeline(
|
| 29 |
"token-classification",
|
| 30 |
+
model="samuelolubukun/pii-ner-finetuned-distilbert",
|
|
|
|
| 31 |
aggregation_strategy="simple"
|
| 32 |
)
|
| 33 |
+
print("Model loaded successfully!")
|
| 34 |
except Exception as e:
|
| 35 |
+
print(f"Error loading model: {e}")
|
| 36 |
nlp_pipeline = None
|
| 37 |
|
| 38 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 106 |
</head>
|
| 107 |
<body>
|
| 108 |
<div class="card">
|
| 109 |
+
<h1>🛡️ PII Warden AI</h1>
|
| 110 |
+
<p>Your hosted PII inference endpoint is live. Configure your browser extension to query the endpoint below for Tier 2 context analysis.</p>
|
| 111 |
<div class="endpoint">POST /analyze</div>
|
| 112 |
<div class="badge">Online & Active</div>
|
| 113 |
</div>
|
|
|
|
| 118 |
@app.post("/analyze")
|
| 119 |
async def analyze_text(request: AnalyzeRequest):
|
| 120 |
if nlp_pipeline is None:
|
| 121 |
+
raise HTTPException(status_code=503, detail="Model pipeline not initialized.")
|
| 122 |
|
| 123 |
try:
|
| 124 |
text = request.text
|
|
|
|
| 140 |
return formatted_results
|
| 141 |
|
| 142 |
except Exception as e:
|
| 143 |
+
raise HTTPException(status_code=500, detail=f"Inference error: {str(e)}")
|
| 144 |
|
| 145 |
@app.get("/health")
|
| 146 |
async def health_check():
|
requirements.txt
CHANGED
|
@@ -2,7 +2,5 @@ fastapi==0.110.0
|
|
| 2 |
uvicorn==0.29.0
|
| 3 |
pydantic==2.6.4
|
| 4 |
transformers==4.38.2
|
| 5 |
-
optimum==1.18.0
|
| 6 |
-
onnxruntime==1.17.1
|
| 7 |
numpy<2
|
| 8 |
torch==2.2.1
|
|
|
|
| 2 |
uvicorn==0.29.0
|
| 3 |
pydantic==2.6.4
|
| 4 |
transformers==4.38.2
|
|
|
|
|
|
|
| 5 |
numpy<2
|
| 6 |
torch==2.2.1
|