Spaces:
Sleeping

smartgit / ingest.py
yamlspace's picture
testing ingest scripts
85ecfcf
Raw History Blame Contribute Delete
1.98 kB
import os
import getpass
import shutil
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import DeepLake
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import TextLoader
from dotenv import load_dotenv
load_dotenv()
embeddings = OpenAIEmbeddings(disallowed_special=())
index_name = "./index/" + 'finecast'
if os.path.exists(index_name):
shutil.rmtree(index_name)
db = DeepLake(dataset_path=index_name, embedding_function=embeddings) #dataset would be publicly available
#root_dirs = ['../../elsewhen/finecast-infrastructure','../../elsewhen/finecast/infra2','../../elsewhen/finecast/infrastructure','../../elsewhen/finecast/ci']
root_dirs = [
'../../elsewhen/finecast-infrastructure/.github',
'../../elsewhen/finecast-infrastructure/environments',
'../../elsewhen/finecast-infrastructure/modules',
'../../elsewhen/finecast-infrastructure/scripts',
'../../elsewhen/finecast-infrastructure/layers',
'../../elsewhen/finecast-infrastructure/*.yaml',
'../../elsewhen/finecast-infrastructure/*.md',
'../../elsewhen/finecast/infra2',
'../../elsewhen/finecast/infrastructure',
'../../elsewhen/finecast/ci'
]
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = []
texts = []
for root_dir in root_dirs:
for dirpath, dirnames, filenames in os.walk(root_dir):
for file in filenames:
try:
loader = TextLoader(os.path.join(dirpath, file), encoding='utf-8')
docs = loader.load_and_split()
docs = text_splitter.split_documents(docs)
for d in docs:
d.metadata['repo'] = root_dir
d.metadata['filename'] = file
texts.extend(docs)
except Exception as e:
print(file, e)
db.add_documents(texts)
print("db.ds:")
db.ds.summary()
print(len(db.ds))
print ("end db.ds")