Python worker service for processing slides (OCR + embeddings) in the OratorAI system.
py-analysis-worker/
├── src/ # Source code
│ ├── processors/ # Processing modules
│ │ ├── __init__.py
│ │ ├── ocr_processor.py # OCR text extraction
│ │ └── slide_processor.py # Main slide processing logic
│ ├── clients/ # External service clients
│ │ ├── __init__.py
│ │ ├── aws_client.py # AWS SQS & S3 client
│ │ └── webhook_client.py # Webhook communication
│ ├── handlers/ # Message handlers
│ │ ├── __init__.py
│ │ └── message_handler.py # SQS message processing
│ └── __init__.py
├── config/ # Configuration
│ ├── __init__.py
│ └── config.py # Environment variables & settings
├── scripts/ # Entry point scripts
│ ├── main.py # Main polling loop
│ └── run_worker.py # Alternative entry point
├── docs/ # Documentation
│ └── README.md # Detailed documentation
├── worker.py # Main entry point (run this!)
├── requirements.txt # Python dependencies
└── __init__.py # Package initialization
# Install system dependencies (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install -y tesseract-ocr tesseract-ocr-vie poppler-utils
# Install Python dependencies
pip install -r requirements.txt# Copy environment template
cp .env.example .env
# Edit environment variables
nano .env# Docker GPU worker
docker compose up --build
# Recommended: Use main entry point
python3 worker.py
# Alternative methods:
python3 scripts/main.py
python3 scripts/run_worker.py- Modular Architecture: Clean separation of concerns
- Asynchronous Processing: AWS SQS integration
- Multi-format Support: PDF, images (PNG, JPG, etc.)
- OCR Text Extraction: Vietnamese/English support
- PDF Multi-page: Process each page separately
- Image Enhancement: OpenCV preprocessing for better OCR
- Webhook Notifications: Real-time result reporting
- Error Handling: Robust error handling and logging
ocr_processor.py: Handles OCR text extraction using pytesseract and easyocrslide_processor.py: Main processing logic, file downloads, and orchestration
aws_client.py: AWS SQS and S3 operations wrapperwebhook_client.py: HTTP webhook communication with Node API
message_handler.py: SQS message processing and workflow orchestration
config.py: Environment variables, validation, and library availability checks
main.py: Main polling loop and application entry pointrun_worker.py: Alternative entry point script
- Poll SQS → Receive slide processing jobs
- Download → Get slide file from S3 (authenticated)
- Process → OCR extraction (PDF multi-page support)
- Enhance → Image preprocessing for better OCR
- Webhook → Send results to Node API
- Cleanup → Remove temporary files and SQS message
The modular structure makes it easy to extend:
- New OCR Engine: Add to
src/processors/ocr_processor.py - New File Format: Extend
src/processors/slide_processor.py - New Client: Add to
src/clients/ - New Handler: Add to
src/handlers/
# Test imports
python3 -c "
import sys
sys.path.insert(0, '.')
from src.processors import get_ocr_processor
from src.clients import get_aws_client
from src.handlers import get_message_handler
print('✅ All modules imported successfully')
"
# Test configuration
python3 -c "
import sys
sys.path.insert(0, '.')
from config.config import validate_config
validate_config()
print('✅ Configuration is valid')
"Required in .env file:
# AWS Configuration
AWS_REGION=ap-southeast-1
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_SQS_SLIDES_QUEUE_URL=https://sqs.region.amazonaws.com/account/queue
# Webhook Configuration
WEBHOOK_URL=http://localhost:8080/api/v1/webhooks/slides-complete
WEBHOOK_SECRET=your_webhook_secret
# OCR Configuration (Optional)
OCR_LANGUAGE=vie+eng
OCR_USE_GPU=trueOCR_USE_GPU=true enables EasyOCR GPU mode when CUDA is visible to PyTorch. If CUDA is not available, the worker logs a warning and falls back to CPU.
Install Docker, NVIDIA driver, and NVIDIA Container Toolkit on the target machine. Verify Docker can see the GPU:
docker run --rm --gpus all nvidia/cuda:12.1.1-base-ubuntu22.04 nvidia-smiThen run the worker:
cp .env.example .env
nano .env
docker compose up --build -d
docker compose logs -f slide-workerThe image installs CUDA-enabled PyTorch from https://download.pytorch.org/whl/cu121. If your target machine needs another CUDA wheel index, build with:
docker compose build --build-arg TORCH_INDEX_URL=https://download.pytorch.org/whl/cu124
docker compose up -d- Import Errors: Ensure you're running from the project root directory
- Module Not Found: Check that all
__init__.pyfiles are present - OCR Issues: Install tesseract and language packs
- PDF Processing: Install poppler-utils
- AWS Access: Verify credentials and permissions
Adjust logging level in config/config.py:
logging.basicConfig(level=logging.DEBUG) # For verbose output- Add unit tests for each module
- Implement actual embedding generation
- Add Docker support
- Add metrics and monitoring
- Support for more file formats
- Batch processing capabilities