| 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 | Base Pipeline for Video Generation |
| 15 | |
| 16 | All custom pipelines should inherit from BasePipeline. |
| 17 | """ |
| 18 | |
| 19 | from abc import ABC, abstractmethod |
| 20 | from typing import Optional, Callable |
| 21 | |
| 22 | from loguru import logger |
| 23 | |
| 24 | from pixelle_video.models.progress import ProgressEvent |
| 25 | from pixelle_video.models.storyboard import VideoGenerationResult |
| 26 | |
| 27 | |
| 28 | class BasePipeline(ABC): |
| 29 | """ |
| 30 | Base pipeline for video generation |
| 31 | |
| 32 | All custom pipelines should inherit from this class and implement __call__. |
| 33 | |
| 34 | Design principles: |
| 35 | - Each pipeline represents a complete video generation workflow |
| 36 | - Pipelines are independent and can have completely different logic |
| 37 | - Pipelines have access to all core services via self.core |
| 38 | - Pipelines should report progress via progress_callback |
| 39 | |
| 40 | Example: |
| 41 | >>> class MyPipeline(BasePipeline): |
| 42 | ... async def __call__(self, text: str, **kwargs): |
| 43 | ... # Step 1: Generate content |
| 44 | ... narrations = await some_logic(text) |
| 45 | ... |
| 46 | ... # Step 2: Process frames |
| 47 | ... for narration in narrations: |
| 48 | ... audio = await self.core.tts(narration) |
| 49 | ... # ... |
| 50 | ... |
| 51 | ... return VideoGenerationResult(...) |
| 52 | """ |
| 53 | |
| 54 | def __init__(self, pixelle_video_core): |
| 55 | """ |
| 56 | Initialize pipeline with core services |
| 57 | |
| 58 | Args: |
| 59 | pixelle_video_core: PixelleVideoCore instance (provides access to all services) |
| 60 | """ |
| 61 | self.core = pixelle_video_core |
| 62 | |
| 63 | # Quick access to services (convenience) |
| 64 | self.llm = pixelle_video_core.llm |
| 65 | self.tts = pixelle_video_core.tts |
| 66 | self.media = pixelle_video_core.media |
| 67 | self.video = pixelle_video_core.video |
| 68 | |
| 69 | # Backward compatibility alias |
| 70 | self.image = pixelle_video_core.media |
| 71 | |
| 72 | @abstractmethod |
| 73 | async def __call__( |
| 74 | self, |
| 75 | text: str, |
| 76 | progress_callback: Optional[Callable[[ProgressEvent], None]] = None, |
| 77 | **kwargs |
| 78 | ) -> VideoGenerationResult: |
| 79 | """ |
| 80 | Execute the pipeline |
| 81 | |
| 82 | Args: |
| 83 | text: Input text (meaning varies by pipeline) |
| 84 | progress_callback: Optional callback for progress updates (receives ProgressEvent) |
| 85 | **kwargs: Pipeline-specific parameters |
| 86 | |
| 87 | Returns: |
| 88 | VideoGenerationResult with video path and metadata |
| 89 | |
| 90 | Raises: |
| 91 | Exception: Pipeline-specific exceptions |
| 92 | """ |
| 93 | pass |
| 94 | |
| 95 | def _report_progress( |
| 96 | self, |
| 97 | callback: Optional[Callable[[ProgressEvent], None]], |
| 98 | event_type: str, |
| 99 | progress: float, |
| 100 | **kwargs |
| 101 | ): |
| 102 | """ |
| 103 | Report progress via callback |
| 104 | |
| 105 | Args: |
| 106 | callback: Progress callback function |
| 107 | event_type: Type of progress event |
| 108 | progress: Progress value (0.0-1.0) |
| 109 | **kwargs: Additional event-specific parameters (frame_current, frame_total, etc.) |
| 110 | """ |
| 111 | if callback: |
| 112 | event = ProgressEvent(event_type=event_type, progress=progress, **kwargs) |
| 113 | callback(event) |
| 114 | logger.debug(f"Progress: {progress*100:.0f}% - {event_type}") |
| 115 | else: |
| 116 | logger.debug(f"Progress: {progress*100:.0f}% - {event_type}") |
| 117 | |
| 118 |