Spaces:
Runtime error
Runtime error
Download ocr/doc2x.py from Chaos0818/LLMDataGenerator: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/Chaos0818/LLMDataGenerator/resolve/main/ocr/doc2x.py
- Command line
-
hf download hf://spaces/Chaos0818/LLMDataGenerator/ocr/doc2x.py
-
curl -L -o doc2x.py https://huggingface.co/spaces/Chaos0818/LLMDataGenerator/resolve/main/ocr/doc2x.py
2.1 kB
| import os | |
| from pdfdeal import Doc2X | |
| from pdfdeal.file_tools import unzips | |
| class PDFProcessor: | |
| def __init__(self, apikey): | |
| """ | |
| Initialize PDFProcessor with API key | |
| """ | |
| self.client = self._init_ocr(apikey) | |
| def _init_ocr(self, apikey): | |
| """ | |
| Initialize Doc2X client | |
| """ | |
| return Doc2X(apikey=apikey, debug=True, full_speed=True) | |
| def pdf2md(self, pdf_file, output_path): | |
| """ | |
| Convert PDF to Markdown using Doc2X API. | |
| # """ | |
| # if isinstance(pdf_file, list): | |
| # output_names = [f"{ts}_{i}" for i in range(len(pdf_file))] | |
| # else: | |
| # output_names = [ts] | |
| output_names = [] | |
| for file in pdf_file: | |
| filename, file_ext = os.path.splitext(os.path.basename(file)) | |
| output_names.append(filename) | |
| success, failed, flag = self.client.pdf2file( | |
| pdf_file=pdf_file, | |
| output_path=output_path, | |
| output_format="md", | |
| output_names=output_names | |
| ) | |
| print(success) | |
| print(failed) | |
| print(flag) | |
| return success, failed, flag | |
| def unzip_markdown_files(self, success): | |
| """ | |
| Unzip Markdown files generated by Doc2X API. | |
| """ | |
| zips = [file for file in success if file.endswith(".zip")] | |
| success, failed, flag = unzips(zip_paths=zips) | |
| print(success) | |
| print(failed) | |
| print(flag) | |
| return success, failed, flag | |
| def process_pdf_files(self, pdf_files, output_path): | |
| """ | |
| Process PDF files and unzip resulting markdown files | |
| """ | |
| success, failed, flag = self.pdf2md(pdf_files, output_path) | |
| if success: | |
| return self.unzip_markdown_files(success) | |
| return None, failed, flag | |
| if __name__ == "__main__": | |
| apikey = "sk-q183d0h5lbz38k3ex46m6pfxbao9p3h1" | |
| processor = PDFProcessor(apikey) | |
| # Example usage | |
| processor.process_pdf_files( | |
| pdf_files=["samples/111.pdf", "samples/222.pdf"], | |
| output_path="./output" | |
| ) | |