ahmdliaqat commited on
Commit
5c76a8f
·
1 Parent(s): c8dc89b
Files changed (2) hide show
  1. app.py +146 -0
  2. style.css +7 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Constants
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
+ # CPU configuration
25
+ device = "cpu"
26
+ dtype = torch.float32
27
+
28
+ # Initialize pipeline for CPU
29
+ pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
30
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
31
+
32
+ # Safety checkers
33
+ from transformers import CLIPFeatureExtractor
34
+
35
+ feature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")
36
+
37
+ # Function
38
+ @spaces.CPU(duration=30,queue=False)
39
+ def generate_image(prompt, base="Realistic", motion="", step=8, progress=gr.Progress()):
40
+ global step_loaded
41
+ global base_loaded
42
+ global motion_loaded
43
+ print(prompt, base, step)
44
+
45
+ if step_loaded != step:
46
+ repo = "ByteDance/AnimateDiff-Lightning"
47
+ ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
48
+ pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
49
+ step_loaded = step
50
+
51
+ if base_loaded != base:
52
+ pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)
53
+ base_loaded = base
54
+
55
+ if motion_loaded != motion:
56
+ pipe.unload_lora_weights()
57
+ if motion != "":
58
+ pipe.load_lora_weights(motion, adapter_name="motion")
59
+ pipe.set_adapters(["motion"], [0.7])
60
+ motion_loaded = motion
61
+
62
+ progress((0, step))
63
+ def progress_callback(i, t, z):
64
+ progress((i+1, step))
65
+
66
+ output = pipe(prompt=prompt, guidance_scale=1.2, num_inference_steps=step, callback=progress_callback, callback_steps=1)
67
+
68
+ name = str(uuid.uuid4()).replace("-", "")
69
+ path = f"/tmp/{name}.mp4"
70
+ export_to_video(output.frames[0], path, fps=10)
71
+ return path
72
+
73
+
74
+ # Gradio Interface
75
+ with gr.Blocks(css="style.css") as demo:
76
+ gr.HTML(
77
+ "<h1><center>Textual Imagination : A Text To Video Synthesis</center></h1>"
78
+ )
79
+ with gr.Group():
80
+ with gr.Row():
81
+ prompt = gr.Textbox(
82
+ label='Prompt'
83
+ )
84
+ with gr.Row():
85
+ select_base = gr.Dropdown(
86
+ label='Base model',
87
+ choices=[
88
+ "Cartoon",
89
+ "Realistic",
90
+ "3d",
91
+ "Anime",
92
+ ],
93
+ value=base_loaded,
94
+ interactive=True
95
+ )
96
+ select_motion = gr.Dropdown(
97
+ label='Motion',
98
+ choices=[
99
+ ("Default", ""),
100
+ ("Zoom in", "guoyww/animatediff-motion-lora-zoom-in"),
101
+ ("Zoom out", "guoyww/animatediff-motion-lora-zoom-out"),
102
+ ("Tilt up", "guoyww/animatediff-motion-lora-tilt-up"),
103
+ ("Tilt down", "guoyww/animatediff-motion-lora-tilt-down"),
104
+ ("Pan left", "guoyww/animatediff-motion-lora-pan-left"),
105
+ ("Pan right", "guoyww/animatediff-motion-lora-pan-right"),
106
+ ("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
107
+ ("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
108
+ ],
109
+ value="guoyww/animatediff-motion-lora-zoom-in",
110
+ interactive=True
111
+ )
112
+ select_step = gr.Dropdown(
113
+ label='Inference steps',
114
+ choices=[
115
+ ('1-Step', 1),
116
+ ('2-Step', 2),
117
+ ('4-Step', 4),
118
+ ('8-Step', 8),
119
+ ],
120
+ value=4,
121
+ interactive=True
122
+ )
123
+ submit = gr.Button(
124
+ scale=1,
125
+ variant='primary'
126
+ )
127
+ video = gr.Video(
128
+ label='AnimateDiff-Lightning',
129
+ autoplay=True,
130
+ height=512,
131
+ width=512,
132
+ elem_id="video_output"
133
+ )
134
+
135
+ gr.on(triggers=[
136
+ submit.click,
137
+ prompt.submit
138
+ ],
139
+ fn = generate_image,
140
+ inputs = [prompt, select_base, select_motion, select_step],
141
+ outputs = [video],
142
+ api_name = "instant_video",
143
+ queue = False
144
+ )
145
+
146
+ demo.queue().launch()
style.css ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ .gradio-container {
2
+ max-width: 800px !important;
3
+ }
4
+
5
+ #video_output {
6
+ margin: 0 auto
7
+ }