|
import os |
|
|
|
|
|
from langchain_community.document_loaders import PyPDFLoader |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain_community.document_loaders.csv_loader import CSVLoader |
|
|
|
from llama_parse import LlamaParse |
|
|
|
from typing import Union, List, Dict |
|
|
|
from abc import ABC, abstractmethod |
|
|
|
class Extractor(ABC): |
|
|
|
def __init__(self, file_or_list: Union[str, List[str]], num_workers: int = 1, verbose: bool = False): |
|
""" We can provide a list of files or a single file """ |
|
if isinstance(file_or_list, str): |
|
self.filelist = [file_or_list] |
|
else: |
|
self.filelist = file_or_list |
|
self.num_workers = num_workers |
|
self.verbose = verbose |
|
super().__init__() |
|
|
|
@abstractmethod |
|
def extract_text(self) -> Dict[str, List[str]]: |
|
""" Extracts text from the PDF, no processing. |
|
Return a dictionary, key = filename, value = list of strings, one for each page. |
|
""" |
|
pass |
|
|
|
@abstractmethod |
|
def extract_images(self): |
|
"""Extracts images from the PDF, no processing.""" |
|
pass |
|
|
|
@abstractmethod |
|
def extract_tables(self): |
|
""" Extracts tables from the PDF, no processing. |
|
Return in json format |
|
""" |
|
pass |
|
|
|
class _PyPDFLoader(Extractor): |
|
|
|
def extract_text(self): |
|
output_dict = {} |
|
for fpath in self.filelist: |
|
fname = fpath.split('/')[-1] |
|
output_dict[fname] = [p.page_content for p in PyPDFLoader(fpath).load()] |
|
return output_dict |
|
|
|
def extract_images(self): |
|
raise NotImplementedError("Not implemented or PyPDFLoader does not support image extraction") |
|
return |
|
|
|
def extract_tables(self): |
|
raise NotImplementedError("Not implemented or PyPDFLoader does not support table extraction") |
|
return |
|
|
|
|
|
class _LlamaParse(Extractor): |
|
|
|
def extract_text(self): |
|
|
|
if os.getenv("LLAMA_PARSE_API_KEY") is None: |
|
raise ValueError("LLAMA_PARSE_API_KEY is not set.") |
|
|
|
parser = LlamaParse( |
|
api_key = os.getenv("LLAMA_PARSE_API_KEY"), |
|
num_workers=self.num_workers, |
|
verbose=self.verbose, |
|
language="en", |
|
result_type="text" |
|
) |
|
output_dict = {} |
|
for fpath in self.filelist: |
|
|
|
docs = parser.get_json_result(fpath) |
|
docs[0]['pages'][0]['text'] |
|
output_dict[fpath] = None |
|
return output_dict |
|
|
|
def extract_images(self): |
|
raise NotImplementedError("Not implemented or LlamaParse does not support image extraction") |
|
return |
|
|
|
def extract_tables(self): |
|
raise NotImplementedError("Not implemented or LlamaParse does not support table extraction") |
|
return |
|
|
|
class _TXTLoader(Extractor): |
|
|
|
def extract_text(self): |
|
output_dict = {} |
|
for fpath in self.filelist: |
|
fname = fpath.split('/')[-1] |
|
output_dict[fname] = [open(fpath, 'r').read()] |
|
|
|
|
|
return output_dict |
|
|
|
def extract_images(self): |
|
raise NotImplementedError("Not implemented or PyPDFLoader does not support image extraction") |
|
return |
|
|
|
def extract_tables(self): |
|
raise NotImplementedError("Not implemented or PyPDFLoader does not support table extraction") |
|
return |
|
|
|
class _CSVLoader(Extractor): |
|
|
|
def extract_text(self): |
|
output_dict = {} |
|
for fpath in self.filelist: |
|
fname = fpath.split('/')[-1] |
|
output_dict[fname] = [CSVLoader(fpath).load()] |
|
|
|
return output_dict |
|
|
|
def extract_images(self): |
|
raise NotImplementedError("Not implemented or CSVLoader does not support image extraction") |
|
return |
|
|
|
def extract_tables(self): |
|
raise NotImplementedError("Not implemented or CSVLoader does not support table extraction") |
|
return |
|
|
|
def extractor(extractor_type: str, *args, **kwargs) -> Extractor: |
|
""" Function factory to return the appropriate PDF extractor instance, properly initialized """ |
|
|
|
if extractor_type == 'PyPDFLoader': |
|
return _PyPDFLoader(*args, **kwargs) |
|
|
|
elif extractor_type == 'LlamaParse': |
|
return _LlamaParse(*args, **kwargs) |
|
|
|
elif extractor_type == 'txt': |
|
return _TXTLoader(*args, **kwargs) |
|
|
|
else: |
|
raise ValueError(f"Unsupported PDF extractor type: {extractor_type}") |
|
|
|
|
|
|
|
|
|
|