Update app.py
Browse files
app.py
CHANGED
@@ -34,27 +34,31 @@ 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 |
|
38 |
# Note Julian: I'm not sure this works well when the pipeline changes dynamically.. to check
|
39 |
helper = DeepCacheSDHelper(pipe=pipe)
|
40 |
helper.set_params(
|
41 |
# cache_interval means the frequency of feature caching, specified as the number of steps between each cache operation.
|
42 |
-
|
43 |
-
cache_interval=4,
|
44 |
|
45 |
-
# cache_branch_id identifies which branch of the network (ordered from the shallowest to the deepest layer) is responsible for executing the caching processes.
|
46 |
-
cache_branch_id=
|
|
|
47 |
|
48 |
# Opting for a lower cache_branch_id or a larger cache_interval can lead to faster inference speed at the expense of reduced image quality
|
49 |
#(ablation experiments of these two hyperparameters can be found in the paper).
|
50 |
)
|
51 |
helper.enable()
|
52 |
|
53 |
-
#
|
54 |
-
#
|
55 |
-
#
|
56 |
-
#
|
57 |
-
# ------------------------------------------------------------------------------
|
58 |
|
59 |
def export_to_video_file(video_frames, output_video_path=None, fps=10):
|
60 |
if output_video_path is None:
|
@@ -85,7 +89,7 @@ def export_to_video_file(video_frames, output_video_path=None, fps=10):
|
|
85 |
# those are way too slow for a AiTube which needs things to be as fast as possible
|
86 |
# -----------------------------------------------------------------------------------
|
87 |
|
88 |
-
def interpolate_video_frames(input_file_path, output_file_path, output_fps=10, desired_duration=
|
89 |
"""
|
90 |
Interpolates frames in a video file to adjust frame rate and duration using ffmpeg's minterpolate.
|
91 |
|
@@ -202,7 +206,7 @@ def generate_image(secret_token, prompt, base, width, height, motion, step, desi
|
|
202 |
final_video_path = raw_video_path
|
203 |
|
204 |
# Optional frame interpolation
|
205 |
-
if desired_duration !=
|
206 |
final_video_path = interpolate_video_frames(raw_video_path, enhanced_video_path, output_fps=desired_fps, desired_duration=desired_duration)
|
207 |
|
208 |
# Read the content of the video file and encode it to base64
|
@@ -290,7 +294,7 @@ with gr.Blocks() as demo:
|
|
290 |
('8-Step', 8)],
|
291 |
value=4,
|
292 |
)
|
293 |
-
duration_slider = gr.Slider(label="Desired Duration (seconds)", min_value=
|
294 |
fps_slider = gr.Slider(label="Desired Frames Per Second", min_value=10, max_value=60, value=10, step=1)
|
295 |
|
296 |
submit = gr.Button()
|
|
|
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 |
+
step = 2
|
38 |
+
repo = "ByteDance/AnimateDiff-Lightning"
|
39 |
+
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
40 |
+
pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
|
41 |
+
step_loaded = step
|
42 |
|
43 |
# Note Julian: I'm not sure this works well when the pipeline changes dynamically.. to check
|
44 |
helper = DeepCacheSDHelper(pipe=pipe)
|
45 |
helper.set_params(
|
46 |
# cache_interval means the frequency of feature caching, specified as the number of steps between each cache operation.
|
47 |
+
cache_interval=2,
|
|
|
48 |
|
49 |
+
# cache_branch_id identifies which branch of the network (ordered from the shallowest to the deepest layer) is responsible for executing the caching processes.
|
50 |
+
# Note Julian: I have tried cache_branch_id=0 but quality was very "smoothed out"
|
51 |
+
cache_branch_id=0,
|
52 |
|
53 |
# Opting for a lower cache_branch_id or a larger cache_interval can lead to faster inference speed at the expense of reduced image quality
|
54 |
#(ablation experiments of these two hyperparameters can be found in the paper).
|
55 |
)
|
56 |
helper.enable()
|
57 |
|
58 |
+
# ----------------------------------- VIDEO ENCODING ---------------------------------
|
59 |
+
# The Diffusers utils hardcode MP4V as a codec which is not supported by all browsers.
|
60 |
+
# This is a critical issue for AiTube so we are forced to implement our own routine.
|
61 |
+
# ------------------------------------------------------------------------------------
|
|
|
62 |
|
63 |
def export_to_video_file(video_frames, output_video_path=None, fps=10):
|
64 |
if output_video_path is None:
|
|
|
89 |
# those are way too slow for a AiTube which needs things to be as fast as possible
|
90 |
# -----------------------------------------------------------------------------------
|
91 |
|
92 |
+
def interpolate_video_frames(input_file_path, output_file_path, output_fps=10, desired_duration=1):
|
93 |
"""
|
94 |
Interpolates frames in a video file to adjust frame rate and duration using ffmpeg's minterpolate.
|
95 |
|
|
|
206 |
final_video_path = raw_video_path
|
207 |
|
208 |
# Optional frame interpolation
|
209 |
+
if desired_duration != 1 or desired_fps != 10:
|
210 |
final_video_path = interpolate_video_frames(raw_video_path, enhanced_video_path, output_fps=desired_fps, desired_duration=desired_duration)
|
211 |
|
212 |
# Read the content of the video file and encode it to base64
|
|
|
294 |
('8-Step', 8)],
|
295 |
value=4,
|
296 |
)
|
297 |
+
duration_slider = gr.Slider(label="Desired Duration (seconds)", min_value=1, max_value=30, value=1, step=1)
|
298 |
fps_slider = gr.Slider(label="Desired Frames Per Second", min_value=10, max_value=60, value=10, step=1)
|
299 |
|
300 |
submit = gr.Button()
|