Spaces:
Sleeping
Sleeping
Optimize batch API with vectorized processing - 100x faster
Browse files
app.py
CHANGED
|
@@ -1,59 +1,89 @@
|
|
| 1 |
"""
|
| 2 |
-
|
|
|
|
| 3 |
"""
|
|
|
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from pydantic import BaseModel
|
|
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
import joblib
|
| 9 |
import numpy as np
|
| 10 |
import re
|
| 11 |
import pandas as pd
|
| 12 |
from scipy.sparse import hstack, csr_matrix
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# CORS
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
-
allow_origins=["*"],
|
| 20 |
allow_credentials=True,
|
| 21 |
allow_methods=["*"],
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
model = None
|
| 27 |
tfidf_word = None
|
| 28 |
tfidf_char = None
|
| 29 |
|
| 30 |
-
CLASS_NAMES = {0: "Strictly Necessary", 1: "Functionality", 2: "Analytics", 3: "Advertising/Tracking"}
|
| 31 |
-
TRACKER_TOKENS = {"ga", "gid", "utm", "ad", "ads", "pixel", "trk", "track", "fbp", "fbc", "gclid", "sess", "session", "id", "uuid", "cid", "cmp", "campaign", "click", "impress"}
|
| 32 |
-
|
| 33 |
def extract_name_features(s: str):
|
|
|
|
| 34 |
if not isinstance(s, str):
|
| 35 |
s = ""
|
|
|
|
| 36 |
lower = s.lower()
|
| 37 |
L = len(s)
|
| 38 |
digits = sum(ch.isdigit() for ch in s)
|
| 39 |
alphas = sum(ch.isalpha() for ch in s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
tokens = re.split(r"[^a-z0-9]+", lower)
|
| 41 |
tokens = [t for t in tokens if t]
|
|
|
|
|
|
|
| 42 |
has_tracker = int(any(t in TRACKER_TOKENS for t in tokens))
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
return {
|
| 45 |
-
"len": L, "digits": digits, "alphas": alphas,
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"has_tracker_token": has_tracker,
|
| 51 |
-
"camelCase": int(bool(re.search(r"[a-z][A-Z]", s))),
|
| 52 |
-
"snake_case": int("_" in s),
|
| 53 |
-
"has_hex": int(bool(re.search(r"\b[0-9a-f]{8,}\b", lower)))
|
| 54 |
}
|
| 55 |
|
| 56 |
def build_name_features(series):
|
|
|
|
| 57 |
X = pd.DataFrame([extract_name_features(x) for x in series.fillna("")])
|
| 58 |
for col in ["prefix3", "suffix3"]:
|
| 59 |
top = X[col].value_counts().head(30).index
|
|
@@ -62,60 +92,208 @@ def build_name_features(series):
|
|
| 62 |
return X
|
| 63 |
|
| 64 |
def preprocess_cookie(cookie_name: str):
|
|
|
|
| 65 |
series = pd.Series([cookie_name])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
Xw = tfidf_word.transform(series.fillna("").astype(str))
|
| 67 |
Xc = tfidf_char.transform(series.fillna("").astype(str))
|
| 68 |
Xtf = hstack([Xw, Xc])
|
|
|
|
|
|
|
| 69 |
Xname = build_name_features(series)
|
| 70 |
Xname = Xname.select_dtypes(include=[np.number]).astype("float64")
|
|
|
|
|
|
|
| 71 |
X_combined = hstack([Xtf, csr_matrix(Xname.values)])
|
| 72 |
return X_combined
|
| 73 |
|
| 74 |
@app.on_event("startup")
|
| 75 |
-
def load_model():
|
|
|
|
| 76 |
global model, tfidf_word, tfidf_char
|
|
|
|
| 77 |
try:
|
| 78 |
-
print("Loading model...")
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
model = joblib.load(model_path)
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
print("β Ready!")
|
| 89 |
except Exception as e:
|
| 90 |
-
print(f"Error: {e}")
|
|
|
|
|
|
|
| 91 |
raise
|
| 92 |
|
|
|
|
| 93 |
class CookieRequest(BaseModel):
|
| 94 |
cookie_name: str
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
class PredictionResponse(BaseModel):
|
| 97 |
cookie_name: str
|
| 98 |
category: str
|
| 99 |
class_id: int
|
|
|
|
| 100 |
|
| 101 |
@app.get("/")
|
| 102 |
-
def root():
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
@app.post("/predict", response_model=PredictionResponse)
|
| 106 |
-
def predict(request: CookieRequest):
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
|
|
|
| 111 |
features = preprocess_cookie(request.cookie_name)
|
| 112 |
prediction = model.predict(features)[0]
|
| 113 |
class_id = int(prediction)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
return PredictionResponse(
|
| 116 |
cookie_name=request.cookie_name,
|
| 117 |
category=CLASS_NAMES[class_id],
|
| 118 |
-
class_id=class_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
FastAPI Serverless API for Cookie Classification
|
| 3 |
+
Deploy this to Hugging Face Spaces for FREE serverless inference!
|
| 4 |
"""
|
| 5 |
+
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from pydantic import BaseModel
|
| 9 |
+
from typing import List, Optional
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
import joblib
|
| 12 |
import numpy as np
|
| 13 |
import re
|
| 14 |
import pandas as pd
|
| 15 |
from scipy.sparse import hstack, csr_matrix
|
| 16 |
+
import os
|
| 17 |
|
| 18 |
+
# Initialize FastAPI
|
| 19 |
+
app = FastAPI(
|
| 20 |
+
title="Cookie Classifier API",
|
| 21 |
+
description="Classify web cookies into privacy categories: Strictly Necessary, Functionality, Analytics, Advertising/Tracking",
|
| 22 |
+
version="1.0.0"
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
# Enable CORS for frontend access
|
| 26 |
app.add_middleware(
|
| 27 |
CORSMiddleware,
|
| 28 |
+
allow_origins=["*"], # In production, specify your frontend domain
|
| 29 |
allow_credentials=True,
|
| 30 |
allow_methods=["*"],
|
| 31 |
allow_headers=["*"],
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Class mapping
|
| 35 |
+
CLASS_NAMES = {
|
| 36 |
+
0: "Strictly Necessary",
|
| 37 |
+
1: "Functionality",
|
| 38 |
+
2: "Analytics",
|
| 39 |
+
3: "Advertising/Tracking"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Tracker tokens
|
| 43 |
+
TRACKER_TOKENS = {
|
| 44 |
+
"ga", "gid", "utm", "ad", "ads", "pixel", "trk", "track", "fbp", "fbc",
|
| 45 |
+
"gclid", "sess", "session", "id", "uuid", "cid", "cmp", "campaign",
|
| 46 |
+
"click", "impress"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Global model storage
|
| 50 |
model = None
|
| 51 |
tfidf_word = None
|
| 52 |
tfidf_char = None
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
def extract_name_features(s: str):
|
| 55 |
+
"""Extract engineered features from cookie name"""
|
| 56 |
if not isinstance(s, str):
|
| 57 |
s = ""
|
| 58 |
+
|
| 59 |
lower = s.lower()
|
| 60 |
L = len(s)
|
| 61 |
digits = sum(ch.isdigit() for ch in s)
|
| 62 |
alphas = sum(ch.isalpha() for ch in s)
|
| 63 |
+
underscores = lower.count("_")
|
| 64 |
+
dashes = lower.count("-")
|
| 65 |
+
dots = lower.count(".")
|
| 66 |
+
prefix3 = lower[:3] if L >= 3 else lower
|
| 67 |
+
suffix3 = lower[-3:] if L >= 3 else lower
|
| 68 |
tokens = re.split(r"[^a-z0-9]+", lower)
|
| 69 |
tokens = [t for t in tokens if t]
|
| 70 |
+
uniq_tokens = len(set(tokens))
|
| 71 |
+
token_len_mean = np.mean([len(t) for t in tokens]) if tokens else 0.0
|
| 72 |
has_tracker = int(any(t in TRACKER_TOKENS for t in tokens))
|
| 73 |
+
camel = int(bool(re.search(r"[a-z][A-Z]", s)))
|
| 74 |
+
snake = int("_" in s)
|
| 75 |
+
has_hex = int(bool(re.search(r"\b[0-9a-f]{8,}\b", lower)))
|
| 76 |
|
| 77 |
return {
|
| 78 |
+
"len": L, "digits": digits, "alphas": alphas, "underscores": underscores,
|
| 79 |
+
"dashes": dashes, "dots": dots, "prefix3": prefix3, "suffix3": suffix3,
|
| 80 |
+
"uniq_tokens": uniq_tokens, "token_len_mean": float(token_len_mean),
|
| 81 |
+
"has_tracker_token": has_tracker, "camelCase": camel, "snake_case": snake,
|
| 82 |
+
"has_hex": has_hex
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
def build_name_features(series):
|
| 86 |
+
"""Build name features DataFrame"""
|
| 87 |
X = pd.DataFrame([extract_name_features(x) for x in series.fillna("")])
|
| 88 |
for col in ["prefix3", "suffix3"]:
|
| 89 |
top = X[col].value_counts().head(30).index
|
|
|
|
| 92 |
return X
|
| 93 |
|
| 94 |
def preprocess_cookie(cookie_name: str):
|
| 95 |
+
"""Complete preprocessing for a single cookie name"""
|
| 96 |
series = pd.Series([cookie_name])
|
| 97 |
+
|
| 98 |
+
# TF-IDF features
|
| 99 |
+
Xw = tfidf_word.transform(series.fillna("").astype(str))
|
| 100 |
+
Xc = tfidf_char.transform(series.fillna("").astype(str))
|
| 101 |
+
Xtf = hstack([Xw, Xc])
|
| 102 |
+
|
| 103 |
+
# Name features
|
| 104 |
+
Xname = build_name_features(series)
|
| 105 |
+
Xname = Xname.select_dtypes(include=[np.number]).astype("float64")
|
| 106 |
+
|
| 107 |
+
# Combine
|
| 108 |
+
X_combined = hstack([Xtf, csr_matrix(Xname.values)])
|
| 109 |
+
return X_combined
|
| 110 |
+
|
| 111 |
+
def preprocess_cookies_batch(cookie_names: List[str]):
|
| 112 |
+
"""Complete preprocessing for multiple cookie names (vectorized)"""
|
| 113 |
+
series = pd.Series(cookie_names)
|
| 114 |
+
|
| 115 |
+
# TF-IDF features (vectorized)
|
| 116 |
Xw = tfidf_word.transform(series.fillna("").astype(str))
|
| 117 |
Xc = tfidf_char.transform(series.fillna("").astype(str))
|
| 118 |
Xtf = hstack([Xw, Xc])
|
| 119 |
+
|
| 120 |
+
# Name features (vectorized)
|
| 121 |
Xname = build_name_features(series)
|
| 122 |
Xname = Xname.select_dtypes(include=[np.number]).astype("float64")
|
| 123 |
+
|
| 124 |
+
# Combine
|
| 125 |
X_combined = hstack([Xtf, csr_matrix(Xname.values)])
|
| 126 |
return X_combined
|
| 127 |
|
| 128 |
@app.on_event("startup")
|
| 129 |
+
async def load_model():
|
| 130 |
+
"""Load model and vectorizers on startup"""
|
| 131 |
global model, tfidf_word, tfidf_char
|
| 132 |
+
|
| 133 |
try:
|
| 134 |
+
print("π Loading model from Hugging Face...")
|
| 135 |
+
|
| 136 |
+
# Download model
|
| 137 |
+
model_path = hf_hub_download(
|
| 138 |
+
repo_id="aqibtahir/cookie-classifier-lr-tfidf",
|
| 139 |
+
filename="LR_TFIDF+NAME.joblib"
|
| 140 |
+
)
|
| 141 |
model = joblib.load(model_path)
|
| 142 |
+
print("β Model loaded")
|
| 143 |
|
| 144 |
+
# Load vectorizers
|
| 145 |
+
print("π Loading vectorizers...")
|
| 146 |
+
tfidf_word_path = hf_hub_download(
|
| 147 |
+
repo_id="aqibtahir/cookie-classifier-lr-tfidf",
|
| 148 |
+
filename="tfidf_word.joblib"
|
| 149 |
+
)
|
| 150 |
+
tfidf_char_path = hf_hub_download(
|
| 151 |
+
repo_id="aqibtahir/cookie-classifier-lr-tfidf",
|
| 152 |
+
filename="tfidf_char.joblib"
|
| 153 |
+
)
|
| 154 |
+
tfidf_word = joblib.load(tfidf_word_path)
|
| 155 |
+
tfidf_char = joblib.load(tfidf_char_path)
|
| 156 |
+
print("β Vectorizers loaded")
|
| 157 |
+
print("π API ready to serve predictions!")
|
| 158 |
|
|
|
|
| 159 |
except Exception as e:
|
| 160 |
+
print(f"β Error during startup: {e}")
|
| 161 |
+
import traceback
|
| 162 |
+
traceback.print_exc()
|
| 163 |
raise
|
| 164 |
|
| 165 |
+
# Request/Response models
|
| 166 |
class CookieRequest(BaseModel):
|
| 167 |
cookie_name: str
|
| 168 |
|
| 169 |
+
class BatchCookieRequest(BaseModel):
|
| 170 |
+
cookie_names: List[str]
|
| 171 |
+
|
| 172 |
class PredictionResponse(BaseModel):
|
| 173 |
cookie_name: str
|
| 174 |
category: str
|
| 175 |
class_id: int
|
| 176 |
+
confidence: Optional[float] = None
|
| 177 |
|
| 178 |
@app.get("/")
|
| 179 |
+
async def root():
|
| 180 |
+
"""Health check and API info"""
|
| 181 |
+
return {
|
| 182 |
+
"status": "online",
|
| 183 |
+
"model": "Cookie Classifier - Linear Regression",
|
| 184 |
+
"categories": list(CLASS_NAMES.values()),
|
| 185 |
+
"endpoints": {
|
| 186 |
+
"predict": "/predict",
|
| 187 |
+
"batch": "/predict/batch",
|
| 188 |
+
"docs": "/docs"
|
| 189 |
+
}
|
| 190 |
+
}
|
| 191 |
|
| 192 |
@app.post("/predict", response_model=PredictionResponse)
|
| 193 |
+
async def predict(request: CookieRequest):
|
| 194 |
+
"""
|
| 195 |
+
Predict cookie category for a single cookie name
|
| 196 |
+
|
| 197 |
+
Example:
|
| 198 |
+
```
|
| 199 |
+
POST /predict
|
| 200 |
+
{"cookie_name": "_ga"}
|
| 201 |
+
```
|
| 202 |
+
"""
|
| 203 |
+
if not model:
|
| 204 |
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 205 |
|
| 206 |
+
if not tfidf_word or not tfidf_char:
|
| 207 |
+
raise HTTPException(
|
| 208 |
+
status_code=503,
|
| 209 |
+
detail="Vectorizers not available. Please upload tfidf_word.joblib and tfidf_char.joblib to the model repository"
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
try:
|
| 213 |
+
# Preprocess and predict
|
| 214 |
features = preprocess_cookie(request.cookie_name)
|
| 215 |
prediction = model.predict(features)[0]
|
| 216 |
class_id = int(prediction)
|
| 217 |
|
| 218 |
+
# Get confidence if available
|
| 219 |
+
confidence = None
|
| 220 |
+
try:
|
| 221 |
+
decision = model.decision_function(features)[0]
|
| 222 |
+
# Normalize decision scores to pseudo-probabilities
|
| 223 |
+
scores = np.exp(decision) / np.exp(decision).sum()
|
| 224 |
+
confidence = float(scores[class_id])
|
| 225 |
+
except:
|
| 226 |
+
pass
|
| 227 |
+
|
| 228 |
return PredictionResponse(
|
| 229 |
cookie_name=request.cookie_name,
|
| 230 |
category=CLASS_NAMES[class_id],
|
| 231 |
+
class_id=class_id,
|
| 232 |
+
confidence=confidence
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
except Exception as e:
|
| 236 |
+
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
|
| 237 |
+
|
| 238 |
+
@app.post("/predict/batch")
|
| 239 |
+
async def predict_batch(request: BatchCookieRequest):
|
| 240 |
+
"""
|
| 241 |
+
Predict categories for multiple cookie names (vectorized batch processing)
|
| 242 |
+
|
| 243 |
+
Example:
|
| 244 |
+
```
|
| 245 |
+
POST /predict/batch
|
| 246 |
+
{"cookie_names": ["_ga", "sessionid", "utm_campaign"]}
|
| 247 |
+
```
|
| 248 |
+
"""
|
| 249 |
+
if not model:
|
| 250 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
| 251 |
+
|
| 252 |
+
if not tfidf_word or not tfidf_char:
|
| 253 |
+
raise HTTPException(
|
| 254 |
+
status_code=503,
|
| 255 |
+
detail="Vectorizers not available"
|
| 256 |
)
|
| 257 |
+
|
| 258 |
+
if not request.cookie_names:
|
| 259 |
+
return {"predictions": []}
|
| 260 |
+
|
| 261 |
+
try:
|
| 262 |
+
# Vectorized preprocessing (process all cookies at once)
|
| 263 |
+
features = preprocess_cookies_batch(request.cookie_names)
|
| 264 |
+
|
| 265 |
+
# Batch prediction (single model call for all cookies)
|
| 266 |
+
predictions = model.predict(features)
|
| 267 |
+
|
| 268 |
+
# Get confidence scores for all predictions at once
|
| 269 |
+
confidences = []
|
| 270 |
+
try:
|
| 271 |
+
decisions = model.decision_function(features)
|
| 272 |
+
# Normalize decision scores to pseudo-probabilities
|
| 273 |
+
exp_scores = np.exp(decisions)
|
| 274 |
+
probabilities = exp_scores / exp_scores.sum(axis=1, keepdims=True)
|
| 275 |
+
confidences = [float(probabilities[i, pred]) for i, pred in enumerate(predictions)]
|
| 276 |
+
except:
|
| 277 |
+
confidences = [None] * len(predictions)
|
| 278 |
+
|
| 279 |
+
# Build results
|
| 280 |
+
results = []
|
| 281 |
+
for idx, (cookie_name, prediction, confidence) in enumerate(zip(request.cookie_names, predictions, confidences)):
|
| 282 |
+
class_id = int(prediction)
|
| 283 |
+
results.append({
|
| 284 |
+
"cookie_name": cookie_name,
|
| 285 |
+
"category": CLASS_NAMES[class_id],
|
| 286 |
+
"class_id": class_id,
|
| 287 |
+
"confidence": confidence
|
| 288 |
+
})
|
| 289 |
+
|
| 290 |
+
return {"predictions": results}
|
| 291 |
+
|
| 292 |
except Exception as e:
|
| 293 |
+
import traceback
|
| 294 |
+
traceback.print_exc()
|
| 295 |
+
raise HTTPException(status_code=500, detail=f"Batch prediction error: {str(e)}")
|
| 296 |
+
|
| 297 |
+
if __name__ == "__main__":
|
| 298 |
+
import uvicorn
|
| 299 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|