Update app.py
Browse files
app.py
CHANGED
|
@@ -144,19 +144,106 @@ def initialize_database():
|
|
| 144 |
st.error(f"Database initialization error: {e}")
|
| 145 |
return False
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
}
|
| 161 |
|
| 162 |
for key, default_value in default_states.items():
|
|
@@ -258,44 +345,44 @@ def initialize_chat_from_collection():
|
|
| 258 |
return False
|
| 259 |
|
| 260 |
def display_collection_sidebar():
|
| 261 |
-
"""Display
|
| 262 |
with st.sidebar:
|
| 263 |
-
st.title("
|
| 264 |
-
|
| 265 |
-
# Collection Creation Button
|
| 266 |
-
if st.button("➕ Create New Collection", use_container_width=True):
|
| 267 |
-
st.session_state.show_collection_dialog = True
|
| 268 |
-
|
| 269 |
-
# Upload Section
|
| 270 |
-
st.header("Upload Documents")
|
| 271 |
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
collections = get_collections(st.session_state.db_conn)
|
| 274 |
-
if collections:
|
| 275 |
-
upload_to = st.selectbox(
|
| 276 |
-
"Upload to",
|
| 277 |
-
options=["Select Collection..."] + [c['name'] for c in collections],
|
| 278 |
-
key="upload_destination"
|
| 279 |
-
)
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
(c['id'] for c in collections if c['name'] == upload_to),
|
| 291 |
-
None
|
| 292 |
)
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
def display_collection_dialog():
|
| 301 |
"""Display the create collection dialog."""
|
|
|
|
| 144 |
st.error(f"Database initialization error: {e}")
|
| 145 |
return False
|
| 146 |
|
| 147 |
+
@dataclass
|
| 148 |
+
class SessionStateDefaults:
|
| 149 |
+
"""Default values for session state variables."""
|
| 150 |
+
# Collection Management
|
| 151 |
+
show_collection_dialog: bool = False
|
| 152 |
+
selected_collection: Optional[Dict] = None
|
| 153 |
+
current_collection: Optional[Dict] = None
|
| 154 |
|
| 155 |
+
# Chat System
|
| 156 |
+
chat_ready: bool = False
|
| 157 |
+
messages: list[BaseMessage] = None # Will be initialized as empty list
|
| 158 |
+
current_chat_id: Optional[int] = None
|
| 159 |
+
|
| 160 |
+
# Document Processing
|
| 161 |
+
vector_store: Optional[FAISS] = None
|
| 162 |
+
qa_system: Optional[ConversationalRetrievalChain] = None
|
| 163 |
+
|
| 164 |
+
# State Management
|
| 165 |
+
reinitialize_chat: bool = False
|
| 166 |
+
debug_mode: bool = False
|
| 167 |
+
|
| 168 |
+
def initialize_session_state() -> None:
|
| 169 |
+
"""
|
| 170 |
+
Initialize all session state variables with proper typing and documentation.
|
| 171 |
+
|
| 172 |
+
This function ensures all necessary session state variables are properly initialized
|
| 173 |
+
with appropriate default values and types.
|
| 174 |
+
"""
|
| 175 |
+
defaults = SessionStateDefaults()
|
| 176 |
+
|
| 177 |
+
# Dictionary mapping state variables to their descriptions
|
| 178 |
+
state_vars: Dict[str, Dict[str, Any]] = {
|
| 179 |
+
'show_collection_dialog': {
|
| 180 |
+
'default': defaults.show_collection_dialog,
|
| 181 |
+
'desc': 'Controls visibility of collection creation dialog'
|
| 182 |
+
},
|
| 183 |
+
'selected_collection': {
|
| 184 |
+
'default': defaults.selected_collection,
|
| 185 |
+
'desc': 'Currently selected collection for viewing/editing'
|
| 186 |
+
},
|
| 187 |
+
'current_collection': {
|
| 188 |
+
'default': defaults.current_collection,
|
| 189 |
+
'desc': 'Active collection for chat/analysis'
|
| 190 |
+
},
|
| 191 |
+
'chat_ready': {
|
| 192 |
+
'default': defaults.chat_ready,
|
| 193 |
+
'desc': 'Indicates if chat system is initialized and ready'
|
| 194 |
+
},
|
| 195 |
+
'messages': {
|
| 196 |
+
'default': [] if defaults.messages is None else defaults.messages,
|
| 197 |
+
'desc': 'List of chat messages in the current session'
|
| 198 |
+
},
|
| 199 |
+
'current_chat_id': {
|
| 200 |
+
'default': defaults.current_chat_id,
|
| 201 |
+
'desc': 'ID of the current chat session'
|
| 202 |
+
},
|
| 203 |
+
'vector_store': {
|
| 204 |
+
'default': defaults.vector_store,
|
| 205 |
+
'desc': 'FAISS vector store for document embeddings'
|
| 206 |
+
},
|
| 207 |
+
'qa_system': {
|
| 208 |
+
'default': defaults.qa_system,
|
| 209 |
+
'desc': 'Initialized QA system for chat'
|
| 210 |
+
},
|
| 211 |
+
'reinitialize_chat': {
|
| 212 |
+
'default': defaults.reinitialize_chat,
|
| 213 |
+
'desc': 'Flag to trigger chat system reinitialization'
|
| 214 |
+
},
|
| 215 |
+
'debug_mode': {
|
| 216 |
+
'default': defaults.debug_mode,
|
| 217 |
+
'desc': 'Enable/disable debug information'
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
# Initialize each state variable if not already present
|
| 222 |
+
for var_name, config in state_vars.items():
|
| 223 |
+
if var_name not in st.session_state:
|
| 224 |
+
st.session_state[var_name] = config['default']
|
| 225 |
+
|
| 226 |
+
def reset_chat_state() -> None:
|
| 227 |
+
"""Reset chat-related session state variables to defaults."""
|
| 228 |
+
st.session_state.messages = []
|
| 229 |
+
st.session_state.current_chat_id = None
|
| 230 |
+
st.session_state.chat_ready = False
|
| 231 |
+
st.session_state.qa_system = None
|
| 232 |
+
|
| 233 |
+
def reset_collection_state() -> None:
|
| 234 |
+
"""Reset collection-related session state variables to defaults."""
|
| 235 |
+
st.session_state.selected_collection = None
|
| 236 |
+
st.session_state.current_collection = None
|
| 237 |
+
st.session_state.show_collection_dialog = False
|
| 238 |
+
|
| 239 |
+
def get_current_state() -> Dict[str, Any]:
|
| 240 |
+
"""
|
| 241 |
+
Get the current state of all session variables.
|
| 242 |
+
Useful for debugging and state management.
|
| 243 |
+
"""
|
| 244 |
+
return {
|
| 245 |
+
key: value for key, value in st.session_state.items()
|
| 246 |
+
if not key.startswith('_') # Exclude internal Streamlit states
|
| 247 |
}
|
| 248 |
|
| 249 |
for key, default_value in default_states.items():
|
|
|
|
| 345 |
return False
|
| 346 |
|
| 347 |
def display_collection_sidebar():
|
| 348 |
+
"""Display enhanced sidebar with collection management."""
|
| 349 |
with st.sidebar:
|
| 350 |
+
st.title("💬 Chat Controls")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
+
if st.button("🔄 Start New Chat", use_container_width=True):
|
| 353 |
+
st.session_state.messages = []
|
| 354 |
+
st.session_state.current_chat_id = None
|
| 355 |
+
if st.session_state.chat_ready:
|
| 356 |
+
st.rerun()
|
| 357 |
+
|
| 358 |
+
st.divider()
|
| 359 |
+
|
| 360 |
+
# Collection Management
|
| 361 |
+
st.title("📚 Collections")
|
| 362 |
collections = get_collections(st.session_state.db_conn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
+
if st.button("➕ Create Collection", use_container_width=True):
|
| 365 |
+
st.session_state.show_collection_dialog = True
|
| 366 |
+
|
| 367 |
+
# Collection selection with document preview
|
| 368 |
+
if collections:
|
| 369 |
+
selected = st.selectbox(
|
| 370 |
+
"Select Collection",
|
| 371 |
+
options=["All Documents"] + [c['name'] for c in collections],
|
| 372 |
+
key="collection_select"
|
|
|
|
|
|
|
| 373 |
)
|
| 374 |
+
|
| 375 |
+
if selected != "All Documents":
|
| 376 |
+
collection = next((c for c in collections if c['name'] == selected), None)
|
| 377 |
+
if collection:
|
| 378 |
+
documents = get_collection_documents(st.session_state.db_conn, collection['id'])
|
| 379 |
+
if documents:
|
| 380 |
+
st.markdown("### Documents in Collection")
|
| 381 |
+
for doc in documents:
|
| 382 |
+
with st.expander(f"📄 {doc['name']}", expanded=False):
|
| 383 |
+
st.caption(f"Uploaded: {doc['upload_date']}")
|
| 384 |
+
if st.button("Use for Chat", key=f"use_{doc['id']}"):
|
| 385 |
+
initialize_chat_for_document(doc['id'])
|
| 386 |
|
| 387 |
def display_collection_dialog():
|
| 388 |
"""Display the create collection dialog."""
|