Skip to main content
HB
MLOps & Model Serving
Freelance client

Handwritten Document OCR Platform

FastAPI · Google Vertex AI · Cloud Run · GCS · Firestore

Hasan Iqbal Butt
By Hasan Iqbal ButtPublished: February 2025
99.4%Character-Level Accuracy After Preprocessing
01

The Challenge

Old handwritten manuscripts present significant legibility challenges due to ink fading, paper degradation, and non standard writing styles. Standard optical character recognition engines failed to transcribe these images accurately. We needed a complete cloud infrastructure to handle document uploads via Cloud Storage, track processing status through Cloud Firestore, store results, and return structured API responses. We initially planned to use Gemini 3.1 Pro through OpenRouter, but quickly encountered severe API rate limits on our new developer account during batch processing runs. We also needed to comply with strict General Data Protection Regulation guidelines since the manuscripts contained historical records with personal names and locations. Sending high resolution images to multi modal models consumed a large number of input tokens, which threatened to make the project financially unviable.

02

The Approach

We built a complete serverless pipeline on Google Cloud Platform. The FastAPI application runs on Cloud Run with Cloud Load Balancing handling SSL termination and health checks. When a client submits a document, the image is uploaded to a GCS input bucket in the europe-west region. The application creates a job record in Cloud Firestore with an initial status of pending, then begins processing. The document is binarized, margins are removed, and the content is sliced into smaller tiles to optimize token consumption. Each tile is sent to Gemini 2.5 Flash on Vertex AI in the Europe West region. If Vertex AI returns a rate limit error, the router falls back to Gemini 3.1 Pro via OpenRouter. As processing progresses, the Firestore job status updates from processing to complete or failed. The final transcribed text is written as a .txt file to the GCS output bucket, organized by date and job identifier. Cloud Monitoring collects custom metrics for API latency, token usage, and error rates.

03

System Architecture

Loading architecture diagram...
04

Cloud Infrastructure

GCP Infrastructure

Architecture for the Handwritten Document OCR Platform. Click a node to view its configuration details.

HTTPSUploadOCRStatusWriteClientHTTPS RequestLoad BalancerCloud HTTPS LBCloud RunFastAPI ContainerGCS InputDocument ImagesVertex AIGemini 2.5 FlashFirestoreStatus TrackingGCS OutputTranscription .txtCloud MonitoringMetrics & Alerts
Select a node to view infrastructure details
Primary data flow
Observability
05

Interactive Demo

Walk through the system design and data flow for this project step by step.

How the Pipeline Works

Handwritten Manuscript

Step 1Document Input

The system receives a scanned image of a handwritten historical document. These manuscripts often suffer from ink fading, paper degradation, water damage, and non-standard cursive styles. Traditional OCR engines cannot handle these conditions because they rely on clean, printed character shapes.

Key Insight

Supported formats include high resolution TIFF and JPEG scans. The API accepts single pages or batched document sets through the FastAPI endpoint.

1 of 6

06

Overview

This platform transcribes historical handwritten manuscripts using vision language models. It uses Python FastAPI for the API layer and combines Gemini 3.1 Pro via OpenRouter and Gemini 2.5 Flash via Google Vertex AI. Document images are uploaded to a Google Cloud Storage input bucket, processed through the OCR pipeline, and the transcribed text is stored as .txt files in a separate GCS output bucket. Cloud Firestore maintains real-time status tracking for each processing job with states like pending, processing, complete, and failed.

07

Business Impact

The production implementation successfully processed over one million manuscript pages. By switching from Gemini 3.1 Pro to Gemini 2.5 Flash and implementing token optimization, API costs were reduced by eighty five percent while retaining high transcription accuracy. Running on Cloud Run serverless infrastructure allowed the system to scale down to zero when idle, keeping operation costs minimal while maintaining prompt responsiveness.

08

Technical Highlights

  • Multi model fallback routing between OpenRouter and Vertex AI
  • GDPR compliant data processing in the Europe West region
  • Token optimization through image tiling and prompt structuring
  • FastAPI backend with asynchronous request handling
  • GCS input and output buckets for document and transcription storage
  • Firestore real-time job status tracking with state management
  • Serverless deployment on Cloud Run with 2 vCPU and 2GB memory limits
  • GCP Cloud Monitoring dashboard for latency and error tracking

Want results like this for your infrastructure?

I specialize in taking complex AI pipelines and cloud setups from concept to high-availability production. Let's discuss how to optimize your workloads, secure your environment, and reduce cloud costs.