andevs commited on
Commit
9f70028
·
verified ·
1 Parent(s): 11b099d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +239 -59
app.py CHANGED
@@ -1,20 +1,35 @@
1
  #!/usr/bin/env python3
2
- # app.py - Background Remover v32.7.0
3
  #
4
- # Fixes vs v32.6.5:
5
- # • UTF-8 filename bug — Content-Disposition uses RFC 5987 (filename*
6
- # + ASCII fallback). Emoji/Arabic/Chinese filenames no longer crash
7
- # with 'latin-1 codec can't encode character'.
8
- # • All custom X-* header values sanitized to ASCII-safe.
9
  #
10
- # Preserved from v32.6.5:
11
- # • Router verbatim from app(3).py.
12
- # • 4 active + 6 dormant models. Dormant requests reroute.
13
- # • Lazy birefnet — idle RSS ~20%.
14
- # • video_safe flag in /api/health.
15
- # • RSS from /proc/self/status (current, not peak).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # • Birefnet semaphore + RSS gate + idle eviction.
17
- # • In-memory bulk zip, alpha cache purge, spooled tempfile patch.
18
 
19
  # ============================================================
20
  # FORCE IN-MEMORY UPLOAD PARSING
@@ -50,6 +65,7 @@ import ctypes
50
  import resource
51
  import re as _re
52
  from collections import defaultdict, Counter, deque, OrderedDict
 
53
  from datetime import datetime
54
  from pathlib import Path
55
  from typing import Dict, List, Any, Optional
@@ -108,10 +124,11 @@ logger = logging.getLogger(__name__)
108
  # ============================================================
109
  def _header_safe(value, max_len: int = 200) -> str:
110
  """Make any string safe to use as an HTTP header value.
111
- Strips non-ASCII characters (emoji, Arabic, Chinese, Cyrillic) that
112
- would trigger 'latin-1 codec can't encode character' errors."""
113
  if not isinstance(value, str):
114
  return ""
 
115
  return value.encode('ascii', 'ignore').decode('ascii')[:max_len]
116
 
117
 
@@ -588,8 +605,15 @@ graphic_processor = GraphicProcessor()
588
 
589
 
590
  # ============================================================
591
- # BIREFRET SAFETY
592
  # ============================================================
 
 
 
 
 
 
 
593
  _birefnet_semaphore = threading.Semaphore(1)
594
  BIREFNET_RSS_GATE_PERCENT = 75.0
595
  BIREFNET_IDLE_EVICT_SECONDS = 60
@@ -710,7 +734,7 @@ BIREFNET_MAX_DIM = 384
710
 
711
 
712
  # ============================================================
713
- # MODEL CONFIG — 4 active, 6 dormant, birefnet lazy
714
  # ============================================================
