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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from transformers import CLIPFeatureExtractor
|
| 13 |
|
| 14 |
+
# Custom message for linking to other demos
|
| 15 |
+
MORE_INFO = """ ## Related Demos
|
| 16 |
+
### Check out other interesting demos here
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
# Model configuration
|
| 20 |
+
BASES = {
|
| 21 |
+
"Cartoon": "frankjoshua/toonyou_beta6",
|
| 22 |
+
"Realistic": "emilianJR/epiCRealism",
|
| 23 |
+
"3d": "Lykon/DreamShaper",
|
| 24 |
+
"Anime": "Yntec/mistoonAnime2"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Global state tracking
|
| 28 |
+
step_loaded = None
|
| 29 |
+
base_loaded = "Realistic"
|
| 30 |
+
motion_loaded = None
|
| 31 |
+
|
| 32 |
+
# GPU check and initialization
|
| 33 |
+
if not torch.cuda.is_available():
|
| 34 |
+
raise NotImplementedError("No GPU detected! This space requires a GPU to run.")
|
| 35 |
+
|
| 36 |
+
device = "cuda"
|
| 37 |
+
dtype = torch.float16
|
| 38 |
+
|
| 39 |
+
# Initialize the pipeline
|
| 40 |
+
pipe = AnimateDiffPipeline.from_pretrained(
|
| 41 |
+
BASES[base_loaded],
|
| 42 |
+
torch_dtype=dtype
|
| 43 |
+
).to(device)
|
| 44 |
+
|
| 45 |
+
# Configure scheduler
|
| 46 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(
|
| 47 |
+
pipe.scheduler.config,
|
| 48 |
+
timestep_spacing="trailing",
|
| 49 |
+
beta_schedule="linear"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Initialize safety checker
|
| 53 |
+
feature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")
|
| 54 |
+
|
| 55 |
+
# Main generation function
|
| 56 |
+
@spaces.GPU(duration=30, queue=False)
|
| 57 |
+
def generate_image(prompt, base="Realistic", motion="", step=8, progress=gr.Progress()):
|
| 58 |
+
global step_loaded
|
| 59 |
+
global base_loaded
|
| 60 |
+
global motion_loaded
|
| 61 |
+
print(f"Generating with prompt: {prompt}, base: {base}, steps: {step}")
|
| 62 |
+
|
| 63 |
+
# Load step configuration if needed
|
| 64 |
+
if step_loaded != step:
|
| 65 |
+
repo = "ByteDance/AnimateDiff-Lightning"
|
| 66 |
+
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
| 67 |
+
pipe.unet.load_state_dict(
|
| 68 |
+
load_file(hf_hub_download(repo, ckpt), device=device),
|
| 69 |
+
strict=False
|
| 70 |
+
)
|
| 71 |
+
step_loaded = step
|
| 72 |
+
|
| 73 |
+
# Load base model if needed
|
| 74 |
+
if base_loaded != base:
|
| 75 |
+
pipe.unet.load_state_dict(
|
| 76 |
+
torch.load(
|
| 77 |
+
hf_hub_download(BASES[base], "unet/diffusion_pytorch_model.bin"),
|
| 78 |
+
map_location=device
|
| 79 |
+
),
|
| 80 |
+
strict=False
|
| 81 |
+
)
|
| 82 |
+
base_loaded = base
|
| 83 |
+
|
| 84 |
+
# Handle motion adaptation
|
| 85 |
+
if motion_loaded != motion:
|
| 86 |
+
pipe.unload_lora_weights()
|
| 87 |
+
if motion != "":
|
| 88 |
+
pipe.load_lora_weights(motion, adapter_name="motion")
|
| 89 |
+
pipe.set_adapters(["motion"], [0.7])
|
| 90 |
+
motion_loaded = motion
|
| 91 |
+
|
| 92 |
+
# Progress tracking
|
| 93 |
+
progress((0, step))
|
| 94 |
+
def progress_callback(i, t, z):
|
| 95 |
+
progress((i+1, step))
|
| 96 |
+
|
| 97 |
+
# Generate the video
|
| 98 |
+
output = pipe(
|
| 99 |
+
prompt=prompt,
|
| 100 |
+
guidance_scale=1.2,
|
| 101 |
+
num_inference_steps=step,
|
| 102 |
+
callback=progress_callback,
|
| 103 |
+
callback_steps=1
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Save and return the video
|
| 107 |
+
name = str(uuid.uuid4()).replace("-", "")
|
| 108 |
+
path = f"/tmp/{name}.mp4"
|
| 109 |
+
export_to_video(output.frames[0], path, fps=10)
|
| 110 |
+
return path
|
| 111 |
+
|
| 112 |
+
# Gradio Interface
|
| 113 |
+
with gr.Blocks(css="style.css") as demo:
|
| 114 |
+
# Header
|
| 115 |
+
gr.HTML(
|
| 116 |
+
"""
|
| 117 |
+
<h1><center>Instant⚡Video Generator</center></h1>
|
| 118 |
+
<p><center><span class='warning'>Adjust steps from 4 to 8 for better results if needed.</span></center></p>
|
| 119 |
+
<p><center><strong>First generation takes longer, subsequent generations are faster.</strong></p>
|
| 120 |
+
<p><center>Follow the example prompts format for best results</p>
|
| 121 |
+
"""
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Input interface
|
| 125 |
+
with gr.Group():
|
| 126 |
+
with gr.Row():
|
| 127 |
+
prompt = gr.Textbox(label='Prompt')
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
select_base = gr.Dropdown(
|
| 131 |
+
label='Base model',
|
| 132 |
+
choices=list(BASES.keys()),
|
| 133 |
+
value=base_loaded,
|
| 134 |
+
interactive=True
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
select_motion = gr.Dropdown(
|
| 138 |
+
label='Motion',
|
| 139 |
+
choices=[
|
| 140 |
+
("Default", ""),
|
| 141 |
+
("Zoom in", "guoyww/animatediff-motion-lora-zoom-in"),
|
| 142 |
+
("Zoom out", "guoyww/animatediff-motion-lora-zoom-out"),
|
| 143 |
+
("Tilt up", "guoyww/animatediff-motion-lora-tilt-up"),
|
| 144 |
+
("Tilt down", "guoyww/animatediff-motion-lora-tilt-down"),
|
| 145 |
+
("Pan left", "guoyww/animatediff-motion-lora-pan-left"),
|
| 146 |
+
("Pan right", "guoyww/animatediff-motion-lora-pan-right"),
|
| 147 |
+
("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
|
| 148 |
+
("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
|
| 149 |
+
],
|
| 150 |
+
value="guoyww/animatediff-motion-lora-zoom-in",
|
| 151 |
+
interactive=True
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
select_step = gr.Dropdown(
|
| 155 |
+
label='Inference steps',
|
| 156 |
+
choices=[
|
| 157 |
+
('1-Step', 1),
|
| 158 |
+
('2-Step', 2),
|
| 159 |
+
('4-Step', 4),
|
| 160 |
+
('8-Step', 8),
|
| 161 |
+
],
|
| 162 |
+
value=4,
|
| 163 |
+
interactive=True
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
submit = gr.Button(
|
| 167 |
+
scale=1,
|
| 168 |
+
variant='primary'
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
# Output video display
|
| 172 |
+
video = gr.Video(
|
| 173 |
+
label='Generated Animation',
|
| 174 |
+
autoplay=True,
|
| 175 |
+
height=512,
|
| 176 |
+
width=512,
|
| 177 |
+
elem_id="video_output"
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Event handlers
|
| 181 |
+
gr.on(
|
| 182 |
+
triggers=[submit.click, prompt.submit],
|
| 183 |
+
fn=generate_image,
|
| 184 |
+
inputs=[prompt, select_base, select_motion, select_step],
|
| 185 |
+
outputs=[video],
|
| 186 |
+
api_name="instant_video",
|
| 187 |
+
queue=False
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
# Example prompts
|
| 191 |
+
gr.Examples(
|
| 192 |
+
examples=[
|
| 193 |
+
["Focus: Eiffel Tower (Animate: Clouds moving)"],
|
| 194 |
+
["Focus: Trees In forest (Animate: Lion running)"],
|
| 195 |
+
["Focus: Astronaut in Space"],
|
| 196 |
+
["Focus: Group of Birds in sky (Animate: Birds Moving) (Shot From distance)"],
|
| 197 |
+
["Focus: Statue of liberty (Shot from Drone) (Animate: Drone coming toward statue)"],
|
| 198 |
+
["Focus: Panda in Forest (Animate: Drinking Tea)"],
|
| 199 |
+
["Focus: Kids Playing (Season: Winter)"],
|
| 200 |
+
["Focus: Cars in Street (Season: Rain, Daytime) (Shot from Distance) (Movement: Cars running)"]
|
| 201 |
+
],
|
| 202 |
+
fn=generate_image,
|
| 203 |
+
inputs=[prompt],
|
| 204 |
+
outputs=[video],
|
| 205 |
+
cache_examples="lazy",
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
# Launch the interface
|
| 209 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
torch
|
| 3 |
+
diffusers
|
| 4 |
+
transformers
|
| 5 |
+
safetensors
|
| 6 |
+
huggingface_hub
|
| 7 |
+
Pillow
|
| 8 |
+
spaces
|
style.css
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#video_output {
|
| 2 |
+
margin: auto;
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
.gradio-container {
|
| 6 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
h1 {
|
| 10 |
+
color: #2c3e50;
|
| 11 |
+
font-size: 2.5em;
|
| 12 |
+
margin-bottom: 1em;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.warning {
|
| 16 |
+
color: red;
|
| 17 |
+
font-weight: bold;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
.examples-table {
|
| 21 |
+
margin-top: 2em;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
.button-primary {
|
| 25 |
+
background-color: #3498db;
|
| 26 |
+
color: white;
|
| 27 |
+
padding: 10px 20px;
|
| 28 |
+
border-radius: 5px;
|
| 29 |
+
border: none;
|
| 30 |
+
cursor: pointer;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.button-primary:hover {
|
| 34 |
+
background-color: #2980b9;
|
| 35 |
+
}
|