Spaces:
Runtime error
Runtime error
kanishka089
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from diffusers import AnimateDiffPipeline, LCMScheduler, MotionAdapter
|
3 |
+
from diffusers.utils import export_to_gif
|
4 |
+
import os
|
5 |
+
import gc
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Environment setup
|
9 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
|
10 |
+
gc.collect()
|
11 |
+
torch.cuda.empty_cache()
|
12 |
+
gc.collect()
|
13 |
+
torch.cuda.empty_cache()
|
14 |
+
|
15 |
+
# Load models and pipeline
|
16 |
+
adapter = MotionAdapter.from_pretrained("wangfuyun/AnimateLCM", torch_dtype=torch.float16)
|
17 |
+
pipe = AnimateDiffPipeline.from_pretrained("emilianJR/epiCRealism", motion_adapter=adapter, torch_dtype=torch.float16)
|
18 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config, beta_schedule="linear")
|
19 |
+
pipe.load_lora_weights("wangfuyun/AnimateLCM", weight_name="AnimateLCM_sd15_t2v_lora.safetensors", adapter_name="lcm-lora")
|
20 |
+
pipe.set_adapters(["lcm-lora"], [0.8])
|
21 |
+
pipe.enable_vae_slicing()
|
22 |
+
pipe.enable_model_cpu_offload()
|
23 |
+
|
24 |
+
# Hardcoded predefined prompts
|
25 |
+
predefined_prompts = [
|
26 |
+
"640*480 pixels, high resolution, ultra realistic",
|
27 |
+
"bad quality, worse quality, low resolution"
|
28 |
+
]
|
29 |
+
|
30 |
+
def generate_gif(custom_prompt):
|
31 |
+
# Combine the predefined prompts with the custom prompt
|
32 |
+
prompt = custom_prompt + ", " + predefined_prompts[0]
|
33 |
+
negative_prompt = predefined_prompts[1]
|
34 |
+
|
35 |
+
output = pipe(
|
36 |
+
prompt=prompt,
|
37 |
+
negative_prompt=negative_prompt,
|
38 |
+
num_frames=32,
|
39 |
+
guidance_scale=2.0,
|
40 |
+
num_inference_steps=6,
|
41 |
+
generator=torch.Generator("cuda").manual_seed(0),
|
42 |
+
)
|
43 |
+
frames = output.frames[0]
|
44 |
+
export_to_gif(frames, "animatelcm.gif")
|
45 |
+
return "animatelcm.gif"
|
46 |
+
|
47 |
+
# Create Gradio interface
|
48 |
+
with gr.Blocks() as demo:
|
49 |
+
gr.Markdown("## Animate LCM GIF Generator")
|
50 |
+
custom_prompt_input = gr.Textbox(label="Custom Prompt", placeholder="Enter your custom prompt here...")
|
51 |
+
output_gif = gr.Image(label="Generated GIF")
|
52 |
+
generate_button = gr.Button("Generate GIF")
|
53 |
+
|
54 |
+
generate_button.click(fn=generate_gif, inputs=custom_prompt_input, outputs=output_gif)
|
55 |
+
|
56 |
+
# Launch the interface
|
57 |
+
demo.launch()
|