715
  PRELOAD_MODELS = [
716
  ("silueta", "video only"),
@@ -775,10 +799,13 @@ NEURAL_CONFIGS = {
775
  'sam': {'fg': 240, 'bg': 10, 'erode': 10},
776
  }
777
 
 
 
 
778
  MODEL_RES_CAPS = {
779
  'u2netp': 512, 'silueta': 640, 'u2net': 640,
780
  'u2net_human_seg': 640, 'birefnet-general-lite': 384,
781
- 'birefnet-general': 384, 'isnet-general-use': 576,
782
  'isnet-anime': 640, 'u2net_cloth_seg': 512, 'sam': 512,
783
  }
784
 
@@ -863,12 +890,33 @@ def _has_meaningful_alpha(image):
863
  return False
864
 
865
 
 
 
 
 
 
 
 
 
 
 
 
866
  # ============================================================
867
  # NEURAL PROCESSOR
868
  # ============================================================
869
- def process_with_neural(image, session, model_name, force_no_matting=False):
 
 
 
 
 
 
870
  cfg = NEURAL_CONFIGS.get(model_name, {'fg': 240, 'bg': 10, 'erode': 10})
871
- use_matting = (model_name in MATTING_MODELS) and not force_no_matting
 
 
 
 
872
 
873
  attempts = []
874
  if use_matting:
@@ -1080,11 +1128,70 @@ def _prewarm_models():
1080
  _log_rss("warmup done")
1081
 
1082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1083
  logger.info("=" * 60)
1084
- logger.info("Background Remover v32.7.0")
1085
  logger.info(f"Preload: {[n for n, _ in PRELOAD_MODELS]}")
1086
  logger.info(f"Lazy active: {sorted(LAZY_ACTIVE_MODELS)}")
1087
  logger.info(f"Dormant: {sorted(DORMANT_MODELS.keys())}")
 
 
 
 
 
1088
  logger.info("=" * 60)
1089
 
1090
  preload_all_models()
@@ -1092,6 +1199,13 @@ initialize_models()
1092
  _prewarm_models()
1093
  _start_housekeeping()
1094
 
 
 
 
 
 
 
 
1095
  _log_rss("boot complete")
1096
  logger.info("=" * 60)
1097
 
@@ -1280,50 +1394,63 @@ def get_default_model():
1280
 
1281
 
1282
  def detect_fragments(image):
 
1283
  if image.mode != 'RGBA':
1284
  return {'fragment_ratio': 0, 'quality_score': 100,
1285
  'has_fragments': False, 'issues': []}
1286
  arr = np.array(image)
1287
- alpha = arr[:, :, 3]
1288
- rgb = arr[:, :, :3]
1289
- frag = 0
1290
- step = 4
1291
- for i in range(0, rgb.shape[0], step):
1292
- for j in range(0, rgb.shape[1], step):
1293
- a = alpha[i, j]
1294
- if 0 < a < 240:
1295
- r, g, b = rgb[i, j]
1296
- avg = (int(r) + int(g) + int(b)) / 3
1297
- if abs(int(r) - avg) > 20 or abs(int(g) - avg) > 20 or abs(int(b) - avg) > 20:
1298
- frag += 1
1299
- sampled = max(1, (rgb.shape[0] // step) * (rgb.shape[1] // step))
1300
- fr = frag / sampled
1301
  qs = max(0, 100 - fr * 300)
1302
- issues = []
1303
- if fr > 0.01:
1304
- issues.append(f'fragments ({fr:.1%})')
1305
  return {'fragment_ratio': fr, 'quality_score': qs,
1306
  'has_fragments': fr > 0.01, 'issues': issues}
1307
 
1308
 
1309
  # ============================================================
1310
- # IMAGE PIPELINE
1311
  # ============================================================
1312
  def process_image_sync(image_data, model_name=None, transparent=True,
1313
  max_size=1024, quality=95, preserve_hair=True,
1314
  use_alpha_matting=True, border_pixels=20, sharpen=True):
1315
  req = str(uuid.uuid4())[:8]
1316
- logger.info(f"[REQ {req}] start")
 
 
 
 
 
 
 
 
 
 
1317
  try:
1318
  image = Image.open(io.BytesIO(image_data))
1319
  original_size = image.size
1320
 
 
1321
  if _has_meaningful_alpha(image) and model_name is None:
1322
  if image.mode != 'RGBA':
1323
  image = image.convert('RGBA')
 
 
 
 
1324
  buf = io.BytesIO()
1325
  if transparent:
1326
- image.save(buf, format='PNG', optimize=True)
1327
  mt, ext = 'image/png', 'png'
1328
  else:
1329
  w = Image.new('RGBA', image.size, (255, 255, 255, 255))
@@ -1346,6 +1473,7 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1346
  'hair_refined': False, 'diagnostics': [],
1347
  'dormant_rerouted': None, 'birefnet_swapped': None}
1348
 
 
1349
  if model_name:
1350
  resolved = MODEL_ALIASES.get(model_name, model_name)
1351
  selected = resolved
@@ -1365,7 +1493,7 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1365
  MODEL_RES_CAPS.get(selected, max_size),
1366
  )
1367
 
1368
- # Birefnet RSS gate
1369
  birefnet_swapped = None
1370
  if selected == 'birefnet-general-lite':
1371
  mem = get_container_memory()
@@ -1381,12 +1509,14 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1381
  MODEL_RES_CAPS.get(selected, max_size),
1382
  )
1383
 
 
1384
  if max(image.size) > target_res:
1385
  image.thumbnail((target_res, target_res), Image.Resampling.LANCZOS)
1386
 
1387
  diagnostics = []
1388
  result = None
1389
 
 
1390
  if selected in CLASSICAL_PROCESSORS:
1391
  t0 = time.time()
1392
  result = process_graphic_classical(image)
@@ -1396,6 +1526,7 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1396
  'elapsed': round(time.time() - t0, 3),
1397
  })
1398
 
 
1399
  if result is None:
1400
  tried = set()
1401
  for candidate in [selected, 'u2netp', 'isnet-general-use', 'birefnet-general-lite', 'silueta']:
@@ -1409,11 +1540,13 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1409
  if candidate == 'birefnet-general-lite':
1410
  with _birefnet_semaphore:
1411
  cand_result, cand_diag = process_with_neural(
1412
- image, cand_session, candidate
 
1413
  )
1414
  else:
1415
  cand_result, cand_diag = process_with_neural(
1416
- image, cand_session, candidate
 
1417
  )
1418
 
1419
  diagnostics.extend([{'model_tried': candidate, **d} for d in cand_diag])
@@ -1425,6 +1558,7 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1425
  if result is None:
1426
  raise HTTPException(500, "All models failed.")
1427
 
 
1428
  hair_refined = False
1429
  mask_quality = {'confidence': 1.0, 'issues': [], 'passed': True}
1430
  if result.mode == 'RGBA' and preserve_hair and cc_category in ('human_hair', 'human'):
@@ -1442,14 +1576,17 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1442
  except Exception:
1443
  pass
1444
 
 
1445
  if selected in ('u2netp', 'silueta', 'flood_fill', 'graphic_classical'):
1446
  frag = {'fragment_ratio': 0.0, 'quality_score': 100.0,
1447
  'has_fragments': False, 'issues': []}
1448
  else:
1449
  frag = detect_fragments(result)
1450
 
1451
- if result.size != original_size:
1452
- result = result.resize(original_size, Image.Resampling.LANCZOS)
 
 
1453
 
1454
  qs = frag['quality_score']
1455
  if qs >= 90: ql = 'Excellent'
