radames commited on
Commit
a17993f
·
1 Parent(s): 869e6be

Update to latest diffusers and enable CPU

Browse files

Thanks for duplicating the Space, this is a fix to make it work on CPU, however it's very slow!
but you can always enable a GPU Hardware and switch back.

Files changed (1) hide show
  1. app.py +117 -6
app.py CHANGED
@@ -1,6 +1,117 @@
1
- from os import system
2
- system('git clone https://huggingface.co/stabilityai/stable-diffusion-2-depth && cd stable-diffusion-2-depth && git lfs fetch && git lfs pull')
3
- system('pip install -e stablediffusion')
4
- system('curl -LJ https://github.com/isl-org/DPT/releases/download/1_0/dpt_hybrid-midas-501f0c75.pt --create-dirs -o stablediffusion/midas_models/dpt_hybrid-midas-501f0c75.pt')
5
- system('curl -LJ https://github.com/isl-org/DPT/releases/download/1_0/dpt_large-midas-2f21e586.pt --create-dirs -o stablediffusion/midas_models/dpt_large-midas-2f21e586.pt')
6
- system('cd stablediffusion && python scripts/gradio/depth2img.py configs/stable-diffusion/v2-midas-inference.yaml ../stable-diffusion-2-depth/512-depth-ema.ckpt')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ import numpy as np
5
+ from diffusers import StableDiffusionDepth2ImgPipeline
6
+ from pathlib import Path
7
+
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+ dept2img = StableDiffusionDepth2ImgPipeline.from_pretrained(
10
+ "stabilityai/stable-diffusion-2-depth",
11
+ torch_dtype=torch.float16,
12
+ ).to(device)
13
+
14
+
15
+ def pad_image(input_image):
16
+ pad_w, pad_h = np.max(((2, 2), np.ceil(
17
+ np.array(input_image.size) / 64).astype(int)), axis=0) * 64 - input_image.size
18
+ im_padded = Image.fromarray(
19
+ np.pad(np.array(input_image), ((0, pad_h), (0, pad_w), (0, 0)), mode='edge'))
20
+ w, h = im_padded.size
21
+ if w == h:
22
+ return im_padded
23
+ elif w > h:
24
+ new_image = Image.new(im_padded.mode, (w, w), (0, 0, 0))
25
+ new_image.paste(im_padded, (0, (w - h) // 2))
26
+ return new_image
27
+ else:
28
+ new_image = Image.new(im_padded.mode, (h, h), (0, 0, 0))
29
+ new_image.paste(im_padded, ((h - w) // 2, 0))
30
+ return new_image
31
+
32
+
33
+ def predict(input_image, prompt, negative_prompt, steps, num_samples, scale, seed, strength, depth_image=None):
34
+ depth = None
35
+ if depth_image is not None:
36
+ depth_image = pad_image(depth_image)
37
+ depth_image = depth_image.resize((512, 512))
38
+ depth = np.array(depth_image.convert("L"))
39
+ depth = depth.astype(np.float32) / 255.0
40
+ depth = depth[None, None]
41
+ depth = torch.from_numpy(depth)
42
+ init_image = input_image.convert("RGB")
43
+ image = pad_image(init_image) # resize to integer multiple of 32
44
+ image = image.resize((512, 512))
45
+ result = dept2img(
46
+ image=image,
47
+ prompt=prompt,
48
+ negative_prompt=negative_prompt,
49
+ depth_image=depth,
50
+ seed=seed,
51
+ strength=strength,
52
+ num_inference_steps=steps,
53
+ guidance_scale=scale,
54
+ num_images_per_prompt=num_samples,
55
+ )
56
+ return result['images']
57
+
58
+
59
+ block = gr.Blocks().queue()
60
+ with block:
61
+ with gr.Row():
62
+ with gr.Column():
63
+ gr.Markdown("## Stable Diffusion 2 Depth2Img")
64
+ gr.HTML("<p><a href='https://huggingface.co/spaces/radames/stable-diffusion-depth2img?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>")
65
+
66
+
67
+ with gr.Row():
68
+ with gr.Column():
69
+ input_image = gr.Image(source='upload', type="pil")
70
+ depth_image = gr.Image(
71
+ source='upload', type="pil", label="Depth image Optional", value=None)
72
+ prompt = gr.Textbox(label="Prompt")
73
+ negative_prompt = gr.Textbox(label="Negative Pompt")
74
+
75
+ run_button = gr.Button(label="Run")
76
+ with gr.Accordion("Advanced options", open=False):
77
+ num_samples = gr.Slider(
78
+ label="Images", minimum=1, maximum=4, value=1, step=1)
79
+ steps = gr.Slider(label="Steps", minimum=1,
80
+ maximum=50, value=50, step=1)
81
+ scale = gr.Slider(
82
+ label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1
83
+ )
84
+ strength = gr.Slider(
85
+ label="Strength", minimum=0.0, maximum=1.0, value=0.9, step=0.01
86
+ )
87
+ seed = gr.Slider(
88
+ label="Seed",
89
+ minimum=0,
90
+ maximum=2147483647,
91
+ step=1,
92
+ randomize=True,
93
+ )
94
+ with gr.Column():
95
+ gallery = gr.Gallery(label="Generated images", show_label=False).style(
96
+ grid=[2], height="auto")
97
+ gr.Examples(
98
+ examples=[
99
+ ["./examples/baby.jpg", "high definition photo of a baby astronaut space walking at the international space station with earth seeing from above in the background",
100
+ "", 50, 4, 9.0, 123123123, 0.8, None],
101
+ ["./examples/gol.jpg", "professional photo of a Elmo jumping between two high rises, beautiful colorful city landscape in the background",
102
+ "", 50, 4, 9.0, 1734133747, 0.9, None],
103
+ ["./examples/bag.jpg", "a photo of a bag of cookies in the bathroom", "low light, dark, blurry", 50, 4, 9.0, 1734133747, 0.9, "./examples/depth.jpg"],
104
+ ["./examples/smile_face.jpg", "a hand holding a very spherical orange", "low light, dark, blurry", 50, 4, 6.0, 961736534, 0.5, "./examples/smile_depth.jpg"]
105
+
106
+ ],
107
+ inputs=[input_image, prompt, negative_prompt, steps,
108
+ num_samples, scale, seed, strength, depth_image],
109
+ outputs=[gallery],
110
+ fn=predict,
111
+ cache_examples=True,
112
+ )
113
+ run_button.click(fn=predict, inputs=[input_image, prompt, negative_prompt,
114
+ steps, num_samples, scale, seed, strength, depth_image], outputs=[gallery])
115
+
116
+
117
+ block.launch(show_api=False)