A Flask-based REST API service that uses OpenAI's GPT models to perform Optical Character Recognition (OCR) on Indonesian identity documents (KTP) and educational certificates (Ijazah). The service can process both image files and PDF documents.
- KTP (Indonesian ID Card) Extraction: Extracts personal information from Indonesian identity cards
- Ijazah (Educational Certificate) Extraction: Extracts educational information from Indonesian certificates
- Multi-format Support: Handles both image files (JPEG, PNG) and PDF documents
- AI-Powered OCR: Uses OpenAI's GPT models for intelligent text extraction and data structuring
- RESTful API: Clean REST endpoints for easy integration
- Docker Support: Containerized application for easy deployment
- KTP ID number
- Full name
- Birth date and place
- Gender
- Blood type
- Address details (street, RT/RW, village, district)
- Religion
- Marital status
- Occupation
- Validity period
- Province and city
- Photo background color
- Photo resolution quality
- Certificate number
- Education level (SD, SMP, SMA, SMK, MA, S1, S2, S3)
- School name
- School province and city
- Study program/major
- Graduation year
- Certificate date
For complete API documentation including detailed endpoint specifications, request/response schemas, and examples, please refer to the OpenAPI specification:
The API provides the following main endpoints:
GET /- Home endpointPOST /ocr-ktp- Extract data from Indonesian ID cards (KTP)POST /ocr-ijazah- Extract data from educational certificates (Ijazah)
http://localhost:5000
- Python 3.11+
- OpenAI API key
- Docker (optional)
- Clone the repository
git clone <repository-url>
cd gpt-ocr- Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Set up environment variables
Create a
.envfile in the root directory:
OPENAI_API_KEY=your_openai_api_key_here
DEFAULT_LLM_MODEL=gpt-4-vision-preview
DATABASE_URL=sqlite:///instance/app.db
SECRET_KEY=your_secret_key_here
FLASK_ENV=development- Run the application
python run.pyThe API will be available at http://localhost:5000
- Build the Docker image
docker build -t gpt-ocr .- Run the container
docker run -p 5000:5000 \
-e OPENAI_API_KEY=your_openai_api_key_here \
-e DEFAULT_LLM_MODEL=gpt-4-vision-preview \
gpt-ocr| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY |
OpenAI API key for GPT models | Required |
DEFAULT_LLM_MODEL |
Default LLM model to use | gpt-4-vision-preview |
DATABASE_URL |
Database connection string | sqlite:///instance/app.db |
SECRET_KEY |
Flask secret key | Required |
FLASK_ENV |
Flask environment | development |
gpt-4-vision-preview(recommended for image analysis)gpt-4-turbogpt-3.5-turbo
Extract KTP data:
curl -X POST http://localhost:5000/ocr-ktp \
-H "Content-Type: application/json" \
-d '{"image_url": "https://example.com/ktp.jpg"}'Extract Ijazah data:
curl -X POST http://localhost:5000/ocr-ijazah \
-H "Content-Type: application/json" \
-d '{"image_url": "https://example.com/ijazah.pdf"}'import requests
# KTP extraction
response = requests.post('http://localhost:5000/ocr-ktp',
json={'image_url': 'https://example.com/ktp.jpg'})
ktp_data = response.json()
# Ijazah extraction
response = requests.post('http://localhost:5000/ocr-ijazah',
json={'image_url': 'https://example.com/ijazah.pdf'})
ijazah_data = response.json()📖 For detailed API documentation, request/response schemas, and more examples, see docs/api_specs.yaml
gpt-ocr/
├── app/
│ ├── __init__.py # Flask app initialization
│ ├── constant.py # Application constants
│ ├── routes.py # API route definitions
│ ├── utils.py # Utility functions
│ ├── handler/
│ │ └── llm_handler.py # OpenAI LLM integration
│ ├── models/ # Database models
│ ├── services/ # Business logic services
│ │ ├── home_service.py
│ │ └── ocr_service.py
│ ├── usecases/ # Use case implementations
│ │ └── ocr_use_case.py
│ └── templates/ # HTML templates
├── docs/
│ └── api_specs.yaml # API documentation
├── instance/ # Database files
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration
└── run.py # Application entry point
The API returns appropriate HTTP status codes and error messages:
200: Success400: Bad Request (validation errors, invalid image URL)500: Internal Server Error
For detailed error response schemas and examples, refer to the API documentation.
- Requires valid OpenAI API key with sufficient credits
- Image quality affects extraction accuracy
- Currently optimized for Indonesian documents
- PDF processing converts to images (may lose some quality)
- Rate limits apply based on OpenAI API limits
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.
For issues and questions, please create an issue in the repository or contact the development team.