Chandra OCR部署教程Docker Compose编排vLLMStreamlitNGINX三容器服务1. 开篇为什么选择Chandra OCR如果你正在处理大量的扫描文档、合同文件、数学试卷或者各种表单需要把它们转换成结构化的数字格式那么Chandra OCR可能就是你要找的解决方案。Chandra是Datalab.to在2025年10月开源的布局感知OCR模型它不仅能识别文字还能保留原始的排版信息。无论是表格、数学公式、手写文字甚至是表单中的复选框它都能准确识别并转换成Markdown、HTML或JSON格式。最让人惊喜的是这个模型在olmOCR基准测试中拿到了83.1的综合分数表现超过了GPT-4o和Gemini Flash 2。而且它只需要4GB显存就能运行对硬件要求相当友好。2. 环境准备与快速部署2.1 系统要求在开始之前请确保你的系统满足以下要求操作系统LinuxUbuntu 20.04推荐macOS或Windows WSL2Docker版本20.10Docker Compose版本2.0显卡NVIDIA显卡至少4GB显存RTX 3060及以上推荐驱动NVIDIA驱动最新版本CUDA 11.82.2 一键部署步骤让我们开始部署完整的Chandra OCR服务栈。首先创建项目目录并准备配置文件# 创建项目目录 mkdir chandra-ocr-deployment cd chandra-ocr-deployment # 创建docker-compose.yml文件 touch docker-compose.yml touch nginx.conf3. Docker Compose编排配置3.1 编写docker-compose.yml将以下内容复制到docker-compose.yml文件中version: 3.8 services: # vLLM推理后端 vllm-backend: image: datalabto/chandra-vllm:latest deploy: resources: reservations: devices: - driver: nvidia count: 1 capabilities: [gpu] environment: - MODEL_NAMEdatalabto/chandra-1.0 - GPU_MEMORY_UTILIZATION0.8 - MAX_MODEL_LEN8192 ports: - 8000:8000 volumes: - ./models:/app/models restart: unless-stopped networks: - chandra-network # Streamlit前端界面 streamlit-frontend: image: datalabto/chandra-streamlit:latest ports: - 8501:8501 environment: - VLLM_ENDPOINThttp://vllm-backend:8000 depends_on: - vllm-backend restart: unless-stopped networks: - chandra-network # NGINX反向代理 nginx-proxy: image: nginx:alpine ports: - 80:80 - 443:443 volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./ssl:/etc/nginx/ssl depends_on: - streamlit-frontend - vllm-backend restart: unless-stopped networks: - chandra-network networks: chandra-network: driver: bridge volumes: models: processed-data:3.2 配置NGINX反向代理创建nginx.conf配置文件events { worker_connections 1024; } http { upstream vllm { server vllm-backend:8000; } upstream streamlit { server streamlit-frontend:8501; } server { listen 80; server_name localhost; # Streamlit前端代理 location / { proxy_pass http://streamlit; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # vLLM API代理 location /v1/ { proxy_pass http://vllm/v1/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 增加超时时间 proxy_read_timeout 300s; proxy_connect_timeout 300s; proxy_send_timeout 300s; } # 健康检查端点 location /health { proxy_pass http://streamlit/health; access_log off; } } }4. 启动与验证服务4.1 启动所有服务现在一切准备就绪让我们启动完整的服务栈# 启动所有服务 docker-compose up -d # 查看服务状态 docker-compose ps # 查看日志如果需要调试 docker-compose logs -f4.2 验证服务运行状态等待几分钟让服务完全启动然后进行验证# 检查vLLM后端是否正常 curl http://localhost:80/v1/models # 检查Streamlit前端是否正常 curl -I http://localhost:80 # 查看容器日志确认无错误 docker-compose logs --tail50如果一切正常你现在可以通过浏览器访问http://localhost来使用Chandra OCR的Web界面了。5. 使用Chandra OCR服务5.1 通过Web界面使用打开浏览器访问http://localhost你会看到Streamlit提供的友好界面上传文件点击上传按钮选择图片或PDF文件选择输出格式Markdown、HTML或JSON开始处理点击处理按钮等待结果查看结果右侧会显示识别结果和保留的排版信息5.2 通过API接口使用你也可以直接通过API使用OCR服务import requests import base64 import json def ocr_with_chandra(image_path, output_formatmarkdown): # 读取图片并编码 with open(image_path, rb) as image_file: encoded_image base64.b64encode(image_file.read()).decode(utf-8) # 准备请求数据 payload { image: encoded_image, format: output_format, language: auto } # 发送请求到NGINX代理 response requests.post( http://localhost:80/v1/ocr, jsonpayload, timeout300 ) if response.status_code 200: return response.json() else: raise Exception(fOCR处理失败: {response.text}) # 使用示例 result ocr_with_chandra(document.png, markdown) print(result[output])5.3 批量处理文件对于需要处理大量文件的情况可以使用批量处理脚本import os import concurrent.futures from pathlib import Path def process_directory(input_dir, output_dir, formatmarkdown): input_path Path(input_dir) output_path Path(output_dir) output_path.mkdir(exist_okTrue) # 获取所有支持的图像文件 image_extensions [.png, .jpg, .jpeg, .tiff, .bmp, .pdf] image_files [] for ext in image_extensions: image_files.extend(input_path.glob(f*{ext})) image_files.extend(input_path.glob(f*{ext.upper()})) # 使用线程池并行处理 with concurrent.futures.ThreadPoolExecutor(max_workers4) as executor: futures [] for image_file in image_files: output_file output_path / f{image_file.stem}.{format} futures.append( executor.submit(process_single_file, image_file, output_file, format) ) # 等待所有任务完成 for future in concurrent.futures.as_completed(futures): try: future.result() except Exception as e: print(f处理失败: {e}) def process_single_file(input_file, output_file, format): result ocr_with_chandra(str(input_file), format) with open(output_file, w, encodingutf-8) as f: f.write(result[output]) print(f已处理: {input_file.name}) # 使用示例 process_directory(./input, ./output, markdown)6. 常见问题与解决方法6.1 显卡内存不足如果遇到显存不足的问题可以调整vLLM的内存使用率# 在docker-compose.yml中修改 environment: - GPU_MEMORY_UTILIZATION0.6 # 降低到60% - MAX_MODEL_LEN4096 # 减少最大序列长度6.2 处理速度优化对于需要更快处理速度的场景environment: - TP_SIZE1 # 张量并行数多GPU时可增加 - MAX_PARALLEL_LOADING_WORKERS4 # 增加并行加载工作线程6.3 服务监控与维护添加健康检查和服务监控# 在docker-compose.yml中添加健康检查 healthcheck: test: [CMD, curl, -f, http://localhost:8000/v1/models] interval: 30s timeout: 10s retries: 3 start_period: 40s7. 总结通过本教程你已经成功部署了一个完整的Chandra OCR服务栈包含vLLM后端提供高性能的OCR推理服务Streamlit前端提供友好的Web交互界面NGINX代理统一入口和负载均衡这个部署方案的优势在于开箱即用一条命令即可启动所有服务资源高效合理利用GPU资源4GB显存即可运行易于扩展支持多GPU并行处理轻松应对批量任务生产就绪包含健康检查、监控和故障恢复机制现在你可以开始使用这个强大的OCR工具来处理各种文档了。无论是扫描的合同、数学公式、表格数据还是手写文字Chandra都能帮你准确识别并保留原有的排版信息。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
Chandra OCR部署教程:Docker Compose编排vLLM+Streamlit+NGINX三容器服务
发布时间:2026/5/29 2:34:53
Chandra OCR部署教程Docker Compose编排vLLMStreamlitNGINX三容器服务1. 开篇为什么选择Chandra OCR如果你正在处理大量的扫描文档、合同文件、数学试卷或者各种表单需要把它们转换成结构化的数字格式那么Chandra OCR可能就是你要找的解决方案。Chandra是Datalab.to在2025年10月开源的布局感知OCR模型它不仅能识别文字还能保留原始的排版信息。无论是表格、数学公式、手写文字甚至是表单中的复选框它都能准确识别并转换成Markdown、HTML或JSON格式。最让人惊喜的是这个模型在olmOCR基准测试中拿到了83.1的综合分数表现超过了GPT-4o和Gemini Flash 2。而且它只需要4GB显存就能运行对硬件要求相当友好。2. 环境准备与快速部署2.1 系统要求在开始之前请确保你的系统满足以下要求操作系统LinuxUbuntu 20.04推荐macOS或Windows WSL2Docker版本20.10Docker Compose版本2.0显卡NVIDIA显卡至少4GB显存RTX 3060及以上推荐驱动NVIDIA驱动最新版本CUDA 11.82.2 一键部署步骤让我们开始部署完整的Chandra OCR服务栈。首先创建项目目录并准备配置文件# 创建项目目录 mkdir chandra-ocr-deployment cd chandra-ocr-deployment # 创建docker-compose.yml文件 touch docker-compose.yml touch nginx.conf3. Docker Compose编排配置3.1 编写docker-compose.yml将以下内容复制到docker-compose.yml文件中version: 3.8 services: # vLLM推理后端 vllm-backend: image: datalabto/chandra-vllm:latest deploy: resources: reservations: devices: - driver: nvidia count: 1 capabilities: [gpu] environment: - MODEL_NAMEdatalabto/chandra-1.0 - GPU_MEMORY_UTILIZATION0.8 - MAX_MODEL_LEN8192 ports: - 8000:8000 volumes: - ./models:/app/models restart: unless-stopped networks: - chandra-network # Streamlit前端界面 streamlit-frontend: image: datalabto/chandra-streamlit:latest ports: - 8501:8501 environment: - VLLM_ENDPOINThttp://vllm-backend:8000 depends_on: - vllm-backend restart: unless-stopped networks: - chandra-network # NGINX反向代理 nginx-proxy: image: nginx:alpine ports: - 80:80 - 443:443 volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./ssl:/etc/nginx/ssl depends_on: - streamlit-frontend - vllm-backend restart: unless-stopped networks: - chandra-network networks: chandra-network: driver: bridge volumes: models: processed-data:3.2 配置NGINX反向代理创建nginx.conf配置文件events { worker_connections 1024; } http { upstream vllm { server vllm-backend:8000; } upstream streamlit { server streamlit-frontend:8501; } server { listen 80; server_name localhost; # Streamlit前端代理 location / { proxy_pass http://streamlit; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # vLLM API代理 location /v1/ { proxy_pass http://vllm/v1/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 增加超时时间 proxy_read_timeout 300s; proxy_connect_timeout 300s; proxy_send_timeout 300s; } # 健康检查端点 location /health { proxy_pass http://streamlit/health; access_log off; } } }4. 启动与验证服务4.1 启动所有服务现在一切准备就绪让我们启动完整的服务栈# 启动所有服务 docker-compose up -d # 查看服务状态 docker-compose ps # 查看日志如果需要调试 docker-compose logs -f4.2 验证服务运行状态等待几分钟让服务完全启动然后进行验证# 检查vLLM后端是否正常 curl http://localhost:80/v1/models # 检查Streamlit前端是否正常 curl -I http://localhost:80 # 查看容器日志确认无错误 docker-compose logs --tail50如果一切正常你现在可以通过浏览器访问http://localhost来使用Chandra OCR的Web界面了。5. 使用Chandra OCR服务5.1 通过Web界面使用打开浏览器访问http://localhost你会看到Streamlit提供的友好界面上传文件点击上传按钮选择图片或PDF文件选择输出格式Markdown、HTML或JSON开始处理点击处理按钮等待结果查看结果右侧会显示识别结果和保留的排版信息5.2 通过API接口使用你也可以直接通过API使用OCR服务import requests import base64 import json def ocr_with_chandra(image_path, output_formatmarkdown): # 读取图片并编码 with open(image_path, rb) as image_file: encoded_image base64.b64encode(image_file.read()).decode(utf-8) # 准备请求数据 payload { image: encoded_image, format: output_format, language: auto } # 发送请求到NGINX代理 response requests.post( http://localhost:80/v1/ocr, jsonpayload, timeout300 ) if response.status_code 200: return response.json() else: raise Exception(fOCR处理失败: {response.text}) # 使用示例 result ocr_with_chandra(document.png, markdown) print(result[output])5.3 批量处理文件对于需要处理大量文件的情况可以使用批量处理脚本import os import concurrent.futures from pathlib import Path def process_directory(input_dir, output_dir, formatmarkdown): input_path Path(input_dir) output_path Path(output_dir) output_path.mkdir(exist_okTrue) # 获取所有支持的图像文件 image_extensions [.png, .jpg, .jpeg, .tiff, .bmp, .pdf] image_files [] for ext in image_extensions: image_files.extend(input_path.glob(f*{ext})) image_files.extend(input_path.glob(f*{ext.upper()})) # 使用线程池并行处理 with concurrent.futures.ThreadPoolExecutor(max_workers4) as executor: futures [] for image_file in image_files: output_file output_path / f{image_file.stem}.{format} futures.append( executor.submit(process_single_file, image_file, output_file, format) ) # 等待所有任务完成 for future in concurrent.futures.as_completed(futures): try: future.result() except Exception as e: print(f处理失败: {e}) def process_single_file(input_file, output_file, format): result ocr_with_chandra(str(input_file), format) with open(output_file, w, encodingutf-8) as f: f.write(result[output]) print(f已处理: {input_file.name}) # 使用示例 process_directory(./input, ./output, markdown)6. 常见问题与解决方法6.1 显卡内存不足如果遇到显存不足的问题可以调整vLLM的内存使用率# 在docker-compose.yml中修改 environment: - GPU_MEMORY_UTILIZATION0.6 # 降低到60% - MAX_MODEL_LEN4096 # 减少最大序列长度6.2 处理速度优化对于需要更快处理速度的场景environment: - TP_SIZE1 # 张量并行数多GPU时可增加 - MAX_PARALLEL_LOADING_WORKERS4 # 增加并行加载工作线程6.3 服务监控与维护添加健康检查和服务监控# 在docker-compose.yml中添加健康检查 healthcheck: test: [CMD, curl, -f, http://localhost:8000/v1/models] interval: 30s timeout: 10s retries: 3 start_period: 40s7. 总结通过本教程你已经成功部署了一个完整的Chandra OCR服务栈包含vLLM后端提供高性能的OCR推理服务Streamlit前端提供友好的Web交互界面NGINX代理统一入口和负载均衡这个部署方案的优势在于开箱即用一条命令即可启动所有服务资源高效合理利用GPU资源4GB显存即可运行易于扩展支持多GPU并行处理轻松应对批量任务生产就绪包含健康检查、监控和故障恢复机制现在你可以开始使用这个强大的OCR工具来处理各种文档了。无论是扫描的合同、数学公式、表格数据还是手写文字Chandra都能帮你准确识别并保留原有的排版信息。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。