orderlymirror commited on
Commit
e89bc50
·
verified ·
1 Parent(s): 624eb24

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +164 -0
  2. requirements.txt +8 -0
  3. style.css +7 -0
app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Ensure model and scheduler are initialized in GPU-enabled function
25
+ if not torch.cuda.is_available():
26
+ raise NotImplementedError("No GPU detected!")
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, resolution="Square", progress=gr.Progress()):
41
+ global step_loaded
42
+ global base_loaded
43
+ global motion_loaded
44
+
45
+ print(prompt, base, step, resolution)
46
+
47
+ # Set resolution
48
+ if resolution == "Square":
49
+ width, height = 512, 512
50
+ elif resolution == "Horizontal":
51
+ width, height = 1280, 720
52
+
53
+ if step_loaded != step:
54
+ repo = "ByteDance/AnimateDiff-Lightning"
55
+ ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
56
+ pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
57
+ step_loaded = step
58
+
59
+ if base_loaded != base:
60
+ pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)
61
+ base_loaded = base
62
+
63
+ if motion_loaded != motion:
64
+ pipe.unload_lora_weights()
65
+ if motion != "":
66
+ pipe.load_lora_weights(motion, adapter_name="motion")
67
+ pipe.set_adapters(["motion"], [0.7])
68
+ motion_loaded = motion
69
+
70
+ progress((0, step))
71
+ def progress_callback(i, t, z):
72
+ progress((i+1, step))
73
+
74
+ output = pipe(prompt=prompt, guidance_scale=1.2, num_inference_steps=step, width=width, height=height, callback=progress_callback, callback_steps=1)
75
+
76
+ name = str(uuid.uuid4()).replace("-", "")
77
+ path = f"/tmp/{name}.mp4"
78
+ export_to_video(output.frames[0], path, fps=10)
79
+ return path
80
+
81
+
82
+ # Gradio Interface
83
+ with gr.Blocks(css="style.css") as demo:
84
+ gr.HTML(
85
+ "<h1><center>Textual Imagination : A Text To Video Synthesis</center></h1>"
86
+ )
87
+ with gr.Group():
88
+ with gr.Row():
89
+ prompt = gr.Textbox(
90
+ label='Prompt'
91
+ )
92
+ with gr.Row():
93
+ select_base = gr.Dropdown(
94
+ label='Base model',
95
+ choices=[
96
+ "Cartoon",
97
+ "Realistic",
98
+ "3d",
99
+ "Anime",
100
+ ],
101
+ value=base_loaded,
102
+ interactive=True
103
+ )
104
+ select_motion = gr.Dropdown(
105
+ label='Motion',
106
+ choices=[
107
+ ("Default", ""),
108
+ ("Zoom in", "guoyww/animatediff-motion-lora-zoom-in"),
109
+ ("Zoom out", "guoyww/animatediff-motion-lora-zoom-out"),
110
+ ("Tilt up", "guoyww/animatediff-motion-lora-tilt-up"),
111
+ ("Tilt down", "guoyww/animatediff-motion-lora-tilt-down"),
112
+ ("Pan left", "guoyww/animatediff-motion-lora-pan-left"),
113
+ ("Pan right", "guoyww/animatediff-motion-lora-pan-right"),
114
+ ("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
115
+ ("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
116
+ ],
117
+ value="guoyww/animatediff-motion-lora-zoom-in",
118
+ interactive=True
119
+ )
120
+ select_step = gr.Dropdown(
121
+ label='Inference steps',
122
+ choices=[
123
+ ('1-Step', 1),
124
+ ('2-Step', 2),
125
+ ('4-Step', 4),
126
+ ('8-Step', 8),
127
+ ],
128
+ value=4,
129
+ interactive=True
130
+ )
131
+ select_resolution = gr.Dropdown(
132
+ label='Resolution',
133
+ choices=[
134
+ "Square",
135
+ "Horizontal",
136
+ ],
137
+ value="Square",
138
+ interactive=True
139
+ )
140
+ submit = gr.Button(
141
+ scale=1,
142
+ variant='primary'
143
+ )
144
+ video = gr.Video(
145
+ label='AnimateDiff-Lightning',
146
+ autoplay=True,
147
+ height=512,
148
+ width=512,
149
+ elem_id="video_output"
150
+ )
151
+
152
+ gr.on(
153
+ triggers=[
154
+ submit.click,
155
+ prompt.submit
156
+ ],
157
+ fn=generate_image,
158
+ inputs=[prompt, select_base, select_motion, select_step, select_resolution],
159
+ outputs=[video],
160
+ api_name="instant_video",
161
+ queue=False
162
+ )
163
+
164
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ gradio
4
+ torch
5
+ transformers
6
+ opencv-python
7
+ peft
8
+ spaces
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
+ }