Update app.py
Browse files
app.py
CHANGED
|
@@ -1772,4 +1772,374 @@ async def process_bulk_images(
|
|
| 1772 |
max_size=max_size, quality=95,
|
| 1773 |
preserve_hair=preserve_hair,
|
| 1774 |
use_alpha_matting=use_alpha_matting,
|
| 1775 |
-
border_pixels=border_pixels
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1772 |
max_size=max_size, quality=95,
|
| 1773 |
preserve_hair=preserve_hair,
|
| 1774 |
use_alpha_matting=use_alpha_matting,
|
| 1775 |
+
border_pixels=border_pixels, sharpen=sharpen,
|
| 1776 |
+
)
|
| 1777 |
+
scores.append(d['quality_score'])
|
| 1778 |
+
stem = Path(f.filename).stem or "image"
|
| 1779 |
+
out_stem = f"{stem}_remova"
|
| 1780 |
+
if out_stem in used_names:
|
| 1781 |
+
n = 2
|
| 1782 |
+
while f"{out_stem}_{n}" in used_names: n += 1
|
| 1783 |
+
out_stem = f"{out_stem}_{n}"
|
| 1784 |
+
used_names.add(out_stem)
|
| 1785 |
+
out_name = f"{out_stem}.{d['extension']}"
|
| 1786 |
+
zf.writestr(out_name, d['data'])
|
| 1787 |
+
count += 1
|
| 1788 |
+
except Exception as e:
|
| 1789 |
+
logger.error(f"Failed {f.filename}: {e}")
|
| 1790 |
+
|
| 1791 |
+
if count == 0: raise HTTPException(500, "No images processed")
|
| 1792 |
+
|
| 1793 |
+
zb.seek(0)
|
| 1794 |
+
pt = time.time() - t0
|
| 1795 |
+
avg = sum(scores) / len(scores) if scores else 0
|
| 1796 |
+
headers = {
|
| 1797 |
+
'X-Processing-Time': f'{pt:.3f}',
|
| 1798 |
+
'X-Model-Used': 'smart_routing',
|
| 1799 |
+
'X-Image-Count': str(count),
|
| 1800 |
+
'X-Avg-Quality-Score': f'{avg:.1f}',
|
| 1801 |
+
'Content-Disposition': f'attachment; filename=processed_images_{int(time.time())}.zip',
|
| 1802 |
+
}
|
| 1803 |
+
return StreamingResponse(zb, media_type='application/zip', headers=headers)
|
| 1804 |
+
except HTTPException:
|
| 1805 |
+
raise
|
| 1806 |
+
except Exception as e:
|
| 1807 |
+
logger.error(f"Bulk error: {e}")
|
| 1808 |
+
raise HTTPException(500, str(e)[:200])
|
| 1809 |
+
|
| 1810 |
+
|
| 1811 |
+
@app.post("/api/process/frames-batch")
|
| 1812 |
+
async def process_frames_batch(
|
| 1813 |
+
files: List[UploadFile] = File(...),
|
| 1814 |
+
model: str = Form(DEFAULT_VIDEO_MODEL),
|
| 1815 |
+
transparent: bool = Form(True),
|
| 1816 |
+
session_id: Optional[str] = Query(None),
|
| 1817 |
+
smooth: bool = Query(True),
|
| 1818 |
+
):
|
| 1819 |
+
if len(files) > VIDEO_MAX_BATCH:
|
| 1820 |
+
raise HTTPException(400, f"Max {VIDEO_MAX_BATCH} frames per batch")
|
| 1821 |
+
|
| 1822 |
+
mem = get_container_memory()
|
| 1823 |
+
if mem['rss_percent'] > VIDEO_RAM_REJECT_PERCENT:
|
| 1824 |
+
logger.warning(f"[frames-batch] RSS {mem['rss_percent']:.1f}% > {VIDEO_RAM_REJECT_PERCENT}%")
|
| 1825 |
+
return JSONResponse(
|
| 1826 |
+
status_code=503,
|
| 1827 |
+
content={
|
| 1828 |
+
'error': 'Server memory high. Retry shortly.',
|
| 1829 |
+
'rss_percent': round(mem['rss_percent'], 1),
|
| 1830 |
+
'rss_mb': mem['process_rss_mb'],
|
| 1831 |
+
'container_percent': round(mem['percent'], 1),
|
| 1832 |
+
'retry_in': 10, 'cold': True,
|
| 1833 |
+
},
|
| 1834 |
+
headers={'Retry-After': '10', 'Cache-Control': 'no-store'},
|
| 1835 |
+
)
|
| 1836 |
+
|
| 1837 |
+
raw_frames = []
|
| 1838 |
+
for f in files:
|
| 1839 |
+
try:
|
| 1840 |
+
raw_frames.append(await f.read())
|
| 1841 |
+
except Exception as e:
|
| 1842 |
+
logger.warning(f"[frames-batch] read failed: {e}")
|
| 1843 |
+
raw_frames.append(b"")
|
| 1844 |
+
|
| 1845 |
+
loop = asyncio.get_event_loop()
|
| 1846 |
+
|
| 1847 |
+
def _worker():
|
| 1848 |
+
with _video_semaphore:
|
| 1849 |
+
return _process_frames_sync(
|
| 1850 |
+
raw_frames, VIDEO_FORCE_MODEL, transparent, session_id, smooth,
|
| 1851 |
+
)
|
| 1852 |
+
|
| 1853 |
+
try:
|
| 1854 |
+
results = await loop.run_in_executor(cpu_executor, _worker)
|
| 1855 |
+
except Exception as e:
|
| 1856 |
+
logger.error(f"[frames-batch] worker crashed: {e}")
|
| 1857 |
+
raise HTTPException(500, f"Frame processing failed: {str(e)[:200]}")
|
| 1858 |
+
finally:
|
| 1859 |
+
gc.collect()
|
| 1860 |
+
trim_memory()
|
| 1861 |
+
|
| 1862 |
+
used = next((r.get("model") for r in results if r.get("model")), VIDEO_FORCE_MODEL)
|
| 1863 |
+
smoothed_count = sum(1 for r in results if r.get("smoothed"))
|
| 1864 |
+
return {
|
| 1865 |
+
"frames": results, "count": len(results), "model": used,
|
| 1866 |
+
"transparent": transparent,
|
| 1867 |
+
"session_id": session_id if (smooth and session_id) else None,
|
| 1868 |
+
"smoothed_count": smoothed_count,
|
| 1869 |
+
"smooth_weight": ALPHA_SMOOTH_WEIGHT if smoothed_count else 0.0,
|
| 1870 |
+
}
|
| 1871 |
+
|
| 1872 |
+
|
| 1873 |
+
@app.post("/api/frames/session/reset")
|
| 1874 |
+
async def reset_frame_session(session_id: str = Query(...)):
|
| 1875 |
+
existed = _reset_session(session_id)
|
| 1876 |
+
return {"ok": True, "session_id": session_id, "existed": existed}
|
| 1877 |
+
|
| 1878 |
+
|
| 1879 |
+
@app.get("/api/frames/session/stats")
|
| 1880 |
+
async def frame_session_stats():
|
| 1881 |
+
return _alpha_cache_stats()
|
| 1882 |
+
|
| 1883 |
+
|
| 1884 |
+
@app.post("/api/editor/auto-crop")
|
| 1885 |
+
async def editor_auto_crop(file: UploadFile = File(...), padding: int = Query(0, ge=0, le=20)):
|
| 1886 |
+
try:
|
| 1887 |
+
contents = await file.read()
|
| 1888 |
+
img = Image.open(io.BytesIO(contents)).convert('RGBA')
|
| 1889 |
+
arr = np.array(img)
|
| 1890 |
+
alpha = arr[:, :, 3]
|
| 1891 |
+
ys, xs = np.where(alpha > 20)
|
| 1892 |
+
if len(ys) == 0:
|
| 1893 |
+
return JSONResponse({'success': False, 'error': 'No foreground found'})
|
| 1894 |
+
x1, x2 = int(xs.min()), int(xs.max()) + 1
|
| 1895 |
+
y1, y2 = int(ys.min()), int(ys.max()) + 1
|
| 1896 |
+
w, h = img.size
|
| 1897 |
+
if padding > 0:
|
| 1898 |
+
pw = int((x2 - x1) * padding / 100)
|
| 1899 |
+
ph = int((y2 - y1) * padding / 100)
|
| 1900 |
+
x1 = max(0, x1 - pw); y1 = max(0, y1 - ph)
|
| 1901 |
+
x2 = min(w, x2 + pw); y2 = min(h, y2 + ph)
|
| 1902 |
+
return {'success': True, 'x': x1, 'y': y1,
|
| 1903 |
+
'width': x2 - x1, 'height': y2 - y1,
|
| 1904 |
+
'original_width': w, 'original_height': h}
|
| 1905 |
+
except Exception as e:
|
| 1906 |
+
logger.error(f"auto-crop failed: {e}")
|
| 1907 |
+
return JSONResponse({'success': False, 'error': str(e)}, status_code=500)
|
| 1908 |
+
|
| 1909 |
+
|
| 1910 |
+
@app.post("/api/editor/inpaint")
|
| 1911 |
+
async def editor_inpaint(file: UploadFile = File(...), mask: UploadFile = File(...),
|
| 1912 |
+
method: str = Query('telea'), dilate: int = Query(3)):
|
| 1913 |
+
try:
|
| 1914 |
+
contents = await file.read()
|
| 1915 |
+
mask_contents = await mask.read()
|
| 1916 |
+
pil = Image.open(io.BytesIO(contents)).convert('RGBA')
|
| 1917 |
+
arr = np.array(pil)
|
| 1918 |
+
rgb = arr[:, :, :3].copy()
|
| 1919 |
+
alpha = arr[:, :, 3].copy()
|
| 1920 |
+
h, w = rgb.shape[:2]
|
| 1921 |
+
mask_pil = Image.open(io.BytesIO(mask_contents)).convert('L')
|
| 1922 |
+
if mask_pil.size != pil.size:
|
| 1923 |
+
mask_pil = mask_pil.resize(pil.size, Image.Resampling.LANCZOS)
|
| 1924 |
+
mask_arr = np.array(mask_pil)
|
| 1925 |
+
if dilate > 0:
|
| 1926 |
+
mask_arr = cv2.dilate(mask_arr, np.ones((dilate, dilate), np.uint8), iterations=1)
|
| 1927 |
+
if method == 'ns':
|
| 1928 |
+
inpainted = cv2.inpaint(rgb, mask_arr, 3, cv2.INPAINT_NS)
|
| 1929 |
+
else:
|
| 1930 |
+
inpainted = cv2.inpaint(rgb, mask_arr, 3, cv2.INPAINT_TELEA)
|
| 1931 |
+
mask_float = mask_arr.astype(np.float32) / 255.0
|
| 1932 |
+
from scipy.ndimage import gaussian_filter
|
| 1933 |
+
mask_blur = gaussian_filter(mask_float, sigma=1.5)
|
| 1934 |
+
mask_3ch = mask_blur[:, :, None]
|
| 1935 |
+
blended = rgb * (1 - mask_3ch) + inpainted * mask_3ch
|
| 1936 |
+
blended = np.clip(blended, 0, 255).astype(np.uint8)
|
| 1937 |
+
result = np.zeros((h, w, 4), dtype=np.uint8)
|
| 1938 |
+
result[:, :, :3] = blended
|
| 1939 |
+
result[:, :, 3] = alpha
|
| 1940 |
+
out = Image.fromarray(result, 'RGBA')
|
| 1941 |
+
buf = io.BytesIO()
|
| 1942 |
+
out.save(buf, format='PNG', optimize=True)
|
| 1943 |
+
buf.seek(0)
|
| 1944 |
+
return StreamingResponse(buf, media_type='image/png')
|
| 1945 |
+
except Exception as e:
|
| 1946 |
+
logger.error(f"inpaint failed: {e}")
|
| 1947 |
+
raise HTTPException(500, f"Inpainting failed: {str(e)[:200]}")
|
| 1948 |
+
|
| 1949 |
+
|
| 1950 |
+
@app.get("/api/warmup")
|
| 1951 |
+
async def warmup():
|
| 1952 |
+
return {"status": "warm", "ts": datetime.now().isoformat()}
|
| 1953 |
+
|
| 1954 |
+
|
| 1955 |
+
@app.get("/api/debug/memory")
|
| 1956 |
+
async def debug_memory():
|
| 1957 |
+
mem = get_container_memory()
|
| 1958 |
+
stats = model_manager.get_stats()
|
| 1959 |
+
return {
|
| 1960 |
+
'container': mem,
|
| 1961 |
+
'process_rss_mb': mem['process_rss_mb'],
|
| 1962 |
+
'rss_percent': round(mem['rss_percent'], 1),
|
| 1963 |
+
'cgroup_percent': round(mem['percent'], 1),
|
| 1964 |
+
'note': 'rss_percent is the real number. cgroup_percent includes page cache.',
|
| 1965 |
+
'models_loaded': stats['loaded_models'],
|
| 1966 |
+
'models_count': stats['model_count'],
|
| 1967 |
+
'models_warmed': stats['warmed'],
|
| 1968 |
+
'use_counts': stats['use_counts'],
|
| 1969 |
+
'protected': stats['protected'],
|
| 1970 |
+
'alpha_cache': _alpha_cache_stats(),
|
| 1971 |
+
'video_reject_pct': VIDEO_RAM_REJECT_PERCENT,
|
| 1972 |
+
'torch_loaded': 'torch' in sys.modules,
|
| 1973 |
+
}
|
| 1974 |
+
|
| 1975 |
+
|
| 1976 |
+
@app.get("/api/debug/tmp")
|
| 1977 |
+
async def debug_tmp():
|
| 1978 |
+
items = []
|
| 1979 |
+
try:
|
| 1980 |
+
for root, dirs, files in os.walk('/tmp'):
|
| 1981 |
+
for f in files:
|
| 1982 |
+
p = os.path.join(root, f)
|
| 1983 |
+
try:
|
| 1984 |
+
items.append({
|
| 1985 |
+
'path': p,
|
| 1986 |
+
'size_mb': round(os.path.getsize(p) / 1048576, 2),
|
| 1987 |
+
'mtime': os.path.getmtime(p),
|
| 1988 |
+
})
|
| 1989 |
+
except Exception:
|
| 1990 |
+
pass
|
| 1991 |
+
except Exception:
|
| 1992 |
+
pass
|
| 1993 |
+
return {
|
| 1994 |
+
'count': len(items),
|
| 1995 |
+
'total_mb': round(sum(i['size_mb'] for i in items), 2),
|
| 1996 |
+
'items': sorted(items, key=lambda x: -x['size_mb'])[:50],
|
| 1997 |
+
}
|
| 1998 |
+
|
| 1999 |
+
|
| 2000 |
+
@app.get("/api/debug/env")
|
| 2001 |
+
async def debug_env():
|
| 2002 |
+
mem = get_container_memory()
|
| 2003 |
+
return {
|
| 2004 |
+
'total_mb': mem['total_mb'],
|
| 2005 |
+
'used_mb': mem['used_mb'],
|
| 2006 |
+
'available_mb': mem['available_mb'],
|
| 2007 |
+
'container_percent': round(mem['percent'], 1),
|
| 2008 |
+
'process_rss_mb': mem['process_rss_mb'],
|
| 2009 |
+
'rss_percent': round(mem['rss_percent'], 1),
|
| 2010 |
+
'memory_source': mem['source'],
|
| 2011 |
+
'models_loaded': list(model_manager.models.keys()),
|
| 2012 |
+
'models_warmed': dict(model_manager.warmed),
|
| 2013 |
+
'active_models': sorted(VALID_REMBG_MODELS),
|
| 2014 |
+
'dormant_models': sorted(DORMANT_MODELS.keys()),
|
| 2015 |
+
'remove_func_ready': remove_func is not None,
|
| 2016 |
+
'ai_available': AI_AVAILABLE,
|
| 2017 |
+
'default_model': default_model_name,
|
| 2018 |
+
'video_force_model': VIDEO_FORCE_MODEL,
|
| 2019 |
+
'video_ram_reject_percent': VIDEO_RAM_REJECT_PERCENT,
|
| 2020 |
+
'video_max_batch': VIDEO_MAX_BATCH,
|
| 2021 |
+
'preload_models': [n for n, _ in PRELOAD_MODELS],
|
| 2022 |
+
'torch_loaded': 'torch' in sys.modules,
|
| 2023 |
+
'onnx_threads': 1,
|
| 2024 |
+
'router': 'v32.6.3 — app(3).py router, 4 active + 6 dormant, RAM optimized',
|
| 2025 |
+
}
|
| 2026 |
+
|
| 2027 |
+
|
| 2028 |
+
@app.get("/")
|
| 2029 |
+
async def home():
|
| 2030 |
+
stats = model_manager.get_stats()
|
| 2031 |
+
mem = get_container_memory()
|
| 2032 |
+
warmed = sum(1 for w in stats['warmed'].values() if w)
|
| 2033 |
+
return HTMLResponse(f"""
|
| 2034 |
+
<!DOCTYPE html><html><head><meta charset="UTF-8">
|
| 2035 |
+
<title>Background Remover v32.6.3</title>
|
| 2036 |
+
<style>body{{font-family:system-ui;background:#0A0A0A;color:#fff;
|
| 2037 |
+
min-height:100vh;display:flex;align-items:center;justify-content:center;
|
| 2038 |
+
margin:0;padding:20px}}.c{{max-width:900px;background:#111;
|
| 2039 |
+
border-radius:20px;padding:40px;text-align:center;
|
| 2040 |
+
border:1px solid #222}}.s{{padding:15px;border-radius:12px;
|
| 2041 |
+
margin:20px 0;background:#1a1a1a}}a{{color:#7ED957}}</style>
|
| 2042 |
+
</head><body><div class="c">
|
| 2043 |
+
<h1>Background Remover v32.6.3</h1>
|
| 2044 |
+
<div class="s">AI online. {len(stats['loaded_models'])} models ({warmed} warmed).
|
| 2045 |
+
RSS {mem['rss_percent']:.0f}% ({mem['process_rss_mb']}/{mem['total_mb']}MB)</div>
|
| 2046 |
+
<p>Active: silueta · u2netp · isnet · birefnet-lite</p>
|
| 2047 |
+
<p>Dormant: u2net · human_seg · anime · birefnet · cloth_seg · sam</p>
|
| 2048 |
+
<p>Router: flat→flood_fill · human→isnet · small→u2netp · complex→birefnet</p>
|
| 2049 |
+
<p><a href="/docs">API Docs</a> ·
|
| 2050 |
+
<a href="/api/models">Models</a> ·
|
| 2051 |
+
<a href="/api/debug/memory">Memory</a> ·
|
| 2052 |
+
<a href="/api/debug/tmp">Disk</a> ·
|
| 2053 |
+
<a href="/api/debug/env">Env</a></p>
|
| 2054 |
+
</div></body></html>
|
| 2055 |
+
""")
|
| 2056 |
+
|
| 2057 |
+
|
| 2058 |
+
@app.get("/api/health")
|
| 2059 |
+
async def health_check():
|
| 2060 |
+
stats = model_manager.get_stats()
|
| 2061 |
+
mem = get_container_memory()
|
| 2062 |
+
|
| 2063 |
+
preload_names = [n for n, _ in PRELOAD_MODELS]
|
| 2064 |
+
all_loaded = all(n in stats['loaded_models'] for n in preload_names)
|
| 2065 |
+
all_warmed = all(stats['warmed'].get(n, False) for n in preload_names)
|
| 2066 |
+
ram_ok = mem['rss_percent'] < (VIDEO_RAM_REJECT_PERCENT - 5)
|
| 2067 |
+
|
| 2068 |
+
if not AI_AVAILABLE:
|
| 2069 |
+
status = 'error'
|
| 2070 |
+
elif not all_loaded:
|
| 2071 |
+
status = 'degraded'
|
| 2072 |
+
elif not all_warmed:
|
| 2073 |
+
status = 'warming'
|
| 2074 |
+
elif not ram_ok:
|
| 2075 |
+
status = 'degraded'
|
| 2076 |
+
else:
|
| 2077 |
+
status = 'healthy'
|
| 2078 |
+
|
| 2079 |
+
return {
|
| 2080 |
+
'status': status,
|
| 2081 |
+
'ai_available': AI_AVAILABLE,
|
| 2082 |
+
'models_loaded': stats['loaded_models'],
|
| 2083 |
+
'models_count': stats['model_count'],
|
| 2084 |
+
'models_warmed': stats['warmed'],
|
| 2085 |
+
'all_loaded': all_loaded,
|
| 2086 |
+
'all_warmed': all_warmed,
|
| 2087 |
+
'active_models': sorted(VALID_REMBG_MODELS),
|
| 2088 |
+
'dormant_models': sorted(DORMANT_MODELS.keys()),
|
| 2089 |
+
'protected_models': stats['protected'],
|
| 2090 |
+
'rss_percent': round(mem['rss_percent'], 1),
|
| 2091 |
+
'rss_mb': mem['process_rss_mb'],
|
| 2092 |
+
'container_percent': round(mem['percent'], 1),
|
| 2093 |
+
'memory_total_mb': mem['total_mb'],
|
| 2094 |
+
'memory_source': mem['source'],
|
| 2095 |
+
'video_ram_reject_percent': VIDEO_RAM_REJECT_PERCENT,
|
| 2096 |
+
'video_max_batch': VIDEO_MAX_BATCH,
|
| 2097 |
+
'video_force_model': VIDEO_FORCE_MODEL,
|
| 2098 |
+
'version': '32.6.3',
|
| 2099 |
+
'preload_models': preload_names,
|
| 2100 |
+
'alpha_cache_ttl': ALPHA_CACHE_TTL_SECONDS,
|
| 2101 |
+
'alpha_purge_interval': ALPHA_PURGE_INTERVAL,
|
| 2102 |
+
'alpha_smoothing': _alpha_cache_stats(),
|
| 2103 |
+
'router_version': 'v32.6.3',
|
| 2104 |
+
'onnx_threads': 1,
|
| 2105 |
+
'torch_loaded': 'torch' in sys.modules,
|
| 2106 |
+
}
|
| 2107 |
+
|
| 2108 |
+
|
| 2109 |
+
@app.exception_handler(HTTPException)
|
| 2110 |
+
async def http_exc(request, exc):
|
| 2111 |
+
return JSONResponse(status_code=exc.status_code,
|
| 2112 |
+
content={'error': exc.detail, 'status_code': exc.status_code})
|
| 2113 |
+
|
| 2114 |
+
|
| 2115 |
+
@app.exception_handler(Exception)
|
| 2116 |
+
async def gen_exc(request, exc):
|
| 2117 |
+
logger.error(f"Unhandled: {exc}")
|
| 2118 |
+
return JSONResponse(status_code=500,
|
| 2119 |
+
content={'error': 'Internal error', 'detail': str(exc)[:200]})
|
| 2120 |
+
|
| 2121 |
+
|
| 2122 |
+
@app.on_event("startup")
|
| 2123 |
+
async def startup():
|
| 2124 |
+
logger.info("=" * 60)
|
| 2125 |
+
logger.info("v32.6.3 ready")
|
| 2126 |
+
logger.info(f"Active: {sorted(VALID_REMBG_MODELS)}")
|
| 2127 |
+
logger.info(f"Dormant: {sorted(DORMANT_MODELS.keys())}")
|
| 2128 |
+
logger.info("Dormant requests reroute through complexity router")
|
| 2129 |
+
logger.info("ONNX threads: 1 (matches Dockerfile)")
|
| 2130 |
+
logger.info("Warm res: silueta 384, u2netp 384, isnet 128, birefnet 128")
|
| 2131 |
+
logger.info("Birefnet cap: 512, isnet cap: 576")
|
| 2132 |
+
logger.info("=" * 60)
|
| 2133 |
+
_log_rss("startup")
|
| 2134 |
+
|
| 2135 |
+
|
| 2136 |
+
@app.on_event("shutdown")
|
| 2137 |
+
async def shutdown():
|
| 2138 |
+
cpu_executor.shutdown(wait=True)
|
| 2139 |
+
|
| 2140 |
+
|
| 2141 |
+
if __name__ == '__main__':
|
| 2142 |
+
import uvicorn
|
| 2143 |
+
uvicorn.run('app:app', host='0.0.0.0',
|
| 2144 |
+
port=int(os.environ.get('PORT', 7860)),
|
| 2145 |
+
timeout_keep_alive=300, log_level='info', reload=False)
|