Spaces:
Sleeping
Sleeping
Update shadow_routes.py
Browse files- shadow_routes.py +77 -6
shadow_routes.py
CHANGED
|
@@ -1,14 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, UploadFile, File, Form
|
| 2 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 3 |
import io
|
| 4 |
import traceback
|
| 5 |
|
| 6 |
-
from shadow_generator import
|
| 7 |
|
| 8 |
router = APIRouter()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@router.get("/health")
|
| 14 |
async def health():
|
|
@@ -38,9 +106,6 @@ async def generate_shadow_endpoint(
|
|
| 38 |
shadow_type: str = Form("drop"),
|
| 39 |
use_ssn: bool = Form(True)
|
| 40 |
):
|
| 41 |
-
"""
|
| 42 |
-
Returns: PNG (RGBA) bytes stream containing object + generated shadow.
|
| 43 |
-
"""
|
| 44 |
try:
|
| 45 |
data = await image.read()
|
| 46 |
params = {
|
|
@@ -53,6 +118,12 @@ async def generate_shadow_endpoint(
|
|
| 53 |
"shadow_type": shadow_type,
|
| 54 |
"prefer_ssn": use_ssn
|
| 55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
out_bytes = generate_shadow_rgba(data, params=params)
|
| 57 |
return StreamingResponse(io.BytesIO(out_bytes), media_type="image/png")
|
| 58 |
except Exception as exc:
|
|
|
|
| 1 |
+
# from fastapi import APIRouter, UploadFile, File, Form
|
| 2 |
+
# from fastapi.responses import StreamingResponse, JSONResponse
|
| 3 |
+
# import io
|
| 4 |
+
# import traceback
|
| 5 |
+
|
| 6 |
+
# from shadow_generator import initialize_once, generate_shadow_rgba
|
| 7 |
+
|
| 8 |
+
# router = APIRouter()
|
| 9 |
+
|
| 10 |
+
# # initialize on import / startup
|
| 11 |
+
# initialize_once()
|
| 12 |
+
|
| 13 |
+
# @router.get("/health")
|
| 14 |
+
# async def health():
|
| 15 |
+
# return {"ok": True}
|
| 16 |
+
|
| 17 |
+
# @router.get("/params")
|
| 18 |
+
# async def params_info():
|
| 19 |
+
# return {
|
| 20 |
+
# "angle_deg": "float. 0 = right, 90 = down. default 135.0",
|
| 21 |
+
# "distance": "float px offset of shadow. default 40.0",
|
| 22 |
+
# "softness": "float blur radius. default 25.0",
|
| 23 |
+
# "opacity": "float 0..1. default 0.7",
|
| 24 |
+
# "color": "hex color for shadow. default '#000000'",
|
| 25 |
+
# "spread": "float px. positive expands alpha, negative contracts. default 0.0",
|
| 26 |
+
# "shadow_type": "string. 'drop' or 'contact'. default 'drop'",
|
| 27 |
+
# }
|
| 28 |
+
|
| 29 |
+
# @router.post("/generate-shadow")
|
| 30 |
+
# async def generate_shadow_endpoint(
|
| 31 |
+
# image: UploadFile = File(..., description="RGBA PNG with transparent background"),
|
| 32 |
+
# angle_deg: float = Form(135.0),
|
| 33 |
+
# distance: float = Form(40.0),
|
| 34 |
+
# softness: float = Form(25.0),
|
| 35 |
+
# opacity: float = Form(0.7),
|
| 36 |
+
# color: str = Form("#000000"),
|
| 37 |
+
# spread: float = Form(0.0),
|
| 38 |
+
# shadow_type: str = Form("drop"),
|
| 39 |
+
# use_ssn: bool = Form(True)
|
| 40 |
+
# ):
|
| 41 |
+
# """
|
| 42 |
+
# Returns: PNG (RGBA) bytes stream containing object + generated shadow.
|
| 43 |
+
# """
|
| 44 |
+
# try:
|
| 45 |
+
# data = await image.read()
|
| 46 |
+
# params = {
|
| 47 |
+
# "angle_deg": angle_deg,
|
| 48 |
+
# "distance": distance,
|
| 49 |
+
# "softness": softness,
|
| 50 |
+
# "opacity": opacity,
|
| 51 |
+
# "color": color,
|
| 52 |
+
# "spread": spread,
|
| 53 |
+
# "shadow_type": shadow_type,
|
| 54 |
+
# "prefer_ssn": use_ssn
|
| 55 |
+
# }
|
| 56 |
+
# out_bytes = generate_shadow_rgba(data, params=params)
|
| 57 |
+
# return StreamingResponse(io.BytesIO(out_bytes), media_type="image/png")
|
| 58 |
+
# except Exception as exc:
|
| 59 |
+
# traceback.print_exc()
|
| 60 |
+
# return JSONResponse(status_code=500, content={"error": str(exc)})
|
| 61 |
+
|
| 62 |
+
|
| 63 |
from fastapi import APIRouter, UploadFile, File, Form
|
| 64 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 65 |
import io
|
| 66 |
import traceback
|
| 67 |
|
| 68 |
+
from shadow_generator import load_ssn_once, generate_shadow_rgba
|
| 69 |
|
| 70 |
router = APIRouter()
|
| 71 |
|
| 72 |
+
# Lazy initialization placeholder
|
| 73 |
+
_ssn_wrapper = None
|
| 74 |
+
|
| 75 |
+
def get_ssn_wrapper():
|
| 76 |
+
global _ssn_wrapper
|
| 77 |
+
if _ssn_wrapper is None:
|
| 78 |
+
_ssn_wrapper = load_ssn_once()
|
| 79 |
+
return _ssn_wrapper
|
| 80 |
|
| 81 |
@router.get("/health")
|
| 82 |
async def health():
|
|
|
|
| 106 |
shadow_type: str = Form("drop"),
|
| 107 |
use_ssn: bool = Form(True)
|
| 108 |
):
|
|
|
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
data = await image.read()
|
| 111 |
params = {
|
|
|
|
| 118 |
"shadow_type": shadow_type,
|
| 119 |
"prefer_ssn": use_ssn
|
| 120 |
}
|
| 121 |
+
|
| 122 |
+
# Lazy load SSN model
|
| 123 |
+
ssn = get_ssn_wrapper()
|
| 124 |
+
if not ssn.available():
|
| 125 |
+
return JSONResponse(status_code=503, content={"error": "SSN model is still loading. Try again shortly."})
|
| 126 |
+
|
| 127 |
out_bytes = generate_shadow_rgba(data, params=params)
|
| 128 |
return StreamingResponse(io.BytesIO(out_bytes), media_type="image/png")
|
| 129 |
except Exception as exc:
|