Views
No views yet
data/ directorypython pdf_to_markdown_processor.py1# Create models directory
2mkdir -p models
3
4# Download using Hugging Face CLI
5pip install huggingface_hub
6huggingface-cli download deepseek-ai/DeepSeek-OCR --local-dir models/deepseek-ai/DeepSeek-OCR
7
8# Or using git
9git clone https://huggingface.co/deepseek-ai/DeepSeek-OCR models/deepseek-ai/DeepSeek-OCR1REM Build the Docker image
2build.bat
3
4REM Start the service
5docker-compose up -d
6
7REM Check logs
8docker-compose logs -f deepseek-ocr1# Build the Docker image
2docker-compose build
3
4# Start the service
5docker-compose up -d
6
7# Check logs
8docker-compose logs -f deepseek-ocr1# Health check
2curl http://localhost:8000/health
3
4# Expected response:
5{
6 "status": "healthy",
7 "model_loaded": true,
8 "model_path": "/app/models/deepseek-ai/DeepSeek-OCR",
9 "cuda_available": true,
10 "cuda_device_count": 1
11}data/ directory for PDF files and convert them to Markdown format with different prompts and post-processing options.document.pdf will create:document-MD.md (markdown processors)document-OCR.md (OCR processor)document-CUSTOM.md (custom prompt processors)'<image>\n<|grounding|>Convert the document to markdown.'-MD.md suffix1# Place PDF files in the data directory
2cp your_document.pdf data/
3
4# Run the processor
5python pdf_to_markdown_processor.py
6
7# Check results
8ls data/*-MD.mddata/images/ folder-MD.md suffix1# Place PDF files in the data directory
2cp your_document.pdf data/
3
4# Run the enhanced processor
5python pdf_to_markdown_processor_enhanced.py
6
7# Check results (including extracted images)
8ls data/*-MD.md
9ls data/images/'<image>\nFree OCR.'-OCR.md suffix1# Place PDF files in the data directory
2cp your_document.pdf data/
3
4# Run the OCR processor
5python pdf_to_ocr_enhanced.py
6
7# Check results
8ls data/*-OCR.mdcustom_prompt.yaml-CUSTOM.md suffixcustom_prompt.yaml to customize the prompt:1# Custom prompt for PDF processing
2prompt: '<image>\n<|grounding|>Convert the document to markdown.'1# Edit the prompt in custom_prompt.yaml
2nano custom_prompt.yaml
3
4# Place PDF files in the data directory
5cp your_document.pdf data/
6
7# Run the custom prompt processor
8python pdf_to_custom_prompt.py
9
10# Check results
11ls data/*-CUSTOM.mdcustom_prompt.yaml-CUSTOM.md suffixpdf_to_custom_prompt.py - edit custom_prompt.yaml to customize the prompt.1# Edit the prompt in custom_prompt.yaml
2nano custom_prompt.yaml
3
4# Place PDF files in the data directory
5cp your_document.pdf data/
6
7# Run the enhanced custom prompt processor
8python pdf_to_custom_prompt_enhanced.py
9
10# Check results (including extracted images)
11ls data/*-CUSTOM.md
12ls data/images/| Processor | Prompt | Post-Processing | Image Extraction | Output Suffix | Use Case |
|---|---|---|---|---|---|
pdf_to_markdown_processor.py | Markdown | ❌ | ❌ | -MD.md | Quick markdown conversion |
pdf_to_markdown_processor_enhanced.py | Markdown | ✅ | ✅ | -MD.md | Full-featured markdown with images |
pdf_to_ocr_enhanced.py | Free OCR | ✅ | ✅ | -OCR.md | Raw text extraction |
pdf_to_custom_prompt.py | Custom (YAML) | ❌ | ❌ | -CUSTOM.md | Testing custom prompts |
pdf_to_custom_prompt_enhanced.py | Custom (YAML) | ✅ | ✅ | -CUSTOM.md | Custom prompts with full features |
1# Place a PDF in the data directory
2cp test_document.pdf data/
3
4# Run all processors
5python pdf_to_markdown_processor.py
6python pdf_to_markdown_processor_enhanced.py
7python pdf_to_ocr_enhanced.py
8python pdf_to_custom_prompt.py
9
10# Compare outputs
11ls data/test_document-*.mdcustom_prompt.yaml with your desired prompt:prompt: '<image>\nExtract all tables and format as CSV.'python pdf_to_custom_prompt_enhanced.pycat data/your_document-CUSTOM.mdGET http://localhost:8000/health1curl -X POST "http://localhost:8000/ocr/image" \
2 -H "accept: application/json" \
3 -H "Content-Type: multipart/form-data" \
4 -F "file=@your_image.jpg"1curl -X POST "http://localhost:8000/ocr/pdf" \
2 -H "accept: application/json" \
3 -H "Content-Type: multipart/form-data" \
4 -F "file=@your_document.pdf"1curl -X POST "http://localhost:8000/ocr/batch" \
2 -H "accept: application/json" \
3 -H "Content-Type: multipart/form-data" \
4 -F "files=@image1.jpg" \
5 -F "files=@document.pdf" \
6 -F "files=@image2.png"1{
2 "success": true,
3 "result": "# Document Title\n\nThis is the OCR result in markdown format...",
4 "page_count": 1
5}1{
2 "success": true,
3 "results": [
4 {
5 "success": true,
6 "result": "# Page 1 Content\n...",
7 "page_count": 1
8 },
9 {
10 "success": true,
11 "result": "# Page 2 Content\n...",
12 "page_count": 2
13 }
14 ],
15 "total_pages": 2,
16 "filename": "document.pdf"
17}1import requests
2
3class DeepSeekOCRClient:
4 def __init__(self, base_url="http://localhost:8000"):
5 self.base_url = base_url
6
7 def process_image(self, image_path):
8 with open(image_path, 'rb') as f:
9 response = requests.post(
10 f"{self.base_url}/ocr/image",
11 files={"file": f}
12 )
13 return response.json()
14
15 def process_pdf(self, pdf_path):
16 with open(pdf_path, 'rb') as f:
17 response = requests.post(
18 f"{self.base_url}/ocr/pdf",
19 files={"file": f}
20 )
21 return response.json()
22
23# Usage
24client = DeepSeekOCRClient()
25result = client.process_pdf("document.pdf")
26
27if result["success"]:
28 for page_result in result["results"]:
29 print(f"Page {page_result['page_count']}:")
30 print(page_result["result"])
31 print("---")1class DeepSeekOCR {
2 constructor(baseUrl = 'http://localhost:8000') {
3 this.baseUrl = baseUrl;
4 }
5
6 async processImage(file) {
7 const formData = new FormData();
8 formData.append('file', file);
9
10 const response = await fetch(`${this.baseUrl}/ocr/image`, {
11 method: 'POST',
12 body: formData
13 });
14
15 return await response.json();
16 }
17
18 async processPDF(file) {
19 const formData = new FormData();
20 formData.append('file', file);
21
22 const response = await fetch(`${this.baseUrl}/ocr/pdf`, {
23 method: 'POST',
24 body: formData
25 });
26
27 return await response.json();
28 }
29}
30
31// Usage in browser
32const ocr = new DeepSeekOCR();
33document.getElementById('fileInput').addEventListener('change', async (e) => {
34 const file = e.target.files[0];
35 const result = await ocr.processPDF(file);
36
37 if (result.success) {
38 result.results.forEach(page => {
39 console.log(`Page ${page.page_count}:`, page.result);
40 });
41 }
42});tokenize_with_images() method is called without the required prompt parameter during model initialization, causing server startup failures.custom_config.py: Custom configuration with customizable default prompt and settingscustom_image_process.py: Fixed version of the image processing module that handles the prompt parameter correctlycustom_run_dpsk_ocr_pdf.py: Enhanced PDF script that accepts --prompt argument and fixes the initialization issuecustom_run_dpsk_ocr_image.py: Enhanced image script that accepts --prompt argument and fixes the initialization issuecustom_run_dpsk_ocr_eval_batch.py: Enhanced batch script that accepts --prompt argument and fixes the initialization issue1# Edit custom_config.py
2PROMPT = '<image>\n<|grounding|>Your custom default prompt here.'1# Using default prompt from custom_config.py
2python custom_run_dpsk_ocr_pdf.py --input your_file.pdf --output output_dir
3
4# Using custom prompt via command line
5python custom_run_dpsk_ocr_pdf.py --prompt "<image>\n<|grounding|>Extract tables as CSV." --input your_file.pdf1# Using default prompt
2curl -X POST "http://localhost:8000/ocr/pdf" -F "file=@your_file.pdf"
3
4# Using custom prompt
5curl -X POST "http://localhost:8000/ocr/pdf" -F "file=@your_file.pdf" -F "prompt=<image>\n<|grounding|>Your custom prompt here."1# Rebuild with custom configuration and fixes
2docker-compose build
3
4# Start the container
5docker-compose up -d1# Copy custom files to replace the originals (transparent replacement approach)
2COPY custom_config.py ./DeepSeek-OCR-vllm/config.py
3COPY custom_image_process.py ./DeepSeek-OCR-vllm/process/image_process.py
4
5# Copy custom run scripts to replace the originals
6COPY custom_run_dpsk_ocr_pdf.py ./DeepSeek-OCR-vllm/run_dpsk_ocr_pdf.py
7COPY custom_run_dpsk_ocr_image.py ./DeepSeek-OCR-vllm/run_dpsk_ocr_image.py
8COPY custom_run_dpsk_ocr_eval_batch.py ./DeepSeek-OCR-vllm/run_dpsk_ocr_eval_batch.pyCUSTOM_CONFIG_README.md.docker-compose.yml to adjust these settings:1environment:
2 - CUDA_VISIBLE_DEVICES=0 # GPU device to use
3 - MODEL_PATH=/app/models/deepseek-ai/DeepSeek-OCR # Model path
4 - MAX_CONCURRENCY=50 # Max concurrent requests
5 - GPU_MEMORY_UTILIZATION=0.85 # GPU memory usage (0.1-1.0)1environment:
2 - MAX_CONCURRENCY=100
3 - GPU_MEMORY_UTILIZATION=0.951environment:
2 - MAX_CONCURRENCY=10
3 - GPU_MEMORY_UTILIZATION=0.71# Reduce concurrency and GPU memory usage
2# Edit docker-compose.yml:
3environment:
4 - MAX_CONCURRENCY=10
5 - GPU_MEMORY_UTILIZATION=0.71# Check model directory structure
2ls -la models/deepseek-ai/DeepSeek-OCR/
3
4# Verify model files are present
5docker-compose exec deepseek-ocr ls -la /app/models/deepseek-ai/DeepSeek-OCR/1# Check GPU availability
2nvidia-smi
3
4# Check Docker GPU support
5docker run --rm --gpus all nvidia/cuda:11.8-base-ubuntu20.04 nvidia-smi1# Check if the API is running
2curl http://localhost:8000/health
3
4# Check container logs
5docker-compose logs -f deepseek-ocr
6
7# Restart the service
8docker-compose restart deepseek-ocr1# Check if PDF files are valid
2file data/your_document.pdf
3
4# Try processing a single PDF manually
5curl -X POST "http://localhost:8000/ocr/pdf" \
6 -H "accept: application/json" \
7 -H "Content-Type: multipart/form-data" \
8 -F "file=@data/your_document.pdf"TypeError: DeepseekOCRProcessor.tokenize_with_images() missing 1 required positional argument: 'prompt'1docker-compose down
2docker-compose build --no-cache
3docker-compose up -d1docker-compose exec deepseek-ocr ls -la /app/DeepSeek-OCR-vllm/run_dpsk_ocr_*.py
2# These should show recent timestamps from the buildtokenize_with_images() method is called with the correct prompt parameter during model initialization.1# Run with shell access
2docker-compose run --rm deepseek-ocr bash
3
4# Check model loading
5python -c "
6import sys
7sys.path.insert(0, '/app/DeepSeek-OCR-master/DeepSeek-OCR-vllm')
8from config import MODEL_PATH
9print(f'Model path: {MODEL_PATH}')
10print(f'Model exists: {os.path.exists(MODEL_PATH)}')
11"/ocr/batch endpointGPU_MEMORY_UTILIZATION based on your GPU capacityMAX_CONCURRENCY for better throughput on powerful GPUsDeepSeek-OCR/
├── README.md # This file
├── CUSTOM_CONFIG_README.md # Custom configuration documentation
├── pdf_to_markdown_processor.py # Basic markdown conversion
├── pdf_to_markdown_processor_enhanced.py # Enhanced markdown with post-processing
├── pdf_to_ocr_enhanced.py # OCR text extraction
├── pdf_to_custom_prompt.py # Custom prompt processing (raw)
├── pdf_to_custom_prompt_enhanced.py # Custom prompt with post-processing
├── custom_prompt.yaml # Configuration for custom prompts
├── custom_config.py # Custom configuration (replaces original config.py)
├── custom_image_process.py # Fixed image processing (replaces original)
├── custom_run_dpsk_ocr_pdf.py # Custom PDF script with prompt support (replaces original)
├── custom_run_dpsk_ocr_image.py # Custom image script with prompt support (replaces original)
├── custom_run_dpsk_ocr_eval_batch.py # Custom batch script with prompt support (replaces original)
├── test_custom_config.py # Test script for custom configuration
├── start_server.py # FastAPI server
├── Dockerfile # Docker container definition (includes custom files)
├── docker-compose.yml # Docker compose configuration
├── build.bat # Windows build script
├── data/ # Input/output directory for PDFs
│ ├── images/ # Extracted images (when using enhanced processors)
│ └── *.md # Generated markdown files
├── models/ # Model weights directory
└── DeepSeek-OCR/ # DeepSeek-OCR source code
└── DeepSeek-OCR-master/
└── DeepSeek-OCR-vllm/ # Original library files (replaced during build)1graph TD
2 A[Start] --> B{Choose Method}
3
4 B -->|Batch Processing| C[Place PDFs in data/ folder]
5 B -->|API Usage| D[Start Docker Container]
6
7 C --> E[Run python pdf_to_markdown_processor.py]
8 D --> F[Use API endpoints]
9
10 E --> G[Check data/ folder for .md files]
11 F --> H[Process results from API response]
12
13 G --> I[Done]
14 H --> I
15
16 style A fill:#e1f5fe
17 style I fill:#e8f5e8
18 style C fill:#fff3e0
19 style D fill:#fff3e0
20 style E fill:#f3e5f5
21 style F fill:#f3e5f5