image.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. Image generation endpoints
  14. """
  15. from fastapi import APIRouter, HTTPException
  16. from loguru import logger
  17. from api.dependencies import PixelleVideoDep
  18. from api.schemas.image import ImageGenerateRequest, ImageGenerateResponse
  19. router = APIRouter(prefix="/image", tags=["Basic Services"])
  20. @router.post("/generate", response_model=ImageGenerateResponse)
  21. async def image_generate(
  22. request: ImageGenerateRequest,
  23. pixelle_video: PixelleVideoDep
  24. ):
  25. """
  26. Image generation endpoint
  27. Generate image from text prompt using ComfyKit.
  28. - **prompt**: Image description/prompt
  29. - **width**: Image width (512-2048)
  30. - **height**: Image height (512-2048)
  31. - **workflow**: Optional custom workflow filename
  32. Returns path to generated image.
  33. """
  34. try:
  35. logger.info(f"Image generation request: {request.prompt[:50]}...")
  36. # Call media service (backward compatible with image API)
  37. media_result = await pixelle_video.media(
  38. prompt=request.prompt,
  39. width=request.width,
  40. height=request.height,
  41. workflow=request.workflow
  42. )
  43. # For backward compatibility, only support image results in /image endpoint
  44. if media_result.is_video:
  45. raise HTTPException(
  46. status_code=400,
  47. detail="Video workflow used. Please use /media/generate endpoint for video generation."
  48. )
  49. return ImageGenerateResponse(
  50. image_path=media_result.url
  51. )
  52. except HTTPException:
  53. raise
  54. except Exception as e:
  55. logger.error(f"Image generation error: {e}")
  56. raise HTTPException(status_code=500, detail=str(e))