ihatebaselines commited on
Commit
d189f8c
Β·
verified Β·
1 Parent(s): 79ca052

Add SocrateX library section + module reference table

Browse files
Files changed (1) hide show
  1. README.md +85 -0
README.md CHANGED
@@ -173,3 +173,88 @@ model.generate_data(
173
  )
174
  images, labels = model.load_data("my_train_data/labels.csv")
175
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  )
174
  images, labels = model.load_data("my_train_data/labels.csv")
175
  ```
176
+
177
+ ---
178
+
179
+ ## Using the SocrateX Library Directly
180
+
181
+ If you prefer to use SocrateX as a standalone library (e.g. in a training script), the API is identical β€” just `import SocrateX as sx`.
182
+
183
+ ```python
184
+ import SocrateX as sx
185
+ import torch
186
+ from torch.utils.data import DataLoader
187
+
188
+ # ─── 1. Tokenizer ────────────────────────────────────────────────────────────
189
+ tokenizer = sx.load_tokenizer("ocr_bpe_tokenizer.json")
190
+ # or build a fresh one:
191
+ # tokenizer = sx.init_tokenizer()
192
+
193
+ # ─── 2. Config ───────────────────────────────────────────────────────────────
194
+ config = sx.Config(
195
+ d_model=640,
196
+ nhead=10,
197
+ num_layers=12,
198
+ dim_feedforward=2560,
199
+ pool_height=4
200
+ )
201
+
202
+ # ─── 3. Model ────────────────────────────────────────────────────────────────
203
+ model = sx.init(config=config, tokenizer=tokenizer, device="cuda")
204
+ # or use a preset:
205
+ # model = sx.cat(tokenizer=tokenizer, weights="previous.pt", device="cuda")
206
+ # model = sx.rat(tokenizer=tokenizer, device="cuda")
207
+ # model = sx.mice(tokenizer=tokenizer, device="cuda")
208
+
209
+ # ─── 4. Dataset ──────────────────────────────────────────────────────────────
210
+ images, labels = sx.load_dataset("label.csv")
211
+ dataset = sx.Makeset(images, labels, tokenizer=tokenizer)
212
+ sampler = sx.SmartBatchSampler(labels, batch_size=32)
213
+ loader = DataLoader(dataset, batch_sampler=sampler)
214
+
215
+ # ─── 5. Train ────────────────────────────────────────────────────────────────
216
+ optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
217
+ criterion = torch.nn.CrossEntropyLoss(ignore_index=tokenizer.token_to_id("<pad>"))
218
+
219
+ trainer = sx.Trainer(model, loader, optimizer, criterion, device="cuda")
220
+ for epoch in range(50):
221
+ loss = trainer.train_epoch()
222
+ print(f"Epoch {epoch} | Loss: {loss:.4f}")
223
+
224
+ # ─── 6. Inference ────────────────────────────────────────────────────────────
225
+ results = model.predict(
226
+ image_paths=["receipt.jpg", "document.png"],
227
+ function="generate",
228
+ max_iter=64,
229
+ temp=0.5,
230
+ top_k=5,
231
+ penalty=1.15
232
+ )
233
+ print(results)
234
+
235
+ # ─── 7. Synthetic Data ───────────────────────────────────────────────────────
236
+ sx.generate_silly_training_set(
237
+ source="https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-no-swears.txt",
238
+ count=1000,
239
+ output_dir="train_data"
240
+ )
241
+ ```
242
+
243
+ ### SocrateX Module Reference
244
+
245
+ | Module | What it does |
246
+ |--------|-------------|
247
+ | `sx.Config(...)` | Define custom architecture |
248
+ | `sx.init(config, tokenizer)` | Build model from scratch |
249
+ | `sx.cat / sx.rat / sx.mice` | Preset-sized models |
250
+ | `sx.load_tokenizer(path)` | Load BPE tokenizer from file |
251
+ | `sx.init_tokenizer()` | Build a fresh tokenizer |
252
+ | `sx.load_dataset(path)` | Load CSV/JSON/TXT β†’ (images, labels) |
253
+ | `sx.Makeset(images, labels, ...)` | Create a PyTorch Dataset |
254
+ | `sx.SmartBatchSampler(labels, batch_size)` | Length-sorted batch sampler |
255
+ | `sx.Trainer(model, loader, opt, crit)` | Training loop wrapper |
256
+ | `sx.predict(...)` | Run inference on image list |
257
+ | `sx.generate(...)` | Greedy autoregressive generation |
258
+ | `sx.beam_search(...)` | Beam search decoding |
259
+ | `sx.generate_silly_training_set(...)` | Generate synthetic word images |
260
+