Download Vector_Db.py from thiru12/Iqbot-V1: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/thiru12/Iqbot-V1/resolve/main/Vector_Db.py
- Command line
-
hf download hf://spaces/thiru12/Iqbot-V1/Vector_Db.py
-
curl -L -o Vector_Db.py https://huggingface.co/spaces/thiru12/Iqbot-V1/resolve/main/Vector_Db.py
1.09 kB
| import os | |
| from langchain_google_genai import GoogleGenerativeAIEmbeddings | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain.vectorstores import Chroma | |
| from langchain_text_splitters import CharacterTextSplitter | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # Get the GOOGLE_API_KEY from environment variable | |
| google_api_key = os.getenv("GOOGLE_API_KEY") | |
| # Ensure the google_api_key is passed to the embeddings class | |
| embeddings = GoogleGenerativeAIEmbeddings( | |
| model='models/embedding-001', | |
| google_api_key=google_api_key | |
| ) | |
| def chunking(path): | |
| loader=PyPDFLoader(path) | |
| text_splitter = CharacterTextSplitter( | |
| chunk_size=225, | |
| chunk_overlap=50, | |
| ) | |
| pages=loader.load_and_split(text_splitter) | |
| print(len(pages)) | |
| return pages | |
| cwd=os.getcwd() | |
| doc=os.path.join(cwd,'IQ_TechMax') | |
| data=[] | |
| for i in os.listdir(doc): | |
| page=chunking(os.path.join(doc,i)) | |
| data.extend(page) | |
| embeddings=GoogleGenerativeAIEmbeddings(model='models/embedding-001') | |
| vectordb=Chroma.from_documents(data,embeddings,persist_directory="db1") | |