File size: 3,962 Bytes
95e4531
 
 
 
 
 
 
 
 
1a7b771
1359190
 
 
95e4531
81b62ca
1359190
 
1a7b771
 
 
95e4531
1a7b771
643db09
95e4531
 
 
 
 
1a7b771
95e4531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7b771
95e4531
 
 
1a7b771
95e4531
 
 
 
 
 
 
 
 
 
 
81b62ca
95e4531
 
1a7b771
95e4531
1a7b771
40ac53b
1359190
95e4531
1359190
 
95e4531
1359190
95e4531
95f082f
1a7b771
 
95f082f
95e4531
def5a24
 
 
 
 
 
95e4531
 
 
def5a24
95e4531
 
 
 
40ac53b
 
95e4531
 
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
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from utils import write_video, dummy
from PIL import Image
import numpy as np
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
import torch
import gradio as gr


orig_prompt = "Create a relaxing atmosphere with the use of plants and other natural elements. Such as a hanging terrarium or a wall-mounted planter. Include plenty of storage options to keep the space organized and clutter-free. Consider adding a vanity with double sinks and plenty of drawers and cabinets. As well as a wall mounted medicine and towel storage."
orig_negative_prompt = "lurry, bad art, blurred, text, watermark"

def stable_diffusion_zoom_out(
  repo_id,
  original_prompt,
  negative_prompt,
  steps,
  num_frames,
  fps
):
    
  pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16")
  pipe.set_use_memory_efficient_attention_xformers(True)
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
  pipe = pipe.to("cuda")
  pipe.safety_checker = dummy

  current_image = Image.new(mode="RGBA", size=(512,512))
  mask_image = np.array(current_image)[:,:,3] # assume image has alpha mask (use .mode to check for "RGBA")
  mask_image = Image.fromarray(255-mask_image).convert("RGB")
  current_image = current_image.convert("RGB")

  num_images = 1
  prompt = [original_prompt] * num_images
  negative_prompt = [negative_prompt] * num_images

  images = pipe(prompt=prompt, negative_prompt=negative_prompt, image=current_image, mask_image=mask_image, num_inference_steps=25)[0]
  current_image = images[0]

  all_frames = []
  all_frames.append(current_image)

  for i in range(num_frames):
    next_image = np.array(current_image.convert("RGBA"))*0
    prev_image = current_image.resize((512-2*steps,512-2*steps))
    prev_image = prev_image.convert("RGBA")
    prev_image = np.array(prev_image)
    next_image[:, :, 3] = 1
    next_image[steps:512-steps,steps:512-steps,:] = prev_image
    prev_image = Image.fromarray(next_image)
    current_image = prev_image
    mask_image = np.array(current_image)[:,:,3] # assume image has alpha mask (use .mode to check for "RGBA")
    mask_image = Image.fromarray(255-mask_image).convert("RGB")
    current_image = current_image.convert("RGB")
    images = pipe(prompt=prompt, negative_prompt=negative_prompt, image=current_image, mask_image=mask_image, num_inference_steps=25)[0]
    current_image = images[0]
    current_image.paste(prev_image, mask=prev_image)
    all_frames.append(current_image)
    
  save_path = "infinite_zoom_out.mp4"  
  write_video(save_path, all_frames, fps=fps)
  return save_path


inputs = [
  gr.inputs.Textbox(lines=1, default="stabilityai/stable-diffusion-2-inpainting", label="Model ID"),
  gr.inputs.Textbox(lines=5, default=orig_prompt, label="Prompt"),
  gr.inputs.Textbox(lines=1, default=orig_negative_prompt, label="Negative Prompt"),
  gr.inputs.Slider(minimum=1, maximum=64, default=32, label="Steps"),
  gr.inputs.Slider(minimum=1, maximum=500, default=10, step=10, label="Frames"),
  gr.inputs.Slider(minimum=1, maximum=100, default=16, step=1, label="FPS")
]

output = gr.outputs.Video()
examples = [
  ["stabilityai/stable-diffusion-2-inpainting", orig_prompt, orig_negative_prompt, 32, 50, 16]
]

title = "Stable Diffusion Infinite Zoom Out"
description = """<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.
<br/>
<a href="https://huggingface.co/spaces/kadirnar/stable-diffusion-2-infinite-zoom-out?duplicate=true">
<img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
<p/>"""


demo_app = gr.Interface(
    fn=stable_diffusion_zoom_out,
    description=description,
    inputs=inputs,
    outputs=output,
    title=title,
    theme='huggingface',
    examples=examples,
    cache_examples=True
)
demo_app.launch(debug=True, enable_queue=True)