kadirnar commited on
Commit
a0157fa
1 Parent(s): 2897826

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -51
app.py CHANGED
@@ -1,80 +1,68 @@
1
  from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
2
- from utils import write_video, dummy
3
  from PIL import Image
4
- import numpy as np
 
5
  import os
6
  os.environ["CUDA_VISIBLE_DEVICES"]="0"
7
- import torch
8
- import gradio as gr
9
 
10
 
11
- 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."
12
  orig_negative_prompt = "lurry, bad art, blurred, text, watermark"
 
13
 
14
  def stable_diffusion_zoom_out(
15
  repo_id,
16
  original_prompt,
17
  negative_prompt,
18
- steps,
19
  num_frames,
20
- fps
21
- ):
 
22
 
23
- pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16")
24
- pipe.set_use_memory_efficient_attention_xformers(True)
25
- pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
26
- pipe = pipe.to("cuda")
27
- pipe.safety_checker = dummy
28
-
29
- current_image = Image.new(mode="RGBA", size=(512,512))
30
- mask_image = np.array(current_image)[:,:,3] # assume image has alpha mask (use .mode to check for "RGBA")
31
- mask_image = Image.fromarray(255-mask_image).convert("RGB")
32
- current_image = current_image.convert("RGB")
33
 
34
- num_images = 1
35
- prompt = [original_prompt] * num_images
36
- negative_prompt = [negative_prompt] * num_images
 
37
 
38
- images = pipe(prompt=prompt, negative_prompt=negative_prompt, image=current_image, mask_image=mask_image, num_inference_steps=25)[0]
39
- current_image = images[0]
 
40
 
41
- all_frames = []
42
- all_frames.append(current_image)
 
 
 
 
 
 
43
 
44
- for i in range(num_frames):
45
- next_image = np.array(current_image.convert("RGBA"))*0
46
- prev_image = current_image.resize((512-2*steps,512-2*steps))
47
- prev_image = prev_image.convert("RGBA")
48
- prev_image = np.array(prev_image)
49
- next_image[:, :, 3] = 1
50
- next_image[steps:512-steps,steps:512-steps,:] = prev_image
51
- prev_image = Image.fromarray(next_image)
52
- current_image = prev_image
53
- mask_image = np.array(current_image)[:,:,3] # assume image has alpha mask (use .mode to check for "RGBA")
54
- mask_image = Image.fromarray(255-mask_image).convert("RGB")
55
- current_image = current_image.convert("RGB")
56
- images = pipe(prompt=prompt, negative_prompt=negative_prompt, image=current_image, mask_image=mask_image, num_inference_steps=25)[0]
57
- current_image = images[0]
58
- current_image.paste(prev_image, mask=prev_image)
59
- all_frames.append(current_image)
60
-
61
- save_path = "infinite_zoom_out.mp4"
62
- write_video(save_path, all_frames, fps=fps)
63
- return save_path
64
 
65
 
66
  inputs = [
67
- gr.inputs.Textbox(lines=1, default="stabilityai/stable-diffusion-2-inpainting", label="Model ID"),
68
  gr.inputs.Textbox(lines=5, default=orig_prompt, label="Prompt"),
69
  gr.inputs.Textbox(lines=1, default=orig_negative_prompt, label="Negative Prompt"),
70
- gr.inputs.Slider(minimum=1, maximum=64, default=32, label="Steps"),
71
- gr.inputs.Slider(minimum=1, maximum=500, default=10, step=10, label="Frames"),
72
- gr.inputs.Slider(minimum=1, maximum=100, default=16, step=1, label="FPS")
 
73
  ]
74
 
75
  output = gr.outputs.Video()
76
  examples = [
77
- ["stabilityai/stable-diffusion-2-inpainting", orig_prompt, orig_negative_prompt, 32, 50, 16]
78
  ]
79
 
80
  title = "Stable Diffusion Infinite Zoom Out"
@@ -84,7 +72,6 @@ description = """<p>For faster inference without waiting in queue, you may dupli
84
  <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
85
  <p/>"""
86
 
87
-
88
  demo_app = gr.Interface(
89
  fn=stable_diffusion_zoom_out,
90
  description=description,
 
1
  from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
2
+ from utils import write_video, dummy, preprocess_image, preprocess_mask_image
3
  from PIL import Image
4
+ import gradio as gr
5
+ import torch
6
  import os
7
  os.environ["CUDA_VISIBLE_DEVICES"]="0"
 
 
8
 
9
 
10
+ orig_prompt = "Ancient underground architectural ruins of Hong Kong in a flooded apocalypse landscape of dead skyscrapers"
11
  orig_negative_prompt = "lurry, bad art, blurred, text, watermark"
12
+ model_list = ["stabilityai/stable-diffusion-2-inpainting", "runwayml/stable-diffusion-inpainting"]
13
 
14
  def stable_diffusion_zoom_out(
15
  repo_id,
16
  original_prompt,
17
  negative_prompt,
18
+ step_size,
19
  num_frames,
20
+ fps,
21
+ num_inference_steps
22
+ ):
23
 
24
+ pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
25
+ pipe.set_use_memory_efficient_attention_xformers(True)
26
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
27
+ pipe = pipe.to("cuda")
28
+ pipe.safety_checker = dummy
 
 
 
 
 
29
 
30
+ new_image = Image.new(mode="RGBA", size=(512,512))
31
+ current_image, mask_image = preprocess_mask_image(new_image)
32
+
33
+ current_image = pipe(prompt=[original_prompt], negative_prompt=[negative_prompt], image=current_image, mask_image=mask_image, num_inference_steps=num_inference_steps).images[0]
34
 
35
+
36
+ all_frames = []
37
+ all_frames.append(current_image)
38
 
39
+ for i in range(num_frames):
40
+ prev_image = preprocess_image(current_image, step_size, 512)
41
+ current_image = prev_image
42
+ current_image, mask_image = preprocess_mask_image(current_image)
43
+ current_image = pipe(prompt=[original_prompt], negative_prompt=[negative_prompt], image=current_image, mask_image=mask_image, num_inference_steps=num_inference_steps).images[0]
44
+
45
+ current_image.paste(prev_image, mask=prev_image)
46
+ all_frames.append(current_image)
47
 
48
+ save_path = "output.mp4"
49
+ write_video(save_path, all_frames, fps=fps)
50
+ return save_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
 
53
  inputs = [
54
+ gr.Dropdown(model_list, value=model_list[0], label="Model"),
55
  gr.inputs.Textbox(lines=5, default=orig_prompt, label="Prompt"),
56
  gr.inputs.Textbox(lines=1, default=orig_negative_prompt, label="Negative Prompt"),
57
+ gr.inputs.Slider(minimum=1, maximum=120, default=25, step=5, label="Steps"),
58
+ gr.inputs.Slider(minimum=1, maximum=100, default=10, step=5, label="Frames"),
59
+ gr.inputs.Slider(minimum=1, maximum=100, default=16, step=1, label="FPS"),
60
+ gr.inputs.Slider(minimum=1, maximum=100, default=15, step=1, label="Inference Steps")
61
  ]
62
 
63
  output = gr.outputs.Video()
64
  examples = [
65
+ ["stabilityai/stable-diffusion-2-inpainting", orig_prompt, orig_negative_prompt, 25, 10, 16, 15],
66
  ]
67
 
68
  title = "Stable Diffusion Infinite Zoom Out"
 
72
  <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
73
  <p/>"""
74
 
 
75
  demo_app = gr.Interface(
76
  fn=stable_diffusion_zoom_out,
77
  description=description,