An OCR (Optical Character Recognition) microservice built with Python and FastAPI. This service accepts PDF files and images, extracts text using Tesseract OCR, and returns high-quality OCR results. It is designed to be modular, scalable, and easily integrated with applications like Arana Assistant.
The OCR Microservice is designed to:
- Accept PDF documents and images via an API.
- Convert PDFs to images if necessary.
- Detect the language of the document.
- Perform OCR using the appropriate language model.
- Return extracted text for further processing.
The microservice consists of the following components:
- API Layer (FastAPI): Handles HTTP requests and responses.
- PDF to Image Conversion: Converts PDF files to images for OCR processing.
- Language Detection: Determines the document's language to select the appropriate Tesseract language model.
- OCR Processing (LlamaParse): Extracts text from images.
- API Layer: Utilizes FastAPI for handling file uploads and routing requests to the appropriate services.
- PDF to Image Conversion: Uses
pdf2imageorPyMuPDFto convert PDF pages into images. - Language Detection: Implements
langdetector Tesseract's language detection on the first page to determine the document's language. - OCR Processing: Uses
llamaparse
- Python 3.10+
- Docker (Optional but recommended)
- Tesseract OCR Engine
- Poppler Utils (if using
pdf2imagefor PDF conversion)
git clone https://github.com/yourusername/ocr-microservice.git
cd ocr-microservicepython -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`pip install -r requirements.txtsudo apt-get update
sudo apt-get install -y tesseract-ocr libtesseract-dev poppler-utilsbrew install tesseract poppleruvicorn app.main:app --reloaddocker build -t ocr-microservice .
docker run -p 80:80 ocr-microserviceThis project includes automatically generated API documentation accessible through two interactive interfaces:
- Swagger UI: Provides an interactive UI to test endpoints and view the API schema.
- ReDoc: Offers a clean, detailed view of the API documentation.
Once the FastAPI server is running, you can access the documentation at the following endpoints:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Replace localhost with your server's IP address if you're accessing it remotely.
- File Type and Size Validation: Ensures only PDF and image files are accepted and that they are within acceptable size limits.
- Input Sanitization: All inputs are sanitized to prevent injection attacks.
- API Key Authentication: Implemented using FastAPI's security utilities. Clients must provide a valid API key to access the endpoints.
- Rate Limiting: Limits the number of requests per client to prevent abuse.
- Secure File Handling: Files are processed in memory or stored in secure temporary directories that are cleaned after processing.
- Error Handling: Robust exception handling is in place to prevent sensitive information leakage.
- HTTPS Enforcement: It is recommended to run the service behind a secure gateway or proxy like Nginx with SSL certificates.