Spaces:
Paused
Paused
File size: 11,982 Bytes
04e8185 a55e212 7f8f3ca a55e212 04e8185 7f8f3ca 04e8185 7f8f3ca 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 3fe488b 04e8185 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
import gradio as gr
import base64
import json
import os
import shutil
import uuid
import glob
from huggingface_hub import CommitScheduler, HfApi, snapshot_download
from pathlib import Path
import git
from datasets import Dataset, Features, Value, Sequence, Image as ImageFeature
import threading
import time
from utils import process_and_push_dataset
from datasets import load_dataset
api = HfApi(token=os.environ["HF_TOKEN"])
VALID_DATASET = load_dataset("taesiri/IERv2-Subset", split="train")
VALID_DATASET_POST_IDS = (
load_dataset("taesiri/IERv2-Subset", split="train", columns=["post_id"])
.to_pandas()["post_id"]
.tolist()
)
POST_ID_TO_ID_MAP = {post_id: idx for idx, post_id in enumerate(VALID_DATASET_POST_IDS)}
DATASET_REPO = "taesiri/AIImageEditingResults_Intemediate"
FINAL_DATASET_REPO = "taesiri/AIImageEditingResults"
# Download existing data from hub
def sync_with_hub():
"""
Synchronize local data with the hub by cloning the dataset repo
"""
print("Starting sync with hub...")
data_dir = Path("./data")
if data_dir.exists():
# Backup existing data
backup_dir = Path("./data_backup")
if backup_dir.exists():
shutil.rmtree(backup_dir)
shutil.copytree(data_dir, backup_dir)
# Clone/pull latest data from hub
# Use token in the URL for authentication following HF's new format
token = os.environ["HF_TOKEN"]
username = "taesiri" # Extract from DATASET_REPO
repo_url = f"https://{username}:{token}@huggingface.co/datasets/{DATASET_REPO}"
hub_data_dir = Path("hub_data")
if hub_data_dir.exists():
# If repo exists, do a git pull
print("Pulling latest changes...")
repo = git.Repo(hub_data_dir)
origin = repo.remotes.origin
# Set the new URL with token
if "https://" in origin.url:
origin.set_url(repo_url)
origin.pull()
else:
# Clone the repo with token
print("Cloning repository...")
git.Repo.clone_from(repo_url, hub_data_dir)
# Merge hub data with local data
hub_data_source = hub_data_dir / "data"
if hub_data_source.exists():
# Create data dir if it doesn't exist
data_dir.mkdir(exist_ok=True)
# Copy files from hub
for item in hub_data_source.glob("*"):
if item.is_dir():
dest = data_dir / item.name
if not dest.exists(): # Only copy if doesn't exist locally
shutil.copytree(item, dest)
# Clean up cloned repo
if hub_data_dir.exists():
shutil.rmtree(hub_data_dir)
print("Finished syncing with hub!")
scheduler = CommitScheduler(
repo_id=DATASET_REPO,
repo_type="dataset",
folder_path="./data",
path_in_repo="data",
every=1,
)
def load_question_data(question_id):
"""
Load a specific question's data
Returns a tuple of all form fields
"""
if not question_id:
return [None] * 11 # Reduced number of fields
# Extract the ID part before the colon from the dropdown selection
question_id = (
question_id.split(":")[0].strip() if ":" in question_id else question_id
)
json_path = os.path.join("./data", question_id, "question.json")
if not os.path.exists(json_path):
print(f"Question file not found: {json_path}")
return [None] * 11
try:
with open(json_path, "r", encoding="utf-8") as f:
data = json.loads(f.read().strip())
# Load images
def load_image(image_path):
if not image_path:
return None
full_path = os.path.join(
"./data", question_id, os.path.basename(image_path)
)
return full_path if os.path.exists(full_path) else None
question_images = data.get("question_images", [])
rationale_images = data.get("rationale_images", [])
return [
(
",".join(data["question_categories"])
if isinstance(data["question_categories"], list)
else data["question_categories"]
),
data["question"],
data["final_answer"],
data.get("rationale_text", ""),
load_image(question_images[0] if question_images else None),
load_image(question_images[1] if len(question_images) > 1 else None),
load_image(question_images[2] if len(question_images) > 2 else None),
load_image(question_images[3] if len(question_images) > 3 else None),
load_image(rationale_images[0] if rationale_images else None),
load_image(rationale_images[1] if len(rationale_images) > 1 else None),
question_id,
]
except Exception as e:
print(f"Error loading question {question_id}: {str(e)}")
return [None] * 11
def load_post_image(post_id):
if not post_id:
return [None] * 31 # source image + 10 triplets of (image, text, notes)
idx = POST_ID_TO_ID_MAP[post_id]
source_image = VALID_DATASET[idx]["image"]
# Load existing responses if any
post_folder = os.path.join("./data", str(post_id))
metadata_path = os.path.join(post_folder, "metadata.json")
if os.path.exists(metadata_path):
with open(metadata_path, "r") as f:
metadata = json.load(f)
# Initialize response data
responses = [(None, "", "")] * 10 # Initialize with empty notes
# Fill in existing responses
for response in metadata["responses"]:
idx = response["response_id"]
if idx < 10: # Ensure we don't exceed our UI limit
image_path = os.path.join(post_folder, response["image_path"])
responses[idx] = (
image_path,
response["answer_text"],
response.get("notes", ""), # Get notes with empty string as default
)
# Flatten responses for output
flat_responses = [item for triplet in responses for item in triplet]
return [source_image] + flat_responses
# If no existing responses, return source image and empty responses
return [source_image] + [None] * 30
def generate_json_files(source_image, responses, post_id):
"""
Save the source image and multiple responses to the data directory
Args:
source_image: Path to the source image
responses: List of (image, answer, notes) tuples
post_id: The post ID from the dataset
"""
# Create parent data folder if it doesn't exist
parent_data_folder = "./data"
os.makedirs(parent_data_folder, exist_ok=True)
# Create/clear post_id folder
post_folder = os.path.join(parent_data_folder, str(post_id))
if os.path.exists(post_folder):
shutil.rmtree(post_folder)
os.makedirs(post_folder)
# Save source image
source_image_path = os.path.join(post_folder, "source_image.png")
if isinstance(source_image, str):
shutil.copy2(source_image, source_image_path)
else:
gr.processing_utils.save_image(source_image, source_image_path)
# Create responses data
responses_data = []
for idx, (response_image, answer_text, notes) in enumerate(responses):
if response_image and answer_text: # Only process if both image and text exist
response_folder = os.path.join(post_folder, f"response_{idx}")
os.makedirs(response_folder)
# Save response image
response_image_path = os.path.join(response_folder, "response_image.png")
if isinstance(response_image, str):
shutil.copy2(response_image, response_image_path)
else:
gr.processing_utils.save_image(response_image, response_image_path)
# Add to responses data
responses_data.append(
{
"response_id": idx,
"answer_text": answer_text,
"notes": notes,
"image_path": f"response_{idx}/response_image.png",
}
)
# Create metadata JSON
metadata = {
"post_id": post_id,
"source_image": "source_image.png",
"responses": responses_data,
}
# Save metadata
with open(os.path.join(post_folder, "metadata.json"), "w", encoding="utf-8") as f:
json.dump(metadata, f, ensure_ascii=False, indent=2)
return post_folder
# Build the Gradio app
with gr.Blocks() as demo:
gr.Markdown("# Image Response Collector")
# Source image selection at the top
with gr.Column():
post_id_dropdown = gr.Dropdown(
label="Select Post ID to Load Image",
choices=VALID_DATASET_POST_IDS,
type="value",
allow_custom_value=False,
)
source_image = gr.Image(label="Source Image", type="filepath")
# Responses in tabs
with gr.Tabs() as response_tabs:
responses = []
for i in range(10):
with gr.Tab(f"Response {i+1}"):
img = gr.Image(label=f"Response Image {i+1}", type="filepath")
txt = gr.Textbox(label=f"Model Name {i+1}", lines=2)
notes = gr.Textbox(label=f"Miscellaneous Notes {i+1}", lines=3)
responses.append((img, txt, notes))
with gr.Row():
submit_btn = gr.Button("Submit All Responses")
clear_btn = gr.Button("Clear Form")
def submit_responses(source_img, post_id, *response_data):
if not source_img:
gr.Warning("Please select a source image first!")
return
if not post_id:
gr.Warning("Please select a post ID first!")
return
# Convert flat response_data into triplets of (image, text, notes)
response_triplets = list(
zip(response_data[::3], response_data[1::3], response_data[2::3])
)
# Filter out empty responses (only checking image and model name, notes are optional)
valid_responses = [
(img, txt, notes)
for img, txt, notes in response_triplets
if img is not None and txt
]
if not valid_responses:
gr.Warning("Please provide at least one response (image + text)!")
return
# Modify generate_json_files call to handle notes
generate_json_files(source_img, valid_responses, post_id)
gr.Info("Responses saved successfully! 🎉")
def clear_form():
outputs = [None] * (
1 + 30
) # 1 source image + 10 triplets of (image, text, notes)
return outputs
# Connect components
post_id_dropdown.change(
fn=load_post_image,
inputs=[post_id_dropdown],
outputs=[source_image] + [comp for triplet in responses for comp in triplet],
)
submit_inputs = [source_image, post_id_dropdown] + [
comp for triplet in responses for comp in triplet
]
submit_btn.click(fn=submit_responses, inputs=submit_inputs)
clear_outputs = [source_image] + [comp for triplet in responses for comp in triplet]
clear_btn.click(fn=clear_form, outputs=clear_outputs)
def process_thread():
while True:
try:
pass
# process_and_push_dataset(
# "./data",
# FINAL_DATASET_REPO,
# token=os.environ["HF_TOKEN"],
# private=True,
# )
except Exception as e:
print(f"Error in process thread: {e}")
time.sleep(120) # Sleep for 2 minutes
if __name__ == "__main__":
print("Initializing app...")
sync_with_hub() # Sync before launching the app
print("Starting Gradio interface...")
# Start the processing thread when the app starts
processing_thread = threading.Thread(target=process_thread, daemon=True)
processing_thread.start()
demo.launch()
|