37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
import os
|
|
from datetime import datetime
|
|
from langchain_community.document_loaders.csv_loader import CSVLoader
|
|
from langchain_community.document_loaders.text import TextLoader
|
|
from langchain_community.document_loaders import UnstructuredPDFLoader
|
|
from langchain_community.document_loaders import UnstructuredWordDocumentLoader
|
|
from langchain_community.document_loaders import UnstructuredExcelLoader
|
|
from langchain_community.document_loaders import UnstructuredPowerPointLoader
|
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
def fileloader(file_path):
|
|
# Load the PDF file and split the data into chunks
|
|
data = None
|
|
if file_path.lower().endswith('.pdf'):
|
|
loader = UnstructuredPDFLoader(file_path=file_path)
|
|
elif file_path.lower().endswith('.docx') or file_path.lower().endswith('.doc'):
|
|
loader = UnstructuredWordDocumentLoader(file_path=file_path)
|
|
elif file_path.lower().endswith('.pptx') or file_path.lower().endswith('.pptx'):
|
|
loader = UnstructuredPowerPointLoader(file_path=file_path)
|
|
elif file_path.lower().endswith('.xlsx') or file_path.lower().endswith('.xls'):
|
|
loader = UnstructuredExcelLoader(file_path=file_path)
|
|
elif file_path.lower().endswith('.csv'):
|
|
loader = CSVLoader(file_path=file_path)
|
|
else:
|
|
loader = TextLoader(file_path=file_path)
|
|
data = loader.load()
|
|
return data
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
if len(sys.argv) < 2:
|
|
print(f'{sys.argv[0]} file\nload a file and get its text')
|
|
sys.exit(1)
|
|
text = fileloader(sys.argv[1])
|
|
print(f'{text}')
|
|
|