| 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 | FastAPI Dependencies |
| 15 | |
| 16 | Provides dependency injection for PixelleVideoCore and other services. |
| 17 | """ |
| 18 | |
| 19 | from typing import Annotated |
| 20 | from fastapi import Depends |
| 21 | from loguru import logger |
| 22 | |
| 23 | from pixelle_video.service import PixelleVideoCore |
| 24 | |
| 25 | |
| 26 | # Global Pixelle-Video instance |
| 27 | _pixelle_video_instance: PixelleVideoCore = None |
| 28 | |
| 29 | |
| 30 | async def get_pixelle_video() -> PixelleVideoCore: |
| 31 | """ |
| 32 | Get Pixelle-Video core instance (dependency injection) |
| 33 | |
| 34 | Returns: |
| 35 | PixelleVideoCore instance |
| 36 | """ |
| 37 | global _pixelle_video_instance |
| 38 | |
| 39 | if _pixelle_video_instance is None: |
| 40 | _pixelle_video_instance = PixelleVideoCore() |
| 41 | await _pixelle_video_instance.initialize() |
| 42 | logger.info("✅ Pixelle-Video initialized for API") |
| 43 | |
| 44 | return _pixelle_video_instance |
| 45 | |
| 46 | |
| 47 | async def shutdown_pixelle_video(): |
| 48 | """Shutdown Pixelle-Video instance and cleanup resources""" |
| 49 | global _pixelle_video_instance |
| 50 | if _pixelle_video_instance: |
| 51 | logger.info("Shutting down Pixelle-Video...") |
| 52 | await _pixelle_video_instance.cleanup() |
| 53 | _pixelle_video_instance = None |
| 54 | |
| 55 | from pixelle_video.services.frame_html import HTMLFrameGenerator |
| 56 | await HTMLFrameGenerator.close_browser() |
| 57 | |
| 58 | |
| 59 | # Type alias for dependency injection |
| 60 | PixelleVideoDep = Annotated[PixelleVideoCore, Depends(get_pixelle_video)] |
| 61 | |
| 62 |