Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

增加重构函数位置

[email protected]02d805ea9bf8
diff --git a/magic-pdf.template.json b/magic-pdf.template.json
new file mode 100644
index 00000000..031944e6
--- /dev/null
+++ b/magic-pdf.template.json
@@ -0,0 +1,7 @@
+{
+ "bucket_info":{
+ "bucket-name-1":["ak", "sk", "endpoint"],
+ "bucket-name-2":["ak", "sk", "endpoint"]
+ },
+ "temp-output-dir":"/tmp"
+}
\ No newline at end of file
diff --git a/magic_pdf/cli/magicpdf.py b/magic_pdf/cli/magicpdf.py
new file mode 100644
index 00000000..2b1e598e
--- /dev/null
+++ b/magic_pdf/cli/magicpdf.py
@@ -0,0 +1,48 @@
+"""
+这里实现2个click命令:
+第一个:
+ 接收一个完整的s3路径,例如:s3://llm-pdf-text/pdf_ebook_and_paper/pre-clean-mm-markdown/v014/part-660420b490be-000008.jsonl?bytes=0,81350
+ 1)根据~/magic-pdf.json里的ak,sk等,构造s3cliReader读取到这个jsonl的对应行,返回json对象。
+ 2)根据Json对象里的pdf的s3路径获取到他的ak,sk,endpoint,构造出s3cliReader用来读取pdf
+ 3)从magic-pdf.json里读取到本地保存图片、Md等的临时目录位置,构造出LocalImageWriter,用来保存截图
+ 4)从magic-pdf.json里读取到本地保存图片、Md等的临时目录位置,构造出LocalIRdWriter,用来读写本地文件
+
+ 最后把以上步骤准备好的对象传入真正的解析API
+
+第二个:
+ 接收1)pdf的本地路径。2)模型json文件(可选)。然后:
+ 1)根据~/magic-pdf.json读取到本地保存图片、md等临时目录的位置,构造出LocalImageWriter,用来保存截图
+ 2)从magic-pdf.json里读取到本地保存图片、Md等的临时目录位置,构造出LocalIRdWriter,用来读写本地文件
+ 3)根据约定,根据pdf本地路径,推导出pdf模型的json,并读入
+
+
+效果:
+python magicpdf.py --json s3://llm-pdf-text/scihub/xxxx.json?bytes=0,81350
+python magicpdf.py --pdf /home/llm/Downloads/xxxx.pdf --model /home/llm/Downloads/xxxx.json 或者 python magicpdf.py --pdf /home/llm/Downloads/xxxx.pdf
+"""
+
+
+
+
+import click
+
+def cli():
+ pass
+
[email protected]('--json', type=str, help='输入一个S3路径')
+def json_command(json):
+ # 这里处理json相关的逻辑
+ print(f'处理JSON: {json}')
+
[email protected]('--pdf', type=click.Path(exists=True), required=True, help='PDF文件的路径')
[email protected]('--model', type=click.Path(exists=True), help='模型的路径')
+def pdf_command(pdf, model):
+ # 这里处理pdf和模型相关的逻辑
+ print(f'处理PDF: {pdf}')
+ print(f'加载模型: {model}')
+
+if __name__ == '__main__':
+ cli()
diff --git a/magic_pdf/io/AbsReaderWriter.py b/magic_pdf/io/AbsReaderWriter.py
new file mode 100644
index 00000000..bca4371b
--- /dev/null
+++ b/magic_pdf/io/AbsReaderWriter.py
@@ -0,0 +1,20 @@
+
+from abc import ABC, abstractmethod
+
+
+class AbsReaderWriter(ABC):
+ """
+ 同时支持二进制和文本读写的抽象类
+ TODO
+ """
+ @abstractmethod
+ def read(self, path: str):
+ pass
+
+ @abstractmethod
+ def write(self, path: str, content: str):
+ pass
+
+
+
+
\ No newline at end of file
diff --git a/magic_pdf/io/DiskReaderWriter.py b/magic_pdf/io/DiskReaderWriter.py
new file mode 100644
index 00000000..e69de29b
diff --git a/magic_pdf/io/S3ReaderWriter.py b/magic_pdf/io/S3ReaderWriter.py
new file mode 100644
index 00000000..af684175
--- /dev/null
+++ b/magic_pdf/io/S3ReaderWriter.py
@@ -0,0 +1,18 @@
+
+
+from magic_pdf.io import AbsReaderWriter
+
+
+class DiskReaderWriter(AbsReaderWriter):
+ def __init__(self, parent_path, encoding='utf-8'):
+ self.path = parent_path
+ self.encoding = encoding
+
+ def read(self):
+ with open(self.path, 'rb') as f:
+ return f.read()
+
+ def write(self, data):
+ with open(self.path, 'wb') as f:
+ f.write(data)
+
\ No newline at end of file
diff --git a/magic_pdf/io/__init__.py b/magic_pdf/io/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/magic_pdf/libs/config_reader.py b/magic_pdf/libs/config_reader.py
new file mode 100644
index 00000000..f9498a4e
--- /dev/null
+++ b/magic_pdf/libs/config_reader.py
@@ -0,0 +1,14 @@
+
+
+"""
+根据bucket的名字返回对应的s3 AK, SK,endpoint三元组
+
+"""
+
+def get_s3_config(bucket_name: str):
+ """
+ ~/magic-pdf.json 读出来
+ """
+ ak , sk, endpoint = "", "", ""
+ # TODO 请实现这个函数
+ return ak, sk, endpoint
diff --git a/magic_pdf/spark/s3.py b/magic_pdf/spark/s3.py
index b5d1686f..48b3ce0b 100644
--- a/magic_pdf/spark/s3.py
+++ b/magic_pdf/spark/s3.py
@@ -6,7 +6,7 @@ import re
import random
from typing import List, Union
try:
- from app.config import s3_buckets, s3_clusters, s3_users
+ from app.config import s3_buckets, s3_clusters, s3_users # TODO delete 循环依赖
from app.common.runtime import get_cluster_name
except ImportError:
from magic_pdf.config import s3_buckets, s3_clusters, get_cluster_name, s3_users
diff --git a/magic_pdf/spark/spark_api.py b/magic_pdf/spark/spark_api.py
new file mode 100644
index 00000000..71e88288
--- /dev/null
+++ b/magic_pdf/spark/spark_api.py
@@ -0,0 +1,38 @@
+
+"""
+用户输入:
+ model数组,每个元素代表一个页面
+ pdf在s3的路径
+ 截图保存的s3位置
+
+然后:
+ 1)根据s3路径,调用spark集群的api,拿到ak,sk,endpoint,构造出s3PDFReader
+ 2)根据用户输入的s3地址,调用spark集群的api,拿到ak,sk,endpoint,构造出s3ImageWriter
+
+其余部分至于构造s3cli, 获取ak,sk都在code-clean里写代码完成。不要反向依赖!!!
+
+"""
+
+
+from magic_pdf.io import AbsReaderWriter
+
+
+def parse_txt_pdf(pdf_bytes:bytes, pdf_models:list, imageWriter: AbsReaderWriter, is_debug=False, start_page=0, *args, **kwargs):
+ """
+ 解析文本类pdf
+ """
+ pass
+
+
+def parse_ocr_pdf(pdf_bytes:bytes, pdf_models:list, imageWriter: AbsReaderWriter, is_debug=False, start_page=0, *args, **kwargs):
+ """
+ 解析ocr类pdf
+ """
+ pass
+
+
+def parse_union_pdf(pdf_bytes:bytes, pdf_models:list, imageWriter: AbsReaderWriter, is_debug=False, start_page=0, *args, **kwargs):
+ """
+ ocr和文本混合的pdf,全部解析出来
+ """
+ pass
\ No newline at end of file