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