Spaces:
Paused
Paused
backup
Browse files
app.py
CHANGED
|
@@ -151,7 +151,7 @@ def load_question_data(question_id):
|
|
| 151 |
|
| 152 |
def load_post_image(post_id):
|
| 153 |
if not post_id:
|
| 154 |
-
return [None] *
|
| 155 |
|
| 156 |
idx = POST_ID_TO_ID_MAP[post_id]
|
| 157 |
source_image = VALID_DATASET[idx]["image"]
|
|
@@ -165,21 +165,25 @@ def load_post_image(post_id):
|
|
| 165 |
metadata = json.load(f)
|
| 166 |
|
| 167 |
# Initialize response data
|
| 168 |
-
responses = [(None, "")] * 10
|
| 169 |
|
| 170 |
# Fill in existing responses
|
| 171 |
for response in metadata["responses"]:
|
| 172 |
idx = response["response_id"]
|
| 173 |
if idx < 10: # Ensure we don't exceed our UI limit
|
| 174 |
image_path = os.path.join(post_folder, response["image_path"])
|
| 175 |
-
responses[idx] = (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# Flatten responses for output
|
| 178 |
-
flat_responses = [item for
|
| 179 |
return [source_image] + flat_responses
|
| 180 |
|
| 181 |
# If no existing responses, return source image and empty responses
|
| 182 |
-
return [source_image] + [None] *
|
| 183 |
|
| 184 |
|
| 185 |
def generate_json_files(source_image, responses, post_id):
|
|
@@ -188,7 +192,7 @@ def generate_json_files(source_image, responses, post_id):
|
|
| 188 |
|
| 189 |
Args:
|
| 190 |
source_image: Path to the source image
|
| 191 |
-
responses: List of (image, answer) tuples
|
| 192 |
post_id: The post ID from the dataset
|
| 193 |
"""
|
| 194 |
# Create parent data folder if it doesn't exist
|
|
@@ -210,7 +214,7 @@ def generate_json_files(source_image, responses, post_id):
|
|
| 210 |
|
| 211 |
# Create responses data
|
| 212 |
responses_data = []
|
| 213 |
-
for idx, (response_image, answer_text) in enumerate(responses):
|
| 214 |
if response_image and answer_text: # Only process if both image and text exist
|
| 215 |
response_folder = os.path.join(post_folder, f"response_{idx}")
|
| 216 |
os.makedirs(response_folder)
|
|
@@ -227,6 +231,7 @@ def generate_json_files(source_image, responses, post_id):
|
|
| 227 |
{
|
| 228 |
"response_id": idx,
|
| 229 |
"answer_text": answer_text,
|
|
|
|
| 230 |
"image_path": f"response_{idx}/response_image.png",
|
| 231 |
}
|
| 232 |
)
|
|
@@ -266,7 +271,8 @@ with gr.Blocks() as demo:
|
|
| 266 |
with gr.Tab(f"Response {i+1}"):
|
| 267 |
img = gr.Image(label=f"Response Image {i+1}", type="filepath")
|
| 268 |
txt = gr.Textbox(label=f"Model Name {i+1}", lines=2)
|
| 269 |
-
|
|
|
|
| 270 |
|
| 271 |
with gr.Row():
|
| 272 |
submit_btn = gr.Button("Submit All Responses")
|
|
@@ -281,38 +287,45 @@ with gr.Blocks() as demo:
|
|
| 281 |
gr.Warning("Please select a post ID first!")
|
| 282 |
return
|
| 283 |
|
| 284 |
-
# Convert flat response_data into
|
| 285 |
-
|
|
|
|
|
|
|
| 286 |
|
| 287 |
-
# Filter out empty responses
|
| 288 |
valid_responses = [
|
| 289 |
-
(img, txt
|
|
|
|
|
|
|
| 290 |
]
|
| 291 |
|
| 292 |
if not valid_responses:
|
| 293 |
gr.Warning("Please provide at least one response (image + text)!")
|
| 294 |
return
|
| 295 |
|
|
|
|
| 296 |
generate_json_files(source_img, valid_responses, post_id)
|
| 297 |
gr.Info("Responses saved successfully! 🎉")
|
| 298 |
|
| 299 |
def clear_form():
|
| 300 |
-
outputs = [None] * (
|
|
|
|
|
|
|
| 301 |
return outputs
|
| 302 |
|
| 303 |
# Connect components
|
| 304 |
post_id_dropdown.change(
|
| 305 |
fn=load_post_image,
|
| 306 |
inputs=[post_id_dropdown],
|
| 307 |
-
outputs=[source_image] + [comp for
|
| 308 |
)
|
| 309 |
|
| 310 |
submit_inputs = [source_image, post_id_dropdown] + [
|
| 311 |
-
comp for
|
| 312 |
]
|
| 313 |
submit_btn.click(fn=submit_responses, inputs=submit_inputs)
|
| 314 |
|
| 315 |
-
clear_outputs = [source_image] + [comp for
|
| 316 |
clear_btn.click(fn=clear_form, outputs=clear_outputs)
|
| 317 |
|
| 318 |
|
|
|
|
| 151 |
|
| 152 |
def load_post_image(post_id):
|
| 153 |
if not post_id:
|
| 154 |
+
return [None] * 31 # source image + 10 triplets of (image, text, notes)
|
| 155 |
|
| 156 |
idx = POST_ID_TO_ID_MAP[post_id]
|
| 157 |
source_image = VALID_DATASET[idx]["image"]
|
|
|
|
| 165 |
metadata = json.load(f)
|
| 166 |
|
| 167 |
# Initialize response data
|
| 168 |
+
responses = [(None, "", "")] * 10 # Initialize with empty notes
|
| 169 |
|
| 170 |
# Fill in existing responses
|
| 171 |
for response in metadata["responses"]:
|
| 172 |
idx = response["response_id"]
|
| 173 |
if idx < 10: # Ensure we don't exceed our UI limit
|
| 174 |
image_path = os.path.join(post_folder, response["image_path"])
|
| 175 |
+
responses[idx] = (
|
| 176 |
+
image_path,
|
| 177 |
+
response["answer_text"],
|
| 178 |
+
response.get("notes", ""), # Get notes with empty string as default
|
| 179 |
+
)
|
| 180 |
|
| 181 |
# Flatten responses for output
|
| 182 |
+
flat_responses = [item for triplet in responses for item in triplet]
|
| 183 |
return [source_image] + flat_responses
|
| 184 |
|
| 185 |
# If no existing responses, return source image and empty responses
|
| 186 |
+
return [source_image] + [None] * 30
|
| 187 |
|
| 188 |
|
| 189 |
def generate_json_files(source_image, responses, post_id):
|
|
|
|
| 192 |
|
| 193 |
Args:
|
| 194 |
source_image: Path to the source image
|
| 195 |
+
responses: List of (image, answer, notes) tuples
|
| 196 |
post_id: The post ID from the dataset
|
| 197 |
"""
|
| 198 |
# Create parent data folder if it doesn't exist
|
|
|
|
| 214 |
|
| 215 |
# Create responses data
|
| 216 |
responses_data = []
|
| 217 |
+
for idx, (response_image, answer_text, notes) in enumerate(responses):
|
| 218 |
if response_image and answer_text: # Only process if both image and text exist
|
| 219 |
response_folder = os.path.join(post_folder, f"response_{idx}")
|
| 220 |
os.makedirs(response_folder)
|
|
|
|
| 231 |
{
|
| 232 |
"response_id": idx,
|
| 233 |
"answer_text": answer_text,
|
| 234 |
+
"notes": notes,
|
| 235 |
"image_path": f"response_{idx}/response_image.png",
|
| 236 |
}
|
| 237 |
)
|
|
|
|
| 271 |
with gr.Tab(f"Response {i+1}"):
|
| 272 |
img = gr.Image(label=f"Response Image {i+1}", type="filepath")
|
| 273 |
txt = gr.Textbox(label=f"Model Name {i+1}", lines=2)
|
| 274 |
+
notes = gr.Textbox(label=f"Miscellaneous Notes {i+1}", lines=3)
|
| 275 |
+
responses.append((img, txt, notes))
|
| 276 |
|
| 277 |
with gr.Row():
|
| 278 |
submit_btn = gr.Button("Submit All Responses")
|
|
|
|
| 287 |
gr.Warning("Please select a post ID first!")
|
| 288 |
return
|
| 289 |
|
| 290 |
+
# Convert flat response_data into triplets of (image, text, notes)
|
| 291 |
+
response_triplets = list(
|
| 292 |
+
zip(response_data[::3], response_data[1::3], response_data[2::3])
|
| 293 |
+
)
|
| 294 |
|
| 295 |
+
# Filter out empty responses (only checking image and model name, notes are optional)
|
| 296 |
valid_responses = [
|
| 297 |
+
(img, txt, notes)
|
| 298 |
+
for img, txt, notes in response_triplets
|
| 299 |
+
if img is not None and txt
|
| 300 |
]
|
| 301 |
|
| 302 |
if not valid_responses:
|
| 303 |
gr.Warning("Please provide at least one response (image + text)!")
|
| 304 |
return
|
| 305 |
|
| 306 |
+
# Modify generate_json_files call to handle notes
|
| 307 |
generate_json_files(source_img, valid_responses, post_id)
|
| 308 |
gr.Info("Responses saved successfully! 🎉")
|
| 309 |
|
| 310 |
def clear_form():
|
| 311 |
+
outputs = [None] * (
|
| 312 |
+
1 + 30
|
| 313 |
+
) # 1 source image + 10 triplets of (image, text, notes)
|
| 314 |
return outputs
|
| 315 |
|
| 316 |
# Connect components
|
| 317 |
post_id_dropdown.change(
|
| 318 |
fn=load_post_image,
|
| 319 |
inputs=[post_id_dropdown],
|
| 320 |
+
outputs=[source_image] + [comp for triplet in responses for comp in triplet],
|
| 321 |
)
|
| 322 |
|
| 323 |
submit_inputs = [source_image, post_id_dropdown] + [
|
| 324 |
+
comp for triplet in responses for comp in triplet
|
| 325 |
]
|
| 326 |
submit_btn.click(fn=submit_responses, inputs=submit_inputs)
|
| 327 |
|
| 328 |
+
clear_outputs = [source_image] + [comp for triplet in responses for comp in triplet]
|
| 329 |
clear_btn.click(fn=clear_form, outputs=clear_outputs)
|
| 330 |
|
| 331 |
|