Spaces:
Sleeping
Sleeping
GeorgeSherif commited on
Commit ·
a7dc28d
1
Parent(s): 8d04afa
updates
Browse files
app.py
CHANGED
|
@@ -26,8 +26,9 @@ except Exception as e:
|
|
| 26 |
})
|
| 27 |
train_dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 28 |
val_dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
image_folder = "images"
|
| 33 |
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
|
@@ -38,13 +39,12 @@ def get_next_image(session_data):
|
|
| 38 |
with lock:
|
| 39 |
annotated_images = set(train_dataset["image_id"]) | set(val_dataset["image_id"]) # Set of annotated images
|
| 40 |
available_images = [img for img in image_files if img not in annotated_images]
|
| 41 |
-
# Check if the user already has an image
|
| 42 |
if session_data["current_image"] is None and available_images:
|
| 43 |
# Assign a new random image to the user
|
| 44 |
session_data["current_image"] = random.choice(available_images)
|
| 45 |
return os.path.join(image_folder, session_data["current_image"]) if session_data["current_image"] else None
|
| 46 |
|
| 47 |
-
# Function to save the annotation to Hugging Face datasets and fetch the next image
|
| 48 |
def save_annotation(caption, session_data):
|
| 49 |
if session_data["current_image"] is None:
|
| 50 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
|
@@ -66,11 +66,11 @@ def save_annotation(caption, session_data):
|
|
| 66 |
else:
|
| 67 |
return gr.update(visible=False), gr.update(value="Unknown dataset split for image!")
|
| 68 |
|
| 69 |
-
# Add the new annotation
|
| 70 |
new_data = Dataset.from_dict({"image_id": [image_id], "caption": [caption]})
|
| 71 |
target_dataset = concatenate_datasets([target_dataset, new_data])
|
| 72 |
|
| 73 |
-
# Save updated
|
| 74 |
target_dataset.push_to_hub(dataset_name, split=split_name)
|
| 75 |
print(f"Pushed updated {split_name} dataset")
|
| 76 |
|
|
|
|
| 26 |
})
|
| 27 |
train_dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 28 |
val_dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 29 |
+
dataset_dict = {"train": train_dataset, "validation": val_dataset}
|
| 30 |
+
for split_name, split_dataset in dataset_dict.items():
|
| 31 |
+
split_dataset.push_to_hub(dataset_name, split=split_name)
|
| 32 |
|
| 33 |
image_folder = "images"
|
| 34 |
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
|
|
|
| 39 |
with lock:
|
| 40 |
annotated_images = set(train_dataset["image_id"]) | set(val_dataset["image_id"]) # Set of annotated images
|
| 41 |
available_images = [img for img in image_files if img not in annotated_images]
|
|
|
|
| 42 |
if session_data["current_image"] is None and available_images:
|
| 43 |
# Assign a new random image to the user
|
| 44 |
session_data["current_image"] = random.choice(available_images)
|
| 45 |
return os.path.join(image_folder, session_data["current_image"]) if session_data["current_image"] else None
|
| 46 |
|
| 47 |
+
# Function to save the annotation to the correct split in Hugging Face datasets and fetch the next image
|
| 48 |
def save_annotation(caption, session_data):
|
| 49 |
if session_data["current_image"] is None:
|
| 50 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
|
|
|
| 66 |
else:
|
| 67 |
return gr.update(visible=False), gr.update(value="Unknown dataset split for image!")
|
| 68 |
|
| 69 |
+
# Add the new annotation to the correct dataset without overwriting
|
| 70 |
new_data = Dataset.from_dict({"image_id": [image_id], "caption": [caption]})
|
| 71 |
target_dataset = concatenate_datasets([target_dataset, new_data])
|
| 72 |
|
| 73 |
+
# Save the updated split to Hugging Face without overwriting
|
| 74 |
target_dataset.push_to_hub(dataset_name, split=split_name)
|
| 75 |
print(f"Pushed updated {split_name} dataset")
|
| 76 |
|