Spaces:
Sleeping
Sleeping
GeorgeSherif commited on
Commit ·
9e13d0b
1
Parent(s): 3777099
handling users
Browse files
app.py
CHANGED
|
@@ -26,27 +26,28 @@ except Exception as e:
|
|
| 26 |
dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 27 |
dataset.push_to_hub(dataset_name) # Push the empty dataset to Hugging Face
|
| 28 |
|
| 29 |
-
image_folder = "
|
| 30 |
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
| 31 |
lock = threading.Lock()
|
| 32 |
-
current_image = None
|
| 33 |
|
| 34 |
# Function to get a random image that hasn’t been annotated or skipped
|
| 35 |
-
def get_next_image():
|
| 36 |
with lock:
|
| 37 |
-
|
| 38 |
-
if
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Function to save the annotation to Hugging Face dataset and fetch the next image
|
| 43 |
-
def save_annotation(caption):
|
| 44 |
-
|
| 45 |
-
if current_image is None:
|
| 46 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
| 47 |
|
| 48 |
with lock:
|
| 49 |
-
image_id =
|
| 50 |
|
| 51 |
# Save caption or "skipped" based on user input
|
| 52 |
if caption.strip().lower() == "skip":
|
|
@@ -61,23 +62,25 @@ def save_annotation(caption):
|
|
| 61 |
dataset.push_to_hub(dataset_name)
|
| 62 |
print("Pushed updated dataset")
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
# Fetch the next image
|
| 65 |
-
|
| 66 |
-
if
|
| 67 |
-
return gr.update(value=
|
| 68 |
else:
|
| 69 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
| 70 |
|
| 71 |
# Function to skip the current image
|
| 72 |
-
def skip_image():
|
| 73 |
-
return save_annotation("skip")
|
| 74 |
|
| 75 |
# Function to initialize the interface
|
| 76 |
-
def initialize_interface():
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
return gr.update(value=current_image), gr.update(value="")
|
| 81 |
else:
|
| 82 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
| 83 |
|
|
@@ -86,6 +89,8 @@ with gr.Blocks() as demo:
|
|
| 86 |
gr.Markdown("# Image Captioning Tool")
|
| 87 |
gr.Markdown("Please provide a caption for each image displayed. Click 'Submit' after writing your caption, or type 'skip' if you don’t want to annotate this image.")
|
| 88 |
|
|
|
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
image = gr.Image()
|
| 91 |
caption = gr.Textbox(placeholder="Enter caption here...")
|
|
@@ -93,10 +98,10 @@ with gr.Blocks() as demo:
|
|
| 93 |
skip = gr.Button("Skip") # Skip button
|
| 94 |
|
| 95 |
# Define actions for buttons
|
| 96 |
-
submit.click(fn=save_annotation, inputs=caption, outputs=[image, caption])
|
| 97 |
-
skip.click(fn=skip_image, inputs=
|
| 98 |
|
| 99 |
# Load initial image
|
| 100 |
-
demo.load(fn=initialize_interface, inputs=
|
| 101 |
|
| 102 |
demo.launch(share=True)
|
|
|
|
| 26 |
dataset = Dataset.from_dict({'image_id': [], 'caption': []}, features=features)
|
| 27 |
dataset.push_to_hub(dataset_name) # Push the empty dataset to Hugging Face
|
| 28 |
|
| 29 |
+
image_folder = "test"
|
| 30 |
image_files = [f for f in os.listdir(image_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
| 31 |
lock = threading.Lock()
|
|
|
|
| 32 |
|
| 33 |
# Function to get a random image that hasn’t been annotated or skipped
|
| 34 |
+
def get_next_image(session_data):
|
| 35 |
with lock:
|
| 36 |
+
annotated_images = set(dataset["image_id"]) # Set of annotated images
|
| 37 |
+
available_images = [img for img in image_files if img not in annotated_images]
|
| 38 |
+
# Check if the user already has an image
|
| 39 |
+
if session_data["current_image"] is None and available_images:
|
| 40 |
+
# Assign a new random image to the user
|
| 41 |
+
session_data["current_image"] = random.choice(available_images)
|
| 42 |
+
return os.path.join(image_folder, session_data["current_image"]) if session_data["current_image"] else None
|
| 43 |
|
| 44 |
# Function to save the annotation to Hugging Face dataset and fetch the next image
|
| 45 |
+
def save_annotation(caption, session_data):
|
| 46 |
+
if session_data["current_image"] is None:
|
|
|
|
| 47 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
| 48 |
|
| 49 |
with lock:
|
| 50 |
+
image_id = session_data["current_image"]
|
| 51 |
|
| 52 |
# Save caption or "skipped" based on user input
|
| 53 |
if caption.strip().lower() == "skip":
|
|
|
|
| 62 |
dataset.push_to_hub(dataset_name)
|
| 63 |
print("Pushed updated dataset")
|
| 64 |
|
| 65 |
+
# Clear user's current image so they get a new one next time
|
| 66 |
+
session_data["current_image"] = None
|
| 67 |
+
|
| 68 |
# Fetch the next image
|
| 69 |
+
next_image = get_next_image(session_data)
|
| 70 |
+
if next_image:
|
| 71 |
+
return gr.update(value=next_image), gr.update(value="")
|
| 72 |
else:
|
| 73 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
| 74 |
|
| 75 |
# Function to skip the current image
|
| 76 |
+
def skip_image(session_data):
|
| 77 |
+
return save_annotation("skip", session_data)
|
| 78 |
|
| 79 |
# Function to initialize the interface
|
| 80 |
+
def initialize_interface(session_data):
|
| 81 |
+
next_image = get_next_image(session_data)
|
| 82 |
+
if next_image:
|
| 83 |
+
return gr.update(value=next_image), gr.update(value="")
|
|
|
|
| 84 |
else:
|
| 85 |
return gr.update(visible=False), gr.update(value="All images have been annotated!")
|
| 86 |
|
|
|
|
| 89 |
gr.Markdown("# Image Captioning Tool")
|
| 90 |
gr.Markdown("Please provide a caption for each image displayed. Click 'Submit' after writing your caption, or type 'skip' if you don’t want to annotate this image.")
|
| 91 |
|
| 92 |
+
session_data = gr.State({"current_image": None}) # Session-specific state
|
| 93 |
+
|
| 94 |
with gr.Row():
|
| 95 |
image = gr.Image()
|
| 96 |
caption = gr.Textbox(placeholder="Enter caption here...")
|
|
|
|
| 98 |
skip = gr.Button("Skip") # Skip button
|
| 99 |
|
| 100 |
# Define actions for buttons
|
| 101 |
+
submit.click(fn=save_annotation, inputs=[caption, session_data], outputs=[image, caption])
|
| 102 |
+
skip.click(fn=skip_image, inputs=session_data, outputs=[image, caption])
|
| 103 |
|
| 104 |
# Load initial image
|
| 105 |
+
demo.load(fn=initialize_interface, inputs=session_data, outputs=[image, caption])
|
| 106 |
|
| 107 |
demo.launch(share=True)
|