Spaces:
Sleeping
Sleeping
GeorgeSherif commited on
Commit ·
ff00ea3
1
Parent(s): a7dc28d
updates
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import threading
|
|
@@ -13,22 +14,18 @@ else:
|
|
| 13 |
print("HUGGINGFACE_TOKEN environment variable not set.")
|
| 14 |
dataset_name = "GeorgeIbrahim/EGYCOCO" # Replace with your dataset name
|
| 15 |
|
| 16 |
-
# Load or create the
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
print("Loaded existing datasets:", train_dataset, val_dataset)
|
| 21 |
except Exception as e:
|
| 22 |
-
# Create empty
|
| 23 |
features = Features({
|
| 24 |
'image_id': Value(dtype='string'),
|
| 25 |
'caption': Value(dtype='string'),
|
| 26 |
})
|
| 27 |
-
|
| 28 |
-
|
| 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'))]
|
|
@@ -37,14 +34,15 @@ lock = threading.Lock()
|
|
| 37 |
# Function to get a random image that hasn’t been annotated or skipped
|
| 38 |
def get_next_image(session_data):
|
| 39 |
with lock:
|
| 40 |
-
annotated_images = set(
|
| 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
|
| 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!")
|
|
@@ -56,23 +54,14 @@ def save_annotation(caption, session_data):
|
|
| 56 |
if caption.strip().lower() == "skip":
|
| 57 |
caption = "skipped"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
if "train" in image_id:
|
| 61 |
-
target_dataset = train_dataset
|
| 62 |
-
split_name = "train"
|
| 63 |
-
elif "val" in image_id:
|
| 64 |
-
target_dataset = val_dataset
|
| 65 |
-
split_name = "validation"
|
| 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 |
-
|
|
|
|
| 72 |
|
| 73 |
-
# Save
|
| 74 |
-
|
| 75 |
-
print(
|
| 76 |
|
| 77 |
# Clear user's current image so they get a new one next time
|
| 78 |
session_data["current_image"] = None
|
|
@@ -116,4 +105,4 @@ with gr.Blocks() as demo:
|
|
| 116 |
# Load initial image
|
| 117 |
demo.load(fn=initialize_interface, inputs=session_data, outputs=[image, caption])
|
| 118 |
|
| 119 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
+
#Latest working version
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import threading
|
|
|
|
| 14 |
print("HUGGINGFACE_TOKEN environment variable not set.")
|
| 15 |
dataset_name = "GeorgeIbrahim/EGYCOCO" # Replace with your dataset name
|
| 16 |
|
| 17 |
+
# Load or create the dataset
|
| 18 |
try:
|
| 19 |
+
dataset = load_dataset(dataset_name, split="train")
|
| 20 |
+
print("Loaded existing dataset:", dataset)
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
+
# Create an empty dataset if it doesn't exist
|
| 23 |
features = Features({
|
| 24 |
'image_id': Value(dtype='string'),
|
| 25 |
'caption': Value(dtype='string'),
|
| 26 |
})
|
| 27 |
+
dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 28 |
+
dataset.push_to_hub(dataset_name) # Push the empty dataset to Hugging Face
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
image_folder = "images"
|
| 31 |
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
|
|
|
| 34 |
# Function to get a random image that hasn’t been annotated or skipped
|
| 35 |
def get_next_image(session_data):
|
| 36 |
with lock:
|
| 37 |
+
annotated_images = set(dataset["image_id"]) # Set of annotated images
|
| 38 |
available_images = [img for img in image_files if img not in annotated_images]
|
| 39 |
+
# Check if the user already has an image
|
| 40 |
if session_data["current_image"] is None and available_images:
|
| 41 |
# Assign a new random image to the user
|
| 42 |
session_data["current_image"] = random.choice(available_images)
|
| 43 |
return os.path.join(image_folder, session_data["current_image"]) if session_data["current_image"] else None
|
| 44 |
|
| 45 |
+
# Function to save the annotation to Hugging Face dataset and fetch the next image
|
| 46 |
def save_annotation(caption, session_data):
|
| 47 |
if session_data["current_image"] is None:
|
| 48 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
|
|
|
| 54 |
if caption.strip().lower() == "skip":
|
| 55 |
caption = "skipped"
|
| 56 |
|
| 57 |
+
# Add the new annotation as a new row to the dataset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
new_data = Dataset.from_dict({"image_id": [image_id], "caption": [caption]})
|
| 59 |
+
global dataset
|
| 60 |
+
dataset = concatenate_datasets([dataset, new_data])
|
| 61 |
|
| 62 |
+
# Save updated dataset to Hugging Face
|
| 63 |
+
dataset.push_to_hub(dataset_name)
|
| 64 |
+
print("Pushed updated dataset")
|
| 65 |
|
| 66 |
# Clear user's current image so they get a new one next time
|
| 67 |
session_data["current_image"] = None
|
|
|
|
| 105 |
# Load initial image
|
| 106 |
demo.load(fn=initialize_interface, inputs=session_data, outputs=[image, caption])
|
| 107 |
|
| 108 |
+
demo.launch(share=True)
|