# -*- coding: utf-8 -*- """ Created on Sat Apr 11 15:03:04 2026 @author: THYAGHARAJAN """ #line 20 import avoids circular error from utils.core_imports import get_current_user from fastapi import APIRouter, Depends, HTTPException import os import shutil import threading #used for rebuild_faiss_index line 54 router = APIRouter() #the app acted as a proxy to Ollama in main file @router.delete("/admin/delete-document") def delete_document(filename: str,current_user: str = Depends(get_current_user)): from utils.core_imports import get_db, rebuild_faiss_index, get_upload_folder #lazy import filename = filename.strip() conn = get_db() cursor = conn.cursor() # Check existence cursor.execute( "SELECT faiss_id FROM document_chunks WHERE TRIM(source)=?", (filename,) ) rows = cursor.fetchall() if not rows: conn.close() raise HTTPException(status_code=404, detail="Document not found") # ✅ DELETE (this was missing in your code) cursor.execute( "DELETE FROM document_chunks WHERE TRIM(source)=?", (filename,) ) conn.commit() conn.close() # Delete physical file file_path = os.path.join(get_upload_folder(), filename) if os.path.exists(file_path): os.remove(file_path) rebuild_faiss_index() # Rebuild FAISS in background threading.Thread(target=rebuild_faiss_index).start() ''' #used for debugging. Found & was not converted to %26 print(f"Incoming filename: [{filename}]") cursor.execute("SELECT DISTINCT source FROM document_chunks") all_sources = cursor.fetchall() print("DB sources:") for s in all_sources: print(f"[{s[0]}]") ''' return {"message": f"{filename} removed from index"} @router.delete("/admin/delete-folder") def delete_folder(folder: str,current_user: str = Depends(get_current_user)): from utils.core_imports import get_db, rebuild_faiss_index conn = get_db() cursor = conn.cursor() cursor.execute( "DELETE FROM document_chunks WHERE source LIKE ?", (f"%{folder}%",) ) deleted_count = cursor.rowcount conn.commit() conn.close() if deleted_count == 0: raise HTTPException(status_code=404, detail="Folder not found") threading.Thread(target=rebuild_faiss_index).start() return {"message": f"{folder} folder removed from index"} @router.delete("/admin/reset-index") def reset_index(confirm: bool = False,current_user: str = Depends(get_current_user)): from utils.core_imports import get_db, rebuild_faiss_index, get_upload_folder #lazy import to avoid circular import #confirm button will be displayed if not confirm: return {"message": "Set confirm=true to reset index"} conn = get_db() cursor = conn.cursor() cursor.execute("DELETE FROM document_chunks") # delete ALL rows in document_chunks table conn.commit() conn.close() #delete the files in the UPLOAD dir doc_ingestion folder upload_dir = get_upload_folder() shutil.rmtree(upload_dir) os.makedirs(upload_dir, exist_ok=True) threading.Thread(target=rebuild_faiss_index).start() return {"message": "Index reset completed"} @router.get("/admin/list-documents") def list_documents(current_user: str = Depends(get_current_user)): from utils.core_imports import get_db conn = get_db() cursor = conn.cursor() cursor.execute(""" SELECT source, COUNT(*) as chunks FROM document_chunks GROUP BY source """) rows = cursor.fetchall() conn.close() docs = [{"document": r[0], "chunks": r[1]} for r in rows] return {"documents": docs}