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