| 1 | # Copyright (C) 2025 AIDC-AI |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """ |
| 14 | Image generation endpoints |
| 15 | """ |
| 16 | |
| 17 | from fastapi import APIRouter, HTTPException |
| 18 | from loguru import logger |
| 19 | |
| 20 | from api.dependencies import PixelleVideoDep |
| 21 | from api.schemas.image import ImageGenerateRequest, ImageGenerateResponse |
| 22 | |
| 23 | router = APIRouter(prefix="/image", tags=["Basic Services"]) |
| 24 | |
| 25 | |
| 26 | @router.post("/generate", response_model=ImageGenerateResponse) |
| 27 | async def image_generate( |
| 28 | request: ImageGenerateRequest, |
| 29 | pixelle_video: PixelleVideoDep |
| 30 | ): |
| 31 | """ |
| 32 | Image generation endpoint |
| 33 | |
| 34 | Generate image from text prompt using ComfyKit. |
| 35 | |
| 36 | - **prompt**: Image description/prompt |
| 37 | - **width**: Image width (512-2048) |
| 38 | - **height**: Image height (512-2048) |
| 39 | - **workflow**: Optional custom workflow filename |
| 40 | |
| 41 | Returns path to generated image. |
| 42 | """ |
| 43 | try: |
| 44 | logger.info(f"Image generation request: {request.prompt[:50]}...") |
| 45 | |
| 46 | # Call media service (backward compatible with image API) |
| 47 | media_result = await pixelle_video.media( |
| 48 | prompt=request.prompt, |
| 49 | width=request.width, |
| 50 | height=request.height, |
| 51 | workflow=request.workflow |
| 52 | ) |
| 53 | |
| 54 | # For backward compatibility, only support image results in /image endpoint |
| 55 | if media_result.is_video: |
| 56 | raise HTTPException( |
| 57 | status_code=400, |
| 58 | detail="Video workflow used. Please use /media/generate endpoint for video generation." |
| 59 | ) |
| 60 | |
| 61 | return ImageGenerateResponse( |
| 62 | image_path=media_result.url |
| 63 | ) |
| 64 | |
| 65 | except HTTPException: |
| 66 | raise |
| 67 | except Exception as e: |
| 68 | logger.error(f"Image generation error: {e}") |
| 69 | raise HTTPException(status_code=500, detail=str(e)) |
| 70 | |
| 71 |