Spaces:
Running
on
Zero
Running
on
Zero
Make a download zone
Browse filesWe can't creazte a zip file and start the downloading...
So, let's create a download zone with gr.File for the moment.
Also, cleanup and type annotation.
app.py
CHANGED
|
@@ -8,29 +8,35 @@ from packager import create_dataset_zip
|
|
| 8 |
from documentation import DOC_CONTENT
|
| 9 |
|
| 10 |
|
| 11 |
-
def auto_label(
|
|
|
|
|
|
|
| 12 |
text = get_task_response("<MORE_DETAILED_CAPTION>", image)
|
| 13 |
ds = dataset.update_label(imid, text)
|
| 14 |
return text, ds
|
| 15 |
|
| 16 |
|
| 17 |
-
def label_changed(label, imid, dataset):
|
| 18 |
return dataset.update_label(imid, label)
|
| 19 |
|
| 20 |
|
| 21 |
-
def update_single_label(
|
|
|
|
|
|
|
| 22 |
"""Update single image label in dataset."""
|
| 23 |
return dataset.update_label(image_id, label_text)
|
| 24 |
|
| 25 |
|
| 26 |
-
def uploaded(files, current_dataset):
|
| 27 |
"""Handle file upload - return new dataset instance."""
|
| 28 |
if current_dataset is None:
|
| 29 |
current_dataset = ImageDataset()
|
| 30 |
return current_dataset.add_images(files)
|
| 31 |
|
| 32 |
|
| 33 |
-
def labelize_all_images(
|
|
|
|
|
|
|
| 34 |
"""Generate labels for all images and return new dataset instance."""
|
| 35 |
|
| 36 |
# Generate actual labels
|
|
@@ -42,13 +48,13 @@ def labelize_all_images(dataset, label, progress=gr.Progress(True)):
|
|
| 42 |
return dataset.update_all_labels(labels_dict), label
|
| 43 |
|
| 44 |
|
| 45 |
-
def
|
| 46 |
"""Create and return zip file for download."""
|
| 47 |
zip_path = create_dataset_zip(dataset, organize_in_folders)
|
| 48 |
-
return zip_path
|
| 49 |
|
| 50 |
|
| 51 |
-
def update_buttons_states(dataset, labeling_in_progress=False):
|
| 52 |
"""Update all button states based on dataset and labeling progress."""
|
| 53 |
count = len(dataset.images)
|
| 54 |
return (
|
|
@@ -61,12 +67,12 @@ def update_buttons_states(dataset, labeling_in_progress=False):
|
|
| 61 |
)
|
| 62 |
|
| 63 |
|
| 64 |
-
def start_labeling(dataset):
|
| 65 |
"""Start labeling process - disable buttons and show progress."""
|
| 66 |
return update_buttons_states(dataset, labeling_in_progress=True)
|
| 67 |
|
| 68 |
|
| 69 |
-
def finish_labeling(dataset):
|
| 70 |
"""Finish labeling process - enable buttons and hide progress."""
|
| 71 |
return update_buttons_states(dataset, labeling_in_progress=False)
|
| 72 |
|
|
@@ -78,17 +84,38 @@ with gr.Blocks(title="Labelizer", fill_width=True) as demo:
|
|
| 78 |
with gr.Group():
|
| 79 |
gr.Markdown("Upload images and add labels to build your dataset.")
|
| 80 |
|
| 81 |
-
upload_button = gr.UploadButton(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
with gr.Group():
|
| 88 |
-
|
| 89 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
)
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
@gr.render(inputs=[dataset, is_labeling_in_progress])
|
| 94 |
def render_grid(ds, is_labeling_in_progress):
|
|
@@ -97,7 +124,6 @@ with gr.Blocks(title="Labelizer", fill_width=True) as demo:
|
|
| 97 |
return
|
| 98 |
|
| 99 |
# Hidden component to trigger label refresh
|
| 100 |
-
gr.Number(value=0, visible=False)
|
| 101 |
with gr.Row(equal_height=True):
|
| 102 |
for im in ds.images:
|
| 103 |
with (
|
|
@@ -112,6 +138,9 @@ with gr.Blocks(title="Labelizer", fill_width=True) as demo:
|
|
| 112 |
],
|
| 113 |
),
|
| 114 |
):
|
|
|
|
|
|
|
|
|
|
| 115 |
image = gr.Image(
|
| 116 |
im["path"],
|
| 117 |
type="pil",
|
|
@@ -123,6 +152,7 @@ with gr.Blocks(title="Labelizer", fill_width=True) as demo:
|
|
| 123 |
)
|
| 124 |
|
| 125 |
label = gr.Text(
|
|
|
|
| 126 |
placeholder="Description...",
|
| 127 |
lines=5,
|
| 128 |
container=False,
|
|
@@ -130,9 +160,6 @@ with gr.Blocks(title="Labelizer", fill_width=True) as demo:
|
|
| 130 |
key=f"text_{im['id']}",
|
| 131 |
)
|
| 132 |
|
| 133 |
-
# Hidden component to store current image ID
|
| 134 |
-
current_image_id = gr.State(value=im["id"])
|
| 135 |
-
|
| 136 |
button = gr.Button(
|
| 137 |
"โจ Generate label",
|
| 138 |
interactive=not is_labeling_in_progress,
|
|
@@ -218,12 +245,15 @@ with gr.Blocks(title="Labelizer", fill_width=True) as demo:
|
|
| 218 |
is_labeling_in_progress,
|
| 219 |
],
|
| 220 |
)
|
| 221 |
-
|
| 222 |
-
# Download zip file
|
| 223 |
download_button.click(
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
inputs=[dataset, organize_files],
|
| 226 |
-
outputs=
|
| 227 |
)
|
| 228 |
|
| 229 |
|
|
|
|
| 8 |
from documentation import DOC_CONTENT
|
| 9 |
|
| 10 |
|
| 11 |
+
def auto_label(
|
| 12 |
+
image: Image.Image, imid: int, dataset: ImageDataset
|
| 13 |
+
) -> tuple[str, ImageDataset]:
|
| 14 |
text = get_task_response("<MORE_DETAILED_CAPTION>", image)
|
| 15 |
ds = dataset.update_label(imid, text)
|
| 16 |
return text, ds
|
| 17 |
|
| 18 |
|
| 19 |
+
def label_changed(label: str, imid: int, dataset: ImageDataset) -> ImageDataset:
|
| 20 |
return dataset.update_label(imid, label)
|
| 21 |
|
| 22 |
|
| 23 |
+
def update_single_label(
|
| 24 |
+
dataset: ImageDataset, label_text: str, image_id: int
|
| 25 |
+
) -> ImageDataset:
|
| 26 |
"""Update single image label in dataset."""
|
| 27 |
return dataset.update_label(image_id, label_text)
|
| 28 |
|
| 29 |
|
| 30 |
+
def uploaded(files: list, current_dataset: ImageDataset | None) -> ImageDataset:
|
| 31 |
"""Handle file upload - return new dataset instance."""
|
| 32 |
if current_dataset is None:
|
| 33 |
current_dataset = ImageDataset()
|
| 34 |
return current_dataset.add_images(files)
|
| 35 |
|
| 36 |
|
| 37 |
+
def labelize_all_images(
|
| 38 |
+
dataset: ImageDataset, label: str, progress=gr.Progress(True)
|
| 39 |
+
) -> tuple[ImageDataset, str]:
|
| 40 |
"""Generate labels for all images and return new dataset instance."""
|
| 41 |
|
| 42 |
# Generate actual labels
|
|
|
|
| 48 |
return dataset.update_all_labels(labels_dict), label
|
| 49 |
|
| 50 |
|
| 51 |
+
def create_dataset_zipfile(dataset: ImageDataset, organize_in_folders: bool):
|
| 52 |
"""Create and return zip file for download."""
|
| 53 |
zip_path = create_dataset_zip(dataset, organize_in_folders)
|
| 54 |
+
return gr.update(visible=True, value=zip_path)
|
| 55 |
|
| 56 |
|
| 57 |
+
def update_buttons_states(dataset: ImageDataset, labeling_in_progress=False):
|
| 58 |
"""Update all button states based on dataset and labeling progress."""
|
| 59 |
count = len(dataset.images)
|
| 60 |
return (
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
|
| 70 |
+
def start_labeling(dataset: ImageDataset):
|
| 71 |
"""Start labeling process - disable buttons and show progress."""
|
| 72 |
return update_buttons_states(dataset, labeling_in_progress=True)
|
| 73 |
|
| 74 |
|
| 75 |
+
def finish_labeling(dataset: ImageDataset):
|
| 76 |
"""Finish labeling process - enable buttons and hide progress."""
|
| 77 |
return update_buttons_states(dataset, labeling_in_progress=False)
|
| 78 |
|
|
|
|
| 84 |
with gr.Group():
|
| 85 |
gr.Markdown("Upload images and add labels to build your dataset.")
|
| 86 |
|
| 87 |
+
upload_button = gr.UploadButton(
|
| 88 |
+
"๐ Upload images",
|
| 89 |
+
file_count="multiple",
|
| 90 |
+
)
|
| 91 |
+
label_all = gr.Button(
|
| 92 |
+
"๐ท๏ธ Labelize all images",
|
| 93 |
+
interactive=False,
|
| 94 |
+
)
|
| 95 |
+
is_labeling_in_progress = gr.State(
|
| 96 |
+
False,
|
| 97 |
+
)
|
| 98 |
+
progressbar = gr.Label(
|
| 99 |
+
"",
|
| 100 |
+
visible=False,
|
| 101 |
+
label="Preparing...",
|
| 102 |
+
)
|
| 103 |
+
remove_all = gr.Button(
|
| 104 |
+
"๐๏ธ Remove all",
|
| 105 |
+
interactive=False,
|
| 106 |
+
)
|
| 107 |
|
| 108 |
with gr.Group():
|
| 109 |
+
organize_files = gr.Checkbox(
|
| 110 |
+
label="๐ Organize in folders", value=False, render=False
|
| 111 |
+
)
|
| 112 |
+
download_button = gr.Button(
|
| 113 |
+
"๐พ Create zip file to download",
|
| 114 |
+
interactive=False,
|
| 115 |
+
size="lg",
|
| 116 |
)
|
| 117 |
+
download_file = gr.File(label="Generated datasets", visible=False)
|
| 118 |
+
organize_files.render()
|
| 119 |
|
| 120 |
@gr.render(inputs=[dataset, is_labeling_in_progress])
|
| 121 |
def render_grid(ds, is_labeling_in_progress):
|
|
|
|
| 124 |
return
|
| 125 |
|
| 126 |
# Hidden component to trigger label refresh
|
|
|
|
| 127 |
with gr.Row(equal_height=True):
|
| 128 |
for im in ds.images:
|
| 129 |
with (
|
|
|
|
| 138 |
],
|
| 139 |
),
|
| 140 |
):
|
| 141 |
+
# Hidden component to store current image ID
|
| 142 |
+
current_image_id = gr.State(value=im["id"])
|
| 143 |
+
|
| 144 |
image = gr.Image(
|
| 145 |
im["path"],
|
| 146 |
type="pil",
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
label = gr.Text(
|
| 155 |
+
im["label"],
|
| 156 |
placeholder="Description...",
|
| 157 |
lines=5,
|
| 158 |
container=False,
|
|
|
|
| 160 |
key=f"text_{im['id']}",
|
| 161 |
)
|
| 162 |
|
|
|
|
|
|
|
|
|
|
| 163 |
button = gr.Button(
|
| 164 |
"โจ Generate label",
|
| 165 |
interactive=not is_labeling_in_progress,
|
|
|
|
| 245 |
is_labeling_in_progress,
|
| 246 |
],
|
| 247 |
)
|
| 248 |
+
# create the zip file and set the download file section ready to use
|
|
|
|
| 249 |
download_button.click(
|
| 250 |
+
lambda: gr.update(visible=True),
|
| 251 |
+
inputs=None,
|
| 252 |
+
outputs=download_file,
|
| 253 |
+
).then(
|
| 254 |
+
create_dataset_zipfile,
|
| 255 |
inputs=[dataset, organize_files],
|
| 256 |
+
outputs=download_file,
|
| 257 |
)
|
| 258 |
|
| 259 |
|