content.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Content generation endpoints
  14. Endpoints for generating narrations, image prompts, and titles.
  15. """
  16. from fastapi import APIRouter, HTTPException
  17. from loguru import logger
  18. from api.dependencies import PixelleVideoDep
  19. from api.schemas.content import (
  20. NarrationGenerateRequest,
  21. NarrationGenerateResponse,
  22. ImagePromptGenerateRequest,
  23. ImagePromptGenerateResponse,
  24. TitleGenerateRequest,
  25. TitleGenerateResponse,
  26. )
  27. from pixelle_video.utils.content_generators import (
  28. generate_narrations_from_topic,
  29. generate_image_prompts,
  30. generate_title,
  31. )
  32. router = APIRouter(prefix="/content", tags=["Content Generation"])
  33. @router.post("/narration", response_model=NarrationGenerateResponse)
  34. async def generate_narration(
  35. request: NarrationGenerateRequest,
  36. pixelle_video: PixelleVideoDep
  37. ):
  38. """
  39. Generate narrations from text
  40. Uses LLM to break down text into multiple narration segments.
  41. - **text**: Source text
  42. - **n_scenes**: Number of narrations to generate
  43. - **min_words**: Minimum words per narration
  44. - **max_words**: Maximum words per narration
  45. Returns list of narration strings.
  46. """
  47. try:
  48. logger.info(f"Generating {request.n_scenes} narrations from text")
  49. # Call narration generator utility function
  50. narrations = await generate_narrations_from_topic(
  51. llm_service=pixelle_video.llm,
  52. topic=request.text,
  53. n_scenes=request.n_scenes,
  54. min_words=request.min_words,
  55. max_words=request.max_words
  56. )
  57. return NarrationGenerateResponse(
  58. narrations=narrations
  59. )
  60. except Exception as e:
  61. logger.error(f"Narration generation error: {e}")
  62. raise HTTPException(status_code=500, detail=str(e))
  63. @router.post("/image-prompt", response_model=ImagePromptGenerateResponse)
  64. async def generate_image_prompt(
  65. request: ImagePromptGenerateRequest,
  66. pixelle_video: PixelleVideoDep
  67. ):
  68. """
  69. Generate image prompts from narrations
  70. Uses LLM to create detailed image generation prompts.
  71. - **narrations**: List of narration texts
  72. - **min_words**: Minimum words per prompt
  73. - **max_words**: Maximum words per prompt
  74. Returns list of image prompts.
  75. """
  76. try:
  77. logger.info(f"Generating image prompts for {len(request.narrations)} narrations")
  78. # Call image prompt generator utility function
  79. image_prompts = await generate_image_prompts(
  80. llm_service=pixelle_video.llm,
  81. narrations=request.narrations,
  82. min_words=request.min_words,
  83. max_words=request.max_words
  84. )
  85. return ImagePromptGenerateResponse(
  86. image_prompts=image_prompts
  87. )
  88. except Exception as e:
  89. logger.error(f"Image prompt generation error: {e}")
  90. raise HTTPException(status_code=500, detail=str(e))
  91. @router.post("/title", response_model=TitleGenerateResponse)
  92. async def generate_title_endpoint(
  93. request: TitleGenerateRequest,
  94. pixelle_video: PixelleVideoDep
  95. ):
  96. """
  97. Generate video title from text
  98. Uses LLM to create an engaging title.
  99. - **text**: Source text
  100. - **style**: Optional title style hint
  101. Returns generated title.
  102. """
  103. try:
  104. logger.info("Generating title from text")
  105. # Call title generator utility function
  106. title = await generate_title(
  107. llm_service=pixelle_video.llm,
  108. content=request.text,
  109. strategy="llm"
  110. )
  111. return TitleGenerateResponse(
  112. title=title
  113. )
  114. except Exception as e:
  115. logger.error(f"Title generation error: {e}")
  116. raise HTTPException(status_code=500, detail=str(e))