taesiri commited on
Commit
602a574
·
1 Parent(s): 0457a29
Files changed (1) hide show
  1. app.py +72 -0
app.py CHANGED
@@ -254,6 +254,62 @@ def generate_json_files(source_image, responses, post_id):
254
  return post_folder
255
 
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  # Build the Gradio app
258
  with gr.Blocks() as demo:
259
  gr.Markdown("# Image Response Collector")
@@ -289,6 +345,22 @@ with gr.Blocks() as demo:
289
  submit_btn = gr.Button("Submit All Responses")
290
  clear_btn = gr.Button("Clear Form")
291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  def submit_responses(
293
  source_img, post_id, instruction, simplified_instruction, *response_data
294
  ):
 
254
  return post_folder
255
 
256
 
257
+ def get_statistics():
258
+ """
259
+ Scan the data folder and return statistics about the responses
260
+ """
261
+ data_dir = Path("./data")
262
+ if not data_dir.exists():
263
+ return "No data directory found"
264
+
265
+ # Count folders with metadata.json
266
+ total_posts = 0
267
+ posts_with_responses = 0
268
+ total_responses = 0
269
+ responses_per_post = [] # List to track number of responses for each post
270
+
271
+ for metadata_file in data_dir.glob("*/metadata.json"):
272
+ total_posts += 1
273
+ try:
274
+ with open(metadata_file, "r") as f:
275
+ metadata = json.load(f)
276
+ num_responses = len(metadata.get("responses", []))
277
+ responses_per_post.append(num_responses)
278
+ if num_responses > 0:
279
+ posts_with_responses += 1
280
+ total_responses += num_responses
281
+ except:
282
+ continue
283
+
284
+ # Calculate additional statistics
285
+ if responses_per_post:
286
+ responses_per_post.sort()
287
+ median_responses = responses_per_post[len(responses_per_post) // 2]
288
+ max_responses = max(responses_per_post)
289
+ avg_responses = (
290
+ total_responses / posts_with_responses if posts_with_responses > 0 else 0
291
+ )
292
+ else:
293
+ median_responses = max_responses = avg_responses = 0
294
+
295
+ stats = f"""
296
+ 📊 Collection Statistics:
297
+
298
+ Overall Progress:
299
+ - Total Posts Processed: {total_posts}
300
+ - Posts with Responses: {posts_with_responses}
301
+ - Total Individual Responses: {total_responses}
302
+ - Completion Rate: {(posts_with_responses/len(VALID_DATASET_POST_IDS)*100):.2f}%
303
+
304
+ Response Distribution:
305
+ - Median Responses per Post: {median_responses}
306
+ - Average Responses per Post: {avg_responses:.2f}
307
+ - Maximum Responses for a Post: {max_responses}
308
+ - Posts with No Responses: {total_posts - posts_with_responses}
309
+ """
310
+ return stats
311
+
312
+
313
  # Build the Gradio app
314
  with gr.Blocks() as demo:
315
  gr.Markdown("# Image Response Collector")
 
345
  submit_btn = gr.Button("Submit All Responses")
346
  clear_btn = gr.Button("Clear Form")
347
 
348
+ # Add statistics accordion
349
+ with gr.Accordion("Collection Statistics", open=False):
350
+ stats_text = gr.Markdown("Loading statistics...")
351
+ refresh_stats_btn = gr.Button("Refresh Statistics")
352
+
353
+ def update_stats():
354
+ return get_statistics()
355
+
356
+ refresh_stats_btn.click(fn=update_stats, outputs=[stats_text])
357
+
358
+ # Move the load event inside the Blocks context
359
+ demo.load(
360
+ fn=get_statistics,
361
+ outputs=[stats_text],
362
+ )
363
+
364
  def submit_responses(
365
  source_img, post_id, instruction, simplified_instruction, *response_data
366
  ):