session.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Session state management for web UI
  14. """
  15. import streamlit as st
  16. from loguru import logger
  17. from web.i18n import get_language, set_language
  18. from web.utils.async_helpers import run_async
  19. def init_session_state():
  20. """Initialize session state variables"""
  21. if "language" not in st.session_state:
  22. # Use auto-detected system language
  23. st.session_state.language = get_language()
  24. def init_i18n():
  25. """Initialize internationalization"""
  26. # Locales are already loaded and system language detected on import
  27. # Get language from session state or use auto-detected system language
  28. if "language" not in st.session_state:
  29. st.session_state.language = get_language() # Use auto-detected language
  30. # Set current language
  31. set_language(st.session_state.language)
  32. def get_pixelle_video():
  33. """
  34. Get initialized Pixelle-Video instance with proper caching and cleanup
  35. Uses st.session_state to cache the instance per user session.
  36. ComfyKit is lazily initialized and automatically recreated on config changes.
  37. """
  38. from pixelle_video.service import PixelleVideoCore
  39. from pixelle_video.config import config_manager
  40. # Compute config hash for change detection
  41. import hashlib
  42. import json
  43. config_dict = config_manager.config.to_dict()
  44. # Only track ComfyUI config for hash (other config changes don't need core recreation)
  45. comfyui_config = config_dict.get("comfyui", {})
  46. config_hash = hashlib.md5(json.dumps(comfyui_config, sort_keys=True).encode()).hexdigest()
  47. # Check if we need to create or recreate core instance
  48. need_recreate = False
  49. if 'pixelle_video' not in st.session_state:
  50. need_recreate = True
  51. logger.info("Creating new PixelleVideoCore instance (first time)")
  52. elif st.session_state.get('pixelle_video_config_hash') != config_hash:
  53. need_recreate = True
  54. logger.info("Configuration changed, recreating PixelleVideoCore instance")
  55. # Cleanup old instance
  56. old_core = st.session_state.pixelle_video
  57. try:
  58. run_async(old_core.cleanup())
  59. except Exception as e:
  60. logger.warning(f"Failed to cleanup old PixelleVideoCore: {e}")
  61. if need_recreate:
  62. # Create and initialize new instance
  63. pixelle_video = PixelleVideoCore()
  64. run_async(pixelle_video.initialize())
  65. # Cache in session state
  66. st.session_state.pixelle_video = pixelle_video
  67. st.session_state.pixelle_video_config_hash = config_hash
  68. logger.info("✅ PixelleVideoCore initialized and cached")
  69. else:
  70. pixelle_video = st.session_state.pixelle_video
  71. logger.debug("Reusing cached PixelleVideoCore instance")
  72. return pixelle_video