Metadata-Version: 2.5
Name: ds-ocr
Version: 0.1.0
Summary: 用 DeepSeek 视觉模型把本地图片和 PDF 识别成 Markdown
License-Expression: MIT
License-File: LICENSE
Keywords: deepseek,markdown,ocr,pdf
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Requires-Dist: loguru>=0.7
Requires-Dist: openai>=1.40
Requires-Dist: pdf-inspector>=1.25
Requires-Dist: pillow>=10
Requires-Dist: pypdfium2>=4
Description-Content-Type: text/markdown

# ds-ocr

用 DeepSeek 视觉模型识别本地图片或 PDF，返回 Markdown。

文字型 PDF 由 [pdf-inspector](https://pypi.org/project/pdf-inspector/) 直接抽取；扫描页和图片交给 DeepSeek。超过入模像素上限的图会先切开，同一张图的切片放在同一次请求里。多张图、多页扫描并行识别，结果按原顺序拼接。

## 安装

```bash
pip install ds-ocr
```

## 调用时配置

创建一个 `OCR` 时传入密钥、接口地址和模型名，之后多次识别不用再传。包不会读取环境变量或 `.env`，也没有内置地址。

可选环境变量只控制本地行为：

| 变量 | 说明 |
| --- | --- |
| `LLM_ENABLED` | `false` 时不请求模型。图片返回空字符串，PDF 只保留文字页 |
| `LLM_TIMEOUT_S` | 单次请求超时秒数，默认 `180` |
| `PDF_OCR_MAX_PAGES` | 含待识别页的 PDF 最大页数，默认 `50`。超过则抛 `PdfPageLimitError`，不上传 |

## 作为库使用

```python
from ds_ocr import OCR, PdfPageLimitError

ocr = OCR(
    api_key="你的密钥",
    base_url="https://api.example.com",
    model="你的模型名",
)
text = ocr.recognize("scan.pdf")
text = ocr.recognize(["a.png", "b.pdf"])

flags: dict[str, bool] = {}
try:
    text = ocr.recognize("mixed.pdf", flags=flags)
except PdfPageLimitError:
    raise
print(flags.get("from_ocr"))  # 本轮是否调用了视觉模型
```

`recognize` 只返回正文，不附加说明性前缀。

## 命令行

```bash
ds-ocr scan.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL"
ds-ocr a.png b.pdf --api-key "$API_KEY" --base-url "$BASE_URL" --model "$MODEL" -o out.md
```

页数超过上限时退出码为 2。

## 支持的文件

png、jpg、gif、webp、bmp、tif，以及 PDF。类型按文件头判断，不依赖扩展名是否写对。
