Spaces:
Runtime error
Runtime error
Update app/api/image_router.py
Browse files- app/api/image_router.py +19 -11
app/api/image_router.py
CHANGED
|
@@ -122,37 +122,46 @@ async def update_image(
|
|
| 122 |
# --- New Virtual Try-On Endpoint with File Uploads ---
|
| 123 |
@router.post("/virtual-try-on")
|
| 124 |
async def virtual_try_on(
|
| 125 |
-
dress_image: UploadFile = File(..., description="The dress image
|
| 126 |
-
person_image: UploadFile = File(..., description="The person image
|
| 127 |
-
|
|
|
|
| 128 |
"""
|
| 129 |
-
Performs a virtual try-on
|
| 130 |
-
|
| 131 |
"""
|
| 132 |
logger.info("Received request to /virtual-try-on with image uploads.")
|
| 133 |
|
|
|
|
| 134 |
if not dress_image.content_type.startswith("image/"):
|
| 135 |
-
raise HTTPException(status_code=400, detail="Dress image must be an image type
|
| 136 |
if not person_image.content_type.startswith("image/"):
|
| 137 |
-
raise HTTPException(status_code=400, detail="Person image must be an image type
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
try:
|
| 140 |
dress_image_bytes = await dress_image.read()
|
| 141 |
person_image_bytes = await person_image.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
# Service returns JSON summary (dict) and BytesIO object
|
| 144 |
summary, try_on_image_bytes_io = image_service.virtual_try_on(
|
| 145 |
dress_image_bytes=dress_image_bytes,
|
| 146 |
-
person_image_bytes=person_image_bytes
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
if try_on_image_bytes_io:
|
| 150 |
logger.info("Virtual try-on successful. Streaming image response.")
|
| 151 |
-
# If an image is returned, stream it directly
|
| 152 |
return create_image_streaming_response(try_on_image_bytes_io)
|
| 153 |
else:
|
| 154 |
logger.warning("Virtual try-on returned no image. Sending JSON summary.")
|
| 155 |
-
# If no image, return the JSON summary as a JSON response
|
| 156 |
return JSONResponse(content=summary)
|
| 157 |
|
| 158 |
except HTTPException:
|
|
@@ -160,7 +169,6 @@ async def virtual_try_on(
|
|
| 160 |
except Exception as e:
|
| 161 |
logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
|
| 162 |
raise HTTPException(status_code=500, detail=str(e))
|
| 163 |
-
|
| 164 |
|
| 165 |
@router.post("/generate-shoe", response_class=StreamingResponse)
|
| 166 |
async def generate_shoe(request: ShoeGenerateRequest):
|
|
|
|
| 122 |
# --- New Virtual Try-On Endpoint with File Uploads ---
|
| 123 |
@router.post("/virtual-try-on")
|
| 124 |
async def virtual_try_on(
|
| 125 |
+
dress_image: UploadFile = File(..., description="The dress image (Input 1)"),
|
| 126 |
+
person_image: UploadFile = File(..., description="The person image (Input 2)"),
|
| 127 |
+
shoes_image: UploadFile = File(None, description="The shoes image (Input 3) - Optional")
|
| 128 |
+
) -> Response:
|
| 129 |
"""
|
| 130 |
+
Performs a virtual try-on.
|
| 131 |
+
Accepts: Dress, Person, and optionally Shoes.
|
| 132 |
"""
|
| 133 |
logger.info("Received request to /virtual-try-on with image uploads.")
|
| 134 |
|
| 135 |
+
# Validate mandatory images
|
| 136 |
if not dress_image.content_type.startswith("image/"):
|
| 137 |
+
raise HTTPException(status_code=400, detail="Dress image must be an image type.")
|
| 138 |
if not person_image.content_type.startswith("image/"):
|
| 139 |
+
raise HTTPException(status_code=400, detail="Person image must be an image type.")
|
| 140 |
+
|
| 141 |
+
# Validate shoes only if provided
|
| 142 |
+
if shoes_image and not shoes_image.content_type.startswith("image/"):
|
| 143 |
+
raise HTTPException(status_code=400, detail="Shoes image must be an image type.")
|
| 144 |
|
| 145 |
try:
|
| 146 |
dress_image_bytes = await dress_image.read()
|
| 147 |
person_image_bytes = await person_image.read()
|
| 148 |
+
|
| 149 |
+
shoes_image_bytes = None
|
| 150 |
+
if shoes_image:
|
| 151 |
+
shoes_image_bytes = await shoes_image.read()
|
| 152 |
|
| 153 |
# Service returns JSON summary (dict) and BytesIO object
|
| 154 |
summary, try_on_image_bytes_io = image_service.virtual_try_on(
|
| 155 |
dress_image_bytes=dress_image_bytes,
|
| 156 |
+
person_image_bytes=person_image_bytes,
|
| 157 |
+
shoes_image_bytes=shoes_image_bytes
|
| 158 |
)
|
| 159 |
|
| 160 |
if try_on_image_bytes_io:
|
| 161 |
logger.info("Virtual try-on successful. Streaming image response.")
|
|
|
|
| 162 |
return create_image_streaming_response(try_on_image_bytes_io)
|
| 163 |
else:
|
| 164 |
logger.warning("Virtual try-on returned no image. Sending JSON summary.")
|
|
|
|
| 165 |
return JSONResponse(content=summary)
|
| 166 |
|
| 167 |
except HTTPException:
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
|
| 171 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 172 |
|
| 173 |
@router.post("/generate-shoe", response_class=StreamingResponse)
|
| 174 |
async def generate_shoe(request: ShoeGenerateRequest):
|