Hammad712 commited on
Commit
d47078a
·
verified ·
1 Parent(s): 1f07969

Update app/api/image_router.py

Browse files
Files changed (1) hide show
  1. app/api/image_router.py +42 -3
app/api/image_router.py CHANGED
@@ -2,9 +2,10 @@ import logging
2
  from fastapi import APIRouter, HTTPException, Response, UploadFile, File, Form
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from app.schemas import image as image_schemas
5
- from app.services import image_service
6
  from io import BytesIO
7
- from typing import Optional, Dict, Any
 
8
 
9
  logger = logging.getLogger(__name__)
10
 
@@ -157,4 +158,42 @@ async def virtual_try_on(
157
  raise
158
  except Exception as e:
159
  logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
160
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from fastapi import APIRouter, HTTPException, Response, UploadFile, File, Form
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from app.schemas import image as image_schemas
5
+ from app.schemas.image import ShoeGenerateRequest
6
  from io import BytesIO
7
+ from app.services import image_service
8
+ from app.services.image_service import generate_shoe_images
9
 
10
  logger = logging.getLogger(__name__)
11
 
 
158
  raise
159
  except Exception as e:
160
  logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
161
+ raise HTTPException(status_code=500, detail=str(e))
162
+
163
+
164
+ @router.post("/generate-shoe", response_class=StreamingResponse)
165
+ async def generate_shoe(request: ShoeGenerateRequest):
166
+ """
167
+ Generate shoe product images from a textual description.
168
+ Streams the first generated image back; if no image, returns JSON with model text.
169
+ """
170
+ logger.info("Received request to /generate-shoe: %s", request.style[:80])
171
+
172
+ if not request.style:
173
+ raise HTTPException(status_code=400, detail="`style` is required.")
174
+
175
+ try:
176
+ gen_text, images = generate_shoe_images(
177
+ style=request.style,
178
+ color=request.color,
179
+ perspective=request.perspective,
180
+ variations=request.variations or 1,
181
+ )
182
+
183
+ if images and len(images) > 0:
184
+ # Stream the first image (consistent with other endpoints)
185
+ img_io: BytesIO = images[0]
186
+ img_io.seek(0)
187
+ return StreamingResponse(img_io, media_type="image/png")
188
+ else:
189
+ # If no image was produced, return any model text or an error summary as JSON
190
+ detail = {"success": False, "notes": "No image generated."}
191
+ if gen_text:
192
+ detail["model_text"] = gen_text
193
+ return JSONResponse(status_code=500, content=detail)
194
+
195
+ except HTTPException:
196
+ raise
197
+ except Exception as e:
198
+ logger.exception("Error in /generate-shoe: %s", e)
199
+ raise HTTPException(status_code=500, detail=str(e))