jbilcke-hf HF staff commited on
Commit
5d48b65
1 Parent(s): e54869f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -34,6 +34,10 @@ dtype = torch.float16
34
  pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
35
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
36
 
 
 
 
 
37
  # unfortunately 2 steps isn't good enough for AiTube, we need 4 steps
38
  step = 4
39
  repo = "ByteDance/AnimateDiff-Lightning"
@@ -63,7 +67,7 @@ step_loaded = step
63
  # This is a critical issue for AiTube so we are forced to implement our own routine.
64
  # ------------------------------------------------------------------------------------
65
 
66
- def export_to_video_file(video_frames, output_video_path=None, fps=10):
67
  if output_video_path is None:
68
  output_video_path = tempfile.NamedTemporaryFile(suffix=".webm").name
69
 
@@ -92,7 +96,7 @@ def export_to_video_file(video_frames, output_video_path=None, fps=10):
92
  # those are way too slow for a AiTube which needs things to be as fast as possible
93
  # -----------------------------------------------------------------------------------
94
 
95
- def interpolate_video_frames(input_file_path, output_file_path, output_fps=10, desired_duration=1.6, original_duration=1.6):
96
  scale_factor = original_duration / desired_duration
97
  interpolation_filter = f'minterpolate=fps={output_fps},setpts={scale_factor}*PTS'
98
 
@@ -165,12 +169,12 @@ def generate_image(secret_token, prompt, base, width, height, motion, step, desi
165
  #
166
  # maybe to make things faster, we could *not* encode the video (as this uses files and external processes, which can be slow)
167
  # and instead return the unencoded frames to the frontend renderer?
168
- raw_video_path = export_to_video_file(output.frames[0], raw_video_path, fps=10)
169
 
170
  final_video_path = raw_video_path
171
 
172
  # Optional frame interpolation
173
- if desired_duration != 1 or desired_fps != 10:
174
  final_video_path = interpolate_video_frames(raw_video_path, enhanced_video_path, output_fps=desired_fps, desired_duration=desired_duration)
175
 
176
  # Read the content of the video file and encode it to base64
@@ -258,8 +262,8 @@ with gr.Blocks() as demo:
258
  ('8-Step', 8)],
259
  value=4,
260
  )
261
- duration_slider = gr.Slider(label="Desired Duration (seconds)", min_value=1, max_value=120, value=1.6, step=0.1)
262
- fps_slider = gr.Slider(label="Desired Frames Per Second", min_value=10, max_value=60, value=10, step=1)
263
 
264
  submit = gr.Button()
265
 
 
34
  pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
35
  pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
36
 
37
+ # those are AnimateDiff defaults - we don't touch them for now
38
+ hardcoded_fps = 10
39
+ hardcoded_duration_sec = 1.6
40
+
41
  # unfortunately 2 steps isn't good enough for AiTube, we need 4 steps
42
  step = 4
43
  repo = "ByteDance/AnimateDiff-Lightning"
 
67
  # This is a critical issue for AiTube so we are forced to implement our own routine.
68
  # ------------------------------------------------------------------------------------
69
 
70
+ def export_to_video_file(video_frames, output_video_path=None, fps=hardcoded_fps):
71
  if output_video_path is None:
72
  output_video_path = tempfile.NamedTemporaryFile(suffix=".webm").name
73
 
 
96
  # those are way too slow for a AiTube which needs things to be as fast as possible
97
  # -----------------------------------------------------------------------------------
98
 
99
+ def interpolate_video_frames(input_file_path, output_file_path, output_fps=hardcoded_fps, desired_duration=hardcoded_duration_sec, original_duration=hardcoded_duration_sec):
100
  scale_factor = original_duration / desired_duration
101
  interpolation_filter = f'minterpolate=fps={output_fps},setpts={scale_factor}*PTS'
102
 
 
169
  #
170
  # maybe to make things faster, we could *not* encode the video (as this uses files and external processes, which can be slow)
171
  # and instead return the unencoded frames to the frontend renderer?
172
+ raw_video_path = export_to_video_file(output.frames[0], raw_video_path, fps=hardcoded_fps)
173
 
174
  final_video_path = raw_video_path
175
 
176
  # Optional frame interpolation
177
+ if desired_duration > hardcoded_duration_sec or desired_duration < hardcoded_duration_sec or desired_fps > hardcoded_fps or desired_fps < hardcoded_fps:
178
  final_video_path = interpolate_video_frames(raw_video_path, enhanced_video_path, output_fps=desired_fps, desired_duration=desired_duration)
179
 
180
  # Read the content of the video file and encode it to base64
 
262
  ('8-Step', 8)],
263
  value=4,
264
  )
265
+ duration_slider = gr.Slider(label="Desired Duration (seconds)", min_value=1, max_value=120, value=hardcoded_duration_sec, step=0.1)
266
+ fps_slider = gr.Slider(label="Desired Frames Per Second", min_value=10, max_value=60, value=hardcoded_fps, step=1)
267
 
268
  submit = gr.Button()
269