File size: 2,255 Bytes
6f64bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from PIL import Image
from datasets import Dataset
from huggingface_hub import HfApi

# Set the path to the CoLeaf dataset directory
coleaf_dir = "/home/goya/CV_Plant_Disease/Datasets/CoLeaf_dataset"

# Set the name and description for the Huggingface dataset
dataset_name = "bhugxer/CoLeafLabels"
dataset_description = "CoLeaf dataset for fine-tuning Stable Diffusion"

# Create lists to store the image paths and labels
image_paths = []
labels = []

api_token = os.environ.get("HUGGINGFACE_API_TOKEN")

# Iterate over the CoLeaf dataset directory
for label in os.listdir(coleaf_dir):
    label_dir = os.path.join(coleaf_dir, label)
    if os.path.isdir(label_dir):
        for image_file in os.listdir(label_dir):
            image_path = os.path.join(label_dir, image_file)
            image_paths.append(image_path)
            labels.append(label)

# Create a Huggingface dataset
dataset = Dataset.from_dict({"image_path": image_paths, "label": labels})

# Define the image loading function
def load_image(example):
    with open(example["image_path"], "rb") as f:
        image_data = f.read()
    return {"image": image_data}

# Apply the image loading function to the dataset
dataset = dataset.map(load_image, batched=False, num_proc=4)

# Remove the "image_path" column from the dataset
dataset = dataset.remove_columns("image_path")

# Push the dataset to the Huggingface Hub
api = HfApi(token=api_token)
api.upload_folder(
    folder_path=".",
    repo_id=dataset_name,
    repo_type="dataset",
    ignore_patterns=["**/.*", "**/__pycache__"],
    use_auth_token=api_token,
)

# Create a dataset card
dataset_card = f"""
# {dataset_name}

{dataset_description}

## Dataset Structure

- `image`: The image data.
- `label`: The label or text description of the image.

## Dataset Info

- Number of examples: {len(dataset)}
- Image format: Various (PNG, JPEG, etc.)

## License

[Insert license information here]

## Citation

[Insert citation information here]
"""

# Push the dataset card to the Huggingface Hub
with open("README.md", "w") as f:
    f.write(dataset_card)

api.upload_file(
    path_or_fileobj="README.md",
    path_in_repo="README.md",
    repo_id=dataset_name,
    repo_type="dataset",
    use_auth_token=api_token,
)