Skip to content

Commit 60b9722

Browse files
authored
v2.6.1
1 parent 74f9c59 commit 60b9722

15 files changed

+676
-51
lines changed

src/config.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
# Global settings and variables
4+
ENABLE_PRINT = False
5+
GLOBAL_VAR = True
6+
7+
# Directories
8+
ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
9+
SOURCE_DIRECTORY = f"{ROOT_DIRECTORY}/Docs_for_DB"
10+
11+
# Ingest settings
12+
INGEST_THREADS = os.cpu_count() or 8
13+
14+
# Document mappings
15+
DOCUMENT_MAP = {
16+
".pdf": "PDFMinerLoader",
17+
".docx": "Docx2txtLoader",
18+
".txt": "TextLoader",
19+
".json": "JSONLoader",
20+
".enex": "EverNoteLoader",
21+
".eml": "UnstructuredEmailLoader",
22+
".msg": "UnstructuredEmailLoader",
23+
".csv": "UnstructuredCSVLoader",
24+
".xls": "UnstructuredExcelLoader",
25+
".xlsx": "UnstructuredExcelLoader",
26+
".rtf": "UnstructuredRTFLoader",
27+
}

src/config.yaml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ AVAILABLE_MODELS:
2323
- jinaai/jina-embedding-b-en-v1
2424
- jinaai/jina-embedding-s-en-v1
2525
- jinaai/jina-embedding-t-en-v1
26-
- jinaai/jina-embeddings-v2-base-en
27-
- jinaai/jina-embeddings-v2-small-en
28-
COMPUTE_DEVICE: cpu
29-
EMBEDDING_MODEL_NAME:
30-
chunk_overlap: 200
31-
chunk_size: 600
26+
COMPUTE_DEVICE: cuda
27+
Compute_Device:
28+
available:
29+
- cuda
30+
- cpu
31+
database_creation: cuda
32+
database_query: cpu
33+
EMBEDDING_MODEL_NAME: C:/PATH/Scripts/ChromaDB-Plugin-for-LM-Studio/v2_6 - working/Embedding_Models/sentence-transformers--gtr-t5-base
3234
database:
33-
contexts: 15
35+
chunk_overlap: 150
36+
chunk_size: 500
37+
contexts: 10
38+
device: null
3439
similarity: 0.9
3540
embedding-models:
3641
bge:
@@ -41,7 +46,7 @@ embedding-models:
4146
server:
4247
api_key: ''
4348
connection_str: http://localhost:1234/v1
44-
model_max_tokens: 512
49+
model_max_tokens: -1
4550
model_temperature: 0.1
4651
prefix: '[INST]'
4752
suffix: '[/INST]'
@@ -51,7 +56,15 @@ styles:
5156
frame: 'background-color: #161b22;'
5257
input: 'background-color: #2e333b; color: light gray; font: 13pt "Segoe UI Historic";'
5358
text: 'background-color: #092327; color: light gray; font: 12pt "Segoe UI Historic";'
54-
transcriber:
59+
transcribe_file:
5560
device: cpu
61+
file: C:/PATH/Scripts/ChromaDB-Plugin-for-LM-Studio/v2_6 - working/test.mp3
62+
language: Option 1
63+
model: base.en
64+
quant: int8
65+
timestamps: true
66+
translate: false
67+
transcriber:
68+
device: cuda
5669
model: base.en
5770
quant: float32

