dependencies.py 1.9 KB

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