Chaitanya-aitf commited on
Commit
a1b611f
·
verified ·
1 Parent(s): 8556570

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -7
app.py CHANGED
@@ -33,6 +33,92 @@ except Exception:
33
  logger = logging.getLogger("app")
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def process_video(
37
  video_file,
38
  domain,
@@ -55,10 +141,10 @@ def process_video(
55
  progress: Gradio progress tracker
56
 
57
  Returns:
58
- Tuple of (status_message, clip1, clip2, clip3, log_text)
59
  """
60
  if video_file is None:
61
- return "Please upload a video first.", None, None, None, ""
62
 
63
  log_messages = []
64
 
@@ -78,7 +164,7 @@ def process_video(
78
  # Validate video
79
  validation = validate_video_file(video_file)
80
  if not validation.is_valid:
81
- return f"Error: {validation.error_message}", None, None, None, "\n".join(log_messages)
82
 
83
  log(f"Video size: {validation.file_size / (1024*1024):.1f} MB")
84
 
@@ -151,6 +237,10 @@ def process_video(
151
  log(f"Clip {i+1}: {format_duration(clip.start_time)} - {format_duration(clip.end_time)} (score: {clip.hype_score:.2f})")
152
 
153
  status = f"Successfully extracted {len(clip_paths)} highlight clips!\nProcessing time: {result.processing_time:.1f}s"
 
 
 
 
154
  pipeline.cleanup()
155
  progress(1.0, desc="Done!")
156
 
@@ -159,18 +249,18 @@ def process_video(
159
  clip2 = clip_paths[1] if len(clip_paths) > 1 else None
160
  clip3 = clip_paths[2] if len(clip_paths) > 2 else None
161
 
162
- return status, clip1, clip2, clip3, "\n".join(log_messages)
163
  else:
164
  log(f"Processing failed: {result.error_message}")
165
  pipeline.cleanup()
166
- return f"Error: {result.error_message}", None, None, None, "\n".join(log_messages)
167
 
168
  except Exception as e:
169
  error_msg = f"Unexpected error: {str(e)}"
170
  log(error_msg)
171
  log(traceback.format_exc())
172
  logger.exception("Pipeline error")
173
- return error_msg, None, None, None, "\n".join(log_messages)
174
 
175
 
176
  # Build Gradio interface
@@ -270,6 +360,15 @@ with gr.Blocks(
270
  show_copy_button=True
271
  )
272
 
 
 
 
 
 
 
 
 
 
273
  gr.Markdown("""
274
  ---
275
  **ShortSmith v2** | Powered by Qwen2-VL, InsightFace, and Librosa |
@@ -292,7 +391,8 @@ with gr.Blocks(
292
  clip1_output,
293
  clip2_output,
294
  clip3_output,
295
- log_output
 
296
  ],
297
  show_progress="full"
298
  )
 
33
  logger = logging.getLogger("app")
34
 
35
 
36
+ def build_metrics_output(result, domain: str) -> str:
37
+ """
38
+ Build formatted metrics output for testing and evaluation.
39
+
40
+ Args:
41
+ result: PipelineResult object
42
+ domain: Content domain used for processing
43
+
44
+ Returns:
45
+ Formatted string with all metrics
46
+ """
47
+ lines = []
48
+ lines.append("=" * 50)
49
+ lines.append("AUTOMATED METRICS (System-Generated)")
50
+ lines.append("=" * 50)
51
+ lines.append("")
52
+
53
+ # Processing Metrics
54
+ lines.append("PROCESSING METRICS")
55
+ lines.append("-" * 30)
56
+ lines.append(f"processing_time_seconds: {result.processing_time:.2f}")
57
+ lines.append(f"frames_analyzed: {len(result.visual_features)}")
58
+ lines.append(f"scenes_detected: {len(result.scenes)}")
59
+ lines.append(f"audio_segments_analyzed: {len(result.audio_features)}")
60
+ lines.append(f"domain: {domain}")
61
+
62
+ # Count hooks from scores (estimate based on high-scoring segments)
63
+ hooks_detected = sum(1 for s in result.scores if s.combined_score > 0.7) if result.scores else 0
64
+ lines.append(f"hooks_detected: {hooks_detected}")
65
+
66
+ if result.metadata:
67
+ lines.append(f"video_duration_seconds: {result.metadata.duration:.2f}")
68
+ lines.append(f"video_resolution: {result.metadata.resolution}")
69
+ lines.append(f"video_fps: {result.metadata.fps:.2f}")
70
+
71
+ lines.append("")
72
+
73
+ # Per Clip Metrics
74
+ lines.append("PER CLIP METRICS")
75
+ lines.append("-" * 30)
76
+
77
+ for i, clip in enumerate(result.clips):
78
+ lines.append("")
79
+ lines.append(f"[Clip {i + 1}]")
80
+ lines.append(f" clip_id: {i + 1}")
81
+ lines.append(f" start_time: {clip.start_time:.2f}")
82
+ lines.append(f" end_time: {clip.end_time:.2f}")
83
+ lines.append(f" duration: {clip.duration:.2f}")
84
+ lines.append(f" hype_score: {clip.hype_score:.4f}")
85
+ lines.append(f" visual_score: {clip.visual_score:.4f}")
86
+ lines.append(f" audio_score: {clip.audio_score:.4f}")
87
+ lines.append(f" motion_score: {clip.motion_score:.4f}")
88
+
89
+ # Hook info - derive from segment scores if available
90
+ hook_type = "none"
91
+ hook_confidence = 0.0
92
+
93
+ # Find matching segment score for this clip
94
+ for score in result.scores:
95
+ if abs(score.start_time - clip.start_time) < 1.0:
96
+ if score.combined_score > 0.7:
97
+ hook_confidence = score.combined_score
98
+ # Infer hook type based on dominant score
99
+ if score.audio_score > score.visual_score and score.audio_score > score.motion_score:
100
+ hook_type = "audio_peak"
101
+ elif score.motion_score > score.visual_score:
102
+ hook_type = "motion_spike"
103
+ else:
104
+ hook_type = "visual_highlight"
105
+ break
106
+
107
+ lines.append(f" hook_type: {hook_type}")
108
+ lines.append(f" hook_confidence: {hook_confidence:.4f}")
109
+
110
+ if clip.person_detected:
111
+ lines.append(f" person_detected: True")
112
+ lines.append(f" person_screen_time: {clip.person_screen_time:.4f}")
113
+
114
+ lines.append("")
115
+ lines.append("=" * 50)
116
+ lines.append("END METRICS")
117
+ lines.append("=" * 50)
118
+
119
+ return "\n".join(lines)
120
+
121
+
122
  def process_video(
123
  video_file,
124
  domain,
 
141
  progress: Gradio progress tracker
142
 
143
  Returns:
144
+ Tuple of (status_message, clip1, clip2, clip3, log_text, metrics_text)
145
  """
146
  if video_file is None:
147
+ return "Please upload a video first.", None, None, None, "", ""
148
 
149
  log_messages = []
150
 
 
164
  # Validate video
165
  validation = validate_video_file(video_file)
166
  if not validation.is_valid:
167
+ return f"Error: {validation.error_message}", None, None, None, "\n".join(log_messages), ""
168
 
169
  log(f"Video size: {validation.file_size / (1024*1024):.1f} MB")
170
 
 
237
  log(f"Clip {i+1}: {format_duration(clip.start_time)} - {format_duration(clip.end_time)} (score: {clip.hype_score:.2f})")
238
 
239
  status = f"Successfully extracted {len(clip_paths)} highlight clips!\nProcessing time: {result.processing_time:.1f}s"
240
+
241
+ # Build metrics output
242
+ metrics_output = build_metrics_output(result, domain_value)
243
+
244
  pipeline.cleanup()
245
  progress(1.0, desc="Done!")
246
 
 
249
  clip2 = clip_paths[1] if len(clip_paths) > 1 else None
250
  clip3 = clip_paths[2] if len(clip_paths) > 2 else None
251
 
252
+ return status, clip1, clip2, clip3, "\n".join(log_messages), metrics_output
253
  else:
254
  log(f"Processing failed: {result.error_message}")
255
  pipeline.cleanup()
256
+ return f"Error: {result.error_message}", None, None, None, "\n".join(log_messages), ""
257
 
258
  except Exception as e:
259
  error_msg = f"Unexpected error: {str(e)}"
260
  log(error_msg)
261
  log(traceback.format_exc())
262
  logger.exception("Pipeline error")
263
+ return error_msg, None, None, None, "\n".join(log_messages), ""
264
 
265
 
266
  # Build Gradio interface
 
360
  show_copy_button=True
361
  )
362
 
363
+ with gr.Accordion("📊 Automated Metrics (System-Generated)", open=True):
364
+ metrics_output = gr.Textbox(
365
+ label="Metrics for Testing",
366
+ lines=20,
367
+ interactive=False,
368
+ show_copy_button=True,
369
+ info="Copy these metrics for evaluation spreadsheets"
370
+ )
371
+
372
  gr.Markdown("""
373
  ---
374
  **ShortSmith v2** | Powered by Qwen2-VL, InsightFace, and Librosa |
 
391
  clip1_output,
392
  clip2_output,
393
  clip3_output,
394
+ log_output,
395
+ metrics_output
396
  ],
397
  show_progress="full"
398
  )