feat: 新增 bat/sh 版 API 客户端,修复 bat 吞退出码与 CRLF 问题
新增 scripts/api-client.bat 与 api-client.sh:上传图片 -> 轮询 -> 打印识别文本与 抽取字段。自动挑解释器(优先 .venv,退回系统 Python);api_client.py 只用标准库, 所以一台只装 Python、不装本项目依赖的机器也能当客户端用。 修掉两个 bat 上的真 bug(都是实测出来的,不是看代码看出来的): 1. 退出码被吞。`if exist ... ( ... exit /b %errorlevel% )` 里的 %errorlevel% 是**解析时**展开的,取到的是启动程序之前的值,于是失败也返回 0。 start.bat 同样中招(启动失败会被吞成成功)。两处都改成 goto 结构。 2. .bat 必须 CRLF。同一份内容,CRLF 正常跑完;LF 会让 cmd 解析挂住—— 无任何输出、也不退出,看起来像服务卡死。download-models.bat 原本也是 LF, 一并修正;新增 .gitattributes 钉死 *.bat=eol:crlf、*.sh=eol:lf。 另:参数校验挪到探活之前,参数写错时不会因为连不上服务报出误导性错误; stdout 加 flush,重定向到文件时输出顺序不再错乱。 测试:新增两条回归用例(bat 必须 CRLF;Windows 上 bat 能被 cmd 跑通,带超时 以免坏掉时把测试卡死)。99 passed。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
@echo off
|
||||
rem 命令行调用 OCR 接口:上传图片 -> 轮询 -> 打印识别文本与抽取字段。
|
||||
rem
|
||||
rem 用法(也可以直接把图片拖到这个 bat 上):
|
||||
rem scripts\api-client.bat 图片.png
|
||||
rem scripts\api-client.bat 图片.png --min-confidence 0.6
|
||||
rem scripts\api-client.bat 图片.png --host http://192.168.1.10:8000
|
||||
rem scripts\api-client.bat --health
|
||||
rem
|
||||
rem 服务默认在 http://127.0.0.1:8000。完整参数见 scripts\api_client.py --help
|
||||
rem
|
||||
rem 两个容易踩的点,改动前先看:
|
||||
rem 1. 本文件必须保持 CRLF 换行。实测 LF 换行的 bat 会让 cmd 解析挂住
|
||||
rem (同一份内容 CRLF 正常、LF 无输出且不退出)。.gitattributes 已钉死 eol=crlf。
|
||||
rem 2. 退出码不要写成 `if ... ( ... exit /b %errorlevel% )` 这种括号块:
|
||||
rem 块里的 %errorlevel% 是**解析时**展开的,拿到的是 python 跑之前的值,
|
||||
rem 会把失败吞成成功。所以下面用 goto 而不是括号块。
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
|
||||
rem 优先用 .venv 里的解释器:不经过 uv,也就不会触发环境同步。
|
||||
rem (uv run 会按 pyproject.toml 同步环境,而 paddle 不在依赖里,会被整个移除)
|
||||
if not exist ".venv\Scripts\python.exe" goto try_system_python
|
||||
".venv\Scripts\python.exe" "scripts\api_client.py" %*
|
||||
exit /b %errorlevel%
|
||||
|
||||
:try_system_python
|
||||
rem 退回系统 Python:api_client.py 只用标准库,任何 Python 3.8+ 都能跑,
|
||||
rem 也就是说这台机器只装 Python、不装本项目依赖也能当客户端用。
|
||||
where python >nul 2>nul
|
||||
if errorlevel 1 goto no_python
|
||||
python "scripts\api_client.py" %*
|
||||
exit /b %errorlevel%
|
||||
|
||||
:no_python
|
||||
echo [错误] 找不到 Python 解释器。
|
||||
echo 请先运行 scripts\switch_env.py --cpu 建好 .venv,或安装 Python 3.8+。
|
||||
exit /b 1
|
||||
Reference in New Issue
Block a user