src/create_database - backup.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import logging
2+
import os
3+
import shutil
4+
import yaml
5+
import gc
6+
from langchain.docstore.document import Document
7+
from langchain.embeddings import HuggingFaceInstructEmbeddings, HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings
8+
from langchain.vectorstores import Chroma
9+
from chromadb.config import Settings
10+
from document_processor import load_documents, split_documents
11+
import torch
12+
from utilities import validate_symbolic_links
13+
from termcolor import cprint
14+
# from memory_profiler import profile
15+
16+
ENABLE_PRINT = True
17+
ENABLE_CUDA_PRINT = False
18+
19+
torch.cuda.reset_peak_memory_stats()
20+
21+
def my_cprint(*args, **kwargs):
22+
if ENABLE_PRINT:
23+
filename = "create_database.py"
24+
modified_message = f"{filename}: {args[0]}"
25+
cprint(modified_message, *args[1:], **kwargs)
26+
27+
def print_cuda_memory():
28+
if ENABLE_CUDA_PRINT:
29+
max_allocated_memory = torch.cuda.max_memory_allocated()
30+
memory_allocated = torch.cuda.memory_allocated()
31+
reserved_memory = torch.cuda.memory_reserved()
32+
33+
my_cprint(f"Max CUDA memory allocated: {max_allocated_memory / (1024**2):.2f} MB", "green")
34+
my_cprint(f"Total CUDA memory allocated: {memory_allocated / (1024**2):.2f} MB", "yellow")
35+
my_cprint(f"Total CUDA memory reserved: {reserved_memory / (1024**2):.2f} MB", "yellow")
36+
37+
print_cuda_memory()
38+
39+
ROOT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
40+
SOURCE_DIRECTORY = f"{ROOT_DIRECTORY}/Docs_for_DB"
41+
PERSIST_DIRECTORY = f"{ROOT_DIRECTORY}/Vector_DB"
42+
INGEST_THREADS = os.cpu_count() or 8
43+
44+
CHROMA_SETTINGS = Settings(
45+
chroma_db_impl="duckdb+parquet",
46+
persist_directory=PERSIST_DIRECTORY,
47+
anonymized_telemetry=False
48+
)
49+
50+
# @profile
51+
def main():
52+
print_cuda_memory()
53+
54+
with open(os.path.join(ROOT_DIRECTORY, "config.yaml"), 'r') as stream:
55+
config_data = yaml.safe_load(stream)
56+
57+
EMBEDDING_MODEL_NAME = config_data.get("EMBEDDING_MODEL_NAME")
58+
59+
# calls document_processor.py
60+
my_cprint(f"Loading documents.", "cyan")
61+
documents = load_documents(SOURCE_DIRECTORY)
62+
my_cprint(f"Successfully loaded documents.", "cyan")
63+
64+
# calls document_processory.py
65+
texts = split_documents(documents)
66+
print_cuda_memory()
67+
68+
# calls get_embeddings function
69+
embeddings = get_embeddings(EMBEDDING_MODEL_NAME, config_data)
70+
my_cprint("Embedding model loaded.", "green")
71+
print_cuda_memory()
72+
73+
if os.path.exists(PERSIST_DIRECTORY):
74+
shutil.rmtree(PERSIST_DIRECTORY)
75+
os.makedirs(PERSIST_DIRECTORY)
76+
77+
my_cprint("Creating database.", "cyan")
78+
79+
db = Chroma.from_documents(
80+
texts, embeddings,
81+
persist_directory=PERSIST_DIRECTORY,
82+
client_settings=CHROMA_SETTINGS,
83+
)
84+
print_cuda_memory()
85+
86+
# persist database
87+
my_cprint("Persisting database.", "cyan")
88+
db.persist()
89+
my_cprint("Database persisted.", "cyan")
90+
print_cuda_memory()
91+
92+
del embeddings.client
93+
# my_cprint("Deleted embeddings.client.", "red")
94+
print_cuda_memory()
95+
96+
del embeddings
97+
# my_cprint("Deleted embeddings variable.", "red")
98+
print_cuda_memory()
99+
100+
torch.cuda.empty_cache()
101+
# my_cprint("CUDA cache emptied.", "red")
102+
print_cuda_memory()
103+
104+
gc.collect()
105+
my_cprint("Embedding model removed from memory.", "red")
106+
print_cuda_memory()
107+
108+
# print(torch.cuda.memory_summary())
109+
110+
# @profile
111+
def get_embeddings(EMBEDDING_MODEL_NAME, config_data, normalize_embeddings=False):
112+
my_cprint("Creating embeddings.", "cyan")
113+
print_cuda_memory()
114+
115+
compute_device = config_data['Compute_Device']['database_creation']
116+
117+
if "instructor" in EMBEDDING_MODEL_NAME:
118+
embed_instruction = config_data['embedding-models']['instructor'].get('embed_instruction')
119+
query_instruction = config_data['embedding-models']['instructor'].get('query_instruction')
120+
121+
return HuggingFaceInstructEmbeddings(# creating model instance
122+
model_name=EMBEDDING_MODEL_NAME,
123+
model_kwargs={"device": compute_device},
124+
encode_kwargs={"normalize_embeddings": normalize_embeddings},
125+
embed_instruction=embed_instruction,
126+
query_instruction=query_instruction
127+
)
128+
129+
elif "bge" in EMBEDDING_MODEL_NAME:
130+
query_instruction = config_data['embedding-models']['bge'].get('query_instruction')
131+
132+
return HuggingFaceBgeEmbeddings(
133+
model_name=EMBEDDING_MODEL_NAME,
134+
model_kwargs={"device": compute_device},
135+
query_instruction=query_instruction,
136+
encode_kwargs={"normalize_embeddings": normalize_embeddings}
137+
)
138+
139+
else:
140+
141+
return HuggingFaceEmbeddings(
142+
model_name=EMBEDDING_MODEL_NAME,
143+
model_kwargs={"device": compute_device},
144+
encode_kwargs={"normalize_embeddings": normalize_embeddings}
145+
)
146+
147+
if __name__ == "__main__":
148+
logging.basicConfig(
149+
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(message)s",
150+
level=logging.INFO
151+
)
152+
main()