@@ -1494,6 +1631,8 @@ def process_image_sync(image_data, model_name=None, transparent=True,
1494
  logger.error(f"Image processing error: {e}")
1495
  raise
1496
  finally:
 
 
1497
  gc.collect()
1498
  trim_memory()
1499
 
@@ -1604,7 +1743,7 @@ def _process_frames_sync(raw_frames, resolved_model, transparent,
1604
  # ============================================================
1605
  # FASTAPI
1606
  # ============================================================
1607
- app = FastAPI(title="Background Remover", version="32.7.0")
1608
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
1609
  allow_methods=["*"], allow_headers=["*"])
1610
 
@@ -1633,7 +1772,10 @@ async def rate_limit_middleware(request, call_next):
1633
  return await call_next(request)
1634
  cid = _get_client_ip(request)
1635
  now = datetime.now()
1636
- rate_limits[cid] = [t for t in rate_limits[cid] if (now - t).seconds < 60]
 
 
 
1637
  if len(rate_limits[cid]) >= RATE_LIMIT:
1638
  return JSONResponse(status_code=429, content={'error': f'Rate limit {RATE_LIMIT}/min'})
1639
  rate_limits[cid].append(now)
@@ -1653,7 +1795,7 @@ def timeout_handler(seconds=90):
1653
 
1654
 
1655
  @app.post("/api/process/image")
1656
- @timeout_handler(seconds=90)
1657
  async def process_image_api(
1658
  file: UploadFile = File(...),
1659
  model: str = Query(None),
@@ -1757,12 +1899,16 @@ async def process_frames_batch(
1757
  except Exception:
1758
  raw_frames.append(b"")
1759
 
 
 
 
 
1760
  loop = asyncio.get_event_loop()
1761
 
1762
  def _worker():
1763
  with _video_semaphore:
1764
  return _process_frames_sync(
1765
- raw_frames, VIDEO_FORCE_MODEL, transparent, session_id, smooth,
1766
  )
1767
 
1768
  try:
@@ -1774,7 +1920,7 @@ async def process_frames_batch(
1774
  gc.collect()
1775
  trim_memory()
1776
 
1777
- used = next((r.get("model") for r in results if r.get("model")), VIDEO_FORCE_MODEL)
1778
  smoothed_count = sum(1 for r in results if r.get("smoothed"))
1779
  return {
1780
  "frames": results, "count": len(results), "model": used,
@@ -1796,8 +1942,28 @@ async def frame_session_stats():
1796
 
1797
 
1798
  @app.get("/api/warmup")
1799
- async def warmup():
1800
- return {"status": "warm", "ts": datetime.now().isoformat()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1801
 
1802
 
1803
  @app.get("/api/models")
@@ -1859,6 +2025,7 @@ async def debug_memory():
1859
  'lazy_active_models': sorted(LAZY_ACTIVE_MODELS),
1860
  'dormant_models': sorted(DORMANT_MODELS.keys()),
1861
  'alpha_cache': _alpha_cache_stats(),
 
1862
  }
1863
 
1864
 
@@ -1941,6 +2108,9 @@ async def debug_env():
1941
  'preload_models': [n for n, _ in PRELOAD_MODELS],
1942
  'torch_loaded': 'torch' in sys.modules,
1943
  'onnx_threads': 1,
 
 
 
1944
  }
1945
 
1946
 
@@ -1951,7 +2121,7 @@ async def home():
1951
  warmed = sum(1 for w in stats['warmed'].values() if w)
1952
  return HTMLResponse(f"""
1953
  <!DOCTYPE html><html><head><meta charset="UTF-8">
1954
- <title>Background Remover v32.7.0</title>
1955
  <style>body{{font-family:system-ui;background:#0A0A0A;color:#fff;
1956
  min-height:100vh;display:flex;align-items:center;justify-content:center;
1957
  margin:0;padding:20px}}.c{{max-width:900px;background:#111;
@@ -1959,12 +2129,12 @@ async def home():
1959
  .s{{padding:15px;border-radius:12px;margin:20px 0;background:#1a1a1a}}
1960
  a{{color:#7ED957}}</style>
1961
  </head><body><div class="c">
1962
- <h1>Background Remover v32.7.0</h1>
1963
  <div class="s">AI online. {len(stats['loaded_models'])} models loaded ({warmed} warmed).<br>
1964
  Current RSS {mem['current_rss_percent']:.0f}% ({mem['current_rss_mb']}MB) ·
1965
  Peak {mem['peak_rss_percent']:.0f}%</div>
1966
  <p>Preload: silueta · u2netp · isnet</p>
1967
- <p>Lazy: birefnet-lite (~15-18s first request)</p>
1968
  <p><a href="/docs">API Docs</a> ·
1969
  <a href="/api/models">Models</a> ·
1970
  <a href="/api/debug/memory">Memory</a> ·
@@ -1991,6 +2161,11 @@ async def health_check():
1991
  and mem['current_rss_percent'] < (VIDEO_RAM_REJECT_PERCENT - VIDEO_SAFETY_MARGIN_PERCENT)
1992
  )
1993
 
 
 
 
 
 
1994
  if not AI_AVAILABLE:
1995
  status = 'error'
1996
  elif not all_preloaded_loaded:
@@ -2004,6 +2179,7 @@ async def health_check():
2004
 
2005
  return {
2006
  'status': status,
 
2007
  'video_safe': video_safe,
2008
  'image_safe': all_preloaded_warmed and mem['current_rss_percent'] < 88,
2009
  'ai_available': AI_AVAILABLE,
@@ -2026,10 +2202,11 @@ async def health_check():
2026
  'video_force_model': VIDEO_FORCE_MODEL,
2027
  'birefnet_res_cap': MODEL_RES_CAPS.get('birefnet-general-lite'),
2028
  'birefnet_rss_gate': BIREFNET_RSS_GATE_PERCENT,
2029
- 'version': '32.7.0',
2030
  'alpha_smoothing': _alpha_cache_stats(),
2031
  'onnx_threads': 1,
2032
  'torch_loaded': 'torch' in sys.modules,
 
2033
  }
2034
 
2035
 
@@ -2049,12 +2226,15 @@ async def gen_exc(request, exc):
2049
  @app.on_event("startup")
2050
  async def startup():
2051
  logger.info("=" * 60)
2052
- logger.info("v32.7.0 ready")
2053
  logger.info(f"Preload: {[n for n, _ in PRELOAD_MODELS]}")
2054
  logger.info(f"Lazy active: {sorted(LAZY_ACTIVE_MODELS)}")
2055
  logger.info(f"Dormant: {sorted(DORMANT_MODELS.keys())}")
 
 
 
 
2056
  logger.info("Header safety: RFC 5987 Content-Disposition (UTF-8 filenames)")
2057
- logger.info("Health exposes video_safe flag")
2058
  logger.info("=" * 60)
2059
  _log_rss("startup")
2060
 
 
1
  #!/usr/bin/env python3
2
+ # app.py - Background Remover v32.9.0
3
  #
4
+ # Merge: v32.8.0 base + v32.7.1 optimizations
 
 
 
 
5
  #
6
+ # From v32.8.0 (the actual 504 fix):
7
+ # • process_image_sync caps output at `max_size` (no LANCZOS upscale
8
+ # back to full resolution). 24 MP photos: 20-30 s → 3-5 s.
9
+ # • detect_fragments vectorized with NumPy.
10
+ # • /api/process/image timeout 28 s (was 90 s).
11
+ # • /api/warmup reports real warming state (+ ?fire=true).
12
+ # • Rate limiter uses total_seconds().
13
+ # • _header_safe strips CR/LF.
14
+ # • use_alpha_matting actually honored.
15
+ # • frames-batch honors `model` form field.
16
+ #
17
+ # From v32.7.1:
18
+ # • isnet matting resolution 576 → 384 (faster inference, negligible
19
+ # quality loss at this scale).
20
+ # • _image_semaphore serializes image inference (env: SERIALIZE_INFERENCE,
21
+ # default on). Prevents proxy retries from doubling CPU on 2-vCPU Space.
22
+ # • Background birefnet preload (env: PRELOAD_BIREFNET, default OFF;
23
+ # RSS gate at 45%, post-load evict at 85%, 45 s post-boot).
24
+ #
25
+ # New in v32.9.0:
26
+ # • /api/health includes `cold` boolean so UptimeRobot can distinguish
27
+ # "alive but still booting" from "alive and ready".
28
+ #
29
+ # Preserved:
30
+ # • Router, 4 active + 6 dormant models, lazy birefnet.
31
  # • Birefnet semaphore + RSS gate + idle eviction.
32
+ # • Alpha cache, in-memory upload parsing, RFC 5987 filenames.
33
 
34
  # ============================================================
35
  # FORCE IN-MEMORY UPLOAD PARSING
 
65
  import resource
66
  import re as _re
67
  from collections import defaultdict, Counter, deque, OrderedDict
68
+ from contextlib import nullcontext # [v32.9.0]
69
  from datetime import datetime
70
  from pathlib import Path
71
  from typing import Dict, List, Any, Optional
 
124
  # ============================================================
125
  def _header_safe(value, max_len: int = 200) -> str:
126
  """Make any string safe to use as an HTTP header value.
127
+ Strips CR/LF first (defensive vs response splitting), then strips
128
+ non-ASCII (emoji, Arabic, Chinese) that would trigger latin-1 errors."""
129
  if not isinstance(value, str):
130
  return ""
131
+ value = value.replace('\r', '').replace('\n', '')
132
  return value.encode('ascii', 'ignore').decode('ascii')[:max_len]
133
 
134
 
 
605
 
606
 
607
  # ============================================================
608
+ # SEMAPHORES [v32.9.0: added _image_semaphore from v32.7.1]
609
  # ============================================================
610
+ # Serialize image inference. On a 2-vCPU Space, proxy-side retries can
611
+ # spawn 2 simultaneous inferences, each taking ~2× longer as they fight
612
+ # for cores. Serialization makes retries queue instead of fight.
613
+ # Set SERIALIZE_INFERENCE=0 to disable (e.g. on multi-core paid tier).
614
+ SERIALIZE_INFERENCE = os.environ.get('SERIALIZE_INFERENCE', '1') == '1'
615
+ _image_semaphore = threading.Semaphore(1)
616
+
617
  _birefnet_semaphore = threading.Semaphore(1)
618
  BIREFNET_RSS_GATE_PERCENT = 75.0
619
  BIREFNET_IDLE_EVICT_SECONDS = 60
 
734
 
735
 
736
  # ============================================================
737
+ # MODEL CONFIG
738
  # ============================================================
739
  PRELOAD_MODELS = [
740
  ("silueta", "video only"),
 
799
  'sam': {'fg': 240, 'bg': 10, 'erode': 10},
800
  }
801
 
802
+ # [v32.9.0] isnet 576 → 384 (cherry-picked from v32.7.1).
803
+ # ~2-4 s faster per isnet request on 2-vCPU. Matting quality at 384 is
804
+ # indistinguishable from 576 for typical subjects.
805
  MODEL_RES_CAPS = {
806
  'u2netp': 512, 'silueta': 640, 'u2net': 640,
807
  'u2net_human_seg': 640, 'birefnet-general-lite': 384,
808
+ 'birefnet-general': 384, 'isnet-general-use': 384, # was 576
809
  'isnet-anime': 640, 'u2net_cloth_seg': 512, 'sam': 512,
810
  }
811
 
 
890
  return False
891
 
892
 
893
+ def _cap_output_size(size: tuple, max_size: int) -> tuple:
894
+ """Return (w, h) capped so the longer edge ≤ max_size.
895
+ Never upscales beyond the original."""
896
+ w, h = size
897
+ longest = max(w, h)
898
+ if longest <= max_size:
899
+ return (w, h)
900
+ scale = max_size / longest
901
+ return (max(1, int(w * scale)), max(1, int(h * scale)))
902
+
903
+
904
  # ============================================================
905
  # NEURAL PROCESSOR
906
  # ============================================================
907
+ def process_with_neural(image, session, model_name,
908
+ force_no_matting=False, use_alpha_matting=True):
909
+ """Run rembg with matting only when appropriate.
910
+ Matting is enabled iff:
911
+ • model is in MATTING_MODELS, AND
912
+ • caller didn't force-disable matting, AND
913
+ • caller's use_alpha_matting flag is True."""
914
  cfg = NEURAL_CONFIGS.get(model_name, {'fg': 240, 'bg': 10, 'erode': 10})
915
+ use_matting = (
916
+ (model_name in MATTING_MODELS)
917
+ and (not force_no_matting)
918
+ and use_alpha_matting
919
+ )
920
 
921
  attempts = []
922
  if use_matting:
 
1128
  _log_rss("warmup done")
1129
 
1130
 
1131
+ # ============================================================
1132
+ # [v32.9.0] Background birefnet preload (from v32.7.1, hardened)
1133
+ # ============================================================
1134
+ def _background_birefnet_preload():
1135
+ """Preload birefnet ~45 s after boot, in the background.
1136
+
1137
+ Off by default (PRELOAD_BIREFNET=1 enables). Rationale: 700 MB of
1138
+ resident memory whether or not anyone requests birefnet, and it adds
1139
+ ~15-20 s to cold start for a model that may go unused. On paid tier
1140
+ with more RAM, this is a clear win. On free tier, decide based on
1141
+ whether birefnet gets traffic.
1142
+
1143
+ Safeguards:
1144
+ • env gate (default off)
1145
+ • waits 45 s post-boot so initial request storm doesn't compete
1146
+ • pre-load RSS gate at 45%
1147
+ • post-load RSS check: if >85%, evict immediately
1148
+ """
1149
+ if os.environ.get('PRELOAD_BIREFNET', '0') != '1':
1150
+ logger.info("[bg-preload] PRELOAD_BIREFNET != 1, skipping")
1151
+ return
1152
+ time.sleep(45)
1153
+ try:
1154
+ if 'birefnet-general-lite' in model_manager.models:
1155
+ logger.info("[bg-preload] birefnet already loaded, skipping")
1156
+ return
1157
+ mem = get_container_memory()
1158
+ if mem['current_rss_percent'] > 45:
1159
+ logger.warning(
1160
+ f"[bg-preload] pre-load RSS {mem['current_rss_percent']:.1f}% > 45%, skipping"
1161
+ )
1162
+ return
1163
+ logger.warning("[bg-preload] loading birefnet in background...")
1164
+ t0 = time.time()
1165
+ session = model_manager.get_model('birefnet-general-lite', _load_session)
1166
+ if session is None:
1167
+ logger.warning("[bg-preload] birefnet load returned None")
1168
+ return
1169
+ post = get_container_memory()
1170
+ if post['current_rss_percent'] > 85:
1171
+ logger.warning(
1172
+ f"[bg-preload] post-load RSS {post['current_rss_percent']:.1f}% > 85%, "
1173
+ f"evicting to protect server"
1174
+ )
1175
+ model_manager.evict_model('birefnet-general-lite', 'post-preload RSS overrun')
1176
+ return
1177
+ _warm_single_model('birefnet-general-lite', session)
1178
+ logger.warning(
1179
+ f"[bg-preload] birefnet ready in {time.time() - t0:.1f}s (background)"
1180
+ )
1181
+ except Exception as e:
1182
+ logger.warning(f"[bg-preload] birefnet failed: {e}")
1183
+
1184
+
1185
  logger.info("=" * 60)
1186
+ logger.info("Background Remover v32.9.0")
1187
  logger.info(f"Preload: {[n for n, _ in PRELOAD_MODELS]}")
1188
  logger.info(f"Lazy active: {sorted(LAZY_ACTIVE_MODELS)}")
1189
  logger.info(f"Dormant: {sorted(DORMANT_MODELS.keys())}")
1190
+ logger.info(f"isnet matting res: {MODEL_RES_CAPS['isnet-general-use']} (was 576)")
1191
+ logger.info(f"SERIALIZE_INFERENCE: {SERIALIZE_INFERENCE}")
1192
+ logger.info(f"PRELOAD_BIREFNET: {os.environ.get('PRELOAD_BIREFNET','0')}")
1193
+ logger.info("Output cap: result ≤ max_size (never upscaled to original)")
1194
+ logger.info("Image timeout: 28s (aligned to proxy 20s+32s)")
1195
  logger.info("=" * 60)
1196
 
1197
  preload_all_models()
 
1199
  _prewarm_models()
1200
  _start_housekeeping()
1201
 
1202
+ # Start background birefnet preload (function itself checks env gate).
1203
+ threading.Thread(
1204
+ target=_background_birefnet_preload,
1205
+ daemon=True,
1206
+ name="birefnet-preload",
1207
+ ).start()
1208
+
1209
  _log_rss("boot complete")
1210
  logger.info("=" * 60)
1211
 
 
1394
 
1395
 
1396
  def detect_fragments(image):
1397
+ """Vectorized fragment detection (was a per-pixel Python loop)."""
1398
  if image.mode != 'RGBA':
1399
  return {'fragment_ratio': 0, 'quality_score': 100,
1400
  'has_fragments': False, 'issues': []}
1401
  arr = np.array(image)
1402
+ a = arr[::4, ::4, 3]
1403
+ rgb = arr[::4, ::4, :3].astype(np.int32)
1404
+ r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
1405
+ avg = (r + g + b) / 3.0
1406
+ chroma = (
1407
+ (np.abs(r - avg) > 20)
1408
+ | (np.abs(g - avg) > 20)
1409
+ | (np.abs(b - avg) > 20)
1410
+ )
1411
+ semi = (a > 0) & (a < 240)
1412
+ frag = int(np.sum(semi & chroma))
1413
+ sampled = a.size
1414
+ fr = frag / sampled if sampled else 0.0
 
1415
  qs = max(0, 100 - fr * 300)
1416
+ issues = [f'fragments ({fr:.1%})'] if fr > 0.01 else []
 
 
1417
  return {'fragment_ratio': fr, 'quality_score': qs,
1418
  'has_fragments': fr > 0.01, 'issues': issues}
1419
 
1420
 
1421
  # ============================================================
1422
+ # IMAGE PIPELINE — serialized via _image_semaphore [v32.9.0]
1423
  # ============================================================
1424
  def process_image_sync(image_data, model_name=None, transparent=True,
1425
  max_size=1024, quality=95, preserve_hair=True,
1426
  use_alpha_matting=True, border_pixels=20, sharpen=True):
1427
  req = str(uuid.uuid4())[:8]
1428
+ logger.info(f"[REQ {req}] start (max_size={max_size}, matting={use_alpha_matting})")
1429
+
1430
+ # [v32.9.0] Serialize inference when SERIALIZE_INFERENCE=1. Prevents
1431
+ # proxy retries from spawning two concurrent inferences on 2-vCPU.
1432
+ acquired = False
1433
+ if SERIALIZE_INFERENCE:
1434
+ logger.info(f"[REQ {req}] waiting for image semaphore")
1435
+ _image_semaphore.acquire()
1436
+ acquired = True
1437
+ logger.info(f"[REQ {req}] acquired")
1438
+
1439
  try:
1440
  image = Image.open(io.BytesIO(image_data))
1441
  original_size = image.size
1442
 
1443
+ # ---- Alpha passthrough ----
1444
  if _has_meaningful_alpha(image) and model_name is None:
1445
  if image.mode != 'RGBA':
1446
  image = image.convert('RGBA')
1447
+ # Cap passthrough output at max_size (was full-res).
1448
+ target = _cap_output_size(image.size, max_size)
1449
+ if target != image.size:
1450
+ image = image.resize(target, Image.Resampling.LANCZOS)
1451
  buf = io.BytesIO()
1452
  if transparent:
1453
+ image.save(buf, format='PNG', optimize=True, compress_level=1)
1454
  mt, ext = 'image/png', 'png'
1455
  else:
1456
  w = Image.new('RGBA', image.size, (255, 255, 255, 255))
 
1473
  'hair_refined': False, 'diagnostics': [],
1474
  'dormant_rerouted': None, 'birefnet_swapped': None}
1475
 
1476
+ # ---- Routing ----
1477
  if model_name:
1478
  resolved = MODEL_ALIASES.get(model_name, model_name)
1479
  selected = resolved
 
1493
  MODEL_RES_CAPS.get(selected, max_size),
1494
  )
1495
 
1496
+ # ---- Birefnet RSS gate ----
1497
  birefnet_swapped = None
1498
  if selected == 'birefnet-general-lite':
1499
  mem = get_container_memory()
 
1509
  MODEL_RES_CAPS.get(selected, max_size),
1510
  )
1511
 
1512
+ # Thumbnail input to target_res for model inference.
1513
  if max(image.size) > target_res:
1514
  image.thumbnail((target_res, target_res), Image.Resampling.LANCZOS)
1515
 
1516
  diagnostics = []
1517
  result = None
1518
 
1519
+ # ---- Classical path ----
1520
  if selected in CLASSICAL_PROCESSORS:
1521
  t0 = time.time()
1522
  result = process_graphic_classical(image)
 
1526
  'elapsed': round(time.time() - t0, 3),
1527
  })
