Spaces:
Runtime error
Runtime error
import tempfile | |
import os | |
from langchain.document_loaders import PyPDFLoader | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
from langchain.text_splitter import RecursiveCharacterTextSplitter | |
from langchain.document_loaders import DirectoryLoader, TextLoader | |
from langchain.vectorstores import Pinecone | |
from PyPDF2 import PdfReader | |
import pinecone | |
from dotenv import load_dotenv | |
from langchain.vectorstores import Chroma | |
load_dotenv() | |
openai_api_key=os.getenv('OPENAI_API_KEY') | |
def create_indexes(file: tempfile, collection_name: str) -> str: | |
try: | |
file_path = file.name | |
reader = PyPDFLoader(file_path) | |
documents = reader.load_and_split() | |
embeddings = OpenAIEmbeddings( | |
openai_api_key=openai_api_key | |
) | |
persist_directory = './db_metadata' | |
vectordb = Chroma.from_documents( | |
collection_name=collection_name, | |
documents=documents, | |
embedding=embeddings, | |
persist_directory=persist_directory | |
) | |
vectordb.persist() | |
return 'Document uploaded and index created successfully. You can chat now.' | |
except Exception as e: | |
return e | |
# def clear_indexes(pinecone_api_key: str, pinecone_environment: str, pinecone_index_name: str) -> str: | |
# try: | |
# pinecone.init( | |
# api_key=pinecone_api_key, | |
# environment=pinecone_environment | |
# ) | |
# indexes_list = pinecone.list_indexes() | |
# if pinecone_index_name in indexes_list: | |
# pinecone.delete_index(name=pinecone_index_name) | |
# return 'Indexes cleared.', None | |
# except Exception as e: | |
# return e, None | |