-
Notifications
You must be signed in to change notification settings - Fork 50
/
local_loader.py
71 lines (57 loc) · 1.95 KB
/
local_loader.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
63
64
65
66
67
68
69
70
import os
from pathlib import Path
from pypdf import PdfReader
from langchain.docstore.document import Document
from langchain_community.document_loaders import TextLoader
from langchain_community.document_loaders.csv_loader import CSVLoader
def list_txt_files(data_dir="./data"):
paths = Path(data_dir).glob('**/*.txt')
for path in paths:
yield str(path)
def load_txt_files(data_dir="./data"):
docs = []
paths = list_txt_files(data_dir)
for path in paths:
print(f"Loading {path}")
loader = TextLoader(path)
docs.extend(loader.load())
return docs
def load_csv_files(data_dir="./data"):
docs = []
paths = Path(data_dir).glob('**/*.csv')
for path in paths:
loader = CSVLoader(file_path=str(path))
docs.extend(loader.load())
return docs
# Use with result of file_to_summarize = st.file_uploader("Choose a file") or a string.
# or a file like object.
def get_document_text(uploaded_file, title=None):
docs = []
fname = uploaded_file.name
if not title:
title = os.path.basename(fname)
if fname.lower().endswith('pdf'):
pdf_reader = PdfReader(uploaded_file)
for num, page in enumerate(pdf_reader.pages):
page = page.extract_text()
doc = Document(page_content=page, metadata={'title': title, 'page': (num + 1)})
docs.append(doc)
else:
# assume text
doc_text = uploaded_file.read().decode()
docs.append(doc_text)
return docs
if __name__ == "__main__":
example_pdf_path = "examples/healthy_meal_10_tips.pdf"
docs = get_document_text(open(example_pdf_path, "rb"))
for doc in docs:
print(doc)
docs = get_document_text(open("examples/us_army_recipes.txt", "rb"))
for doc in docs:
print(doc)
txt_docs = load_txt_files("examples")
for doc in txt_docs:
print(doc)
csv_docs = load_csv_files("examples")
for doc in csv_docs:
print(doc)