-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathadmin_database.py
More file actions
198 lines (153 loc) · 7.17 KB
/
admin_database.py
File metadata and controls
198 lines (153 loc) · 7.17 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import gradio as gr
import chromadb
from chromadb.utils import embedding_functions
from chromadb import Collection
from docling.document_converter import DocumentConverter
from docling.chunking import HybridChunker
from docling_core.transforms.chunker.tokenizer.huggingface import HuggingFaceTokenizer
from transformers import AutoTokenizer
from pathlib import Path
import re
# --- Setup ChromaDB client ---
chroma_client = chromadb.PersistentClient(path="./db")
embedding_fn = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="all-mpnet-base-v2")
EMBED_MODEL_ID = "sentence-transformers/all-mpnet-base-v2"
MAX_TOKENS = 512
custom_tokenizer = HuggingFaceTokenizer(
tokenizer=AutoTokenizer.from_pretrained(EMBED_MODEL_ID),
max_tokens=MAX_TOKENS,
)
chunker = HybridChunker(tokenizer=custom_tokenizer, merge_peers=True)
# --- Core Utility Functions ---
def ensure_collection(client: chromadb.ClientAPI, collection_name: str) -> Collection:
try:
return client.get_collection(name=collection_name, embedding_function=embedding_fn)
except Exception:
return client.create_collection(name=collection_name, embedding_function=embedding_fn)
def insert_markdown_from_path(path_str, collection_name):
try:
collection = ensure_collection(chroma_client, collection_name)
path = Path(path_str)
if not path.exists():
return f"❌ Path '{path}' does not exist."
files_to_process = []
if path.is_file() and path.suffix == ".md":
files_to_process = [path]
elif path.is_dir():
files_to_process = [p for p in path.glob("*.md")]
else:
return f"❌ Invalid file or folder: {path}"
if not files_to_process:
return "⚠️ No markdown (.md) files found."
total_chunks = 0
total_files = len(files_to_process)
for idx, file_path in enumerate(files_to_process, start=1):
print(f"Processing file {idx} of {total_files}: {file_path.name}") # progress bar on terminal
doc = DocumentConverter().convert(source=str(file_path)).document
chunk_iter = chunker.chunk(dl_doc=doc)
BATCH_SIZE = 10 # adjust depending on your DB limits
document_ids = []
document_chunks = []
document_name = file_path.stem.replace(" ", "-").replace("_", "-")
for i, chunk in enumerate(chunk_iter):
if not chunk or not chunk.text:
continue # skip empty chunks
document_ids.append(f"{document_name}_chunk{i}")
enriched_text = chunker.contextualize(chunk=chunk)
document_chunks.append(enriched_text)
if len(document_chunks) >= BATCH_SIZE:
print("Adding chunks now in batch = 10")
collection.add(documents=document_chunks, ids=document_ids)
document_ids = []
document_chunks = []
# --- Insert remaining chunks ---
if document_chunks:
collection.add(documents=document_chunks, ids=document_ids)
return f"✅ Imported {total_files} file(s) with total {total_chunks} chunks into '{collection_name}'"
except Exception as e:
return f"❌ Failed to insert documents: {e}"
def list_collections():
try:
collections = chroma_client.list_collections()
return [col for col in collections]
except Exception as e:
return [f"Error: {e}"]
def delete_collection(collection_name):
try:
chroma_client.delete_collection(name=collection_name)
return f"✅ Deleted collection '{collection_name}'"
except Exception as e:
return f"❌ Failed to delete collection '{collection_name}': {e}"
def list_documents(collection_name):
try:
collection = chroma_client.get_collection(name=collection_name, embedding_function=embedding_fn)
results = collection.get()
ids = results["ids"]
if not ids:
return "No documents found in this collection."
# Create a mapping filename -> list of chunks
file_to_chunks = {}
for doc_id in ids:
# Extract filename prefix before '_chunk'
m = re.match(r"(.+)_chunk\d+", doc_id)
filename = m.group(1) if m else "UnknownFile"
file_to_chunks.setdefault(filename, []).append(doc_id)
# Format output nicely
output_lines = []
for filename, chunks in file_to_chunks.items():
output_lines.append(f"File: {filename} ({len(chunks)} chunks)")
#for chunk_id in chunks:
# output_lines.append(f" - {chunk_id}")
#output_lines.append("") # blank line for separation
return "\n".join(output_lines)
except Exception as e:
return f"Error: {e}"
def delete_document(collection_name, document_id):
try:
collection = chroma_client.get_collection(name=collection_name, embedding_function=embedding_fn)
collection.delete(ids=[document_id])
return f"✅ Deleted document ID '{document_id}' from '{collection_name}'"
except Exception as e:
return f"❌ Failed to delete document: {e}"
# --- Gradio Interface ---
with gr.Blocks(title="ChromaDB Admin UI") as demo:
gr.Markdown("## 🧠 ChromaDB Vector Database Admin Panel")
gr.Markdown("### 🔄 List all collection which are in VectorDB")
with gr.Row():
collection_output = gr.Textbox(label="Collections", lines=5)
with gr.Row():
collection_list_btn = gr.Button("🔄 List Collections")
gr.Markdown("### ❌ Delete whole collection")
with gr.Row():
del_col_name = gr.Textbox(label="Collection to Delete")
with gr.Row():
del_col_output = gr.Textbox(label="Delete Status")
with gr.Row():
del_col_btn = gr.Button("❌ Delete Collection")
gr.Markdown("### 📄 List Documents in Vector Database")
with gr.Row():
doc_col_name = gr.Textbox(label="Collection Name (to list docs)")
with gr.Row():
doc_ids_output = gr.Textbox(label="Document IDs", lines=15)
with gr.Row():
list_docs_btn = gr.Button("📄 List Documents")
gr.Markdown("### 📂 Import Markdown Files from Local Path")
with gr.Row():
local_path_input = gr.Textbox(label="Path to File or Folder (must be .md)", placeholder="./my_docs")
with gr.Row():
local_path_collection = gr.Textbox(label="Target Collection Name")
with gr.Row():
local_path_output = gr.Textbox(label="Import Status")
with gr.Row():
local_path_btn = gr.Button("📁 Import from Path")
local_path_btn.click(
fn=insert_markdown_from_path,
inputs=[local_path_input, local_path_collection],
outputs=local_path_output,
)
# --- Button Functionality Bindings ---
collection_list_btn.click(fn=list_collections, outputs=collection_output)
del_col_btn.click(fn=delete_collection, inputs=del_col_name, outputs=del_col_output)
list_docs_btn.click(fn=list_documents, inputs=doc_col_name, outputs=doc_ids_output)
# --- Launch App ---
demo.launch(server_name="0.0.0.0", server_port=8082)