ds-ocr 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ds_ocr-0.1.0/.gitignore +7 -0
- ds_ocr-0.1.0/LICENSE +21 -0
- ds_ocr-0.1.0/PKG-INFO +76 -0
- ds_ocr-0.1.0/README.md +59 -0
- ds_ocr-0.1.0/pyproject.toml +30 -0
- ds_ocr-0.1.0/src/ds_ocr/__init__.py +12 -0
- ds_ocr-0.1.0/src/ds_ocr/__main__.py +4 -0
- ds_ocr-0.1.0/src/ds_ocr/cli.py +42 -0
- ds_ocr-0.1.0/src/ds_ocr/config.py +49 -0
- ds_ocr-0.1.0/src/ds_ocr/ocr.py +955 -0
- ds_ocr-0.1.0/src/ds_ocr/sniff.py +25 -0
- ds_ocr-0.1.0/tests/test_package.py +63 -0
- ds_ocr-0.1.0/uv.lock +1031 -0
ds_ocr-0.1.0/.gitignore
ADDED
ds_ocr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ds-ocr contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
ds_ocr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ds-ocr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 用 DeepSeek 视觉模型把本地图片和 PDF 识别成 Markdown
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: deepseek,markdown,ocr,pdf
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Requires-Dist: loguru>=0.7
|
|
12
|
+
Requires-Dist: openai>=1.40
|
|
13
|
+
Requires-Dist: pdf-inspector>=1.25
|
|
14
|
+
Requires-Dist: pillow>=10
|
|
15
|
+
Requires-Dist: pypdfium2>=4
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# ds-ocr
|
|
19
|
+
|
|
20
|
+
用 DeepSeek 视觉模型识别本地图片或 PDF,返回 Markdown。
|
|
21
|
+
|
|
22
|
+
文字型 PDF 由 [pdf-inspector](https://pypi.org/project/pdf-inspector/) 直接抽取;扫描页和图片交给 DeepSeek。超过入模像素上限的图会先切开,同一张图的切片放在同一次请求里。多张图、多页扫描并行识别,结果按原顺序拼接。
|
|
23
|
+
|
|
24
|
+
## 安装
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install ds-ocr
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 调用时配置
|
|
31
|
+
|
|
32
|
+
创建一个 `OCR` 时传入密钥、接口地址和模型名,之后多次识别不用再传。包不会读取环境变量或 `.env`,也没有内置地址。
|
|
33
|
+
|
|
34
|
+
可选环境变量只控制本地行为:
|
|
35
|
+
|
|
36
|
+
| 变量 | 说明 |
|
|
37
|
+
| --- | --- |
|
|
38
|
+
| `LLM_ENABLED` | `false` 时不请求模型。图片返回空字符串,PDF 只保留文字页 |
|
|
39
|
+
| `LLM_TIMEOUT_S` | 单次请求超时秒数,默认 `180` |
|
|
40
|
+
| `PDF_OCR_MAX_PAGES` | 含待识别页的 PDF 最大页数,默认 `50`。超过则抛 `PdfPageLimitError`,不上传 |
|
|
41
|
+
|
|
42
|
+
## 作为库使用
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from ds_ocr import OCR, PdfPageLimitError
|
|
46
|
+
|
|
47
|
+
ocr = OCR(
|
|
48
|
+
api_key="你的密钥",
|
|
49
|
+
base_url="https://api.example.com",
|
|
50
|
+
model="你的模型名",
|
|
51
|
+
)
|
|
52
|
+
text = ocr.recognize("scan.pdf")
|
|
53
|
+
text = ocr.recognize(["a.png", "b.pdf"])
|
|
54
|
+
|
|
55
|
+
flags: dict[str, bool] = {}
|
|
56
|
+
try:
|
|
57
|
+
text = ocr.recognize("mixed.pdf", flags=flags)
|
|
58
|
+
except PdfPageLimitError:
|
|
59
|
+
raise
|
|
60
|
+
print(flags.get("from_ocr")) # 本轮是否调用了视觉模型
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`recognize` 只返回正文,不附加说明性前缀。
|
|
64
|
+
|
|
65
|
+
## 命令行
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
ds-ocr scan.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL"
|
|
69
|
+
ds-ocr a.png b.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL" -o out.md
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
页数超过上限时退出码为 2。
|
|
73
|
+
|
|
74
|
+
## 支持的文件
|
|
75
|
+
|
|
76
|
+
png、jpg、gif、webp、bmp、tif,以及 PDF。类型按文件头判断,不依赖扩展名是否写对。
|
ds_ocr-0.1.0/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# ds-ocr
|
|
2
|
+
|
|
3
|
+
用 DeepSeek 视觉模型识别本地图片或 PDF,返回 Markdown。
|
|
4
|
+
|
|
5
|
+
文字型 PDF 由 [pdf-inspector](https://pypi.org/project/pdf-inspector/) 直接抽取;扫描页和图片交给 DeepSeek。超过入模像素上限的图会先切开,同一张图的切片放在同一次请求里。多张图、多页扫描并行识别,结果按原顺序拼接。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install ds-ocr
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 调用时配置
|
|
14
|
+
|
|
15
|
+
创建一个 `OCR` 时传入密钥、接口地址和模型名,之后多次识别不用再传。包不会读取环境变量或 `.env`,也没有内置地址。
|
|
16
|
+
|
|
17
|
+
可选环境变量只控制本地行为:
|
|
18
|
+
|
|
19
|
+
| 变量 | 说明 |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `LLM_ENABLED` | `false` 时不请求模型。图片返回空字符串,PDF 只保留文字页 |
|
|
22
|
+
| `LLM_TIMEOUT_S` | 单次请求超时秒数,默认 `180` |
|
|
23
|
+
| `PDF_OCR_MAX_PAGES` | 含待识别页的 PDF 最大页数,默认 `50`。超过则抛 `PdfPageLimitError`,不上传 |
|
|
24
|
+
|
|
25
|
+
## 作为库使用
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from ds_ocr import OCR, PdfPageLimitError
|
|
29
|
+
|
|
30
|
+
ocr = OCR(
|
|
31
|
+
api_key="你的密钥",
|
|
32
|
+
base_url="https://api.example.com",
|
|
33
|
+
model="你的模型名",
|
|
34
|
+
)
|
|
35
|
+
text = ocr.recognize("scan.pdf")
|
|
36
|
+
text = ocr.recognize(["a.png", "b.pdf"])
|
|
37
|
+
|
|
38
|
+
flags: dict[str, bool] = {}
|
|
39
|
+
try:
|
|
40
|
+
text = ocr.recognize("mixed.pdf", flags=flags)
|
|
41
|
+
except PdfPageLimitError:
|
|
42
|
+
raise
|
|
43
|
+
print(flags.get("from_ocr")) # 本轮是否调用了视觉模型
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`recognize` 只返回正文,不附加说明性前缀。
|
|
47
|
+
|
|
48
|
+
## 命令行
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
ds-ocr scan.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL"
|
|
52
|
+
ds-ocr a.png b.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL" -o out.md
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
页数超过上限时退出码为 2。
|
|
56
|
+
|
|
57
|
+
## 支持的文件
|
|
58
|
+
|
|
59
|
+
png、jpg、gif、webp、bmp、tif,以及 PDF。类型按文件头判断,不依赖扩展名是否写对。
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "ds-ocr"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "用 DeepSeek 视觉模型把本地图片和 PDF 识别成 Markdown"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
keywords = ["ocr", "deepseek", "pdf", "markdown"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
]
|
|
14
|
+
dependencies = [
|
|
15
|
+
"openai>=1.40",
|
|
16
|
+
"pillow>=10",
|
|
17
|
+
"pypdfium2>=4",
|
|
18
|
+
"pdf-inspector>=1.25",
|
|
19
|
+
"loguru>=0.7",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
ds-ocr = "ds_ocr.cli:main"
|
|
24
|
+
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
dev = ["pytest>=8"]
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["hatchling"]
|
|
30
|
+
build-backend = "hatchling.build"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""用 DeepSeek 视觉模型把本地图片和 PDF 识别成 Markdown。"""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
from ds_ocr.ocr import OCR, PdfPageLimitError
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
__version__ = version("ds-ocr")
|
|
9
|
+
except PackageNotFoundError:
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
__all__ = ["OCR", "PdfPageLimitError", "__version__"]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""命令行:把图片或 PDF 识别成 Markdown。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from ds_ocr.ocr import OCR, PdfPageLimitError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main(argv: list[str] | None = None) -> None:
|
|
13
|
+
parser = argparse.ArgumentParser(
|
|
14
|
+
prog="ds-ocr",
|
|
15
|
+
description="用 DeepSeek 识别本地图片或 PDF,输出 Markdown。",
|
|
16
|
+
)
|
|
17
|
+
parser.add_argument("paths", nargs="+", help="图片或 PDF 路径,按顺序识别")
|
|
18
|
+
parser.add_argument("--api-key", required=True, help="调用时传入的 API 密钥")
|
|
19
|
+
parser.add_argument("--base-url", required=True, help="调用时传入的接口地址")
|
|
20
|
+
parser.add_argument("--model", required=True, help="调用时传入的模型名")
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"-o",
|
|
23
|
+
"--output",
|
|
24
|
+
help="写入该文件;省略则打印到标准输出",
|
|
25
|
+
)
|
|
26
|
+
args = parser.parse_args(argv)
|
|
27
|
+
try:
|
|
28
|
+
ocr = OCR(api_key=args.api_key, base_url=args.base_url, model=args.model)
|
|
29
|
+
text = ocr.recognize(args.paths)
|
|
30
|
+
except PdfPageLimitError as exc:
|
|
31
|
+
print(exc, file=sys.stderr)
|
|
32
|
+
raise SystemExit(2) from exc
|
|
33
|
+
if args.output:
|
|
34
|
+
Path(args.output).write_text(text, encoding="utf-8")
|
|
35
|
+
return
|
|
36
|
+
sys.stdout.write(text)
|
|
37
|
+
if text and not text.endswith("\n"):
|
|
38
|
+
sys.stdout.write("\n")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
main()
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""运行时可调参数,均来自环境变量。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
_TRUE = {"1", "true", "yes", "on"}
|
|
8
|
+
_FALSE = {"0", "false", "no", "off"}
|
|
9
|
+
|
|
10
|
+
_DEFAULT_TIMEOUT_S = 180.0
|
|
11
|
+
_DEFAULT_PDF_OCR_MAX_PAGES = 50
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def llm_enabled() -> bool:
|
|
15
|
+
"""是否调用 DeepSeek。未设置或无法识别时为开启。"""
|
|
16
|
+
raw = os.getenv("LLM_ENABLED", "").strip().lower()
|
|
17
|
+
if not raw or raw in _TRUE:
|
|
18
|
+
return True
|
|
19
|
+
if raw in _FALSE:
|
|
20
|
+
return False
|
|
21
|
+
return True
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def llm_timeout_s() -> float:
|
|
25
|
+
"""单次 API 请求超时(秒)。"""
|
|
26
|
+
raw = os.getenv("LLM_TIMEOUT_S", "").strip()
|
|
27
|
+
if not raw:
|
|
28
|
+
return _DEFAULT_TIMEOUT_S
|
|
29
|
+
try:
|
|
30
|
+
value = float(raw)
|
|
31
|
+
except ValueError:
|
|
32
|
+
return _DEFAULT_TIMEOUT_S
|
|
33
|
+
if value <= 0:
|
|
34
|
+
return _DEFAULT_TIMEOUT_S
|
|
35
|
+
return value
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def pdf_ocr_max_pages() -> int:
|
|
39
|
+
"""含待识别页的 PDF 允许的最大页数。"""
|
|
40
|
+
raw = os.getenv("PDF_OCR_MAX_PAGES", "").strip()
|
|
41
|
+
if not raw:
|
|
42
|
+
return _DEFAULT_PDF_OCR_MAX_PAGES
|
|
43
|
+
try:
|
|
44
|
+
value = int(raw)
|
|
45
|
+
except ValueError:
|
|
46
|
+
return _DEFAULT_PDF_OCR_MAX_PAGES
|
|
47
|
+
if value < 1:
|
|
48
|
+
return _DEFAULT_PDF_OCR_MAX_PAGES
|
|
49
|
+
return value
|