NavyDevilDoc commited on
Commit
184448e
Β·
verified Β·
1 Parent(s): 141bf13

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +47 -49
src/app.py CHANGED
@@ -357,64 +357,64 @@ with tab3:
357
 
358
  # --- TAB 4: KNOWLEDGE BASE ---
359
  with tab4:
360
- st.header("🧠 Unit Knowledge Base")
 
361
 
362
- is_admin = "admin" in st.session_state.roles
363
  kb_tab1, kb_tab2 = st.tabs(["πŸ“€ Add Documents", "πŸ—‚οΈ Manage Database"])
364
 
365
- # --- SUB-TAB 1: UPLOAD ---
366
  with kb_tab1:
367
- if is_admin:
368
- st.subheader("Ingest New Knowledge")
369
- uploaded_file = st.file_uploader("Upload Instructions, Manuals, or Logs", type=["pdf", "docx", "txt", "md"])
370
-
371
- col1, col2 = st.columns([1, 2])
372
- with col1:
373
- chunk_strategy = st.selectbox(
374
- "Chunking Strategy",
375
- ["paragraph", "token", "page"],
376
- help="Paragraph: Manuals. Token: Dense text. Page: Forms."
 
 
 
 
 
 
 
 
 
 
 
377
  )
378
-
379
- if uploaded_file and st.button("Process & Add"):
380
- with st.spinner("Analyzing and Indexing..."):
381
- # Use safe save + process
382
- temp_path = rag_engine.save_uploaded_file(uploaded_file)
383
- success, msg = rag_engine.process_and_add_document(
384
- temp_path,
385
- st.session_state.username,
386
- chunk_strategy
387
- )
388
-
389
- if success:
390
- st.success(msg)
391
- st.rerun()
392
- else:
393
- st.error(f"Failed: {msg}")
394
- else:
395
- st.info("πŸ”’ Only Admins can upload documents.")
396
 
397
  st.divider()
398
  st.subheader("πŸ”Ž Quick Test")
399
- test_query = st.text_input("Ask the brain something...")
400
  if test_query:
401
  results = rag_engine.search_knowledge_base(test_query, st.session_state.username)
 
 
402
  for i, doc in enumerate(results):
403
- # Using cleaned safe basename
404
  src_name = os.path.basename(doc.metadata.get('source', '?'))
405
  score = doc.metadata.get('relevance_score', 'N/A')
406
  with st.expander(f"Match {i+1}: {src_name} (Score: {score})"):
407
  st.write(doc.page_content)
408
 
409
- # --- SUB-TAB 2: MANAGE ---
410
  with kb_tab2:
411
  st.subheader("πŸ—„οΈ Database Inventory")
412
 
413
- # 1. Fetch current docs
414
  docs = rag_engine.list_documents(st.session_state.username)
415
 
416
  if not docs:
417
- st.info("Knowledge Base is empty.")
418
  else:
419
  st.markdown(f"**Total Documents:** {len(docs)}")
420
 
@@ -425,22 +425,20 @@ with tab4:
425
  with c2:
426
  st.caption(f"{doc['chunks']} chunks")
427
  with c3:
428
- if is_admin:
429
- if st.button("πŸ—‘οΈ Delete", key=doc['source']):
430
- with st.spinner("Deleting..."):
431
- success, msg = rag_engine.delete_document(st.session_state.username, doc['source'])
432
- if success:
433
- st.success(msg)
434
- st.rerun()
435
- else:
436
- st.error(msg)
437
- else:
438
- st.caption("Read Only")
439
 
440
- if is_admin and docs:
441
  st.divider()
442
  with st.expander("🚨 Danger Zone"):
443
- if st.button("☒️ RESET ENTIRE DATABASE", type="primary"):
 
444
  success, msg = rag_engine.reset_knowledge_base(st.session_state.username)
445
  if success:
446
  st.success(msg)
 
357
 
358
  # --- TAB 4: KNOWLEDGE BASE ---
359
  with tab4:
360
+ st.header("🧠 Personal Knowledge Base")
361
+ st.info(f"Managing knowledge for: **{st.session_state.username}**")
362
 
363
+ # We no longer check 'is_admin' for the whole tab
364
  kb_tab1, kb_tab2 = st.tabs(["πŸ“€ Add Documents", "πŸ—‚οΈ Manage Database"])
365
 
366
+ # --- SUB-TAB 1: UPLOAD (Unlocked for Everyone) ---
367
  with kb_tab1:
368
+ st.subheader("Ingest New Knowledge")
369
+ uploaded_file = st.file_uploader("Upload Instructions, Manuals, or Logs", type=["pdf", "docx", "txt", "md"])
370
+
371
+ col1, col2 = st.columns([1, 2])
372
+ with col1:
373
+ chunk_strategy = st.selectbox(
374
+ "Chunking Strategy",
375
+ ["paragraph", "token", "page"],
376
+ help="Paragraph: Manuals. Token: Dense text. Page: Forms."
377
+ )
378
+
379
+ if uploaded_file and st.button("Process & Add"):
380
+ with st.spinner("Analyzing and Indexing..."):
381
+ # 1. Save temp file
382
+ temp_path = rag_engine.save_uploaded_file(uploaded_file)
383
+
384
+ # 2. Process into USER'S specific DB (st.session_state.username)
385
+ success, msg = rag_engine.process_and_add_document(
386
+ temp_path,
387
+ st.session_state.username,
388
+ chunk_strategy
389
  )
390
+
391
+ if success:
392
+ st.success(msg)
393
+ st.rerun()
394
+ else:
395
+ st.error(f"Failed: {msg}")
 
 
 
 
 
 
 
 
 
 
 
 
396
 
397
  st.divider()
398
  st.subheader("πŸ”Ž Quick Test")
399
+ test_query = st.text_input("Ask your brain something...")
400
  if test_query:
401
  results = rag_engine.search_knowledge_base(test_query, st.session_state.username)
402
+ if not results:
403
+ st.warning("No matches found.")
404
  for i, doc in enumerate(results):
 
405
  src_name = os.path.basename(doc.metadata.get('source', '?'))
406
  score = doc.metadata.get('relevance_score', 'N/A')
407
  with st.expander(f"Match {i+1}: {src_name} (Score: {score})"):
408
  st.write(doc.page_content)
409
 
410
+ # --- SUB-TAB 2: MANAGE (Unlocked for Everyone) ---
411
  with kb_tab2:
412
  st.subheader("πŸ—„οΈ Database Inventory")
413
 
 
414
  docs = rag_engine.list_documents(st.session_state.username)
415
 
416
  if not docs:
417
+ st.info("Your Knowledge Base is empty.")
418
  else:
419
  st.markdown(f"**Total Documents:** {len(docs)}")
420
 
 
425
  with c2:
426
  st.caption(f"{doc['chunks']} chunks")
427
  with c3:
428
+ # Allow ANY user to delete their OWN documents
429
+ if st.button("πŸ—‘οΈ Delete", key=doc['source']):
430
+ with st.spinner("Deleting..."):
431
+ success, msg = rag_engine.delete_document(st.session_state.username, doc['source'])
432
+ if success:
433
+ st.success(msg)
434
+ st.rerun()
435
+ else:
436
+ st.error(msg)
 
 
437
 
 
438
  st.divider()
439
  with st.expander("🚨 Danger Zone"):
440
+ # Allow ANY user to reset their OWN database
441
+ if st.button("☒️ RESET MY DATABASE", type="primary"):
442
  success, msg = rag_engine.reset_knowledge_base(st.session_state.username)
443
  if success:
444
  st.success(msg)