Spaces:
Paused
Paused
Update utils/database.py
Browse files- utils/database.py +22 -8
utils/database.py
CHANGED
|
@@ -150,6 +150,12 @@ def verify_vector_store(vector_store):
|
|
| 150 |
def handle_document_upload(uploaded_files):
|
| 151 |
"""Handle document upload with progress tracking."""
|
| 152 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Create a progress container
|
| 154 |
progress_container = st.empty()
|
| 155 |
status_container = st.empty()
|
|
@@ -160,10 +166,10 @@ def handle_document_upload(uploaded_files):
|
|
| 160 |
status_container.info("π Initializing document processing...")
|
| 161 |
|
| 162 |
# Reset existing states
|
| 163 |
-
if
|
| 164 |
-
|
| 165 |
-
if
|
| 166 |
-
|
| 167 |
|
| 168 |
# Initialize embeddings (10% progress)
|
| 169 |
status_container.info("π Initializing embeddings model...")
|
|
@@ -224,6 +230,9 @@ def handle_document_upload(uploaded_files):
|
|
| 224 |
if not vector_store:
|
| 225 |
status_container.error("β Failed to initialize vector store")
|
| 226 |
return
|
|
|
|
|
|
|
|
|
|
| 227 |
progress_bar.progress(90)
|
| 228 |
|
| 229 |
# Verify vector store
|
|
@@ -233,9 +242,6 @@ def handle_document_upload(uploaded_files):
|
|
| 233 |
status_container.error("β Vector store verification failed")
|
| 234 |
return
|
| 235 |
|
| 236 |
-
# Store in session state
|
| 237 |
-
st.session_state.vector_store = vector_store
|
| 238 |
-
|
| 239 |
# Initialize QA system (90-100% progress)
|
| 240 |
status_container.info("π Setting up QA system...")
|
| 241 |
qa_system = initialize_qa_system(vector_store)
|
|
@@ -243,6 +249,7 @@ def handle_document_upload(uploaded_files):
|
|
| 243 |
status_container.error("β Failed to initialize QA system")
|
| 244 |
return
|
| 245 |
|
|
|
|
| 246 |
st.session_state.qa_system = qa_system
|
| 247 |
|
| 248 |
# Complete!
|
|
@@ -264,13 +271,20 @@ def handle_document_upload(uploaded_files):
|
|
| 264 |
# Add notification
|
| 265 |
st.balloons()
|
| 266 |
|
|
|
|
|
|
|
|
|
|
| 267 |
except Exception as e:
|
| 268 |
status_container.error(f"β Error processing documents: {e}")
|
| 269 |
details_container.error(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
finally:
|
| 272 |
# Clean up progress display after 5 seconds if successful
|
| 273 |
-
if st.session_state.get('qa_system'):
|
| 274 |
time.sleep(5)
|
| 275 |
progress_container.empty()
|
| 276 |
|
|
|
|
| 150 |
def handle_document_upload(uploaded_files):
|
| 151 |
"""Handle document upload with progress tracking."""
|
| 152 |
try:
|
| 153 |
+
# Initialize session state variables if they don't exist
|
| 154 |
+
if 'qa_system' not in st.session_state:
|
| 155 |
+
st.session_state.qa_system = None
|
| 156 |
+
if 'vector_store' not in st.session_state:
|
| 157 |
+
st.session_state.vector_store = None
|
| 158 |
+
|
| 159 |
# Create a progress container
|
| 160 |
progress_container = st.empty()
|
| 161 |
status_container = st.empty()
|
|
|
|
| 166 |
status_container.info("π Initializing document processing...")
|
| 167 |
|
| 168 |
# Reset existing states
|
| 169 |
+
if st.session_state.vector_store is not None:
|
| 170 |
+
st.session_state.vector_store = None
|
| 171 |
+
if st.session_state.qa_system is not None:
|
| 172 |
+
st.session_state.qa_system = None
|
| 173 |
|
| 174 |
# Initialize embeddings (10% progress)
|
| 175 |
status_container.info("π Initializing embeddings model...")
|
|
|
|
| 230 |
if not vector_store:
|
| 231 |
status_container.error("β Failed to initialize vector store")
|
| 232 |
return
|
| 233 |
+
|
| 234 |
+
# Store vector store in session state
|
| 235 |
+
st.session_state.vector_store = vector_store
|
| 236 |
progress_bar.progress(90)
|
| 237 |
|
| 238 |
# Verify vector store
|
|
|
|
| 242 |
status_container.error("β Vector store verification failed")
|
| 243 |
return
|
| 244 |
|
|
|
|
|
|
|
|
|
|
| 245 |
# Initialize QA system (90-100% progress)
|
| 246 |
status_container.info("π Setting up QA system...")
|
| 247 |
qa_system = initialize_qa_system(vector_store)
|
|
|
|
| 249 |
status_container.error("β Failed to initialize QA system")
|
| 250 |
return
|
| 251 |
|
| 252 |
+
# Store QA system in session state
|
| 253 |
st.session_state.qa_system = qa_system
|
| 254 |
|
| 255 |
# Complete!
|
|
|
|
| 271 |
# Add notification
|
| 272 |
st.balloons()
|
| 273 |
|
| 274 |
+
# Set chat ready flag
|
| 275 |
+
st.session_state.chat_ready = True
|
| 276 |
+
|
| 277 |
except Exception as e:
|
| 278 |
status_container.error(f"β Error processing documents: {e}")
|
| 279 |
details_container.error(traceback.format_exc())
|
| 280 |
+
# Reset states on error
|
| 281 |
+
st.session_state.vector_store = None
|
| 282 |
+
st.session_state.qa_system = None
|
| 283 |
+
st.session_state.chat_ready = False
|
| 284 |
|
| 285 |
finally:
|
| 286 |
# Clean up progress display after 5 seconds if successful
|
| 287 |
+
if st.session_state.get('qa_system') is not None:
|
| 288 |
time.sleep(5)
|
| 289 |
progress_container.empty()
|
| 290 |
|