1528
 
1529
+ # ---- Neural path (with fallbacks) ----
1530
  if result is None:
1531
  tried = set()
1532
  for candidate in [selected, 'u2netp', 'isnet-general-use', 'birefnet-general-lite', 'silueta']:
 
1540
  if candidate == 'birefnet-general-lite':
1541
  with _birefnet_semaphore:
1542
  cand_result, cand_diag = process_with_neural(
1543
+ image, cand_session, candidate,
1544
+ use_alpha_matting=use_alpha_matting,
1545
  )
1546
  else:
1547
  cand_result, cand_diag = process_with_neural(
1548
+ image, cand_session, candidate,
1549
+ use_alpha_matting=use_alpha_matting,
1550
  )
1551
 
1552
  diagnostics.extend([{'model_tried': candidate, **d} for d in cand_diag])
 
1558
  if result is None:
1559
  raise HTTPException(500, "All models failed.")
1560
 
1561
+ # ---- Hair refinement ----
1562
  hair_refined = False
1563
  mask_quality = {'confidence': 1.0, 'issues': [], 'passed': True}
1564
  if result.mode == 'RGBA' and preserve_hair and cc_category in ('human_hair', 'human'):
 
1576
  except Exception:
1577
  pass
1578
 
1579
+ # ---- Fragment detection (fast, vectorized) ----
1580
  if selected in ('u2netp', 'silueta', 'flood_fill', 'graphic_classical'):
