Spaces:
Runtime error
Runtime error
File size: 12,613 Bytes
8c4daf1 b83ebfb 8c4daf1 3b76971 8c4daf1 b83ebfb 8c4daf1 c8d7214 810b825 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 556b961 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 5b09d17 b83ebfb 8c4daf1 5b09d17 8c4daf1 5b09d17 8c4daf1 181019b c7bf29f b83ebfb 181019b b83ebfb 181019b b83ebfb 181019b b83ebfb 181019b 8c4daf1 181019b 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 c7bf29f 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 556b961 b83ebfb 8c4daf1 b83ebfb 556b961 b83ebfb 8c4daf1 b83ebfb 8c4daf1 b83ebfb 5b09d17 8c4daf1 b83ebfb 5b09d17 b83ebfb 8c4daf1 c7bf29f 8c4daf1 b83ebfb 8c4daf1 b83ebfb |
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
import os
from io import BytesIO
import base64
from functools import partial
from PIL import Image, ImageOps
import gradio as gr
from makeavid_sd.inference import (
InferenceUNetPseudo3D,
jnp,
SCHEDULERS
)
print(os.environ.get('XLA_PYTHON_CLIENT_PREALLOCATE', 'NotSet'))
print(os.environ.get('XLA_PYTHON_CLIENT_ALLOCATOR', 'NotSet'))
_preheat: bool = False
_seen_compilations = set()
_model = InferenceUNetPseudo3D(
model_path = '/mnt/work1/make_a_vid/makeavid-space/model/model',
dtype = jnp.float16,
hf_auth_token = os.environ.get('HUGGING_FACE_HUB_TOKEN', None)
)
if _model.failed != False:
trace = f'```{_model.failed}```'
with gr.Blocks(title = 'Make-A-Video Stable Diffusion JAX', analytics_enabled = False) as demo:
exception = gr.Markdown(trace)
demo.launch()
_output_formats = (
'webp', 'gif'
)
# gradio is illiterate. type hints make it go poopoo in pantsu.
def generate(
prompt = 'An elderly man having a great time in the park.',
neg_prompt = '',
hint_image = None,
inference_steps = 20,
cfg = 15.0,
cfg_image = 9.0,
seed = 0,
fps = 24,
num_frames = 24,
height = 512,
width = 512,
scheduler_type = 'DPM',
output_format = 'webp'
) -> str:
num_frames = int(num_frames)
inference_steps = int(inference_steps)
height = int(height)
width = int(width)
height = (height // 64) * 64
width = (width // 64) * 64
cfg = max(cfg, 1.0)
cfg_image = max(cfg_image, 1.0)
seed = int(seed)
if seed < 0:
seed = -seed
if hint_image is not None:
if hint_image.mode != 'RGB':
hint_image = hint_image.convert('RGB')
if hint_image.size != (width, height):
hint_image = ImageOps.fit(hint_image, (width, height), method = Image.Resampling.LANCZOS)
if scheduler_type not in SCHEDULERS:
scheduler_type = 'DPM'
output_format = output_format.lower()
if output_format not in _output_formats:
output_format = 'webp'
mask_image = None
images = _model.generate(
prompt = [prompt] * _model.device_count,
neg_prompt = neg_prompt,
hint_image = hint_image,
mask_image = mask_image,
inference_steps = inference_steps,
cfg = cfg,
cfg_image = cfg_image,
height = height,
width = width,
num_frames = num_frames,
seed = seed,
scheduler_type = scheduler_type
)
_seen_compilations.add((hint_image is None, inference_steps, height, width, num_frames))
buffer = BytesIO()
images[1].save(
buffer,
format = output_format,
save_all = True,
append_images = images[2:],
loop = 0,
duration = round(1000 / fps),
allow_mixed = True
)
data = base64.b64encode(buffer.getvalue()).decode()
buffer.close()
data = f'data:image/{output_format};base64,' + data
return data
def check_if_compiled(hint_image, inference_steps, height, width, num_frames, scheduler_type, message):
height = int(height)
width = int(width)
inference_steps = int(inference_steps)
height = (height // 64) * 64
width = (width // 64) * 64
if (hint_image is None, inference_steps, height, width, num_frames, scheduler_type) in _seen_compilations:
return ''
else:
return f"""{message}"""
if _preheat:
print('\npreheating the oven')
generate(
prompt = 'preheating the oven',
neg_prompt = '',
image = None,
inference_steps = 20,
cfg = 12.0,
seed = 0
)
print('Entertaining the guests with sailor songs played on an old piano.')
dada = generate(
prompt = 'Entertaining the guests with sailor songs played on an old harmonium.',
neg_prompt = '',
image = Image.new('RGB', size = (512, 512), color = (0, 0, 0)),
inference_steps = 20,
cfg = 12.0,
seed = 0
)
print('dinner is ready\n')
with gr.Blocks(title = 'Make-A-Video Stable Diffusion JAX', analytics_enabled = False) as demo:
variant = 'panel'
with gr.Row():
with gr.Column():
intro1 = gr.Markdown("""
# Make-A-Video Stable Diffusion JAX
We have extended a pretrained LDM inpainting image generation model with temporal convolutions and attention.
By taking advantage of the extra 5 input channels of the inpaint model, we guide the video generation with a hint image.
In this demo the hint image can be given by the user, otherwise it is generated by an generative image model.
The temporal layers are a port of [Make-A-Video PyTorch](https://github.com/lucidrains/make-a-video-pytorch) to FLAX.
The convolution is pseudo 3D and seperately convolves accross the spatial dimension in 2D and over the temporal dimension in 1D.
Temporal attention is purely self attention and also separately attends to time.
Only the new temporal layers have been fine tuned on a dataset of videos themed around dance.
The model has been trained for 80 epochs on a dataset of 18,000 Videos with 120 frames each, randomly selecting a 24 frame range from each sample.
See model and dataset links in the metadata.
Model implementation and training code can be found at <https://github.com/lopho/makeavid-sd-tpu>
""")
with gr.Column():
intro3 = gr.Markdown("""
**Please be patient. The model might have to compile with current parameters.**
This can take up to 5 minutes on the first run, and 2-3 minutes on later runs.
The compilation will be cached and consecutive runs with the same parameters
will be much faster.
Changes to the following parameters require the model to compile
- Number of frames
- Width & Height
- Inference steps
- Input image vs. no input image
- Noise scheduler type
If you encounter any issues, please report them here: [Space discussions](https://huggingface.co/spaces/TempoFunk/makeavid-sd-jax/discussions)
""")
with gr.Row(variant = variant):
with gr.Column():
with gr.Row():
#cancel_button = gr.Button(value = 'Cancel')
submit_button = gr.Button(value = 'Make A Video', variant = 'primary')
prompt_input = gr.Textbox(
label = 'Prompt',
value = 'They are dancing in the club but everybody is a 3d cg hairy monster wearing a hairy costume.',
interactive = True
)
neg_prompt_input = gr.Textbox(
label = 'Negative prompt (optional)',
value = 'monochrome, saturated',
interactive = True
)
cfg_input = gr.Slider(
label = 'Guidance scale video',
minimum = 1.0,
maximum = 20.0,
step = 0.1,
value = 15.0,
interactive = True
)
cfg_image_input = gr.Slider(
label = 'Guidance scale hint (no effect with input image)',
minimum = 1.0,
maximum = 20.0,
step = 0.1,
value = 9.0,
interactive = True
)
seed_input = gr.Number(
label = 'Random seed',
value = 0,
interactive = True,
precision = 0
)
image_input = gr.Image(
label = 'Hint image (optional)',
interactive = True,
image_mode = 'RGB',
type = 'pil',
optional = True,
source = 'upload',
value = 'example_input.png'
)
inference_steps_input = gr.Slider(
label = 'Steps',
minimum = 2,
maximum = 100,
value = 20,
step = 1,
interactive = True
)
num_frames_input = gr.Slider(
label = 'Number of frames to generate',
minimum = 1,
maximum = 24,
step = 1,
value = 24,
interactive = True
)
width_input = gr.Slider(
label = 'Width',
minimum = 64,
maximum = 576,
step = 64,
value = 512,
interactive = True
)
height_input = gr.Slider(
label = 'Height',
minimum = 64,
maximum = 576,
step = 64,
value = 512,
interactive = True
)
scheduler_input = gr.Dropdown(
label = 'Noise scheduler',
choices = list(SCHEDULERS.keys()),
value = 'DPM',
interactive = True
)
with gr.Row():
fps_input = gr.Slider(
label = 'Output FPS',
minimum = 1,
maximum = 1000,
step = 1,
value = 12,
interactive = True
)
output_format = gr.Dropdown(
label = 'Output format',
choices = _output_formats,
value = 'gif',
interactive = True
)
with gr.Column():
#will_trigger = gr.Markdown('')
patience = gr.Markdown('**Please be patient. The model might have to compile with current parameters.**')
image_output = gr.Image(
label = 'Output',
value = 'example.webp',
interactive = False
)
#trigger_inputs = [ image_input, inference_steps_input, height_input, width_input, num_frames_input, scheduler_input ]
#trigger_check_fun = partial(check_if_compiled, message = 'Current parameters need compilation.')
#height_input.change(fn = trigger_check_fun, inputs = trigger_inputs, outputs = will_trigger)
#width_input.change(fn = trigger_check_fun, inputs = trigger_inputs, outputs = will_trigger)
#num_frames_input.change(fn = trigger_check_fun, inputs = trigger_inputs, outputs = will_trigger)
#image_input.change(fn = trigger_check_fun, inputs = trigger_inputs, outputs = will_trigger)
#inference_steps_input.change(fn = trigger_check_fun, inputs = trigger_inputs, outputs = will_trigger)
#scheduler_input.change(fn = trigger_check_fun, inputs = trigger_inputs, outputs = will_trigger)
submit_button.click(
fn = generate,
inputs = [
prompt_input,
neg_prompt_input,
image_input,
inference_steps_input,
cfg_input,
cfg_image_input,
seed_input,
fps_input,
num_frames_input,
height_input,
width_input,
scheduler_input,
output_format
],
outputs = image_output,
postprocess = False
)
#cancel_button.click(fn = lambda: None, cancels = ev)
demo.queue(concurrency_count = 1, max_size = 12)
demo.launch()
# Photorealistic fantasy oil painting of the angry minotaur in a threatening pose by Randy Vargas.
# A girl is dancing by a beautiful lake by sophie anderson and greg rutkowski and alphonse mucha.
# They are dancing in the club but everybody is a 3d cg hairy monster wearing a hairy costume.
|