ihatebaselines commited on
Commit
04adc54
·
verified ·
1 Parent(s): abbd2b9

Upload __init__.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. __init__.py +130 -0
__init__.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SocrateX - Custom OCR library based on Transformer architecture.
3
+
4
+ This library was developed to facilitate training, inference,
5
+ and experimentation with SOCRATE models. Everything is modular and customizable.
6
+
7
+ Quick Start:
8
+ -----------------------------------
9
+ import SocrateX as sx
10
+
11
+ # 1. Build a custom model architecture using sx.Config:
12
+ config = sx.Config(
13
+ d_model=640,
14
+ nhead=10,
15
+ num_layers=12,
16
+ dim_feedforward=2560,
17
+ pool_height=4 # nn.AdaptiveMaxPool2d((pool_height, None))
18
+ )
19
+ tokenizer = sx.load_tokenizer("ocr_bpe_tokenizer.json")
20
+ model = sx.init(config=config, tokenizer=tokenizer)
21
+
22
+ # 2. Build a dataset (height and max_length go here, not in Config):
23
+ train_set = model.make_dataset(images, labels, height=32, max_length=64)
24
+
25
+ # 3. Train:
26
+ model.fit(dataloader, optimizer, criterion, epochs=50)
27
+
28
+ # 4. Predict (inference params go here):
29
+ results = model.predict(
30
+ image_paths=["document.jpg"],
31
+ function="generate",
32
+ temp=0.5,
33
+ max_iter=64,
34
+ penalty=1.15,
35
+ top_k=5
36
+ )
37
+ """
38
+
39
+ from .configuration_socrate import SocrateConfig
40
+ from .config import Config
41
+ from .model import SOCRATE, cat, rat, mice, ResidualBlock, PositionalEncoding, SocratePool
42
+ from .dataset import Makeset, SmartBatchSampler, load_dataset
43
+ from .trainer import train, Trainer
44
+ from .inference import predict, generate, generate_fast, beam_search, extract_crops_from_image
45
+ from .tokenizer import init_tokenizer, SocrateXTokenizer
46
+ from .synthetic import generate_silly_training_set, generate_silly_testing_set
47
+
48
+ __all__ = [
49
+ "Config",
50
+ "SOCRATE",
51
+ "SocrateConfig",
52
+ "cat",
53
+ "rat",
54
+ "mice",
55
+ "ResidualBlock",
56
+ "PositionalEncoding",
57
+ "SocratePool",
58
+ "Makeset",
59
+ "SmartBatchSampler",
60
+ "train",
61
+ "Trainer",
62
+ "predict",
63
+ "generate",
64
+ "generate_fast",
65
+ "beam_search",
66
+ "init_tokenizer",
67
+ "load_tokenizer",
68
+ "SocrateXTokenizer",
69
+ "generate_silly_training_set",
70
+ "generate_silly_testing_set",
71
+ "load_dataset",
72
+ ]
73
+
74
+ def load_tokenizer(path="ocr_bpe_tokenizer.json"):
75
+ """
76
+ Alias to easily load a tokenizer from a JSON file.
77
+ """
78
+ return SocrateXTokenizer.from_file(path)
79
+
80
+ def load(model_type="cat", weights=None, tokenizer_path="ocr_bpe_tokenizer.json", device="cuda"):
81
+ """
82
+ Automatically loads the desired model and tokenizer.
83
+ Returns (model, tokenizer).
84
+ """
85
+ from tokenizers import Tokenizer
86
+ tokenizer = Tokenizer.from_file(tokenizer_path)
87
+
88
+ if model_type == "cat":
89
+ model = cat(tokenizer=tokenizer, weights=weights if weights else cat.pretrained, device=device)
90
+ elif model_type == "rat":
91
+ model = rat(tokenizer=tokenizer, weights=weights if weights else rat.pretrained, device=device)
92
+ elif model_type == "mice":
93
+ model = mice(tokenizer=tokenizer, weights=weights if weights else mice.pretrained, device=device)
94
+ else:
95
+ raise ValueError(f"Unknown model type: {model_type}")
96
+
97
+ return model, tokenizer
98
+
99
+ def init(tokenizer=None, config=None, device="cuda"):
100
+ """
101
+ Initializes a SOCRATE model from scratch.
102
+
103
+ Pass an sx.Config() object to fully control the architecture:
104
+ config = sx.Config(d_model=256, nhead=4, num_layers=3, pool_height=4)
105
+ model = sx.init(config=config, tokenizer=tokenizer)
106
+
107
+ If config is None, defaults to the cat (158M) architecture.
108
+ """
109
+ if tokenizer is None:
110
+ raise ValueError("You must provide a tokenizer (sx.init_tokenizer() or sx.load_tokenizer()).")
111
+
112
+ if config is None:
113
+ # Default: cat architecture
114
+ config = Config()
115
+
116
+ hf_config = SocrateConfig(
117
+ d_model=config.d_model,
118
+ max_len=config.max_len,
119
+ nhead=config.nhead,
120
+ dim_feedforward=config.dim_feedforward,
121
+ activation=config.activation,
122
+ norm_first=config.norm_first,
123
+ num_layers=config.num_layers,
124
+ vocab_size=tokenizer.get_vocab_size(),
125
+ pad_id=tokenizer.token_to_id("<pad>"),
126
+ bos_id=tokenizer.token_to_id("<bos>"),
127
+ eos_id=tokenizer.token_to_id("<eos>"),
128
+ )
129
+ model = SOCRATE(hf_config, tokenizer=tokenizer, sx_config=config).to(device)
130
+ return model