这是一个基于allenai/olmocr的FastAPI包装器,提供了REST API接口,用于将PDF和图像文档转换为可读文本。
- 使用Bearer令牌进行API认证
- 支持PDF、PNG、JPG文件处理
- 异步任务处理
- 完整的任务状态跟踪
- 基于FastAPI的现代API设计
- 支持Docker容器化部署
- NVIDIA GPU (至少20GB显存)
- CUDA 12.x
- Ubuntu 20.04或更高版本
- 安装系统依赖:
sudo apt-get update
sudo apt-get install -y poppler-utils ttf-mscorefonts-installer msttcorefonts fonts-crosextra-caladea fonts-crosextra-carlito gsfonts lcdf-typetools- 创建Python虚拟环境:
python -m venv venv
source venv/bin/activate- 安装Python依赖:
pip install -r requirements.txt --find-links https://flashinfer.ai/whl/cu124/torch2.4/flashinfer/- 启动服务:
uvicorn main:app --host 0.0.0.0 --port 8000- 构建Docker镜像:
docker build -t olmocr-api .- 运行容器:
docker run -d --gpus all -p 8000:8000 --name olmocr-api olmocr-api- 确保服务器已安装NVIDIA驱动和CUDA 12.x
- 确保已安装宝塔面板
- 在宝塔面板中创建一个新网站,例如
olmocr.yourdomain.com - 选择纯静态网站,不需要安装任何环境
- 通过SSH连接到服务器
- 安装系统依赖:
sudo apt-get update
sudo apt-get install -y poppler-utils ttf-mscorefonts-installer msttcorefonts fonts-crosextra-caladea fonts-crosextra-carlito gsfonts lcdf-typetools- 在网站根目录创建Python虚拟环境:
cd /www/wwwroot/olmocr.yourdomain.com
python -m venv venv
source venv/bin/activate- 安装Python依赖:
pip install -r requirements.txt --find-links https://flashinfer.ai/whl/cu124/torch2.4/flashinfer/- 在宝塔面板中,选择「网站」>「olmocr.yourdomain.com」>「网站设置」>「反向代理」
- 添加一个新的反向代理,目标URL设置为
http://127.0.0.1:8000
- 在宝塔面板中,选择「软件商店」>「Supervisor管理器」>「安装」
- 安装完成后,添加一个新的守护进程:
- 名称:
olmocr_api - 启动用户:
www - 运行目录:
/www/wwwroot/olmocr.yourdomain.com - 启动命令:
/www/wwwroot/olmocr.yourdomain.com/venv/bin/uvicorn main:app --host 127.0.0.1 --port 8000
- 名称:
- 点击Supervisor中的「启动」按钮启动服务
- 访问
https://olmocr.yourdomain.com/docs查看API文档
curl -X POST "http://localhost:8000/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=secret"响应:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}curl -X POST "http://localhost:8000/ocr/upload" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-F "file=@/path/to/your/document.pdf"响应:
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "queued",
"progress": 0,
"created_at": "2025-05-30T12:34:56.789012"
}curl -X GET "http://localhost:8000/ocr/status/550e8400-e29b-41d4-a716-446655440000" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"响应:
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "processing",
"progress": 0.5,
"created_at": "2025-05-30T12:34:56.789012"
}curl -X GET "http://localhost:8000/ocr/result/550e8400-e29b-41d4-a716-446655440000" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"响应:
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"text": "这是从PDF提取的文本内容...",
"metadata": {
"created_at": "2025-05-30T12:34:56.789012",
"file_path": "/app/olmocr_workdir/550e8400-e29b-41d4-a716-446655440000_document.pdf",
"result_path": "/app/olmocr_workdir/550e8400-e29b-41d4-a716-446655440000/markdown/document.md"
}
}- 在生产环境中,请修改
main.py中的SECRET_KEY为一个安全的随机字符串 - 建议配置HTTPS以保护API通信
- 可以修改
fake_users_db字典,添加更多用户或集成数据库认证
本项目遵循Apache 2.0许可证。olmOCR的原始代码版权归Allen Institute for Artificial Intelligence所有。
欢迎提交问题和拉取请求!