-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathlocal_chat_utils.py
53 lines (34 loc) · 1.63 KB
/
local_chat_utils.py
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
import os
from langchain import FAISS
from langchain.embeddings import HuggingFaceInstructEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from summary_utils import doc_loader, remove_special_tokens
import streamlit as st
def create_and_save_local(file_path, model_name):
name = os.path.split(file_path)[1].split('.')[0]
embeddings = HuggingFaceInstructEmbeddings(model_name=model_name)
doc = doc_loader(file_path)
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
split_docs = splitter.split_documents(doc)
processed_split_docs = remove_special_tokens(split_docs)
db = FAISS.from_documents(processed_split_docs, embeddings)
db.save_local(folder_path='local_embeddings', index_name=name)
def load_local_embeddings(file_path, model_name):
name = os.path.split(file_path)[1].split('.')[0]
embeddings = HuggingFaceInstructEmbeddings(model_name=model_name)
db = FAISS.load_local(folder_path='local_embeddings', index_name=name, embeddings=embeddings)
return db
def load_db_from_file_and_create_if_not_exists_local(file_path, model_name):
with st.spinner('Loading chat embeddings...'):
try:
db = load_local_embeddings(file_path, model_name)
print('success')
except RuntimeError:
print('not found')
create_and_save_local(file_path, model_name)
db = load_local_embeddings(file_path, model_name)
if db:
st.success('Loaded successfully! Start a chat below.')
else:
st.warning('Something went wrong... failed to load chat embeddings.')
return db