src/create_database.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ENABLE_PRINT = True
1717
ENABLE_CUDA_PRINT = False
1818

19-
torch.cuda.reset_peak_memory_stats()
19+
# torch.cuda.reset_peak_memory_stats()
2020

2121
def my_cprint(*args, **kwargs):
2222
if ENABLE_PRINT:
@@ -96,7 +96,6 @@ def main():
9696
gc.collect()
9797
my_cprint("Embedding model removed from memory.", "red")
9898
print_cuda_memory()
99-
10099

101100
# @profile
102101
def get_embeddings(EMBEDDING_MODEL_NAME, config_data, normalize_embeddings=False):
@@ -140,4 +139,4 @@ def get_embeddings(EMBEDDING_MODEL_NAME, config_data, normalize_embeddings=False
140139
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)s - %(message)s",
141140
level=logging.INFO
142141
)
143-
main()
142+
main()

src/gui.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from PySide6.QtWidgets import (
22
QApplication, QWidget, QPushButton, QVBoxLayout, QTabWidget,
3-
QTextEdit, QSplitter, QFrame, QStyleFactory, QLabel, QHBoxLayout
3+
QTextEdit, QSplitter, QFrame, QStyleFactory, QLabel, QGridLayout
44
)
55
from PySide6.QtCore import Qt, QThread, Signal, QUrl
66
from PySide6.QtWebEngineWidgets import QWebEngineView
@@ -37,34 +37,35 @@ def init_ui(self):
3737
self.setGeometry(300, 300, 975, 975)
3838
self.setMinimumSize(550, 610)
3939

40+
# Left panel setup with grid layout
4041
self.left_frame = QFrame()
41-
left_vbox = QVBoxLayout()
42+
grid_layout = QGridLayout()
43+
44+
# Tab widget spanning two columns
4245
tab_widget = create_tabs()
43-
left_vbox.addWidget(tab_widget)
44-
46+
grid_layout.addWidget(tab_widget, 0, 0, 1, 2) # Span two columns
47+
48+
# Button definitions and positions in the grid
4549
button_data = [
4650
("Download Embedding Model", lambda: download_embedding_model(self)),
4751
("Set Embedding Model Directory", select_embedding_model_directory),
4852
("Choose Documents for Database", choose_documents_directory),
4953
("Create Vector Database", self.on_create_button_clicked)
5054
]
51-
52-
# Create two rows of buttons
53-
for i in range(0, len(button_data), 2):
54-
button_row = QHBoxLayout()
55-
for j in range(2):
56-
if i + j < len(button_data):
57-
text, handler = button_data[i+j]
58-
button = QPushButton(text)
59-
button.setStyleSheet(styles.get('button', ''))
60-
button.clicked.connect(handler)
61-
button_row.addWidget(button)
62-
left_vbox.addLayout(button_row)
63-
64-
self.left_frame.setLayout(left_vbox)
55+
button_positions = [(1, 0), (1, 1), (2, 0), (2, 1)]
56+
57+
# Create and add buttons to the grid layout
58+
for position, (text, handler) in zip(button_positions, button_data):
59+
button = QPushButton(text)
60+
button.setStyleSheet(styles.get('button', ''))
61+
button.clicked.connect(handler)
62+
grid_layout.addWidget(button, *position)
63+
64+
self.left_frame.setLayout(grid_layout)
6565
self.left_frame.setStyleSheet(styles.get('frame', ''))
6666
main_splitter.addWidget(self.left_frame)
6767

68+
# Right panel setup
6869
right_frame = QFrame()
6970
right_vbox = QVBoxLayout()
7071

@@ -84,8 +85,10 @@ def init_ui(self):
8485

8586
right_vbox.addWidget(submit_questions_button)
8687

88+
# Define widget containing buttons
8789
button_row_widget = create_button_row(self.on_submit_button_clicked, styles.get('button', ''))
8890

91+
# Add widgets from button_module.py
8992
right_vbox.addWidget(button_row_widget)
9093

9194
right_frame.setLayout(right_vbox)

0 commit comments

Comments
 (0)