Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,102 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Gradio Interface
|
6 |
with gr.Blocks(css="style.css") as demo:
|
7 |
-
gr.HTML(
|
8 |
-
|
9 |
-
)
|
10 |
with gr.Group():
|
11 |
with gr.Row():
|
12 |
-
prompt = gr.Textbox(
|
13 |
-
|
14 |
-
)
|
15 |
with gr.Row():
|
16 |
select_base = gr.Dropdown(
|
17 |
label='Base model',
|
18 |
-
choices=[
|
19 |
-
"Cartoon",
|
20 |
-
"Realistic",
|
21 |
-
"3d",
|
22 |
-
"Anime",
|
23 |
-
],
|
24 |
value=base_loaded,
|
25 |
interactive=True
|
26 |
)
|
@@ -53,17 +129,12 @@ with gr.Blocks(css="style.css") as demo:
|
|
53 |
)
|
54 |
select_resolution = gr.Dropdown(
|
55 |
label='Resolution',
|
56 |
-
choices=[
|
57 |
-
"Square",
|
58 |
-
"Horizontal",
|
59 |
-
],
|
60 |
value="Square",
|
61 |
interactive=True
|
62 |
)
|
63 |
-
submit = gr.Button(
|
64 |
-
|
65 |
-
variant='primary'
|
66 |
-
)
|
67 |
video = gr.Video(
|
68 |
label='AnimateDiff-Lightning',
|
69 |
autoplay=True,
|
@@ -73,10 +144,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
73 |
)
|
74 |
|
75 |
gr.on(
|
76 |
-
triggers=[
|
77 |
-
submit.click,
|
78 |
-
prompt.submit
|
79 |
-
],
|
80 |
fn=generate_image,
|
81 |
inputs=[prompt, select_base, select_motion, select_step, select_resolution],
|
82 |
outputs=[video],
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import spaces
|
5 |
+
import uuid
|
6 |
+
|
7 |
+
from diffusers import AnimateDiffPipeline, EulerDiscreteScheduler
|
8 |
+
from diffusers.utils import export_to_video
|
9 |
+
from huggingface_hub import hf_hub_download
|
10 |
+
from safetensors.torch import load_file
|
11 |
+
from PIL import Image
|
12 |
+
from transformers import CLIPFeatureExtractor
|
13 |
+
|
14 |
+
# Constants
|
15 |
+
bases = {
|
16 |
+
"Cartoon": "frankjoshua/toonyou_beta6",
|
17 |
+
"Realistic": "emilianJR/epiCRealism",
|
18 |
+
"3d": "Lykon/DreamShaper",
|
19 |
+
"Anime": "Yntec/mistoonAnime2"
|
20 |
+
}
|
21 |
+
step_loaded = None
|
22 |
+
base_loaded = "Realistic"
|
23 |
+
motion_loaded = None
|
24 |
+
|
25 |
+
# Ensure model and scheduler are initialized in GPU-enabled function
|
26 |
+
if not torch.cuda.is_available():
|
27 |
+
raise NotImplementedError("No GPU detected!")
|
28 |
+
|
29 |
+
device = "cuda"
|
30 |
+
dtype = torch.float16
|
31 |
+
pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
|
32 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
33 |
+
|
34 |
+
feature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")
|
35 |
+
|
36 |
+
@spaces.GPU(duration=30, queue=False)
|
37 |
+
def generate_image(prompt, base="Realistic", motion="", step=8, resolution="Square", progress=gr.Progress()):
|
38 |
+
global step_loaded
|
39 |
+
global base_loaded
|
40 |
+
global motion_loaded
|
41 |
+
|
42 |
+
print(prompt, base, step, resolution)
|
43 |
+
|
44 |
+
# Set resolution
|
45 |
+
if resolution == "Square":
|
46 |
+
width, height = 512, 512
|
47 |
+
elif resolution == "Horizontal":
|
48 |
+
width, height = 1280, 720
|
49 |
+
else:
|
50 |
+
width, height = 512, 512 # default fallback
|
51 |
+
|
52 |
+
if step_loaded != step:
|
53 |
+
repo = "ByteDance/AnimateDiff-Lightning"
|
54 |
+
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
55 |
+
pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
|
56 |
+
step_loaded = step
|
57 |
+
|
58 |
+
if base_loaded != base:
|
59 |
+
pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)
|
60 |
+
base_loaded = base
|
61 |
+
|
62 |
+
if motion_loaded != motion:
|
63 |
+
pipe.unload_lora_weights()
|
64 |
+
if motion != "":
|
65 |
+
pipe.load_lora_weights(motion, adapter_name="motion")
|
66 |
+
pipe.set_adapters(["motion"], [0.7])
|
67 |
+
motion_loaded = motion
|
68 |
|
69 |
+
progress((0, step))
|
70 |
+
def progress_callback(i, t, z):
|
71 |
+
progress((i+1, step))
|
72 |
+
|
73 |
+
output = pipe(
|
74 |
+
prompt=prompt,
|
75 |
+
guidance_scale=1.2,
|
76 |
+
num_inference_steps=step,
|
77 |
+
width=width,
|
78 |
+
height=height,
|
79 |
+
callback=progress_callback,
|
80 |
+
callback_steps=1
|
81 |
+
)
|
82 |
+
|
83 |
+
name = str(uuid.uuid4()).replace("-", "")
|
84 |
+
path = f"/tmp/{name}.mp4"
|
85 |
+
export_to_video(output.frames[0], path, fps=10)
|
86 |
+
return path
|
87 |
|
88 |
# Gradio Interface
|
89 |
with gr.Blocks(css="style.css") as demo:
|
90 |
+
gr.HTML("<h1><center>Textual Imagination : A Text To Video Synthesis</center></h1>")
|
91 |
+
|
|
|
92 |
with gr.Group():
|
93 |
with gr.Row():
|
94 |
+
prompt = gr.Textbox(label='Prompt')
|
95 |
+
|
|
|
96 |
with gr.Row():
|
97 |
select_base = gr.Dropdown(
|
98 |
label='Base model',
|
99 |
+
choices=["Cartoon", "Realistic", "3d", "Anime"],
|
|
|
|
|
|
|
|
|
|
|
100 |
value=base_loaded,
|
101 |
interactive=True
|
102 |
)
|
|
|
129 |
)
|
130 |
select_resolution = gr.Dropdown(
|
131 |
label='Resolution',
|
132 |
+
choices=["Square", "Horizontal"],
|
|
|
|
|
|
|
133 |
value="Square",
|
134 |
interactive=True
|
135 |
)
|
136 |
+
submit = gr.Button(scale=1, variant='primary')
|
137 |
+
|
|
|
|
|
138 |
video = gr.Video(
|
139 |
label='AnimateDiff-Lightning',
|
140 |
autoplay=True,
|
|
|
144 |
)
|
145 |
|
146 |
gr.on(
|
147 |
+
triggers=[submit.click, prompt.submit],
|
|
|
|
|
|
|
148 |
fn=generate_image,
|
149 |
inputs=[prompt, select_base, select_motion, select_step, select_resolution],
|
150 |
outputs=[video],
|