jfforero commited on
Commit
145a57f
·
verified ·
1 Parent(s): b8bc520

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -72
app.py CHANGED
@@ -335,7 +335,10 @@ def get_predictions(audio_input, generate_audio=True, chunk_duration=10):
335
 
336
  return results
337
 
338
- # Function to create a fade transition between images
 
 
 
339
  def create_fade_transition(images, fade_duration=1.0, fps=24):
340
  """
341
  Create a smooth fade transition between a sequence of images.
@@ -347,7 +350,7 @@ def create_fade_transition(images, fade_duration=1.0, fps=24):
347
 
348
  Returns:
349
  PIL Image containing the first frame of the animation (for display)
350
- BytesIO object containing the animation as a GIF (for download)
351
  """
352
  if not images or len(images) < 2:
353
  return None, None
@@ -382,20 +385,25 @@ def create_fade_transition(images, fade_duration=1.0, fps=24):
382
  for _ in range(fade_frames):
383
  frames.append(img2)
384
 
385
- # Save as GIF to BytesIO
386
- buffer = BytesIO()
387
- frames[0].save(
388
- buffer,
389
- format='GIF',
390
- save_all=True,
391
- append_images=frames[1:],
392
- duration=1000//fps, # Duration per frame in milliseconds
393
- loop=0 # Loop indefinitely
394
- )
395
- buffer.seek(0)
396
 
397
- # Return the first frame for display and the buffer for download
398
- return images[0], buffer
 
 
 
 
 
399
 
400
  # Update the process_and_display function to handle the fade animation correctly
401
  def process_and_display(audio_input, generate_audio, chunk_duration):
@@ -445,13 +453,13 @@ def process_and_display(audio_input, generate_audio, chunk_duration):
445
 
446
  # Create fade animation if we have multiple images
447
  fade_preview = None
448
- fade_animation = None
449
  if len(all_images) > 1:
450
  # Create a fade animation (GIF)
451
- fade_preview, fade_animation = create_fade_transition(all_images, fade_duration=1.5, fps=15)
452
 
453
  # Hide loading indicator and show results
454
- yield [gr.HTML("")] + group_visibility + outputs + [fade_preview, fade_animation]
455
 
456
  # Update the clear_all function to handle the new outputs
457
  def clear_all():
@@ -567,60 +575,7 @@ with gr.Blocks(title="Affective Virtual Environments - Chunked Processing") as i
567
  interactive=False
568
  )
569
 
570
- # Function to process and display results
571
- def process_and_display(audio_input, generate_audio, chunk_duration):
572
- # Validate chunk duration
573
- if chunk_duration is None or chunk_duration <= 0:
574
- chunk_duration = 10
575
-
576
- # Show loading indicator
577
- yield [gr.HTML(f"""
578
- <div style="text-align: center; margin: 20px;">
579
- <p style="font-size: 18px; color: #4a4a4a;">Processing audio in {chunk_duration}-second chunks...</p>
580
- <div style="border: 4px solid #f3f3f3; border-top: 4px solid #3498db; border-radius: 50%; width: 30px; height: 30px; animation: spin 2s linear infinite; margin: 0 auto;"></div>
581
- <style>@keyframes spin {{ 0% {{ transform: rotate(0deg); }} 100% {{ transform: rotate(360deg); }} }}</style>
582
- </div>
583
- """)] + [gr.Group(visible=False)] * len(group_components) + [None] * (len(output_containers) * 5) + [None, None]
584
-
585
- results = get_predictions(audio_input, generate_audio, chunk_duration)
586
-
587
- # Initialize outputs list
588
- outputs = []
589
- group_visibility = []
590
- all_images = [] # Collect all generated images for the fade animation
591
-
592
- # Process each result
593
- for i, result in enumerate(results):
594
- if i < len(output_containers):
595
- group_visibility.append(gr.Group(visible=True))
596
- outputs.extend([
597
- result['emotion'],
598
- result['transcription'],
599
- result['sentiment'],
600
- result['image'],
601
- result['music']
602
- ])
603
- # Collect the image for the fade animation
604
- all_images.append(result['image'])
605
- else:
606
- # If we have more results than containers, just extend with None
607
- group_visibility.append(gr.Group(visible=False))
608
- outputs.extend([None] * 5)
609
-
610
- # Hide remaining containers
611
- for i in range(len(results), len(output_containers)):
612
- group_visibility.append(gr.Group(visible=False))
613
- outputs.extend([None] * 5)
614
-
615
- # Create fade animation if we have multiple images
616
- fade_preview = None
617
- fade_animation = None
618
- if len(all_images) > 1:
619
- # Create a fade animation (GIF)
620
- fade_preview, fade_animation = create_fade_transition(all_images, fade_duration=1.5, fps=15)
621
-
622
- # Hide loading indicator and show results
623
- yield [gr.HTML("")] + group_visibility + outputs + [fade_preview, fade_animation]
624
 
625
  # Function to handle example selection
626
  def load_example(example_name):
 
335
 
336
  return results
337
 
338
+
339
+
340
+
341
+ # Replace the create_fade_transition function with this updated version
342
  def create_fade_transition(images, fade_duration=1.0, fps=24):
343
  """
344
  Create a smooth fade transition between a sequence of images.
 
350
 
351
  Returns:
352
  PIL Image containing the first frame of the animation (for display)
353
+ Path to the saved GIF file (for download)
354
  """
355
  if not images or len(images) < 2:
356
  return None, None
 
385
  for _ in range(fade_frames):
386
  frames.append(img2)
387
 
388
+ # Create a temporary file to save the GIF
389
+ with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as tmp_file:
390
+ # Save as GIF
391
+ frames[0].save(
392
+ tmp_file.name,
393
+ format='GIF',
394
+ save_all=True,
395
+ append_images=frames[1:],
396
+ duration=1000//fps, # Duration per frame in milliseconds
397
+ loop=0 # Loop indefinitely
398
+ )
399
 
400
+ # Return the first frame for display and the file path for download
401
+ return images[0], tmp_file.name
402
+
403
+
404
+
405
+
406
+
407
 
408
  # Update the process_and_display function to handle the fade animation correctly
409
  def process_and_display(audio_input, generate_audio, chunk_duration):
 
453
 
454
  # Create fade animation if we have multiple images
455
  fade_preview = None
456
+ fade_animation_path = None
457
  if len(all_images) > 1:
458
  # Create a fade animation (GIF)
459
+ fade_preview, fade_animation_path = create_fade_transition(all_images, fade_duration=1.5, fps=15)
460
 
461
  # Hide loading indicator and show results
462
+ yield [gr.HTML("")] + group_visibility + outputs + [fade_preview, fade_animation_path]
463
 
464
  # Update the clear_all function to handle the new outputs
465
  def clear_all():
 
575
  interactive=False
576
  )
577
 
578
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579
 
580
  # Function to handle example selection
581
  def load_example(example_name):