-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorSave.py
62 lines (54 loc) · 1.75 KB
/
vectorSave.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from langchain.vectorstores import Qdrant
from langchain.embeddings.openai import OpenAIEmbeddings
import qdrant_client
import os
from langchain.text_splitter import CharacterTextSplitter
from dotenv import load_dotenv
import streamlit as st
from PyPDF2 import PdfReader
def get_pdf_text(pdf_file):
text =""
for pdf in pdf_file:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def get_text_chunks(text):
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
return chunks
def main():
load_dotenv()
st.set_page_config(page_title="Save user book vertex to the Qdrant cloud")
pdf_file = st.file_uploader("upload the pdf" ,
accept_multiple_files=True
)
client = qdrant_client.QdrantClient(
os.getenv("QDRANT_HOST"),
api_key=os.getenv("QDRANT_API_KEY")
)
vectors_config = qdrant_client.http.models.VectorParams(
size=1536,
distance=qdrant_client.http.models.Distance.COSINE)
client.recreate_collection(
collection_name=os.getenv("QDRANT_COLLECTION_NAME"),
vectors_config = vectors_config,
)
embedding =OpenAIEmbeddings()
vector_store = Qdrant(
client=client,
collection_name=os.getenv("QDRANT_COLLECTION_NAME"),
embeddings=embedding
)
if st.button("Submit"):
with st.spinner("Processing"):
text = get_pdf_text(pdf_file)
chunks = get_text_chunks(text)
vector_store.add_texts(chunks)
if __name__ == "__main__":
main()