defalt-here commited on
Commit
368313b
·
verified ·
1 Parent(s): 45a2b85

Updated html video viewer to gradio video viewer

Browse files
Files changed (1) hide show
  1. app.py +98 -41
app.py CHANGED
@@ -158,10 +158,20 @@ def process_video(
158
  progress: Gradio progress tracker
159
 
160
  Returns:
161
- Tuple of (status_message, clip1, clip2, clip3, log_text, metrics_text)
162
  """
163
  if video_file is None:
164
- return "Please upload a video first.", None, None, None, "", ""
 
 
 
 
 
 
 
 
 
 
165
 
166
  log_messages = []
167
 
@@ -181,7 +191,17 @@ def process_video(
181
  # Validate video
182
  validation = validate_video_file(video_file)
183
  if not validation.is_valid:
184
- return f"Error: {validation.error_message}", None, None, None, "\n".join(log_messages), ""
 
 
 
 
 
 
 
 
 
 
185
 
186
  log(f"Video size: {validation.file_size / (1024*1024):.1f} MB")
187
 
@@ -192,22 +212,14 @@ def process_video(
192
  log("TEST MODE: Skipping processing, showing mock highlights")
193
  progress(0.5, desc="Generating test highlights...")
194
 
195
- # Build dynamic HTML for mock highlights
196
- clips_html = ""
197
- for i in range(int(num_clips)):
198
- clips_html += f'''
199
- <div style="margin-bottom: 20px; border: 1px solid #ccc; border-radius: 8px; padding: 15px; background-color: #0b1619;">
200
- <h4 style="margin-top: 0; color: #f2fafc;">Highlight Clip {i+1}</h4>
201
- <div style="margin-bottom: 10px; font-size: 14px; color: #f2fafc;">
202
- <strong>Time:</strong> {i*30}-{i*30+45}s |
203
- <strong>Score:</strong> {95-i*5:.1f} |
204
- <strong>Domain:</strong> {domain}
205
- </div>
206
- <video width="100%" controls style="border-radius: 4px;">
207
- <source src="{video_file}" type="video/mp4">
208
- </video>
209
- </div>
210
- '''
211
 
212
  status = f"TEST MODE: Generated {int(num_clips)} mock highlight clips from {Path(video_file).name}"
213
  metrics_output = f"""
@@ -221,7 +233,17 @@ Test Mode Metrics:
221
  """.strip()
222
 
223
  progress(1.0, desc="Test complete!")
224
- return status, clips_html, "\n".join(log_messages), metrics_output, ""
 
 
 
 
 
 
 
 
 
 
225
  ###----------------------------------------------------------------------------------------------------------------------
226
  # Validate reference image if provided
227
  ref_path = None
@@ -304,35 +326,57 @@ Test Mode Metrics:
304
  pipeline.cleanup()
305
  progress(1.0, desc="Done!")
306
 
307
- # Build dynamic HTML for clips with extracted highlights
308
- clips_html = ""
309
- for i, clip_path in enumerate(clip_paths):
310
- clips_html += f'''
311
- <div style="margin-bottom: 20px; border: 1px solid #ddd; border-radius: 8px; padding: 15px; background-color: #f9f9f9;">
312
- <h4 style="margin-top: 0; color: #2e7d32;">Highlight Clip {i+1}</h4>
313
- <div style="margin-bottom: 10px; font-size: 14px; color: #666;">
314
- <strong>Time:</strong> {format_duration(result.clips[i].start_time)} - {format_duration(result.clips[i].end_time)} |
315
- <strong>Score:</strong> {result.clips[i].hype_score:.2f} |
316
- <strong>Domain:</strong> {domain_value}
317
- </div>
318
- <video width="100%" controls style="border-radius: 4px;">
319
- <source src="{clip_path}" type="video/mp4">
320
- </video>
321
- </div>
322
- '''
323
-
324
- return status, clips_html, "\n".join(log_messages), metrics_output, ""
 
 
325
  else:
326
  log(f"Processing failed: {result.error_message}")
327
  pipeline.cleanup()
328
- return f"Error: {result.error_message}", "", "\n".join(log_messages), "", ""
 
 
 
 
 
 
 
 
 
 
329
 
330
  except Exception as e:
331
  error_msg = f"Unexpected error: {str(e)}"
332
  log(error_msg)
333
  log(traceback.format_exc())
334
  logger.exception("Pipeline error")
335
- return error_msg, "", "\n".join(log_messages), "", ""
 
 
 
 
 
 
 
 
 
 
336
 
337
 
338
  # =============================================================================
@@ -865,6 +909,14 @@ with gr.Blocks(
865
  gr.Markdown("#### Extracted Clips")
866
  clips_output = gr.HTML(label="Extracted Clips")
867
 
 
 
 
 
 
 
 
 
868
  with gr.Accordion("Processing Log", open=True):
869
  log_output = gr.Textbox(
870
  label="Log",
@@ -896,8 +948,13 @@ with gr.Blocks(
896
  outputs=[
897
  status_output,
898
  clips_output,
 
 
 
 
 
899
  log_output,
900
- metrics_output
901
  ],
902
  show_progress="full"
903
  )
 
158
  progress: Gradio progress tracker
159
 
160
  Returns:
161
+ Tuple of (status_message, clips_html_update, clip1, clip2, clip3, clip4, clip5, log_text, metrics_text)
162
  """
163
  if video_file is None:
164
+ return (
165
+ "Please upload a video first.",
166
+ gr.update(value="", visible=False),
167
+ gr.update(value=None, visible=False),
168
+ gr.update(value=None, visible=False),
169
+ gr.update(value=None, visible=False),
170
+ gr.update(value=None, visible=False),
171
+ gr.update(value=None, visible=False),
172
+ "",
173
+ "",
174
+ )
175
 
176
  log_messages = []
177
 
 
191
  # Validate video
192
  validation = validate_video_file(video_file)
193
  if not validation.is_valid:
194
+ return (
195
+ f"Error: {validation.error_message}",
196
+ gr.update(value="", visible=False),
197
+ gr.update(value=None, visible=False),
198
+ gr.update(value=None, visible=False),
199
+ gr.update(value=None, visible=False),
200
+ gr.update(value=None, visible=False),
201
+ gr.update(value=None, visible=False),
202
+ "\n".join(log_messages),
203
+ "",
204
+ )
205
 
206
  log(f"Video size: {validation.file_size / (1024*1024):.1f} MB")
207
 
 
212
  log("TEST MODE: Skipping processing, showing mock highlights")
213
  progress(0.5, desc="Generating test highlights...")
214
 
215
+ # Prepare updates: show only the requested number of clips (use the uploaded video as source)
216
+ clip_updates = []
217
+ requested = min(int(num_clips), 5)
218
+ for i in range(5):
219
+ if i < requested:
220
+ clip_updates.append(gr.update(value=str(video_path), visible=True))
221
+ else:
222
+ clip_updates.append(gr.update(value=None, visible=False))
 
 
 
 
 
 
 
 
223
 
224
  status = f"TEST MODE: Generated {int(num_clips)} mock highlight clips from {Path(video_file).name}"
225
  metrics_output = f"""
 
233
  """.strip()
234
 
235
  progress(1.0, desc="Test complete!")
236
+ return (
237
+ status,
238
+ gr.update(value="", visible=False),
239
+ clip_updates[0],
240
+ clip_updates[1],
241
+ clip_updates[2],
242
+ clip_updates[3],
243
+ clip_updates[4],
244
+ "\n".join(log_messages),
245
+ metrics_output,
246
+ )
247
  ###----------------------------------------------------------------------------------------------------------------------
248
  # Validate reference image if provided
249
  ref_path = None
 
326
  pipeline.cleanup()
327
  progress(1.0, desc="Done!")
328
 
329
+ # Prepare updates: show only the extracted clips (up to 5), hide the rest
330
+ clip_updates = []
331
+ for i in range(5):
332
+ if i < len(clip_paths):
333
+ clip_updates.append(gr.update(value=str(clip_paths[i]), visible=True))
334
+ else:
335
+ clip_updates.append(gr.update(value=None, visible=False))
336
+
337
+ # Clear the HTML preview (`clips_output`) when returning final video outputs
338
+ return (
339
+ status,
340
+ gr.update(value="", visible=False),
341
+ clip_updates[0],
342
+ clip_updates[1],
343
+ clip_updates[2],
344
+ clip_updates[3],
345
+ clip_updates[4],
346
+ "\n".join(log_messages),
347
+ metrics_output,
348
+ )
349
  else:
350
  log(f"Processing failed: {result.error_message}")
351
  pipeline.cleanup()
352
+ return (
353
+ f"Error: {result.error_message}",
354
+ gr.update(value="", visible=False),
355
+ gr.update(value=None, visible=False),
356
+ gr.update(value=None, visible=False),
357
+ gr.update(value=None, visible=False),
358
+ gr.update(value=None, visible=False),
359
+ gr.update(value=None, visible=False),
360
+ "\n".join(log_messages),
361
+ "",
362
+ )
363
 
364
  except Exception as e:
365
  error_msg = f"Unexpected error: {str(e)}"
366
  log(error_msg)
367
  log(traceback.format_exc())
368
  logger.exception("Pipeline error")
369
+ return (
370
+ error_msg,
371
+ gr.update(value="", visible=False),
372
+ gr.update(value=None, visible=False),
373
+ gr.update(value=None, visible=False),
374
+ gr.update(value=None, visible=False),
375
+ gr.update(value=None, visible=False),
376
+ gr.update(value=None, visible=False),
377
+ "\n".join(log_messages),
378
+ "",
379
+ )
380
 
381
 
382
  # =============================================================================
 
909
  gr.Markdown("#### Extracted Clips")
910
  clips_output = gr.HTML(label="Extracted Clips")
911
 
912
+ # Pre-created video outputs (max 5) - will be populated by the callback
913
+ with gr.Row():
914
+ clip_video1 = gr.Video(label="Clip 1", interactive=False, visible=False)
915
+ clip_video2 = gr.Video(label="Clip 2", interactive=False, visible=False)
916
+ clip_video3 = gr.Video(label="Clip 3", interactive=False, visible=False)
917
+ clip_video4 = gr.Video(label="Clip 4", interactive=False, visible=False)
918
+ clip_video5 = gr.Video(label="Clip 5", interactive=False, visible=False)
919
+
920
  with gr.Accordion("Processing Log", open=True):
921
  log_output = gr.Textbox(
922
  label="Log",
 
948
  outputs=[
949
  status_output,
950
  clips_output,
951
+ clip_video1,
952
+ clip_video2,
953
+ clip_video3,
954
+ clip_video4,
955
+ clip_video5,
956
  log_output,
957
+ metrics_output,
958
  ],
959
  show_progress="full"
960
  )