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|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
subprocess.run(
|
3 |
+
'pip install numpy==1.26.4',
|
4 |
+
shell=True
|
5 |
+
)
|
6 |
+
|
7 |
+
import os
|
8 |
+
import gradio as gr
|
9 |
+
import torch
|
10 |
+
import spaces
|
11 |
+
import random
|
12 |
+
from PIL import Image
|
13 |
+
import numpy as np
|
14 |
+
|
15 |
+
from glob import glob
|
16 |
+
from pathlib import Path
|
17 |
+
from typing import Optional
|
18 |
+
|
19 |
+
#Core functions from https://github.com/modelscope/DiffSynth-Studio
|
20 |
+
from diffsynth import save_video, ModelManager, SVDVideoPipeline
|
21 |
+
from diffsynth import SDVideoPipeline, ControlNetConfigUnit, VideoData, save_frames
|
22 |
+
from diffsynth.extensions.RIFE import RIFESmoother
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
# Constants
|
27 |
+
MAX_SEED = np.iinfo(np.int32).max
|
28 |
+
CSS = """
|
29 |
+
footer {
|
30 |
+
visibility: hidden;
|
31 |
+
}
|
32 |
+
"""
|
33 |
+
|
34 |
+
JS = """function () {
|
35 |
+
gradioURL = window.location.href
|
36 |
+
if (!gradioURL.endsWith('?__theme=dark')) {
|
37 |
+
window.location.replace(gradioURL + '?__theme=dark');
|
38 |
+
}
|
39 |
+
}"""
|
40 |
+
|
41 |
+
|
42 |
+
# Ensure model and scheduler are initialized in GPU-enabled function
|
43 |
+
if torch.cuda.is_available():
|
44 |
+
|
45 |
+
model_manager2 = ModelManager(torch_dtype=torch.float16, device="cuda")
|
46 |
+
model_manager2.load_textual_inversions("models/textual_inversion")
|
47 |
+
model_manager2.load_models([
|
48 |
+
"models/stable_diffusion/flat2DAnimerge_v45Sharp.safetensors",
|
49 |
+
"models/AnimateDiff/mm_sd_v15_v2.ckpt",
|
50 |
+
"models/ControlNet/control_v11p_sd15_lineart.pth",
|
51 |
+
"models/ControlNet/control_v11f1e_sd15_tile.pth",
|
52 |
+
"models/RIFE/flownet.pkl"
|
53 |
+
])
|
54 |
+
pipe2 = SDVideoPipeline.from_model_manager(
|
55 |
+
model_manager2,
|
56 |
+
[
|
57 |
+
ControlNetConfigUnit(
|
58 |
+
processor_id="lineart",
|
59 |
+
model_path="models/ControlNet/control_v11p_sd15_lineart.pth",
|
60 |
+
scale=0.5
|
61 |
+
),
|
62 |
+
ControlNetConfigUnit(
|
63 |
+
processor_id="tile",
|
64 |
+
model_path="models/ControlNet/control_v11f1e_sd15_tile.pth",
|
65 |
+
scale=0.5
|
66 |
+
)
|
67 |
+
]
|
68 |
+
)
|
69 |
+
smoother = RIFESmoother.from_model_manager(model_manager2)
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
def update_frames(video_in):
|
74 |
+
up_video = VideoData(
|
75 |
+
video_file=video_in)
|
76 |
+
frame_len = len(up_video)
|
77 |
+
return gr.update(maximum=frame_len)
|
78 |
+
|
79 |
+
@spaces.GPU(duration=180)
|
80 |
+
def generate(
|
81 |
+
video_in,
|
82 |
+
image_in,
|
83 |
+
prompt: str = "best quality",
|
84 |
+
seed: int = -1,
|
85 |
+
num_inference_steps: int = 10,
|
86 |
+
num_frames: int = 30,
|
87 |
+
height: int = 512,
|
88 |
+
width: int = 512,
|
89 |
+
animatediff_batch_size: int = 32,
|
90 |
+
animatediff_stride: int = 16,
|
91 |
+
fps_id: int = 25,
|
92 |
+
output_folder: str = "outputs",
|
93 |
+
progress=gr.Progress(track_tqdm=True)):
|
94 |
+
|
95 |
+
video = ""
|
96 |
+
if seed == -1:
|
97 |
+
seed = random.randint(0, MAX_SEED)
|
98 |
+
|
99 |
+
torch.manual_seed(seed)
|
100 |
+
|
101 |
+
os.makedirs(output_folder, exist_ok=True)
|
102 |
+
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
103 |
+
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
104 |
+
|
105 |
+
up_video = VideoData(
|
106 |
+
video_file=video_in,
|
107 |
+
height=height, width=width)
|
108 |
+
input_video = [up_video[i] for i in range(1, num_frames)]
|
109 |
+
|
110 |
+
video = pipe2(
|
111 |
+
prompt=prompt,
|
112 |
+
negative_prompt="verybadimagenegative_v1.3",
|
113 |
+
cfg_scale=3,
|
114 |
+
clip_skip=2,
|
115 |
+
controlnet_frames=input_video,
|
116 |
+
num_frames=len(input_video),
|
117 |
+
num_inference_steps=num_inference_steps,
|
118 |
+
height=height,
|
119 |
+
width=width,
|
120 |
+
animatediff_batch_size=animatediff_batch_size,
|
121 |
+
animatediff_stride=animatediff_stride,
|
122 |
+
unet_batch_size=8,
|
123 |
+
controlnet_batch_size=8,
|
124 |
+
vram_limit_level=0,
|
125 |
+
)
|
126 |
+
video = smoother(video)
|
127 |
+
|
128 |
+
|
129 |
+
save_video(video, video_path, fps=fps_id)
|
130 |
+
|
131 |
+
return video_path, seed
|
132 |
+
|
133 |
+
|
134 |
+
examples = [
|
135 |
+
['./walking.mp4', None, "Diffutoon", "A woman walking on the street"],
|
136 |
+
['./smilegirl.mp4', None, "Diffutoon", "A girl stand on the grass"],
|
137 |
+
['./working.mp4', None, "Diffutoon", "A woman is doing the dishes"],
|
138 |
+
[None, "./train.jpg", "ExVideo", ""],
|
139 |
+
[None, "./girl.webp", "ExVideo", ""],
|
140 |
+
[None, "./robo.jpg", "ExVideo", ""],
|
141 |
+
]
|
142 |
+
|
143 |
+
|
144 |
+
# Gradio Interface
|
145 |
+
|
146 |
+
with gr.Blocks(css=CSS, js=JS, theme="soft") as demo:
|
147 |
+
gr.HTML("<h1><center>Exvideo📽️Diffutoon</center></h1>")
|
148 |
+
gr.HTML("""
|
149 |
+
<p><center>Exvideo and Diffutoon video generation
|
150 |
+
<br><b>Update</b>: Output resize, Frames length control.
|
151 |
+
<br><b>Note</b>: ZeroGPU limited, Set the parameters appropriately.</center></p>
|
152 |
+
""")
|
153 |
+
with gr.Row():
|
154 |
+
video_in = gr.Video(label='Upload Video', height=600, scale=2)
|
155 |
+
image_in = gr.Image(label='Upload Image', height=600, scale=2, image_mode="RGB", type="filepath", visible=False)
|
156 |
+
video = gr.Video(label="Generated Video", height=600, scale=2)
|
157 |
+
with gr.Column(scale=1):
|
158 |
+
seed = gr.Slider(
|
159 |
+
label="Seed (-1 Random)",
|
160 |
+
minimum=-1,
|
161 |
+
maximum=MAX_SEED,
|
162 |
+
step=1,
|
163 |
+
value=-1,
|
164 |
+
)
|
165 |
+
num_inference_steps = gr.Slider(
|
166 |
+
label="Inference steps",
|
167 |
+
info="Inference steps",
|
168 |
+
step=1,
|
169 |
+
value=10,
|
170 |
+
minimum=1,
|
171 |
+
maximum=50,
|
172 |
+
)
|
173 |
+
num_frames = gr.Slider(
|
174 |
+
label="Num frames",
|
175 |
+
info="Output Frames",
|
176 |
+
step=1,
|
177 |
+
value=30,
|
178 |
+
minimum=1,
|
179 |
+
maximum=128,
|
180 |
+
)
|
181 |
+
with gr.Row():
|
182 |
+
height = gr.Slider(
|
183 |
+
label="Height",
|
184 |
+
step=8,
|
185 |
+
value=512,
|
186 |
+
minimum=256,
|
187 |
+
maximum=2560,
|
188 |
+
)
|
189 |
+
width = gr.Slider(
|
190 |
+
label="Width",
|
191 |
+
step=8,
|
192 |
+
value=512,
|
193 |
+
minimum=256,
|
194 |
+
maximum=2560,
|
195 |
+
)
|
196 |
+
with gr.Accordion("Diffutoon Options", open=False):
|
197 |
+
animatediff_batch_size = gr.Slider(
|
198 |
+
label="Animatediff batch size",
|
199 |
+
minimum=1,
|
200 |
+
maximum=50,
|
201 |
+
step=1,
|
202 |
+
value=32,
|
203 |
+
)
|
204 |
+
animatediff_stride = gr.Slider(
|
205 |
+
label="Animatediff stride",
|
206 |
+
minimum=1,
|
207 |
+
maximum=50,
|
208 |
+
step=1,
|
209 |
+
value=16,
|
210 |
+
)
|
211 |
+
fps_id = gr.Slider(
|
212 |
+
label="Frames per second",
|
213 |
+
info="The length of your video in seconds will be 25/fps",
|
214 |
+
value=6,
|
215 |
+
step=1,
|
216 |
+
minimum=5,
|
217 |
+
maximum=30,
|
218 |
+
)
|
219 |
+
prompt = gr.Textbox(label="Prompt", value="best quality")
|
220 |
+
with gr.Row():
|
221 |
+
submit_btn = gr.Button(value="Generate")
|
222 |
+
#stop_btn = gr.Button(value="Stop", variant="stop")
|
223 |
+
clear_btn = gr.ClearButton([video_in, image_in, seed, video])
|
224 |
+
|
225 |
+
gr.Examples(
|
226 |
+
examples=examples,
|
227 |
+
fn=generate,
|
228 |
+
inputs=[video_in, image_in, selected, prompt],
|
229 |
+
outputs=[video, seed],
|
230 |
+
cache_examples="lazy",
|
231 |
+
examples_per_page=4,
|
232 |
+
)
|
233 |
+
video_in.upload(update_frames, inputs=[video_in], outputs=[num_frames])
|
234 |
+
submit_event = submit_btn.click(fn=generate, inputs=[video_in, image_in, prompt, seed, num_inference_steps, num_frames, height, width, animatediff_batch_size, animatediff_stride, fps_id], outputs=[video, seed], api_name="video")
|
235 |
+
#stop_btn.click(fn=None, inputs=None, outputs=None, cancels=[submit_event])
|
236 |
+
|
237 |
+
demo.queue().launch()
|