1581
  frag = {'fragment_ratio': 0.0, 'quality_score': 100.0,
1582
  'has_fragments': False, 'issues': []}
1583
  else:
1584
  frag = detect_fragments(result)
1585
 
1586
+ # ---- Cap output at max_size — DO NOT upscale to original ----
1587
+ target_output = _cap_output_size(original_size, max_size)
1588
+ if result.size != target_output:
1589
+ result = result.resize(target_output, Image.Resampling.LANCZOS)
1590
 
1591
  qs = frag['quality_score']
1592
  if qs >= 90: ql = 'Excellent'
 
1631
  logger.error(f"Image processing error: {e}")
1632
  raise
1633
  finally:
1634
+ if acquired:
1635
+ _image_semaphore.release()
1636
  gc.collect()
1637
  trim_memory()
1638
 
 
1743
  # ============================================================
1744
  # FASTAPI
1745
  # ============================================================
1746
+ app = FastAPI(title="Background Remover", version="32.9.0")
1747
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
1748
  allow_methods=["*"], allow_headers=["*"])
1749
 
 
1772
  return await call_next(request)
1773
  cid = _get_client_ip(request)
1774
  now = datetime.now()
1775
+ rate_limits[cid] = [
1776
+ t for t in rate_limits[cid]
1777
+ if (now - t).total_seconds() < 60
1778
+ ]
1779
  if len(rate_limits[cid]) >= RATE_LIMIT:
