Image-Text-to-Text
Transformers
PEFT
sft
trl
qlora
kyc
document-extraction
document-classification
aadhaar
pan-card
passport
visa
election-card
gemma4
vision-language-model
vllm
Instructions to use Jwalit/gemma4-e4b-kyc-document-extractor with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Jwalit/gemma4-e4b-kyc-document-extractor with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Jwalit/gemma4-e4b-kyc-document-extractor")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Jwalit/gemma4-e4b-kyc-document-extractor", device_map="auto") - PEFT
How to use Jwalit/gemma4-e4b-kyc-document-extractor with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Jwalit/gemma4-e4b-kyc-document-extractor with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Jwalit/gemma4-e4b-kyc-document-extractor" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Jwalit/gemma4-e4b-kyc-document-extractor", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Jwalit/gemma4-e4b-kyc-document-extractor
- SGLang
How to use Jwalit/gemma4-e4b-kyc-document-extractor with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Jwalit/gemma4-e4b-kyc-document-extractor" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Jwalit/gemma4-e4b-kyc-document-extractor", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Jwalit/gemma4-e4b-kyc-document-extractor" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Jwalit/gemma4-e4b-kyc-document-extractor", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Jwalit/gemma4-e4b-kyc-document-extractor with Docker Model Runner:
docker model run hf.co/Jwalit/gemma4-e4b-kyc-document-extractor
Add model README with full documentation
Browse files
README.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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c
|
| 10 |
+
- document-extraction
|
| 11 |
+
- document-classification
|
| 12 |
+
- aadhaar
|
| 13 |
+
- pan-card
|
| 14 |
+
- passport
|
| 15 |
+
- visa
|
| 16 |
+
- election-card
|
| 17 |
+
- gemma4
|
| 18 |
+
- vision-language-model
|
| 19 |
+
- vllm
|
| 20 |
+
datasets:
|
| 21 |
+
- Jwalit/kyc-document-extraction-vlm
|
| 22 |
+
pipeline_tag: image-text-to-text
|
| 23 |
+
library_name: transformers
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
# Gemma 4 E4B β KYC Document Extractor & Classifier
|
| 27 |
+
|
| 28 |
+
**Production-ready Vision-Language Model for Indian KYC Document Extraction and Classification**
|
| 29 |
+
|
| 30 |
+
Fine-tuned from [`google/gemma-4-E4B-it`](https://huggingface.co/google/gemma-4-E4B-it) using QLoRA SFT on a synthetic KYC document dataset covering 5 Indian identity document types.
|
| 31 |
+
|
| 32 |
+
## π― Capabilities
|
| 33 |
+
|
| 34 |
+
| Task | Description |
|
| 35 |
+
|------|-------------|
|
| 36 |
+
| **Document Classification** | Classify document as: Aadhaar Card, PAN Card, Passport, Visa, or Election Card (Voter ID) |
|
| 37 |
+
| **Field Extraction** | Extract all structured fields (name, DOB, ID number, address, etc.) as JSON |
|
| 38 |
+
| **Combined** | Classify + Extract in a single pass |
|
| 39 |
+
|
| 40 |
+
## π Supported Document Types
|
| 41 |
+
|
| 42 |
+
| Document | Fields Extracted |
|
| 43 |
+
|----------|-----------------|
|
| 44 |
+
| **Aadhaar Card** | full_name, date_of_birth, gender, father_name, aadhaar_number, address, VID |
|
| 45 |
+
| **PAN Card** | full_name, father_name, date_of_birth, pan_number |
|
| 46 |
+
| **Passport** | surname, given_name, nationality, gender, date_of_birth, passport_number, place_of_birth, date_of_issue, date_of_expiry, place_of_issue |
|
| 47 |
+
| **Visa** | issuing_country, visa_type, visa_category, visa_number, full_name, nationality, gender, date_of_birth, passport_number, date_of_issue, date_of_expiry, entries |
|
| 48 |
+
| **Election Card** | voter_id, full_name, relative_name, gender, date_of_birth, age, state, constituency, address |
|
| 49 |
+
|
| 50 |
+
## π Quick Start
|
| 51 |
+
|
| 52 |
+
### With Transformers
|
| 53 |
+
|
| 54 |
+
```python
|
| 55 |
+
import torch
|
| 56 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 57 |
+
from PIL import Image
|
| 58 |
+
|
| 59 |
+
model_id = "Jwalit/gemma4-e4b-kyc-document-extractor"
|
| 60 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 61 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 62 |
+
model_id, device_map="auto", torch_dtype=torch.bfloat16
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
image = Image.open("document.jpg").convert("RGB")
|
| 66 |
+
|
| 67 |
+
messages = [
|
| 68 |
+
{"role": "system", "content": [{"type": "text", "text": "You are an expert KYC document analyst. Always respond with accurate, structured JSON output."}]},
|
| 69 |
+
{"role": "user", "content": [
|
| 70 |
+
{"type": "image"},
|
| 71 |
+
{"type": "text", "text": "Classify this document and extract all information as structured JSON."}
|
| 72 |
+
]}
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
inputs = processor.apply_chat_template(
|
| 76 |
+
messages, add_generation_prompt=True, tokenize=True,
|
| 77 |
+
return_dict=True, return_tensors="pt", images=[image]
|
| 78 |
+
).to(model.device)
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
output = model.generate(**inputs, max_new_tokens=1024, temperature=0.1)
|
| 82 |
+
|
| 83 |
+
result = processor.batch_decode(output[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True)[0]
|
| 84 |
+
print(result)
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
### With vLLM (Production Deployment)
|
| 88 |
+
|
| 89 |
+
```bash
|
| 90 |
+
# Start OpenAI-compatible server
|
| 91 |
+
python -m vllm.entrypoints.openai.api_server \
|
| 92 |
+
--model Jwalit/gemma4-e4b-kyc-document-extractor \
|
| 93 |
+
--trust-remote-code \
|
| 94 |
+
--max-model-len 4096 \
|
| 95 |
+
--dtype bfloat16 \
|
| 96 |
+
--gpu-memory-utilization 0.9
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
from openai import OpenAI
|
| 101 |
+
import base64
|
| 102 |
+
|
| 103 |
+
client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy")
|
| 104 |
+
|
| 105 |
+
with open("document.jpg", "rb") as f:
|
| 106 |
+
img_b64 = base64.b64encode(f.read()).decode()
|
| 107 |
+
|
| 108 |
+
response = client.chat.completions.create(
|
| 109 |
+
model="Jwalit/gemma4-e4b-kyc-document-extractor",
|
| 110 |
+
messages=[
|
| 111 |
+
{"role": "system", "content": "You are an expert KYC document analyst. Always respond with accurate, structured JSON output."},
|
| 112 |
+
{"role": "user", "content": [
|
| 113 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}},
|
| 114 |
+
{"type": "text", "text": "Classify and extract all fields from this KYC document as JSON."}
|
| 115 |
+
]}
|
| 116 |
+
],
|
| 117 |
+
max_tokens=1024,
|
| 118 |
+
temperature=0.1
|
| 119 |
+
)
|
| 120 |
+
print(response.choices[0].message.content)
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
### With vLLM Offline (Batch Processing)
|
| 124 |
+
|
| 125 |
+
```python
|
| 126 |
+
from vllm import LLM, SamplingParams
|
| 127 |
+
|
| 128 |
+
llm = LLM(
|
| 129 |
+
model="Jwalit/gemma4-e4b-kyc-document-extractor",
|
| 130 |
+
trust_remote_code=True,
|
| 131 |
+
max_model_len=4096,
|
| 132 |
+
dtype="bfloat16",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
sampling_params = SamplingParams(temperature=0.1, max_tokens=1024)
|
| 136 |
+
# Use llm.chat() with image messages for batch processing
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
## ποΈ Training Details
|
| 140 |
+
|
| 141 |
+
### Method
|
| 142 |
+
- **Base Model**: `google/gemma-4-E4B-it` (~8B params, Gemma4ForConditionalGeneration)
|
| 143 |
+
- **Fine-tuning**: QLoRA SFT (4-bit NF4 quantization + LoRA rank-16 on text decoder)
|
| 144 |
+
- **Vision Encoder**: Frozen SigLIP (280 tokens per image, 768-dim, 16 layers)
|
| 145 |
+
- **Framework**: TRL SFTTrainer + PEFT + BitsAndBytes
|
| 146 |
+
|
| 147 |
+
### Hyperparameters
|
| 148 |
+
| Parameter | Value |
|
| 149 |
+
|-----------|-------|
|
| 150 |
+
| Learning Rate | 2e-4 |
|
| 151 |
+
| Epochs | 3 |
|
| 152 |
+
| Batch Size | 2 Γ 8 (gradient accumulation) = 16 effective |
|
| 153 |
+
| LoRA Rank (r) | 16 |
|
| 154 |
+
| LoRA Alpha | 32 |
|
| 155 |
+
| LoRA Dropout | 0.05 |
|
| 156 |
+
| Optimizer | AdamW (fused) |
|
| 157 |
+
| LR Scheduler | Cosine with 5% warmup |
|
| 158 |
+
| Precision | bf16 |
|
| 159 |
+
| Gradient Checkpointing | β
|
|
| 160 |
+
| Target Modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
|
| 161 |
+
|
| 162 |
+
### Dataset
|
| 163 |
+
- **Dataset**: [`Jwalit/kyc-document-extraction-vlm`](https://huggingface.co/datasets/Jwalit/kyc-document-extraction-vlm)
|
| 164 |
+
- **Size**: 2,704 train / 296 eval samples
|
| 165 |
+
- **Document Types**: 5 (Aadhaar, PAN, Passport, Visa, Election Card)
|
| 166 |
+
- **Task Types**: Classification, Extraction, Combined (balanced across all)
|
| 167 |
+
- **Format**: Conversational VLM (messages with `{"type": "image"}` + `{"type": "text"}`)
|
| 168 |
+
|
| 169 |
+
### Architecture
|
| 170 |
+
|
| 171 |
+
```
|
| 172 |
+
Gemma4ForConditionalGeneration
|
| 173 |
+
βββ Vision Encoder (SigLIP, FROZEN)
|
| 174 |
+
β βββ 16 layers, 768-dim, 12 attention heads
|
| 175 |
+
β βββ Patch size: 16, Pooling kernel: 3
|
| 176 |
+
β βββ Output: 280 soft tokens per image
|
| 177 |
+
βββ Text Decoder (LoRA applied here)
|
| 178 |
+
β βββ 42 layers (36 sliding + 6 full attention)
|
| 179 |
+
β βββ 2560 hidden, 8 heads, GQA
|
| 180 |
+
β βββ 262K vocab, 131K context
|
| 181 |
+
β βββ LoRA on: q/k/v/o_proj + gate/up/down_proj
|
| 182 |
+
βββ Audio Encoder (unused, frozen)
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
## π§ Reproduce Training
|
| 186 |
+
|
| 187 |
+
```bash
|
| 188 |
+
# Install dependencies
|
| 189 |
+
pip install torch transformers trl datasets peft accelerate bitsandbytes trackio flash-attn pillow
|
| 190 |
+
|
| 191 |
+
# Run training (requires GPU with β₯24GB VRAM, recommended: A100 80GB)
|
| 192 |
+
python train_kyc_vlm.py
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
Or via TRL CLI:
|
| 196 |
+
```bash
|
| 197 |
+
trl sft \
|
| 198 |
+
--model_name_or_path google/gemma-4-E4B-it \
|
| 199 |
+
--dataset_name Jwalit/kyc-document-extraction-vlm \
|
| 200 |
+
--output_dir ./gemma4-kyc-extractor \
|
| 201 |
+
--learning_rate 2e-4 \
|
| 202 |
+
--num_train_epochs 3 \
|
| 203 |
+
--per_device_train_batch_size 2 \
|
| 204 |
+
--gradient_accumulation_steps 8 \
|
| 205 |
+
--bf16 \
|
| 206 |
+
--gradient_checkpointing \
|
| 207 |
+
--push_to_hub \
|
| 208 |
+
--hub_model_id Jwalit/gemma4-e4b-kyc-document-extractor
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
## β‘ Performance & Deployment Notes
|
| 212 |
+
|
| 213 |
+
- **vLLM compatible**: Native support via `Gemma4ForConditionalGeneration` architecture
|
| 214 |
+
- **280 image tokens**: Efficient β processes document images in ~280 tokens (vs 1024+ for other VLMs)
|
| 215 |
+
- **128K context**: Can handle multiple document pages in a single request
|
| 216 |
+
- **QLoRA deployment**: Merge adapters for full-speed inference, or serve with PEFT for memory efficiency
|
| 217 |
+
|
| 218 |
+
### Merging Adapters (for production β recommended before vLLM serving)
|
| 219 |
+
|
| 220 |
+
```python
|
| 221 |
+
from peft import AutoPeftModelForCausalLM
|
| 222 |
+
import torch
|
| 223 |
+
|
| 224 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 225 |
+
"Jwalit/gemma4-e4b-kyc-document-extractor",
|
| 226 |
+
device_map="auto",
|
| 227 |
+
torch_dtype=torch.bfloat16,
|
| 228 |
+
)
|
| 229 |
+
merged_model = model.merge_and_unload()
|
| 230 |
+
merged_model.save_pretrained("./merged-kyc-extractor")
|
| 231 |
+
# Then push merged model for faster vLLM serving
|
| 232 |
+
```
|
| 233 |
+
|
| 234 |
+
## π Expected Output Format
|
| 235 |
+
|
| 236 |
+
```json
|
| 237 |
+
{
|
| 238 |
+
"document_type": "aadhaar_card",
|
| 239 |
+
"full_name": "Rajesh Kumar Singh",
|
| 240 |
+
"date_of_birth": "15/03/1985",
|
| 241 |
+
"gender": "Male",
|
| 242 |
+
"father_name": "Suresh Kumar Singh",
|
| 243 |
+
"aadhaar_number": "1234 5678 9012",
|
| 244 |
+
"address": "123, MG Road, Mumbai, Maharashtra - 400001",
|
| 245 |
+
"vid": "1234 5678 9012 3456"
|
| 246 |
+
}
|
| 247 |
+
```
|
| 248 |
+
|
| 249 |
+
## β οΈ Limitations
|
| 250 |
+
|
| 251 |
+
- Trained on **synthetic** KYC documents β accuracy on real-world documents will improve with fine-tuning on real (anonymized) KYC samples
|
| 252 |
+
- Best results when further fine-tuned with 200-500 real document images per type
|
| 253 |
+
- Vision encoder is frozen β cannot learn new visual features beyond base SigLIP capabilities
|
| 254 |
+
- Indian documents only (Aadhaar, PAN, Passport, Visa, Election Card)
|
| 255 |
+
|
| 256 |
+
## π License
|
| 257 |
+
|
| 258 |
+
Apache 2.0 (same as base model `google/gemma-4-E4B-it`)
|