File size: 1,639 Bytes
57704c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Run this ONCE, locally, before you deploy the Space, to populate the
examples/ folder with a handful of sample skin-lesion images so users
have something to click on without needing their own photo.

Usage:
    pip install datasets pillow
    python scripts/download_examples.py

This pulls a few images (streaming, no full download) from the public
"marmal88/skin_cancer" dataset on the Hugging Face Hub and saves them as
JPEGs into ../examples/. You can also just drop your own sample images
into that folder instead — any .jpg/.jpeg/.png works.
"""

import os

from datasets import load_dataset

OUT_DIR = os.path.join(os.path.dirname(__file__), "..", "examples")
NUM_EXAMPLES = 6
DATASET_ID = "marmal88/skin_cancer"


def main():
    os.makedirs(OUT_DIR, exist_ok=True)
    print(f"Streaming a few samples from {DATASET_ID} ...")
    ds = load_dataset(DATASET_ID, split="test", streaming=True)

    seen_labels = set()
    count = 0
    for example in ds:
        if count >= NUM_EXAMPLES:
            break
        img = example.get("image")
        label = str(example.get("dx", example.get("label", count)))
        if img is None:
            continue
        # try to get a spread of different classes rather than duplicates
        if label in seen_labels and len(seen_labels) < NUM_EXAMPLES:
            continue
        seen_labels.add(label)
        path = os.path.join(OUT_DIR, f"example_{count:02d}_{label}.jpg")
        img.convert("RGB").save(path, "JPEG")
        print(f"  saved {path}")
        count += 1

    print(f"Done — {count} example image(s) saved to {OUT_DIR}")


if __name__ == "__main__":
    main()