GeorgeSherif commited on
Commit
ff00ea3
·
1 Parent(s): a7dc28d
Files changed (1) hide show
  1. app.py +17 -28
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 datasets
17
  try:
18
- train_dataset = load_dataset(dataset_name, split="train")
19
- val_dataset = load_dataset(dataset_name, split="validation")
20
- print("Loaded existing datasets:", train_dataset, val_dataset)
21
  except Exception as e:
22
- # Create empty datasets if they don't exist
23
  features = Features({
24
  'image_id': Value(dtype='string'),
25
  'caption': Value(dtype='string'),
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'))]
@@ -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(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!")
@@ -56,23 +54,14 @@ def save_annotation(caption, session_data):
56
  if caption.strip().lower() == "skip":
57
  caption = "skipped"
58
 
59
- # Determine the dataset split based on the image name
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
- 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
 
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)