1780
  return JSONResponse(status_code=429, content={'error': f'Rate limit {RATE_LIMIT}/min'})
1781
  rate_limits[cid].append(now)
 
1795
 
1796
 
1797
  @app.post("/api/process/image")
1798
+ @timeout_handler(seconds=28)
1799
  async def process_image_api(
1800
  file: UploadFile = File(...),
1801
  model: str = Query(None),
 
1899
  except Exception:
1900
  raw_frames.append(b"")
1901
 
1902
+ requested = MODEL_ALIASES.get(model, model)
1903
+ if requested not in VALID_REMBG_MODELS:
1904
+ requested = VIDEO_FORCE_MODEL
1905
+
1906
  loop = asyncio.get_event_loop()
1907
 
1908
  def _worker():
1909
  with _video_semaphore:
1910
  return _process_frames_sync(
1911
+ raw_frames, requested, transparent, session_id, smooth,
1912
  )
1913
 
1914
  try:
 
1920
  gc.collect()
1921
  trim_memory()
1922
 
1923
+ used = next((r.get("model") for r in results if r.get("model")), requested)
1924
  smoothed_count = sum(1 for r in results if r.get("smoothed"))
1925
  return {
1926
  "frames": results, "count": len(results), "model": used,
 
1942
 
1943
 
1944
  @app.get("/api/warmup")
1945
+ async def warmup(fire: bool = Query(False)):
1946
+ """Report real warming state.
1947
+ Uptime monitors can hit this cheaply to detect cold-start.
1948
+ Pass ?fire=true to force a warmup if not already warm."""
1949
+ if remove_func is None:
1950
+ raise HTTPException(503, "AI not initialized")
1951
+ loaded_names = list(model_manager.models.keys())
1952
+ warmed = {n: model_manager.warmed.get(n, False) for n in loaded_names}
1953
+ all_warmed = bool(warmed) and all(warmed.values())
1954
+ if fire and not all_warmed:
1955
+ loop = asyncio.get_event_loop()
1956
+ await loop.run_in_executor(cpu_executor, _prewarm_models)
1957
+ warmed = {n: model_manager.warmed.get(n, False) for n in loaded_names}
1958
+ all_warmed = bool(warmed) and all(warmed.values())
1959
+ return {
1960
+ "status": "warm" if all_warmed else "warming",
1961
+ "cold": not all_warmed, # [v32.9.0]
1962
+ "warmed_models": warmed,
1963
+ "loaded_models": loaded_names,
1964
+ "current_rss_percent": round(get_container_memory()['current_rss_percent'], 1),
1965
+ "ts": datetime.now().isoformat(),
1966
+ }
1967
 
1968
 
1969
  @app.get("/api/models")
 
2025
  'lazy_active_models': sorted(LAZY_ACTIVE_MODELS),
2026
  'dormant_models': sorted(DORMANT_MODELS.keys()),
2027
  'alpha_cache': _alpha_cache_stats(),
2028
+ 'serialize_inference': SERIALIZE_INFERENCE, # [v32.9.0]
2029
  }
2030
 
2031
 
 
2108
  'preload_models': [n for n, _ in PRELOAD_MODELS],
2109
  'torch_loaded': 'torch' in sys.modules,
2110
  'onnx_threads': 1,
2111
+ 'version': '32.9.0',
2112
+ 'serialize_inference': SERIALIZE_INFERENCE,
2113
+ 'preload_birefnet': os.environ.get('PRELOAD_BIREFNET', '0') == '1',
2114
  }
