File size: 1,855 Bytes
7e7196e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from PIL import Image, UnidentifiedImageError
from tqdm import tqdm

INPUT_JSONL = "0508.jsonl"
OUTPUT_JSONL = "0508_clean.jsonl"

def is_valid_image(path):
    if not os.path.exists(path):
        return False

    try:
        with Image.open(path) as img:
            img.verify()  # 🔥 checks corruption without loading full image
        return True
    except (UnidentifiedImageError, OSError):
        return False


def clean_jsonl():
    total = 0
    kept = 0
    removed = 0

    with open(INPUT_JSONL, "r", encoding="utf-8") as infile, \
         open(OUTPUT_JSONL, "w", encoding="utf-8") as outfile:

        for line in tqdm(infile, desc="Cleaning dataset"):
            total += 1

            try:
                data = json.loads(line)
            except json.JSONDecodeError:
                removed += 1
                continue

            image_path = data.get("image", "")
            markdown = data.get("markdown", "")

            # 🔴 Check 1: image validity
            if not is_valid_image(image_path):
                removed += 1
                continue

            # 🔴 Check 2: empty or tiny markdown
            if not markdown or len(markdown.strip()) < 10:
                removed += 1
                continue

            # 🔴 Optional: cut extremely large samples (prevents future crashes)
            if len(markdown) > 10000:
                data["markdown"] = markdown[:10000]

            outfile.write(json.dumps(data, ensure_ascii=False) + "\n")
            kept += 1

    print("\n===== CLEANING SUMMARY =====")
    print(f"Total rows:   {total}")
    print(f"Kept rows:    {kept}")
    print(f"Removed rows: {removed}")
    print(f"Saved to:     {OUTPUT_JSONL}")


if __name__ == "__main__":
    clean_jsonl()