File size: 12,763 Bytes
b6124f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
Data Preparation Script for VLM Thumbnail Generation Training

Converts PosterCraft/Poster100K and ShareGPT-4o-Image datasets into
OmniGen-compatible JSONL format for fine-tuning.

Output format (JSONL):
  Text-to-Image: {"instruction": "...", "output_image": "path.jpg"}
  Image+Text-to-Image: {"instruction": "... <img><|image_1|></img> ...", "input_images": ["path.jpg"], "output_image": "out.jpg"}
"""

import os
import json
import io
import base64
import random
import hashlib
from pathlib import Path
from PIL import Image
from datasets import load_dataset
from tqdm import tqdm

OUTPUT_DIR = "/app/thumbnail_training_data"
IMAGE_DIR = os.path.join(OUTPUT_DIR, "images")
JSONL_PATH = os.path.join(OUTPUT_DIR, "train.jsonl")

os.makedirs(IMAGE_DIR, exist_ok=True)

# Thumbnail-specific prompt templates for T2I
T2I_TEMPLATES = [
    "Generate a professional thumbnail image: {caption}",
    "Create an eye-catching thumbnail with the following description: {caption}",
    "Design a visually compelling thumbnail: {caption}",
    "Generate a thumbnail image that captures attention: {caption}",
    "Create a high-quality thumbnail: {caption}",
]

# Image+Text-to-Image templates (for image editing/conditioning tasks)
I2I_TEMPLATES = [
    "Transform this image <img><|image_1|></img> into a professional thumbnail. {instruction}",
    "Based on this reference image <img><|image_1|></img>, create a thumbnail. {instruction}",
    "Use this image <img><|image_1|></img> as inspiration to generate a thumbnail. {instruction}",
    "Redesign this image <img><|image_1|></img> as an engaging thumbnail. {instruction}",
]

def save_image_from_bytes(image_bytes, filename):
    """Save binary image data to file."""
    filepath = os.path.join(IMAGE_DIR, filename)
    if isinstance(image_bytes, bytes):
        img = Image.open(io.BytesIO(image_bytes))
    elif isinstance(image_bytes, str):
        # base64 encoded
        img_data = base64.b64decode(image_bytes)
        img = Image.open(io.BytesIO(img_data))
    elif isinstance(image_bytes, Image.Image):
        img = image_bytes
    else:
        raise ValueError(f"Unknown image type: {type(image_bytes)}")
    
    # Resize to max 1024 maintaining aspect ratio
    max_size = 1024
    w, h = img.size
    if max(w, h) > max_size:
        ratio = max_size / max(w, h)
        img = img.resize((int(w * ratio), int(h * ratio)), Image.LANCZOS)
    
    img = img.convert("RGB")
    img.save(filepath, "JPEG", quality=95)
    return filename


def process_poster100k(max_samples=10000):
    """Process PosterCraft/Poster100K → T2I thumbnail training data."""
    print("=" * 60)
    print("Processing PosterCraft/Poster100K...")
    print("=" * 60)
    
    entries = []
    try:
        ds = load_dataset("PosterCraft/Poster100K", split="train", streaming=True)
        count = 0
        for sample in tqdm(ds, desc="PosterCraft", total=max_samples):
            if count >= max_samples:
                break
            
            caption = sample.get("caption", "")
            if not caption or len(caption) < 20:
                continue
            
            image_data = sample.get("image")
            if image_data is None:
                continue
            
            # Generate unique filename
            fname = f"poster_{count:06d}.jpg"
            try:
                save_image_from_bytes(image_data, fname)
            except Exception as e:
                print(f"  Skipping image {count}: {e}")
                continue
            
            # Create T2I entry
            template = random.choice(T2I_TEMPLATES)
            # Truncate very long captions
            if len(caption) > 500:
                caption = caption[:500] + "..."
            
            instruction = template.format(caption=caption)
            entry = {
                "instruction": instruction,
                "output_image": fname
            }
            entries.append(entry)
            count += 1
            
            if count % 1000 == 0:
                print(f"  Processed {count}/{max_samples} PosterCraft samples")
    
    except Exception as e:
        print(f"Error loading PosterCraft: {e}")
    
    print(f"  Total PosterCraft entries: {len(entries)}")
    return entries


def process_sharegpt_t2i(max_samples=5000):
    """Process ShareGPT-4o-Image text-to-image config."""
    print("=" * 60)
    print("Processing ShareGPT-4o-Image (text-to-image)...")
    print("=" * 60)
    
    entries = []
    try:
        ds = load_dataset(
            "FreedomIntelligence/ShareGPT-4o-Image",
            "1_text_to_image",
            split="train",
            streaming=True
        )
        count = 0
        for sample in tqdm(ds, desc="ShareGPT-T2I", total=max_samples):
            if count >= max_samples:
                break
            
            prompt = sample.get("input_prompt", "")
            if not prompt:
                continue
            
            # This dataset has image paths, not actual images in parquet
            # We store the prompt with a thumbnail-generation framing
            fname = f"sgpt_t2i_{count:06d}.jpg"
            
            template = random.choice(T2I_TEMPLATES)
            instruction = template.format(caption=prompt)
            entry = {
                "instruction": instruction,
                "output_image": fname
            }
            entries.append(entry)
            count += 1
    
    except Exception as e:
        print(f"Error loading ShareGPT T2I: {e}")
    
    print(f"  Total ShareGPT T2I entries: {len(entries)}")
    return entries


def process_sharegpt_ti2i(max_samples=5000):
    """Process ShareGPT-4o-Image text+image-to-image config."""
    print("=" * 60)
    print("Processing ShareGPT-4o-Image (text+image-to-image)...")
    print("=" * 60)
    
    entries = []
    try:
        ds = load_dataset(
            "FreedomIntelligence/ShareGPT-4o-Image",
            "2_text_and_image_to_image",
            split="train",
            streaming=True
        )
        count = 0
        for sample in tqdm(ds, desc="ShareGPT-TI2I", total=max_samples):
            if count >= max_samples:
                break
            
            prompt = sample.get("input_prompt", "")
            if not prompt:
                continue
            
            input_fname = f"sgpt_ti2i_input_{count:06d}.jpg"
            output_fname = f"sgpt_ti2i_output_{count:06d}.jpg"
            
            template = random.choice(I2I_TEMPLATES)
            instruction = template.format(instruction=prompt)
            entry = {
                "instruction": instruction,
                "input_images": [input_fname],
                "output_image": output_fname
            }
            entries.append(entry)
            count += 1
    
    except Exception as e:
        print(f"Error loading ShareGPT TI2I: {e}")
    
    print(f"  Total ShareGPT TI2I entries: {len(entries)}")
    return entries


def create_synthetic_thumbnail_prompts(n=2000):
    """Create synthetic thumbnail generation prompts for diverse training."""
    print("=" * 60)
    print(f"Generating {n} synthetic thumbnail prompts...")
    print("=" * 60)
    
    categories = [
        # YouTube-style thumbnails
        ("tech review", [
            "A sleek tech review thumbnail showing {product} with dramatic lighting, bold text overlay saying '{title}', modern gradient background",
            "Professional tech thumbnail: {product} product shot with comparison graphics, rating stars, and the text '{title}'",
        ]),
        ("cooking", [
            "Appetizing cooking thumbnail: close-up of {dish} with steam rising, warm golden lighting, text overlay '{title}' in bold font",
            "Food tutorial thumbnail: beautiful plated {dish}, overhead shot, rustic wooden background, text '{title}'",
        ]),
        ("gaming", [
            "Epic gaming thumbnail: dramatic scene from {game} with character in action pose, glowing effects, bold text '{title}'",
            "Gaming content thumbnail: split-screen reaction shot with {game} gameplay, neon accents, text '{title}'",
        ]),
        ("fitness", [
            "Fitness motivation thumbnail: athletic figure doing {exercise}, dynamic lighting, energetic colors, text '{title}'",
            "Health and fitness thumbnail: before/after transformation graphic, clean design, text '{title}'",
        ]),
        ("education", [
            "Educational content thumbnail: clean whiteboard-style graphic explaining {topic}, colorful diagrams, text '{title}'",
            "Learning video thumbnail: engaging infographic about {topic}, modern flat design, text '{title}'",
        ]),
        ("vlog", [
            "Travel vlog thumbnail: stunning panoramic view of {place}, warm color grading, bold title '{title}'",
            "Daily vlog thumbnail: candid lifestyle shot, bright and airy, playful text '{title}'",
        ]),
        ("music", [
            "Music video thumbnail: artistic portrait with {style} aesthetic, moody lighting, song title '{title}'",
            "Music content thumbnail: abstract sound wave visualization, vibrant colors, artist name and '{title}'",
        ]),
        ("business", [
            "Business advice thumbnail: professional portrait with speech bubble, clean corporate design, text '{title}'",
            "Entrepreneurship thumbnail: rising graph graphic, motivational pose, bold text '{title}'",
        ]),
    ]
    
    products = ["iPhone 16", "MacBook Pro", "PS5", "Nintendo Switch", "Tesla Model S", "AirPods Pro"]
    dishes = ["pasta carbonara", "sushi rolls", "chocolate cake", "grilled steak", "avocado toast"]
    games = ["Zelda", "Elden Ring", "GTA VI", "Minecraft", "Fortnite", "Call of Duty"]
    exercises = ["deadlifts", "yoga", "HIIT training", "pull-ups", "running"]
    topics = ["quantum physics", "machine learning", "history", "economics", "psychology"]
    places = ["Tokyo", "Paris", "Bali", "New York", "Iceland", "Santorini"]
    styles = ["synthwave", "lo-fi", "rock concert", "jazz club", "EDM festival"]
    titles = [
        "You Won't Believe This!", "GAME CHANGER", "The Ultimate Guide",
        "Top 10 Secrets", "I Tried This for 30 Days", "Watch Before You Buy",
        "Is It Worth It?", "My Honest Review", "This Changed Everything",
        "The Truth About...", "How I Made $10K", "Best of 2025"
    ]
    
    fill_map = {
        "product": products, "dish": dishes, "game": games,
        "exercise": exercises, "topic": topics, "place": places,
        "style": styles, "title": titles
    }
    
    entries = []
    for i in range(n):
        cat_name, templates = random.choice(categories)
        template = random.choice(templates)
        
        # Fill in placeholders
        prompt = template
        for key, values in fill_map.items():
            placeholder = "{" + key + "}"
            if placeholder in prompt:
                prompt = prompt.replace(placeholder, random.choice(values))
        
        fname = f"synth_{i:06d}.jpg"
        instruction = f"Generate a professional YouTube thumbnail: {prompt}"
        entry = {
            "instruction": instruction,
            "output_image": fname
        }
        entries.append(entry)
    
    print(f"  Total synthetic entries: {len(entries)}")
    return entries


def main():
    print("=" * 60)
    print("VLM Thumbnail Training Data Preparation")
    print("=" * 60)
    
    all_entries = []
    
    # 1. PosterCraft/Poster100K (primary visual data)
    poster_entries = process_poster100k(max_samples=10000)
    all_entries.extend(poster_entries)
    
    # 2. Synthetic thumbnail prompts (domain-specific text)
    synthetic_entries = create_synthetic_thumbnail_prompts(n=3000)
    all_entries.extend(synthetic_entries)
    
    # Shuffle
    random.seed(42)
    random.shuffle(all_entries)
    
    # Write JSONL
    print(f"\nWriting {len(all_entries)} entries to {JSONL_PATH}")
    with open(JSONL_PATH, "w") as f:
        for entry in all_entries:
            f.write(json.dumps(entry) + "\n")
    
    # Print statistics
    t2i_count = sum(1 for e in all_entries if "input_images" not in e)
    ti2i_count = sum(1 for e in all_entries if "input_images" in e)
    print(f"\nDataset Statistics:")
    print(f"  Total samples: {len(all_entries)}")
    print(f"  Text-to-Image: {t2i_count}")
    print(f"  Text+Image-to-Image: {ti2i_count}")
    print(f"  Images saved to: {IMAGE_DIR}")
    print(f"  JSONL saved to: {JSONL_PATH}")
    
    # Show sample entries
    print(f"\nSample entries:")
    for i, entry in enumerate(all_entries[:3]):
        print(f"  [{i}] {json.dumps(entry, indent=2)[:200]}...")


if __name__ == "__main__":
    main()