File size: 6,026 Bytes
e83ff6c
 
 
 
 
 
 
 
 
 
 
e38baa3
 
e83ff6c
 
 
 
 
 
 
 
 
 
 
e38baa3
e83ff6c
 
e38baa3
 
 
 
e83ff6c
 
e38baa3
 
 
e83ff6c
a674d29
e83ff6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e38baa3
 
 
 
 
e83ff6c
e38baa3
e83ff6c
 
 
e38baa3
e83ff6c
 
 
 
 
 
 
 
 
 
 
e38baa3
e83ff6c
e38baa3
 
 
e83ff6c
 
e38baa3
e83ff6c
 
 
 
 
 
 
 
 
e38baa3
 
 
 
 
 
 
 
 
 
e83ff6c
 
 
 
 
e38baa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e83ff6c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
Patch-MFFT Tampering Forensics API
==================================
FastAPI service providing:
- 3-Class Forensics: Authentic vs Full-AI vs Spliced/Inpainted Tampering
- Real-time Pixel-Level Tampering Localization Masks (Base64 PNG)
- Radial Fourier Spectral Band Energy Decomposition
"""

import io
import os
import hashlib
from collections import OrderedDict
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict, List, Optional
from contextlib import asynccontextmanager

from PIL import Image
from fastapi import FastAPI, UploadFile, File, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from .schemas import HealthResponse, ModelsResponse, PredictionResponse, ModelInfo
from .model_server import TamperingModelServer, predict_ensemble

MODEL_INFO = {
    "tiny":     {"params": "563K",   "description": "Fastest - edge/mobile tampering detection profile (~4 ms)"},
    "base":     {"params": "3.28M",  "description": "Standard Profile - balanced accuracy and localized inpainting"},
    "large":    {"params": "13.94M", "description": "Deep Profile - recommended for vintage/retro AI & degraded media"},
    "ensemble": {"params": "17.22M", "description": "SOTA Production Ensemble - Conservative Fusion of Base + Large with Heatmap Max-Gating"},
}

PREDICTION_CACHE: OrderedDict = OrderedDict()
MAX_CACHE_SIZE = 256

model_registry: Dict[str, TamperingModelServer] = {}
DEFAULT_VARIANT = os.environ.get("PATCH_MFFT_VARIANT", "base")
IMG_SIZE = int(os.environ.get("PATCH_MFFT_IMG_SIZE", "384"))


@asynccontextmanager
async def lifespan(app: FastAPI):
    print("[startup] Initializing Patch-MFFT model registry...")
    ckpt_dir_env = os.environ.get("PATCH_MFFT_CHECKPOINT_DIR")
    if ckpt_dir_env and Path(ckpt_dir_env).exists():
        ckpt_dir = Path(ckpt_dir_env)
    elif (Path(__file__).parent.parent / "checkpoints").exists():
        ckpt_dir = Path(__file__).parent.parent / "checkpoints"
    else:
        ckpt_dir = Path(__file__).parent.parent / "model" / "checkpoints"

    for variant in ["tiny", "base", "large"]:
        ckpt_path = ckpt_dir / f"{variant}.pt"
        try:
            model_registry[variant] = TamperingModelServer(
                str(ckpt_path) if ckpt_path.exists() else None,
                variant=variant,
                img_size=IMG_SIZE,
            )
        except Exception as e:
            print(f"[startup] Failed to initialize variant '{variant}': {e}")
    print(f"[startup] Loaded variants: {list(model_registry.keys())}")
    yield
    model_registry.clear()


app = FastAPI(
    title="Patch-MFFT Tampering Forensics API",
    description="Multi-Frequency Transformer for Localized Inpainting, Two-Person Splicing, and Composite Blackmail Detection",
    version="1.0.0",
    lifespan=lifespan,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/", response_model=HealthResponse)
async def health():
    return HealthResponse(
        status="healthy",
        version="1.0.0",
        models_loaded=list(model_registry.keys()),
        timestamp=datetime.now(timezone.utc).isoformat(),
    )


@app.get("/models", response_model=ModelsResponse)
async def list_models():
    default_model = "ensemble" if ("base" in model_registry and "large" in model_registry) else (
        DEFAULT_VARIANT if DEFAULT_VARIANT in model_registry else (
            list(model_registry.keys())[0] if model_registry else "base"
        )
    )
    return ModelsResponse(
        default=default_model,
        models=[
            ModelInfo(
                id=v,
                loaded=(v in model_registry) or (v == "ensemble" and "base" in model_registry and "large" in model_registry),
                params=MODEL_INFO[v]["params"],
                description=MODEL_INFO[v]["description"],
            )
            for v in MODEL_INFO
        ],
    )


@app.post("/predict", response_model=PredictionResponse)
async def predict(
    file: UploadFile = File(...),
    model: str = "ensemble",
):
    variant = (model or "ensemble").lower()
    valid_variants = list(model_registry.keys()) + (["ensemble"] if "base" in model_registry and "large" in model_registry else [])
    if variant not in valid_variants:
        raise HTTPException(
            status_code=404,
            detail=f"Model variant '{variant}' not found. Available: {valid_variants}",
        )

    if not file.content_type or not file.content_type.startswith("image/"):
        raise HTTPException(status_code=400, detail="Uploaded file must be a valid image.")

    content = await file.read()
    if len(content) > 25 * 1024 * 1024:
        raise HTTPException(status_code=400, detail="Image size exceeds 25 MB limit.")

    sha256_hash = hashlib.sha256(content).hexdigest()
    cache_key = f"{sha256_hash}_{variant}"

    # Instant In-Memory LRU Cache Check (< 1 ms latency)
    if cache_key in PREDICTION_CACHE:
        cached_result = dict(PREDICTION_CACHE[cache_key])
        cached_result["cached"] = True
        PREDICTION_CACHE.move_to_end(cache_key)
        return PredictionResponse(**cached_result)

    try:
        image = Image.open(io.BytesIO(content)).convert("RGB")
    except Exception:
        raise HTTPException(status_code=400, detail="Corrupted image file.")

    if variant == "ensemble":
        result = predict_ensemble(
            model_registry["base"],
            model_registry["large"],
            image,
            filename=file.filename,
            sha256_hash=sha256_hash,
        )
    else:
        server = model_registry[variant]
        result = server.predict(image, filename=file.filename, sha256_hash=sha256_hash)

    # Save to high-speed LRU memory cache
    PREDICTION_CACHE[cache_key] = result
    if len(PREDICTION_CACHE) > MAX_CACHE_SIZE:
        PREDICTION_CACHE.popitem(last=False)

    return PredictionResponse(**result)