Spaces:
Sleeping
Sleeping
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.
7 unmodified lines891011121314151617181920212223242526272829303132333435365 unmodified lines424344454647484950515253546 unmodified lines6162636465666768697071725 unmodified lines78798081828384858687888990919293942 unmodified lines9798991001011021038 unmodified lines1121131141151161175 unmodified lines1231241251261271281 unmodified line13013113213313413513613713879 unmodified lines2182192202212222232242252262272282297 unmodified linesfrom documentation import DOC_CONTENTdef auto_label(image, imid, dataset):text = get_task_response("<MORE_DETAILED_CAPTION>", image)ds = dataset.update_label(imid, text)return text, dsdef label_changed(label, imid, dataset):return dataset.update_label(imid, label)def update_single_label(dataset, label_text, image_id):"""Update single image label in dataset."""return dataset.update_label(image_id, label_text)def uploaded(files, current_dataset):"""Handle file upload - return new dataset instance."""if current_dataset is None:current_dataset = ImageDataset()return current_dataset.add_images(files)def labelize_all_images(dataset, label, progress=gr.Progress(True)):"""Generate labels for all images and return new dataset instance."""# Generate actual labels5 unmodified linesreturn dataset.update_all_labels(labels_dict), labeldef download_dataset(dataset, organize_in_folders):"""Create and return zip file for download."""zip_path = create_dataset_zip(dataset, organize_in_folders)return zip_pathdef update_buttons_states(dataset, labeling_in_progress=False):"""Update all button states based on dataset and labeling progress."""count = len(dataset.images)return (6 unmodified lines)def start_labeling(dataset):"""Start labeling process - disable buttons and show progress."""return update_buttons_states(dataset, labeling_in_progress=True)def finish_labeling(dataset):"""Finish labeling process - enable buttons and hide progress."""return update_buttons_states(dataset, labeling_in_progress=False)5 unmodified lineswith gr.Group():gr.Markdown("Upload images and add labels to build your dataset.")upload_button = gr.UploadButton("📁 Upload images", file_count="multiple")label_all = gr.Button("🏷️ Labelize all images", interactive=False)is_labeling_in_progress = gr.State(False)progressbar = gr.Label("", visible=False, label="Preparing...")remove_all = gr.Button("🗑️ Remove all", interactive=False)with gr.Group():download_button = gr.DownloadButton("💾 Download dataset", interactive=False, size="lg")organize_files = gr.Checkbox(label="📂 Organize in folders", value=False)@gr.render(inputs=[dataset, is_labeling_in_progress])def render_grid(ds, is_labeling_in_progress):2 unmodified linesreturn# Hidden component to trigger label refreshgr.Number(value=0, visible=False)with gr.Row(equal_height=True):for im in ds.images:with (8 unmodified lines],),):image = gr.Image(im["path"],type="pil",5 unmodified lines)label = gr.Text(placeholder="Description...",lines=5,container=False,1 unmodified linekey=f"text_{im['id']}",)# Hidden component to store current image IDcurrent_image_id = gr.State(value=im["id"])button = gr.Button("✨ Generate label",interactive=not is_labeling_in_progress,79 unmodified linesis_labeling_in_progress,],)# Download zip filedownload_button.click(download_dataset,inputs=[dataset, organize_files],outputs=[download_button],)7 unmodified lines891011121314151617181920212223242526272829303132333435363738394041425 unmodified lines484950515253545556575859606 unmodified lines6768697071727374757677785 unmodified lines848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201212 unmodified lines1241251261271281298 unmodified lines1381391401411421431441451465 unmodified lines1521531541551561571581 unmodified line16016116216316416579 unmodified lines2452462472482492502512522532542552562572582597 unmodified linesfrom documentation import DOC_CONTENTdef auto_label(image: Image.Image, imid: int, dataset: ImageDataset) -> tuple[str, ImageDataset]:text = get_task_response("<MORE_DETAILED_CAPTION>", image)ds = dataset.update_label(imid, text)return text, dsdef label_changed(label: str, imid: int, dataset: ImageDataset) -> ImageDataset:return dataset.update_label(imid, label)def update_single_label(dataset: ImageDataset, label_text: str, image_id: int) -> ImageDataset:"""Update single image label in dataset."""return dataset.update_label(image_id, label_text)def uploaded(files: list, current_dataset: ImageDataset | None) -> ImageDataset:"""Handle file upload - return new dataset instance."""if current_dataset is None:current_dataset = ImageDataset()return current_dataset.add_images(files)def labelize_all_images(dataset: ImageDataset, label: str, progress=gr.Progress(True)) -> tuple[ImageDataset, str]:"""Generate labels for all images and return new dataset instance."""# Generate actual labels5 unmodified linesreturn dataset.update_all_labels(labels_dict), labeldef create_dataset_zipfile(dataset: ImageDataset, organize_in_folders: bool):"""Create and return zip file for download."""zip_path = create_dataset_zip(dataset, organize_in_folders)return gr.update(visible=True, value=zip_path)def update_buttons_states(dataset: ImageDataset, labeling_in_progress=False):"""Update all button states based on dataset and labeling progress."""count = len(dataset.images)return (6 unmodified lines)def start_labeling(dataset: ImageDataset):"""Start labeling process - disable buttons and show progress."""return update_buttons_states(dataset, labeling_in_progress=True)def finish_labeling(dataset: ImageDataset):"""Finish labeling process - enable buttons and hide progress."""return update_buttons_states(dataset, labeling_in_progress=False)5 unmodified lineswith gr.Group():gr.Markdown("Upload images and add labels to build your dataset.")upload_button = gr.UploadButton("📁 Upload images",file_count="multiple",)label_all = gr.Button("🏷️ Labelize all images",interactive=False,)is_labeling_in_progress = gr.State(False,)progressbar = gr.Label("",visible=False,label="Preparing...",)remove_all = gr.Button("🗑️ Remove all",interactive=False,)with gr.Group():organize_files = gr.Checkbox(label="📂 Organize in folders", value=False, render=False)download_button = gr.Button("💾 Create zip file to download",interactive=False,size="lg",)download_file = gr.File(label="Generated datasets", visible=False)organize_files.render()@gr.render(inputs=[dataset, is_labeling_in_progress])def render_grid(ds, is_labeling_in_progress):2 unmodified linesreturn# Hidden component to trigger label refreshwith gr.Row(equal_height=True):for im in ds.images:with (8 unmodified lines],),):# Hidden component to store current image IDcurrent_image_id = gr.State(value=im["id"])image = gr.Image(im["path"],type="pil",5 unmodified lines)label = gr.Text(im["label"],placeholder="Description...",lines=5,container=False,1 unmodified linekey=f"text_{im['id']}",)button = gr.Button("✨ Generate label",interactive=not is_labeling_in_progress,79 unmodified linesis_labeling_in_progress,],)# create the zip file and set the download file section ready to usedownload_button.click(lambda: gr.update(visible=True),inputs=None,outputs=download_file,).then(create_dataset_zipfile,inputs=[dataset, organize_files],outputs=download_file,)
app.py+56-26