r"""A class representing a toolkit for processing document and return the content of the document. This class provides method for processing docx, pdf, pptx, etc. It cannot process excel files.
| 39 | |
| 40 | |
| 41 | class DocumentProcessingToolkit(BaseToolkit): |
| 42 | r"""A class representing a toolkit for processing document and return the content of the document. |
| 43 | |
| 44 | This class provides method for processing docx, pdf, pptx, etc. It cannot process excel files. |
| 45 | """ |
| 46 | |
| 47 | def __init__( |
| 48 | self, cache_dir: Optional[str] = None, model: Optional[BaseModelBackend] = None |
| 49 | ): |
| 50 | self.image_tool = ImageAnalysisToolkit(model=model) |
| 51 | # self.audio_tool = AudioAnalysisToolkit() |
| 52 | self.excel_tool = ExcelToolkit() |
| 53 | |
| 54 | self.cache_dir = "tmp/" |
| 55 | if cache_dir: |
| 56 | self.cache_dir = cache_dir |
| 57 | |
| 58 | self.uio = UnstructuredIO() |
| 59 | |
| 60 | @retry_on_error() |
| 61 | def extract_document_content(self, document_path: str) -> Tuple[bool, str]: |
| 62 | r"""Extract the content of a given document (or url) and return the processed text. |
| 63 | It may filter out some information, resulting in inaccurate content. |
| 64 | |
| 65 | Args: |
| 66 | document_path (str): The path of the document to be processed, either a local path or a URL. It can process image, audio files, zip files and webpages, etc. |
| 67 | |
| 68 | Returns: |
| 69 | Tuple[bool, str]: A tuple containing a boolean indicating whether the document was processed successfully, and the content of the document (if success). |
| 70 | """ |
| 71 | |
| 72 | logger.debug( |
| 73 | f"Calling extract_document_content function with document_path=`{document_path}`" |
| 74 | ) |
| 75 | |
| 76 | if any(document_path.endswith(ext) for ext in [".jpg", ".jpeg", ".png"]): |
| 77 | res = self.image_tool.ask_question_about_image( |
| 78 | document_path, "Please make a detailed caption about the image." |
| 79 | ) |
| 80 | return True, res |
| 81 | |
| 82 | # if any(document_path.endswith(ext) for ext in ['.mp3', '.wav']): |
| 83 | # res = self.audio_tool.ask_question_about_audio(document_path, "Please transcribe the audio content to text.") |
| 84 | # return True, res |
| 85 | |
| 86 | if any(document_path.endswith(ext) for ext in ["xls", "xlsx"]): |
| 87 | res = self.excel_tool.extract_excel_content(document_path) |
| 88 | return True, res |
| 89 | |
| 90 | if any(document_path.endswith(ext) for ext in ["zip"]): |
| 91 | extracted_files = self._unzip_file(document_path) |
| 92 | return True, f"The extracted files are: {extracted_files}" |
| 93 | |
| 94 | if any(document_path.endswith(ext) for ext in ["json", "jsonl", "jsonld"]): |
| 95 | with open(document_path, "r", encoding="utf-8") as f: |
| 96 | content = json.load(f) |
| 97 | f.close() |
| 98 | return True, content |
no outgoing calls
no test coverage detected