Spaces:
Sleeping
Sleeping
Update shadow_routes.py
Browse files- shadow_routes.py +12 -8
shadow_routes.py
CHANGED
|
@@ -130,9 +130,8 @@
|
|
| 130 |
# traceback.print_exc()
|
| 131 |
# return JSONResponse(status_code=500, content={"error": str(exc)})
|
| 132 |
|
| 133 |
-
|
| 134 |
from fastapi import APIRouter, File, UploadFile, Form
|
| 135 |
-
from fastapi.responses import Response
|
| 136 |
import shadow_generator
|
| 137 |
from typing import Optional
|
| 138 |
import io
|
|
@@ -142,33 +141,38 @@ router = APIRouter()
|
|
| 142 |
@router.post("/generate-shadow")
|
| 143 |
async def generate_shadow_route(
|
| 144 |
file: UploadFile = File(...),
|
|
|
|
| 145 |
softness: Optional[float] = Form(28.0),
|
| 146 |
opacity: Optional[float] = Form(0.7),
|
| 147 |
color: Optional[str] = Form("#000000"),
|
| 148 |
-
direction: Optional[float] = Form(0.0)
|
|
|
|
| 149 |
):
|
| 150 |
"""
|
| 151 |
-
|
| 152 |
-
|
| 153 |
"""
|
| 154 |
image_bytes = await file.read()
|
| 155 |
|
| 156 |
params = {
|
|
|
|
| 157 |
"softness": softness,
|
| 158 |
"opacity": opacity,
|
| 159 |
"color": color,
|
| 160 |
-
"direction": direction,
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
try:
|
| 164 |
output_image_bytes = shadow_generator.generate_shadow_rgba(image_bytes, params)
|
| 165 |
return Response(content=output_image_bytes, media_type="image/png")
|
| 166 |
except Exception as e:
|
| 167 |
-
return {"error": str(e)}
|
|
|
|
| 168 |
|
| 169 |
@router.on_event("startup")
|
| 170 |
async def startup_event():
|
| 171 |
"""
|
| 172 |
-
Initialize
|
| 173 |
"""
|
| 174 |
shadow_generator.initialize_once()
|
|
|
|
| 130 |
# traceback.print_exc()
|
| 131 |
# return JSONResponse(status_code=500, content={"error": str(exc)})
|
| 132 |
|
|
|
|
| 133 |
from fastapi import APIRouter, File, UploadFile, Form
|
| 134 |
+
from fastapi.responses import Response, JSONResponse
|
| 135 |
import shadow_generator
|
| 136 |
from typing import Optional
|
| 137 |
import io
|
|
|
|
| 141 |
@router.post("/generate-shadow")
|
| 142 |
async def generate_shadow_route(
|
| 143 |
file: UploadFile = File(...),
|
| 144 |
+
style: Optional[str] = Form("cast"), # NEW: allow drop / cast / soft / bent
|
| 145 |
softness: Optional[float] = Form(28.0),
|
| 146 |
opacity: Optional[float] = Form(0.7),
|
| 147 |
color: Optional[str] = Form("#000000"),
|
| 148 |
+
direction: Optional[float] = Form(0.0),
|
| 149 |
+
distance: Optional[float] = Form(80.0), # NEW: needed for cast shadows
|
| 150 |
):
|
| 151 |
"""
|
| 152 |
+
Generate a shadowed image from input.
|
| 153 |
+
Styles: 'drop', 'cast', 'angled', 'soft'
|
| 154 |
"""
|
| 155 |
image_bytes = await file.read()
|
| 156 |
|
| 157 |
params = {
|
| 158 |
+
"style": style,
|
| 159 |
"softness": softness,
|
| 160 |
"opacity": opacity,
|
| 161 |
"color": color,
|
| 162 |
+
"direction": direction,
|
| 163 |
+
"distance": distance,
|
| 164 |
}
|
| 165 |
|
| 166 |
try:
|
| 167 |
output_image_bytes = shadow_generator.generate_shadow_rgba(image_bytes, params)
|
| 168 |
return Response(content=output_image_bytes, media_type="image/png")
|
| 169 |
except Exception as e:
|
| 170 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 171 |
+
|
| 172 |
|
| 173 |
@router.on_event("startup")
|
| 174 |
async def startup_event():
|
| 175 |
"""
|
| 176 |
+
Initialize model once on server start.
|
| 177 |
"""
|
| 178 |
shadow_generator.initialize_once()
|