Image-to-Text
Transformers
Safetensors
Tibetan
vision-encoder-decoder
image-text-to-text
ocr
tibetan
document-ai
trocr
Instructions to use TibetanCodexAITeam/PechaBridgeOCR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use TibetanCodexAITeam/PechaBridgeOCR with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("image-to-text", model="TibetanCodexAITeam/PechaBridgeOCR")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR") model = AutoModelForMultimodalLM.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update model card (README.md) via PechaBridge
Browse files
README.md
CHANGED
|
@@ -44,21 +44,35 @@ Each image produces a `.txt` transcript and an `*_overlay.jpg` with detected lin
|
|
| 44 |
If you need to call the model directly from Python, you must apply the BDRC-style preprocessing manually before passing pixel values to the model. See `pechabridge/ocr/preprocess_bdrc.py` in the PechaBridge repository for the full preprocessing pipeline (`gray` pipeline was used for this checkpoint).
|
| 45 |
|
| 46 |
```python
|
| 47 |
-
# Minimal example —
|
| 48 |
import torch
|
| 49 |
from transformers import VisionEncoderDecoderModel, AutoTokenizer
|
| 50 |
-
from pechabridge.ocr.preprocess_bdrc import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
from PIL import Image
|
| 52 |
import numpy as np
|
| 53 |
|
| 54 |
model = VisionEncoderDecoderModel.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
|
| 55 |
tokenizer = AutoTokenizer.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
|
| 56 |
|
| 57 |
-
# Load
|
| 58 |
image = Image.open('line_crop.png').convert('RGB')
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
generated_ids = model.generate(pixel_values)
|
| 64 |
text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
| 44 |
If you need to call the model directly from Python, you must apply the BDRC-style preprocessing manually before passing pixel values to the model. See `pechabridge/ocr/preprocess_bdrc.py` in the PechaBridge repository for the full preprocessing pipeline (`gray` pipeline was used for this checkpoint).
|
| 45 |
|
| 46 |
```python
|
| 47 |
+
# Minimal example — BDRC gray preprocessing applied explicitly
|
| 48 |
import torch
|
| 49 |
from transformers import VisionEncoderDecoderModel, AutoTokenizer
|
| 50 |
+
from pechabridge.ocr.preprocess_bdrc import (
|
| 51 |
+
BDRCPreprocessConfig,
|
| 52 |
+
preprocess_image_bdrc, # returns grayscale PIL Image (mode 'L')
|
| 53 |
+
bdrc_image_to_normalized_tensor, # grayscale PIL → float32 HW in [-1, 1]
|
| 54 |
+
)
|
| 55 |
from PIL import Image
|
| 56 |
import numpy as np
|
| 57 |
|
| 58 |
model = VisionEncoderDecoderModel.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
|
| 59 |
tokenizer = AutoTokenizer.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
|
| 60 |
|
| 61 |
+
# 1. Load the line crop as RGB
|
| 62 |
image = Image.open('line_crop.png').convert('RGB')
|
| 63 |
+
|
| 64 |
+
# 2. Apply BDRC preprocessing:
|
| 65 |
+
# - converts to grayscale (luma by default)
|
| 66 |
+
# - optionally normalises background, binarises, pads/resizes
|
| 67 |
+
# Returns a grayscale PIL Image (mode 'L')
|
| 68 |
+
cfg = BDRCPreprocessConfig.ocr_line_defaults() # defaults used during training
|
| 69 |
+
gray_pil = preprocess_image_bdrc(image, cfg)
|
| 70 |
+
|
| 71 |
+
# 3. Normalise to float32 in [-1, 1] and build a 3-channel tensor
|
| 72 |
+
# (model expects C=3; replicate the single gray channel)
|
| 73 |
+
gray_hw = bdrc_image_to_normalized_tensor(image, cfg) # shape: (H, W), float32
|
| 74 |
+
pixel_values = torch.tensor(gray_hw).unsqueeze(0).unsqueeze(0) # (1, 1, H, W)
|
| 75 |
+
pixel_values = pixel_values.expand(-1, 3, -1, -1) # (1, 3, H, W)
|
| 76 |
|
| 77 |
generated_ids = model.generate(pixel_values)
|
| 78 |
text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|