Spaces:
Building
Building
File size: 4,496 Bytes
a543e33 ca02ec3 a543e33 ca02ec3 a543e33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | """
Session state management module for the text analysis application.
Handles initialization, clearing, and management of session state.
"""
import streamlit as st
from typing import Dict, Any, Optional
class SessionManager:
"""Manages Streamlit session state for the application."""
@staticmethod
def initialize_session_state():
"""Initialize all session state variables with default values."""
defaults = {
'language': 'en',
'model_size': 'trf',
'analyzer': None,
'pos_parser': None,
'reference_lists': {},
'uploaded_file_configs': {},
'uploaded_files_content': {}, # Store file contents in memory
'last_language_change': st.session_state.get('language', 'en'),
'show_language_warning': False
}
for key, default_value in defaults.items():
if key not in st.session_state:
st.session_state[key] = default_value
@staticmethod
def clear_analyzers():
"""Clear analyzer and parser instances (e.g., when language/model changes)."""
st.session_state.analyzer = None
st.session_state.pos_parser = None
@staticmethod
def handle_language_change():
"""Handle language change logic."""
current_lang = st.session_state.get('language', 'en')
if st.session_state.last_language_change != current_lang:
SessionManager.clear_analyzers()
st.session_state.last_language_change = current_lang
@staticmethod
def get_reference_lists() -> Dict[str, Any]:
"""Get reference lists from session state."""
return st.session_state.get('reference_lists', {})
@staticmethod
def set_reference_lists(reference_lists: Dict[str, Any]):
"""Set reference lists in session state."""
st.session_state.reference_lists = reference_lists
@staticmethod
def add_reference_list(name: str, data: Dict[str, Any]):
"""Add a single reference list to session state."""
if 'reference_lists' not in st.session_state:
st.session_state.reference_lists = {}
st.session_state.reference_lists[name] = data
@staticmethod
def remove_reference_list(name: str):
"""Remove a reference list from session state."""
if name in st.session_state.get('reference_lists', {}):
del st.session_state.reference_lists[name]
@staticmethod
def get_uploaded_file_configs() -> Dict[str, Any]:
"""Get uploaded file configurations from session state."""
return st.session_state.get('uploaded_file_configs', {})
@staticmethod
def set_uploaded_file_config(file_key: str, config: Dict[str, Any]):
"""Set configuration for an uploaded file."""
if 'uploaded_file_configs' not in st.session_state:
st.session_state.uploaded_file_configs = {}
st.session_state.uploaded_file_configs[file_key] = config
@staticmethod
def get_temp_dir() -> Optional[str]:
"""
DEPRECATED: This method is no longer used.
File handling is now done in-memory to support read-only filesystems.
"""
import warnings
warnings.warn(
"get_temp_dir() is deprecated. Use in-memory file handling instead.",
DeprecationWarning,
stacklevel=2
)
return None
@staticmethod
def is_custom_reference_list(name: str) -> bool:
"""Check if a reference list is custom (uploaded) or default."""
reference_lists = SessionManager.get_reference_lists()
if name not in reference_lists:
return False
ref_data = reference_lists[name]
if 'token' in ref_data and isinstance(ref_data['token'], dict):
return ref_data['token'].get('is_custom_config', False)
return False
@staticmethod
def get_session_info() -> Dict[str, Any]:
"""Get session state information for debugging."""
return {
'language': st.session_state.get('language'),
'model_size': st.session_state.get('model_size'),
'analyzer_loaded': st.session_state.get('analyzer') is not None,
'reference_lists_count': len(st.session_state.get('reference_lists', {})),
'uploaded_files_count': len(st.session_state.get('uploaded_file_configs', {}))
} |