Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import asyncio
|
| 2 |
import io
|
| 3 |
import os
|
|
|
|
| 4 |
import socket
|
| 5 |
import time
|
| 6 |
from contextlib import asynccontextmanager
|
|
@@ -19,31 +20,50 @@ from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor
|
|
| 19 |
|
| 20 |
MODEL_ID = "IDEA-Research/grounding-dino-base"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"female arm. female hand. female leg. female foot."
|
| 27 |
-
)
|
| 28 |
DETECTION_PROMPT = os.getenv("DETECTION_PROMPT", DEFAULT_PROMPT).strip().lower()
|
| 29 |
if not DETECTION_PROMPT.endswith("."):
|
| 30 |
DETECTION_PROMPT += "."
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
IMAGE_SHORT_EDGE = int(os.getenv("IMAGE_SHORT_EDGE", "640"))
|
| 35 |
IMAGE_LONG_EDGE = int(os.getenv("IMAGE_LONG_EDGE", "960"))
|
| 36 |
MAX_IMAGE_BYTES = int(os.getenv("MAX_IMAGE_BYTES", str(10 * 1024 * 1024)))
|
| 37 |
MAX_IMAGE_PIXELS = int(os.getenv("MAX_IMAGE_PIXELS", str(20_000_000)))
|
| 38 |
REQUEST_TIMEOUT_SECONDS = float(os.getenv("REQUEST_TIMEOUT_SECONDS", "20"))
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if IMAGE_SHORT_EDGE <= 0 or IMAGE_LONG_EDGE < IMAGE_SHORT_EDGE:
|
| 45 |
raise RuntimeError("Invalid image resize configuration")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Avoid CPU oversubscription on the 2-vCPU Space.
|
| 48 |
torch.set_num_threads(max(1, int(os.getenv("TORCH_NUM_THREADS", "2"))))
|
| 49 |
try:
|
|
@@ -190,6 +210,51 @@ def _load_model() -> None:
|
|
| 190 |
print(f"Grounding DINO ready in {time.perf_counter() - started:.1f}s")
|
| 191 |
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
| 194 |
processor = MODEL_DATA["processor"]
|
| 195 |
model = MODEL_DATA["model"]
|
|
@@ -202,17 +267,26 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 202 |
result = processor.post_process_grounded_object_detection(
|
| 203 |
outputs,
|
| 204 |
inputs.input_ids,
|
| 205 |
-
box_threshold=
|
| 206 |
-
text_threshold=
|
| 207 |
target_sizes=[(image.height, image.width)],
|
| 208 |
)[0]
|
| 209 |
|
| 210 |
-
detections = []
|
| 211 |
for score, label, box in zip(result["scores"], result["labels"], result["boxes"]):
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
detections.append(
|
| 214 |
{
|
| 215 |
-
"label":
|
|
|
|
| 216 |
"score": round(float(score), 4),
|
| 217 |
"box": {
|
| 218 |
"x_min": x_min,
|
|
@@ -224,16 +298,76 @@ def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
|
| 224 |
)
|
| 225 |
|
| 226 |
detections.sort(key=lambda item: item["score"], reverse=True)
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
return {
|
| 229 |
-
"decision":
|
| 230 |
-
"reason":
|
|
|
|
|
|
|
|
|
|
| 231 |
"detections": detections,
|
| 232 |
"detection_count": len(detections),
|
|
|
|
|
|
|
| 233 |
"top_score": detections[0]["score"] if detections else None,
|
| 234 |
"prompt": DETECTION_PROMPT,
|
| 235 |
-
"
|
| 236 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
"image": {"width": image.width, "height": image.height},
|
| 238 |
"elapsed": round(time.perf_counter() - started, 3),
|
| 239 |
"model": MODEL_ID,
|
|
@@ -255,8 +389,8 @@ async def lifespan(_: FastAPI):
|
|
| 255 |
|
| 256 |
app = FastAPI(
|
| 257 |
title="AI Shield - Grounding DINO Base",
|
| 258 |
-
description="
|
| 259 |
-
version="1.
|
| 260 |
lifespan=lifespan,
|
| 261 |
)
|
| 262 |
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import io
|
| 3 |
import os
|
| 4 |
+
import re
|
| 5 |
import socket
|
| 6 |
import time
|
| 7 |
from contextlib import asynccontextmanager
|
|
|
|
| 20 |
|
| 21 |
MODEL_ID = "IDEA-Research/grounding-dino-base"
|
| 22 |
|
| 23 |
+
# A balanced prompt is essential. A female-only prompt forces ordinary people
|
| 24 |
+
# and faces toward the closest female token, producing high false positives.
|
| 25 |
+
# Keep both male and female alternatives in the same inference pass.
|
| 26 |
+
DEFAULT_PROMPT = "adult man. adult woman. boy. girl."
|
|
|
|
|
|
|
| 27 |
DETECTION_PROMPT = os.getenv("DETECTION_PROMPT", DEFAULT_PROMPT).strip().lower()
|
| 28 |
if not DETECTION_PROMPT.endswith("."):
|
| 29 |
DETECTION_PROMPT += "."
|
| 30 |
|
| 31 |
+
# Low candidate thresholds retain competing labels. CLASS_THRESHOLD and
|
| 32 |
+
# GENDER_MARGIN make the final decision after male/female comparison.
|
| 33 |
+
CANDIDATE_BOX_THRESHOLD = float(os.getenv("CANDIDATE_BOX_THRESHOLD", "0.10"))
|
| 34 |
+
CANDIDATE_TEXT_THRESHOLD = float(os.getenv("CANDIDATE_TEXT_THRESHOLD", "0.10"))
|
| 35 |
+
CLASS_THRESHOLD = float(os.getenv("CLASS_THRESHOLD", "0.30"))
|
| 36 |
+
GENDER_MARGIN = float(os.getenv("GENDER_MARGIN", "0.10"))
|
| 37 |
+
MATCH_IOU_THRESHOLD = float(os.getenv("MATCH_IOU_THRESHOLD", "0.45"))
|
| 38 |
+
MATCH_CONTAINMENT_THRESHOLD = float(os.getenv("MATCH_CONTAINMENT_THRESHOLD", "0.80"))
|
| 39 |
+
UNCERTAIN_DECISION = os.getenv("UNCERTAIN_DECISION", "ALLOW").strip().upper()
|
| 40 |
IMAGE_SHORT_EDGE = int(os.getenv("IMAGE_SHORT_EDGE", "640"))
|
| 41 |
IMAGE_LONG_EDGE = int(os.getenv("IMAGE_LONG_EDGE", "960"))
|
| 42 |
MAX_IMAGE_BYTES = int(os.getenv("MAX_IMAGE_BYTES", str(10 * 1024 * 1024)))
|
| 43 |
MAX_IMAGE_PIXELS = int(os.getenv("MAX_IMAGE_PIXELS", str(20_000_000)))
|
| 44 |
REQUEST_TIMEOUT_SECONDS = float(os.getenv("REQUEST_TIMEOUT_SECONDS", "20"))
|
| 45 |
|
| 46 |
+
for threshold_name, threshold_value in {
|
| 47 |
+
"CANDIDATE_BOX_THRESHOLD": CANDIDATE_BOX_THRESHOLD,
|
| 48 |
+
"CANDIDATE_TEXT_THRESHOLD": CANDIDATE_TEXT_THRESHOLD,
|
| 49 |
+
"CLASS_THRESHOLD": CLASS_THRESHOLD,
|
| 50 |
+
"GENDER_MARGIN": GENDER_MARGIN,
|
| 51 |
+
"MATCH_IOU_THRESHOLD": MATCH_IOU_THRESHOLD,
|
| 52 |
+
"MATCH_CONTAINMENT_THRESHOLD": MATCH_CONTAINMENT_THRESHOLD,
|
| 53 |
+
}.items():
|
| 54 |
+
if not 0.0 <= threshold_value <= 1.0:
|
| 55 |
+
raise RuntimeError(f"{threshold_name} must be between 0 and 1")
|
| 56 |
+
if UNCERTAIN_DECISION not in {"ALLOW", "BLOCK"}:
|
| 57 |
+
raise RuntimeError("UNCERTAIN_DECISION must be ALLOW or BLOCK")
|
| 58 |
if IMAGE_SHORT_EDGE <= 0 or IMAGE_LONG_EDGE < IMAGE_SHORT_EDGE:
|
| 59 |
raise RuntimeError("Invalid image resize configuration")
|
| 60 |
|
| 61 |
+
_prompt_words = set(re.findall(r"[a-z]+", DETECTION_PROMPT))
|
| 62 |
+
if not (_prompt_words & {"man", "boy", "male"}):
|
| 63 |
+
raise RuntimeError("DETECTION_PROMPT must contain at least one male class")
|
| 64 |
+
if not (_prompt_words & {"woman", "girl", "female"}):
|
| 65 |
+
raise RuntimeError("DETECTION_PROMPT must contain at least one female class")
|
| 66 |
+
|
| 67 |
# Avoid CPU oversubscription on the 2-vCPU Space.
|
| 68 |
torch.set_num_threads(max(1, int(os.getenv("TORCH_NUM_THREADS", "2"))))
|
| 69 |
try:
|
|
|
|
| 210 |
print(f"Grounding DINO ready in {time.perf_counter() - started:.1f}s")
|
| 211 |
|
| 212 |
|
| 213 |
+
def _label_gender(label: str) -> str:
|
| 214 |
+
words = set(re.findall(r"[a-z]+", label.lower()))
|
| 215 |
+
has_female = bool(words & {"woman", "girl", "female"})
|
| 216 |
+
has_male = bool(words & {"man", "boy", "male"})
|
| 217 |
+
if has_female and not has_male:
|
| 218 |
+
return "female"
|
| 219 |
+
if has_male and not has_female:
|
| 220 |
+
return "male"
|
| 221 |
+
return "ambiguous"
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def _boxes_match(first: dict[str, float], second: dict[str, float]) -> bool:
|
| 225 |
+
intersection_width = max(
|
| 226 |
+
0.0,
|
| 227 |
+
min(first["x_max"], second["x_max"]) - max(first["x_min"], second["x_min"]),
|
| 228 |
+
)
|
| 229 |
+
intersection_height = max(
|
| 230 |
+
0.0,
|
| 231 |
+
min(first["y_max"], second["y_max"]) - max(first["y_min"], second["y_min"]),
|
| 232 |
+
)
|
| 233 |
+
intersection = intersection_width * intersection_height
|
| 234 |
+
first_area = max(0.0, first["x_max"] - first["x_min"]) * max(
|
| 235 |
+
0.0, first["y_max"] - first["y_min"]
|
| 236 |
+
)
|
| 237 |
+
second_area = max(0.0, second["x_max"] - second["x_min"]) * max(
|
| 238 |
+
0.0, second["y_max"] - second["y_min"]
|
| 239 |
+
)
|
| 240 |
+
union = first_area + second_area - intersection
|
| 241 |
+
iou = intersection / union if union > 0.0 else 0.0
|
| 242 |
+
smaller_area = min(first_area, second_area)
|
| 243 |
+
containment = intersection / smaller_area if smaller_area > 0.0 else 0.0
|
| 244 |
+
return iou >= MATCH_IOU_THRESHOLD or containment >= MATCH_CONTAINMENT_THRESHOLD
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def _best_overlapping_score(
|
| 248 |
+
detection: dict[str, Any], opposite_detections: list[dict[str, Any]]
|
| 249 |
+
) -> float:
|
| 250 |
+
scores = [
|
| 251 |
+
opposite["score"]
|
| 252 |
+
for opposite in opposite_detections
|
| 253 |
+
if _boxes_match(detection["box"], opposite["box"])
|
| 254 |
+
]
|
| 255 |
+
return max(scores, default=0.0)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
def _run_grounding_dino(image: Image.Image) -> dict[str, Any]:
|
| 259 |
processor = MODEL_DATA["processor"]
|
| 260 |
model = MODEL_DATA["model"]
|
|
|
|
| 267 |
result = processor.post_process_grounded_object_detection(
|
| 268 |
outputs,
|
| 269 |
inputs.input_ids,
|
| 270 |
+
box_threshold=CANDIDATE_BOX_THRESHOLD,
|
| 271 |
+
text_threshold=CANDIDATE_TEXT_THRESHOLD,
|
| 272 |
target_sizes=[(image.height, image.width)],
|
| 273 |
)[0]
|
| 274 |
|
| 275 |
+
detections: list[dict[str, Any]] = []
|
| 276 |
for score, label, box in zip(result["scores"], result["labels"], result["boxes"]):
|
| 277 |
+
raw_x_min, raw_y_min, raw_x_max, raw_y_max = [float(value) for value in box.tolist()]
|
| 278 |
+
x_min = round(max(0.0, min(float(image.width), raw_x_min)), 2)
|
| 279 |
+
y_min = round(max(0.0, min(float(image.height), raw_y_min)), 2)
|
| 280 |
+
x_max = round(max(0.0, min(float(image.width), raw_x_max)), 2)
|
| 281 |
+
y_max = round(max(0.0, min(float(image.height), raw_y_max)), 2)
|
| 282 |
+
if x_max <= x_min or y_max <= y_min:
|
| 283 |
+
continue
|
| 284 |
+
|
| 285 |
+
label_text = str(label)
|
| 286 |
detections.append(
|
| 287 |
{
|
| 288 |
+
"label": label_text,
|
| 289 |
+
"gender_hint": _label_gender(label_text),
|
| 290 |
"score": round(float(score), 4),
|
| 291 |
"box": {
|
| 292 |
"x_min": x_min,
|
|
|
|
| 298 |
)
|
| 299 |
|
| 300 |
detections.sort(key=lambda item: item["score"], reverse=True)
|
| 301 |
+
female_detections = [item for item in detections if item["gender_hint"] == "female"]
|
| 302 |
+
male_detections = [item for item in detections if item["gender_hint"] == "male"]
|
| 303 |
+
|
| 304 |
+
for detection in detections:
|
| 305 |
+
if detection["gender_hint"] == "female":
|
| 306 |
+
opposing_score = _best_overlapping_score(detection, male_detections)
|
| 307 |
+
elif detection["gender_hint"] == "male":
|
| 308 |
+
opposing_score = _best_overlapping_score(detection, female_detections)
|
| 309 |
+
else:
|
| 310 |
+
detection["opposing_score"] = None
|
| 311 |
+
detection["score_margin"] = None
|
| 312 |
+
detection["confirmed"] = False
|
| 313 |
+
continue
|
| 314 |
+
|
| 315 |
+
score_margin = detection["score"] - opposing_score
|
| 316 |
+
detection["opposing_score"] = round(opposing_score, 4)
|
| 317 |
+
detection["score_margin"] = round(score_margin, 4)
|
| 318 |
+
detection["confirmed"] = (
|
| 319 |
+
detection["score"] >= CLASS_THRESHOLD and score_margin >= GENDER_MARGIN
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
confirmed_females = [
|
| 323 |
+
item
|
| 324 |
+
for item in female_detections
|
| 325 |
+
if item["confirmed"]
|
| 326 |
+
]
|
| 327 |
+
confirmed_males = [
|
| 328 |
+
item
|
| 329 |
+
for item in male_detections
|
| 330 |
+
if item["confirmed"]
|
| 331 |
+
]
|
| 332 |
+
|
| 333 |
+
if confirmed_females:
|
| 334 |
+
decision = "BLOCK"
|
| 335 |
+
gender = "female"
|
| 336 |
+
reason = "female_detection_won_comparison"
|
| 337 |
+
elif confirmed_males:
|
| 338 |
+
decision = "ALLOW"
|
| 339 |
+
gender = "male"
|
| 340 |
+
reason = "male_detection_won_comparison"
|
| 341 |
+
elif detections:
|
| 342 |
+
decision = UNCERTAIN_DECISION
|
| 343 |
+
gender = "uncertain"
|
| 344 |
+
reason = "uncertain_gender_comparison"
|
| 345 |
+
else:
|
| 346 |
+
decision = "ALLOW"
|
| 347 |
+
gender = "none"
|
| 348 |
+
reason = "no_gender_candidate_detected"
|
| 349 |
+
|
| 350 |
+
female_score = max((item["score"] for item in female_detections), default=0.0)
|
| 351 |
+
male_score = max((item["score"] for item in male_detections), default=0.0)
|
| 352 |
return {
|
| 353 |
+
"decision": decision,
|
| 354 |
+
"reason": reason,
|
| 355 |
+
"gender": gender,
|
| 356 |
+
"male_score": round(male_score, 4),
|
| 357 |
+
"female_score": round(female_score, 4),
|
| 358 |
"detections": detections,
|
| 359 |
"detection_count": len(detections),
|
| 360 |
+
"confirmed_male_count": len(confirmed_males),
|
| 361 |
+
"confirmed_female_count": len(confirmed_females),
|
| 362 |
"top_score": detections[0]["score"] if detections else None,
|
| 363 |
"prompt": DETECTION_PROMPT,
|
| 364 |
+
"candidate_box_threshold": CANDIDATE_BOX_THRESHOLD,
|
| 365 |
+
"candidate_text_threshold": CANDIDATE_TEXT_THRESHOLD,
|
| 366 |
+
"class_threshold": CLASS_THRESHOLD,
|
| 367 |
+
"gender_margin": GENDER_MARGIN,
|
| 368 |
+
"match_iou_threshold": MATCH_IOU_THRESHOLD,
|
| 369 |
+
"match_containment_threshold": MATCH_CONTAINMENT_THRESHOLD,
|
| 370 |
+
"uncertain_decision": UNCERTAIN_DECISION,
|
| 371 |
"image": {"width": image.width, "height": image.height},
|
| 372 |
"elapsed": round(time.perf_counter() - started, 3),
|
| 373 |
"model": MODEL_ID,
|
|
|
|
| 389 |
|
| 390 |
app = FastAPI(
|
| 391 |
title="AI Shield - Grounding DINO Base",
|
| 392 |
+
description="Balanced male/female comparison using one Grounding DINO Base model",
|
| 393 |
+
version="1.1.0",
|
| 394 |
lifespan=lifespan,
|
| 395 |
)
|
| 396 |
|