Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- main.py +19 -0
- shadow_routes.py +60 -0
main.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from app.api.shadow_routes import router as api_router
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="Shadow Generator (SSN + Procedural Fallback)")
|
| 6 |
+
|
| 7 |
+
# CORS (tighten in production)
|
| 8 |
+
app.add_middleware(
|
| 9 |
+
CORSMiddleware,
|
| 10 |
+
allow_origins=["*"],
|
| 11 |
+
allow_methods=["*"],
|
| 12 |
+
allow_headers=["*"],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
app.include_router(api_router, prefix="/api")
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
async def root():
|
| 19 |
+
return {"message": "Shadow Generator API running. See /api/docs or /docs"}
|
shadow_routes.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, UploadFile, File, Form
|
| 2 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 3 |
+
import io
|
| 4 |
+
import traceback
|
| 5 |
+
|
| 6 |
+
from app.core.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)})
|