Custom OCR library based on Transformer architecture.
Project description
SocrateX 🚀
SocrateX is a modular, easy-to-use PyTorch framework for training and deploying custom OCR (Optical Character Recognition) Transformer models.
It allows you to build, train, and run inference on OCR models entirely from scratch, or load pre-trained architectures (like the 159M-parameter Socrate model) seamlessly.
Installation
pip install socratex
Quick Start — Training a Custom OCR Model
With SocrateX, you don't need pre-trained weights to get started. You can define your own architecture, generate synthetic training data on the fly, and start training in just a few lines of code.
import SocrateX as sx
import torch
from torch.utils.data import DataLoader
# 1. Initialize a fresh Tokenizer
tokenizer = sx.init_tokenizer()
# 2. Define your architecture (e.g., ~15M parameters)
config = sx.Config(
d_model=256,
nhead=4,
num_layers=4,
dim_feedforward=1024,
pool_height=4
)
# 3. Initialize the model
model = sx.init(config=config, tokenizer=tokenizer, device="cuda")
# 4. Generate some quick synthetic data for training!
sx.generate_silly_training_set(
source="https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-no-swears.txt",
count=1000,
output_dir="my_dataset"
)
# 5. Load the dataset
images, labels = sx.load_dataset("my_dataset/labels.csv")
dataset = sx.Makeset(images, labels, tokenizer=tokenizer)
sampler = sx.SmartBatchSampler(labels, batch_size=16)
loader = DataLoader(dataset, batch_sampler=sampler)
# 6. Train the model
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
criterion = torch.nn.CrossEntropyLoss(ignore_index=tokenizer.token_to_id("<pad>"))
trainer = sx.Trainer(model, loader, optimizer, criterion, device="cuda")
for epoch in range(30):
loss = trainer.train_epoch()
print(f"Epoch {epoch} | Loss: {loss:.4f}")
# 7. Run Inference
results = model.predict(["my_dataset/word_0.jpg"], function="generate", max_iter=32)
print("Predicted:", results)
Pre-defined Architectures
SocrateX includes factory presets for different model scales:
sx.cat(tokenizer)— Large model (159M params, d_model=640, 12 layers)sx.rat(tokenizer)— Medium model (~80M params, d_model=512, 8 layers)sx.mice(tokenizer)— Small model (~40M params, d_model=384, 6 layers)
Hugging Face Integration
SocrateX plays perfectly with the Hugging Face Hub. Our flagship pre-trained model is hosted on Hugging Face and embeds the SocrateX API directly!
from transformers import AutoModel
import torch
# Load the heavy 159M model (weights are downloaded automatically)
model = AutoModel.from_pretrained("ihatebaselines/Socrate", trust_remote_code=True)
# You can use the entire SocrateX API directly on the HuggingFace model object!
results = model.predict(["receipt.jpg"], function="beam_search", beam_width=4)
print(results)
Available Modules
| Module | What it does |
|---|---|
sx.Config(...) |
Define custom Transformer OCR architectures |
sx.init(config, tokenizer) |
Build model from scratch |
sx.load_tokenizer(path) |
Load BPE tokenizer from file |
sx.init_tokenizer() |
Build a fresh tokenizer |
sx.load_dataset(path) |
Parse CSV/JSON/TXT datasets |
sx.Makeset(images, labels, ...) |
Create a PyTorch Dataset |
sx.SmartBatchSampler(...) |
Optimize padding by sorting batches by length |
sx.Trainer(...) |
Wrapper for clean training loops |
sx.predict(...) |
Greedy / Fast / Beam Search inference |
sx.generate_silly_training_set() |
Generate synthetic text image datasets |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file socratex-1.3.1.tar.gz.
File metadata
- Download URL: socratex-1.3.1.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
469e4b77c34993410838f18ec3ea264e9497ce5db177e9630b120aa8034504e3
|
|
| MD5 |
4143e64388b328acd3d3303aaca4991a
|
|
| BLAKE2b-256 |
04a9585747c9513c8e9039885e535aaf34d45180ea6dafd3114d8fefa380f6b3
|
File details
Details for the file socratex-1.3.1-py3-none-any.whl.
File metadata
- Download URL: socratex-1.3.1-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a99e17ca2f261745fb3b9a3675996886bee6e58e7b0b8519a8b067db5ec383a1
|
|
| MD5 |
b283ea82e9d151776a68570f80ff0f5b
|
|
| BLAKE2b-256 |
6b2b8d19229b918c8b56302836d9957e345376e1a08dc8d36a82ef1b9b86274b
|