File size: 1,053 Bytes
3af157b 786f732 3af157b 786f732 3af157b 786f732 3af157b f2b7073 786f732 3af157b 786f732 3af157b |
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 |
import streamlit as st
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain_community.vectorstores import InMemoryVectorStore
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
@st.cache_resource()
def load_embedding_model(model):
model = HuggingFaceEmbeddings(model_name=model)
return model
@st.cache_resource()
def load_vector_store():
model = load_embedding_model("sentence-transformers/all-MiniLM-L12-v2")
vector_store = Chroma(
collection_name="main_store",
embedding_function=model,
persist_directory="./chroma",
)
return vector_store
def process_pdf(pdf, vector_store):
"""
Loads a pdf and splits it into chunks
"""
loader = PyPDFLoader(pdf)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vector_store.add_documents(splits)
|