Views
No views yet
transformers’s trust_remote_code=True, you can simply clone this repository and load the model directly.Note: For:
- Full training instructions
- Advanced features (style cycle loss, punctuation modes, etc.)
- Original code details
git clone https://huggingface.co/blowing-up-groundhogs/vatrpp1conda create --name vatr python=3.9
2conda activate vatrpip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126pip install transformers opencv-python matplotlib1from vatrpp import VATrPP
2
3model_vatr_pp = VATrPP.from_pretrained(
4 "vatrpp", # Local folder name or path
5 local_files_only=True
6)1from vatrpp import VATrPP
2
3model_vatr = VATrPP.from_pretrained(
4 "vatrpp",
5 local_files_only=True,
6 subfolder="vatr" # Points to the original VATr checkpoint
7)1import numpy as np
2from PIL import Image
3import torch
4from torchvision import transforms as T
5
6# 1. Load the model (VATr++)
7from vatrpp import VATrPP
8model = VATrPP.from_pretrained("vatrpp", local_files_only=True)
9model.cuda()
10
11# 2. Helper functions to load and process style images
12def load_image(img, chunk_width=192):
13 # Convert to grayscale and resize to height 32
14 img = img.convert("L")
15 img = img.resize((img.width * 32 // img.height, 32))
16 arr = np.array(img)
17
18 # Setup transforms: invert + normalize
19 transform = T.Compose([
20 T.Grayscale(num_output_channels=1),
21 T.ToTensor(),
22 T.Normalize((0.5,), (0.5,))
23 ])
24
25 # Pad / chunk the image to a fixed width
26 arr = 255 - arr
27 height, width = arr.shape
28 out = np.zeros((height, chunk_width), dtype="float32")
29 out[:, :width] = arr[:, :chunk_width]
30 out = 255 - out
31
32 # Apply transforms
33 out = transform(Image.fromarray(out.astype(np.uint8)))
34 return out, width
35
36def load_image_line(img, chunk_width=192, style_imgs_count=15):
37 # Convert to grayscale and resize
38 img = img.convert("L")
39 img = img.resize((img.width * 32 // img.height, 32))
40 arr = np.array(img)
41
42 # Split into fixed-width chunks
43 chunks = []
44 for start in range(0, arr.shape[1], chunk_width):
45 chunk = arr[:, start:start+chunk_width]
46 chunks.append(chunk)
47
48 # Transform each chunk
49 transformed = []
50 for c in chunks:
51 t, _ = load_image(Image.fromarray(c), chunk_width)
52 transformed.append(t)
53
54 # If fewer than `style_imgs_count` chunks, repeat them
55 while len(transformed) < style_imgs_count:
56 transformed += transformed
57 transformed = transformed[:style_imgs_count]
58
59 # Combine
60 return torch.cat(transformed, 0)
61
62# 3. Load a style image of your handwriting (or any handwriting sample)
63style_image_path = "path/to/your_style_image.png"
64img = Image.open(style_image_path)
65style_imgs = load_image_line(img)
66
67# 4. Generate text in the style of `style_image_path`
68generated_pil_image = model.generate(
69 gen_text="This is a test", # Text to generate
70 style_imgs=style_imgs, # Preprocessed style chunks
71 align_words=True, # Align words at baseline
72 at_once=True, # Generate line at once
73)
74
75# 5. Save the generated image
76generated_pil_image.save("generated_output.png")style_imgs: A batch of fixed-width image chunks from your style reference. In practice, you can supply multiple small style samples or a single line image split into chunks.gen_text: The text to render in the given style.align_words and at_once: Optional arguments controlling how the text is laid out and generated.