A production-grade, high-performance microservice designed for extracting text from images using Tesseract OCR. Built with FastAPI and React, this service offers a robust, stateless architecture with strict resource safety and modern observability.
You can run the entire stack (Backend + Frontend) using Docker for an orchestrated experience, or manually for local development.
This method uses Docker Compose to spin up both the backend and frontend in a shared network. The frontend automatically proxies requests to the backend.
# 1. Clone the repository
git clone https://github.com/mo-hossam-stack/fastapi-ocr-microservice.git
cd fastapi-ocr-microservice
# 2. Configure environment
cp .env-example .env
# 3. Start the services
docker compose up --build- Frontend: http://localhost:3000
- Backend (API Docs): http://localhost:8000/docs
Use this method for debugging or making changes to individual components.
# From repository root
cd app
# 1. Create and activate virtual environment
python -m venv venv
source venv/bin/activate # Linux/macOS
# 2. Install dependencies
pip install -r ../requirements.txt
# 3. Setup environment
cp ../.env-example ../.env
# 4. Run the server
uvicorn main:app --reload --port 8000# From repository root
cd frontend
# 1. Install dependencies
npm install
# 2. Start dev server
npm run dev- Frontend: http://localhost:5173 (Proxies
/apito port 8000 via Vite)
- Core: FastAPI (Python 3.8+)
- OCR Engine: Tesseract (via
pytesseract) - Image Processing: Pillow (PIL)
- Validation: Pydantic v2
- Logging: Structured JSON Logging
- Framework: React 18 + TypeScript
- Styling: Tailwind CSS
- API Client: Axios with Interceptors
- Build Tool: Vite
- Containerization: Docker (Multi-stage builds)
- Proxy: Nginx (Production) / Vite Proxy (Development)
- Orchestration: Docker Compose
The microservice follows a stateless architecture designed for horizontal scalability and resource isolation.
graph TD
User([End User]) --> Frontend[React Frontend]
Frontend -- "Nginx / Vite Proxy (/api)" --> Backend[FastAPI Backend]
subgraph "Backend Instance"
Backend -- "Shared Secret Auth" --> Auth[Auth Layer]
Backend -- "Validation" --> Pydantic[Resource Safety Layer]
Backend -- "Offload (Thread Pool)" --> Tesseract[Tesseract Engine]
end
Backend -- "JSON Response" --> Frontend
- Non-Blocking OCR: Tesseract execution is offloaded to a thread pool (
run_in_executor) to keep the FastAPI event loop responsive. - Resource Hardening:
- 10MB Content Limit: Proactive validation via
Content-Lengthand reactive validation during stream reading. - 30s Hard Timeout: Prevents runaway OCR processes from saturating CPU.
- 10MB Content Limit: Proactive validation via
- Stateless Design: No persistent cache or database required for core logic, enabling instant scaling.
- Production Observability: Structured JSON logging and built-in
/health(Liveness) and/ready(Readiness) probes. - Security: Shared secret authentication via Bearer tokens for internal service-to-service communication.
- Architecture Decisions (ADRs): Logic behind the stack and design choices.
- System Design: C4 models and request lifecycles.
The project maintains high test coverage using Pytest.
# Run all tests
pytest
# Run with integration tests (requires real Tesseract)
pytest -m integrationContributions are welcome! Please open issues or pull requests for bug fixes, enhancements, or new features.