Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
#2
by
Tennish
- opened
app.py
CHANGED
@@ -1,149 +1,109 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import os
|
4 |
-
import spaces
|
5 |
import uuid
|
6 |
|
7 |
from diffusers import AnimateDiffPipeline, MotionAdapter, 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 |
|
13 |
-
#
|
14 |
-
bases = {
|
15 |
-
"Cartoon": "frankjoshua/toonyou_beta6",
|
16 |
-
"Realistic": "emilianJR/epiCRealism",
|
17 |
-
"3d": "Lykon/DreamShaper",
|
18 |
-
"Anime": "Yntec/mistoonAnime2"
|
19 |
-
}
|
20 |
-
step_loaded = None
|
21 |
-
base_loaded = "Realistic"
|
22 |
-
motion_loaded = None
|
23 |
-
|
24 |
-
# Ensure model and scheduler are initialized in GPU-enabled function
|
25 |
if not torch.cuda.is_available():
|
26 |
-
raise NotImplementedError("
|
27 |
|
28 |
device = "cuda"
|
29 |
dtype = torch.float16
|
30 |
-
pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
|
31 |
-
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
32 |
-
|
33 |
-
# Safety checkers
|
34 |
-
from transformers import CLIPFeatureExtractor
|
35 |
-
|
36 |
-
feature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")
|
37 |
-
|
38 |
-
# Function
|
39 |
-
@spaces.GPU(duration=30,queue=False)
|
40 |
-
def generate_image(prompt, base="Realistic", motion="", step=8, progress=gr.Progress()):
|
41 |
-
global step_loaded
|
42 |
-
global base_loaded
|
43 |
-
global motion_loaded
|
44 |
-
print(prompt, base, step)
|
45 |
-
|
46 |
-
if step_loaded != step:
|
47 |
-
repo = "ByteDance/AnimateDiff-Lightning"
|
48 |
-
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
49 |
-
pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
|
50 |
-
step_loaded = step
|
51 |
-
|
52 |
-
if base_loaded != base:
|
53 |
-
pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)
|
54 |
-
base_loaded = base
|
55 |
-
|
56 |
-
if motion_loaded != motion:
|
57 |
-
pipe.unload_lora_weights()
|
58 |
-
if motion != "":
|
59 |
-
pipe.load_lora_weights(motion, adapter_name="motion")
|
60 |
-
pipe.set_adapters(["motion"], [0.7])
|
61 |
-
motion_loaded = motion
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
|
|
69 |
name = str(uuid.uuid4()).replace("-", "")
|
70 |
-
|
71 |
-
export_to_video(
|
72 |
-
|
73 |
-
|
74 |
|
75 |
# Gradio Interface
|
76 |
-
with gr.Blocks(
|
77 |
-
gr.HTML(
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
)
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
|
109 |
-
],
|
110 |
-
value="guoyww/animatediff-motion-lora-zoom-in",
|
111 |
-
interactive=True
|
112 |
-
)
|
113 |
-
select_step = gr.Dropdown(
|
114 |
-
label='Inference steps',
|
115 |
-
choices=[
|
116 |
-
('1-Step', 1),
|
117 |
-
('2-Step', 2),
|
118 |
-
('4-Step', 4),
|
119 |
-
('8-Step', 8),
|
120 |
-
],
|
121 |
-
value=4,
|
122 |
-
interactive=True
|
123 |
-
)
|
124 |
-
submit = gr.Button(
|
125 |
-
scale=1,
|
126 |
-
variant='primary'
|
127 |
-
)
|
128 |
-
video = gr.Video(
|
129 |
-
label='AnimateDiff-Lightning',
|
130 |
-
autoplay=True,
|
131 |
-
height=512,
|
132 |
-
width=512,
|
133 |
-
elem_id="video_output"
|
134 |
-
)
|
135 |
-
|
136 |
-
gr.on(triggers=[
|
137 |
-
submit.click,
|
138 |
-
prompt.submit
|
139 |
-
],
|
140 |
-
fn = generate_image,
|
141 |
-
inputs = [prompt, select_base, select_motion, select_step],
|
142 |
-
outputs = [video],
|
143 |
-
api_name = "instant_video",
|
144 |
-
queue = False
|
145 |
)
|
146 |
|
147 |
-
demo.
|
148 |
-
|
149 |
-
Translate
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import os
|
|
|
4 |
import uuid
|
5 |
|
6 |
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
|
7 |
from diffusers.utils import export_to_video
|
8 |
from huggingface_hub import hf_hub_download
|
9 |
from safetensors.torch import load_file
|
|
|
10 |
|
11 |
+
# Ensure GPU Availability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if not torch.cuda.is_available():
|
13 |
+
raise NotImplementedError("A GPU is required for this task.")
|
14 |
|
15 |
device = "cuda"
|
16 |
dtype = torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Base Model Paths
|
19 |
+
BASE_MODELS = {
|
20 |
+
"Realistic": "emilianJR/epiCRealism",
|
21 |
+
"Cartoon": "frankjoshua/toonyou_beta6",
|
22 |
+
"3D": "Lykon/DreamShaper",
|
23 |
+
"Anime": "Yntec/mistoonAnime2",
|
24 |
+
}
|
25 |
|
26 |
+
# Initialize Pipeline
|
27 |
+
print("Loading AnimateDiff pipeline...")
|
28 |
+
base_model = "Realistic"
|
29 |
+
pipe = AnimateDiffPipeline.from_pretrained(BASE_MODELS[base_model], torch_dtype=dtype).to(device)
|
30 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
31 |
+
print("Pipeline loaded successfully.")
|
32 |
+
|
33 |
+
# Video Generation Function
|
34 |
+
def generate_video(prompt, base="Realistic", motion="", steps=8):
|
35 |
+
global pipe
|
36 |
+
print(f"Generating video: Prompt='{prompt}', Base='{base}', Steps='{steps}'")
|
37 |
+
|
38 |
+
# Switch Base Model
|
39 |
+
if base in BASE_MODELS:
|
40 |
+
print(f"Loading base model: {base}")
|
41 |
+
pipe = AnimateDiffPipeline.from_pretrained(BASE_MODELS[base], torch_dtype=dtype).to(device)
|
42 |
+
|
43 |
+
# Set Inference Steps
|
44 |
+
steps = int(steps)
|
45 |
+
fps = 10 # Frames per second
|
46 |
+
duration = 30 # Video duration in seconds
|
47 |
+
total_frames = fps * duration # Total frames to generate
|
48 |
+
|
49 |
+
# Generate Frames
|
50 |
+
video_frames = []
|
51 |
+
for i in range(total_frames):
|
52 |
+
output = pipe(
|
53 |
+
prompt=prompt,
|
54 |
+
guidance_scale=1.2,
|
55 |
+
num_inference_steps=steps
|
56 |
+
)
|
57 |
+
video_frames.extend(output.frames[0])
|
58 |
|
59 |
+
# Export to Video
|
60 |
name = str(uuid.uuid4()).replace("-", "")
|
61 |
+
output_path = f"/tmp/{name}.mp4"
|
62 |
+
export_to_video(video_frames, output_path, fps=fps)
|
63 |
+
print(f"Video saved to {output_path}")
|
64 |
+
return output_path
|
65 |
|
66 |
# Gradio Interface
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.HTML("<h1><center>30-Second Text-to-Video Generation</center></h1>")
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
prompt = gr.Textbox(label="Text Prompt", placeholder="Describe your scene...")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
base_model = gr.Dropdown(
|
75 |
+
label="Base Model",
|
76 |
+
choices=["Realistic", "Cartoon", "3D", "Anime"],
|
77 |
+
value="Realistic"
|
78 |
)
|
79 |
+
motion = gr.Dropdown(
|
80 |
+
label="Motion Adapter",
|
81 |
+
choices=[
|
82 |
+
("None", ""),
|
83 |
+
("Zoom In", "guoyww/animatediff-motion-lora-zoom-in"),
|
84 |
+
("Zoom Out", "guoyww/animatediff-motion-lora-zoom-out"),
|
85 |
+
("Tilt Up", "guoyww/animatediff-motion-lora-tilt-up"),
|
86 |
+
("Tilt Down", "guoyww/animatediff-motion-lora-tilt-down"),
|
87 |
+
("Pan Left", "guoyww/animatediff-motion-lora-pan-left"),
|
88 |
+
("Pan Right", "guoyww/animatediff-motion-lora-pan-right"),
|
89 |
+
],
|
90 |
+
value=""
|
91 |
+
)
|
92 |
+
steps = gr.Dropdown(
|
93 |
+
label="Inference Steps",
|
94 |
+
choices=["4", "8", "12"],
|
95 |
+
value="8"
|
96 |
+
)
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
generate_button = gr.Button("Generate Video")
|
100 |
+
|
101 |
+
video_output = gr.Video(label="Generated Video", autoplay=True, height=512, width=512)
|
102 |
+
|
103 |
+
generate_button.click(
|
104 |
+
fn=generate_video,
|
105 |
+
inputs=[prompt, base_model, motion, steps],
|
106 |
+
outputs=video_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
)
|
108 |
|
109 |
+
demo.launch()
|
|
|
|