2115
 
2116
 
 
2121
  warmed = sum(1 for w in stats['warmed'].values() if w)
2122
  return HTMLResponse(f"""
2123
  <!DOCTYPE html><html><head><meta charset="UTF-8">
2124
+ <title>Background Remover v32.9.0</title>
2125
  <style>body{{font-family:system-ui;background:#0A0A0A;color:#fff;
2126
  min-height:100vh;display:flex;align-items:center;justify-content:center;
2127
  margin:0;padding:20px}}.c{{max-width:900px;background:#111;
 
2129
  .s{{padding:15px;border-radius:12px;margin:20px 0;background:#1a1a1a}}
2130
  a{{color:#7ED957}}</style>
2131
  </head><body><div class="c">
2132
+ <h1>Background Remover v32.9.0</h1>
2133
  <div class="s">AI online. {len(stats['loaded_models'])} models loaded ({warmed} warmed).<br>
2134
  Current RSS {mem['current_rss_percent']:.0f}% ({mem['current_rss_mb']}MB) ·
2135
  Peak {mem['peak_rss_percent']:.0f}%</div>
2136
  <p>Preload: silueta · u2netp · isnet</p>
2137
+ <p>Lazy: birefnet-lite · Serialize: {'ON' if SERIALIZE_INFERENCE else 'OFF'}</p>
2138
  <p><a href="/docs">API Docs</a> ·
2139
  <a href="/api/models">Models</a> ·
2140
  <a href="/api/debug/memory">Memory</a> ·
 
2161
  and mem['current_rss_percent'] < (VIDEO_RAM_REJECT_PERCENT - VIDEO_SAFETY_MARGIN_PERCENT)
2162
  )
2163
 
2164
+ # [v32.9.0] cold=True means still booting. Uptime monitors should
2165
+ # treat cold=True as "alive but don't send real traffic yet" and
2166
+ # call /api/warmup?fire=true to accelerate warming.
2167
+ cold = not all_preloaded_warmed
2168
+
2169
  if not AI_AVAILABLE:
2170
  status = 'error'
2171
  elif not all_preloaded_loaded:
 
2179
 
2180
  return {
2181
  'status': status,
2182
+ 'cold': cold, # [v32.9.0]
2183
  'video_safe': video_safe,
2184
  'image_safe': all_preloaded_warmed and mem['current_rss_percent'] < 88,
2185
  'ai_available': AI_AVAILABLE,
 
2202
  'video_force_model': VIDEO_FORCE_MODEL,
2203
  'birefnet_res_cap': MODEL_RES_CAPS.get('birefnet-general-lite'),
2204
  'birefnet_rss_gate': BIREFNET_RSS_GATE_PERCENT,
2205
+ 'version': '32.9.0',
2206
  'alpha_smoothing': _alpha_cache_stats(),
2207
  'onnx_threads': 1,
2208
  'torch_loaded': 'torch' in sys.modules,
2209
+ 'serialize_inference': SERIALIZE_INFERENCE, # [v32.9.0]
2210
  }
2211
 
2212
 
 
2226
  @app.on_event("startup")
2227
  async def startup():
2228
  logger.info("=" * 60)
2229
+ logger.info("v32.9.0 ready")
2230
  logger.info(f"Preload: {[n for n, _ in PRELOAD_MODELS]}")
2231
  logger.info(f"Lazy active: {sorted(LAZY_ACTIVE_MODELS)}")
2232
  logger.info(f"Dormant: {sorted(DORMANT_MODELS.keys())}")
2233
+ logger.info(f"SERIALIZE_INFERENCE: {SERIALIZE_INFERENCE}")
2234
+ logger.info(f"PRELOAD_BIREFNET: {os.environ.get('PRELOAD_BIREFNET','0')}")
2235
+ logger.info("Output cap: result ≤ max_size (never upscaled to original)")
2236
+ logger.info("Image timeout: 28s (aligned to proxy 20s+32s)")
2237
  logger.info("Header safety: RFC 5987 Content-Disposition (UTF-8 filenames)")
 
2238
  logger.info("=" * 60)
2239
  _log_rss("startup")
2240