Spaces:
Sleeping
Sleeping
File size: 6,045 Bytes
630d83d 13aa2f4 3bea95f 13aa2f4 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 3bea95f 13aa2f4 630d83d 13aa2f4 3bea95f 13aa2f4 3bea95f 13aa2f4 3bea95f 13aa2f4 3bea95f 13aa2f4 3bea95f 13aa2f4 3bea95f 13aa2f4 3bea95f 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 13aa2f4 630d83d 3bea95f 13aa2f4 630d83d 13aa2f4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import torch
import gradio as gr
import sys
import traceback
# Improved import with fallback handling
try:
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
from diffusers.utils import export_to_gif
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
except ImportError as e:
print(f"Import Error: {e}")
sys.exit(1)
# Comprehensive Base Models
BASE_MODELS = {
"Realistic": [
"emilianJR/epiCRealism",
"SG161222/Realistic_Vision_V5.1_noVAE",
"Lykon/dreamshaper-8",
"digiplay/AbsoluteReality_v1.8.1",
],
"Anime & Cartoon": [
"cagliostroaic/ToonYou",
"Sangyun/IMP",
"Lykon/Mistoon_Anime",
"digiplay/DynaVision_v1.0",
]
}
def detect_device():
"""
Robust device detection with detailed logging
"""
try:
if torch.cuda.is_available():
print(f"CUDA Available. Using GPU: {torch.cuda.get_device_name(0)}")
return "cuda"
elif torch.backends.mps.is_available():
print("Using MPS (Apple Silicon)")
return "mps"
else:
print("No GPU detected. Falling back to CPU.")
return "cpu"
except Exception as e:
print(f"Device detection error: {e}")
return "cpu"
def generate_video(prompt, base_model, steps=4, motion_strength=0.7, guidance_scale=1.0):
try:
device = detect_device()
dtype = torch.float16 if device == "cuda" else torch.float32
# Official AnimateDiff-Lightning Repository
repo = "ByteDance/AnimateDiff-Lightning"
ckpt = f"animatediff_lightning_{steps}step_diffusers.safetensors"
# Motion Adapter Setup with Error Handling
try:
adapter = MotionAdapter().to(device, dtype)
adapter.load_state_dict(
load_file(
hf_hub_download(repo, ckpt),
device=device
)
)
except Exception as adapter_error:
print(f"Motion Adapter Loading Error: {adapter_error}")
return None
# Flexible Model Loading
try:
pipe = AnimateDiffPipeline.from_pretrained(
base_model,
motion_adapter=adapter,
torch_dtype=dtype
).to(device)
except Exception as model_error:
print(f"Model loading error with {base_model}: {model_error}")
base_model = "emilianJR/epiCRealism"
pipe = AnimateDiffPipeline.from_pretrained(
base_model,
motion_adapter=adapter,
torch_dtype=dtype
).to(device)
pipe.scheduler = EulerDiscreteScheduler.from_config(
pipe.scheduler.config,
timestep_spacing="trailing",
beta_schedule="linear"
)
# Generation with Enhanced Error Handling
try:
output = pipe(
prompt=prompt,
guidance_scale=guidance_scale,
num_inference_steps=steps
)
gif_path = "animation.gif"
export_to_gif(output.frames[0], gif_path)
return gif_path
except Exception as gen_error:
print(f"Video Generation Error: {gen_error}")
return None
except Exception as e:
print(f"Unexpected error in video generation: {e}")
traceback.print_exc()
return None
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("## AnimateDiff-Lightning Video Generator")
with gr.Row():
prompt = gr.Textbox(
label="Prompt",
placeholder="Describe the video you want to generate...",
lines=3
)
base_model = gr.Dropdown(
choices=[
*BASE_MODELS["Realistic"],
*BASE_MODELS["Anime & Cartoon"]
],
label="Base Model",
value="emilianJR/epiCRealism"
)
with gr.Row():
steps = gr.Slider(
minimum=1,
maximum=8,
step=1,
value=4,
label="Inference Steps"
)
guidance_scale = gr.Slider(
minimum=0.1,
maximum=20,
step=0.1,
value=1.0,
label="Guidance Scale"
)
motion_strength = gr.Slider(
minimum=0,
maximum=1,
step=0.1,
value=0.7,
label="Motion Strength"
)
generate_btn = gr.Button("Generate Video", variant="primary")
output = gr.Image(label="Generated Video")
generate_btn.click(
generate_video,
inputs=[prompt, base_model, steps, motion_strength, guidance_scale],
outputs=output
)
return demo
def main():
print("Initializing AnimateDiff-Lightning Gradio Interface...")
try:
demo = create_interface()
# Public sharing with detailed config
demo.launch(
share=True, # Create public link
debug=True, # Detailed error reporting
show_error=True, # Display errors in UI
server_name="0.0.0.0" # Accessible from any IP
)
except Exception as e:
print(f"Gradio Launch Error: {e}")
traceback.print_exc()
if __name__ == "__main__":
main() |