michaelj commited on
Commit
6ad0b38
1 Parent(s): faf9078
Files changed (4) hide show
  1. constants.py +211 -0
  2. model.py +43 -0
  3. requirements.txt +4 -2
  4. utils.py +15 -0
constants.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ DESCRIPTION = """
2
+ # Real Time Latent Consistency Model
3
+
4
+ This space is using a **CPU runtime**, and it takes about 30 seconds to generate an image.
5
+ For a faster experience, you can duplicate it and use with a **GPU runtime**.
6
+
7
+ At the meantime you can use **[dynamic 🔥](https://www.fal.ai/dynamic)** by [fal](fal.ai), or **[a hosted space 🤗](https://huggingface.co/spaces/fal-ai/realtime-stable-diffusion)**
8
+ to generate images in real time.
9
+ """
10
+
11
+
12
+ LOGO = """
13
+ <svg
14
+ width="100%"
15
+ height="100%"
16
+ viewBox="0 0 89 32"
17
+ fill="none"
18
+ xmlns="http://www.w3.org/2000/svg"
19
+ >
20
+ <path
21
+ d="M52.308 3.07812H57.8465V4.92428H56.0003V6.77043H54.1541V10.4627H57.8465V12.3089H54.1541V25.232H52.308V27.0781H46.7695V25.232H48.6157V12.3089H46.7695V10.4627H48.6157V6.77043H50.4618V4.92428H52.308V3.07812Z"
22
+ fill="currentColor"
23
+ ></path>
24
+ <path
25
+ d="M79.3849 23.3858H81.2311V25.232H83.0772V27.0781H88.6157V25.232H86.7695V23.3858H84.9234V4.92428H79.3849V23.3858Z"
26
+ fill="currentColor"
27
+ ></path>
28
+ <path
29
+ d="M57.8465 14.155H59.6926V12.3089H61.5388V10.4627H70.7695V12.3089H74.4618V23.3858H76.308V25.232H78.1541V27.0781H72.6157V25.232H70.7695V23.3858H68.9234V14.155H67.0772V12.3089H65.2311V14.155H63.3849V23.3858H65.2311V25.232H67.0772V27.0781H61.5388V25.232H59.6926V23.3858H57.8465V14.155Z"
30
+ fill="currentColor"
31
+ ></path>
32
+ <path
33
+ d="M67.0772 25.232V23.3858H68.9234V25.232H67.0772Z"
34
+ fill="currentColor"
35
+ ></path>
36
+ <rect
37
+ opacity="0.22"
38
+ x="7.38477"
39
+ y="29.5391"
40
+ width="2.46154"
41
+ height="2.46154"
42
+ fill="#5F4CD9"
43
+ ></rect>
44
+ <rect
45
+ opacity="0.85"
46
+ x="2.46094"
47
+ y="19.6914"
48
+ width="12.3077"
49
+ height="2.46154"
50
+ fill="#5F4CD9"
51
+ ></rect>
52
+ <rect
53
+ x="4.92383"
54
+ y="17.2305"
55
+ width="9.84615"
56
+ height="2.46154"
57
+ fill="#5F4CD9"
58
+ ></rect>
59
+ <rect
60
+ opacity="0.4"
61
+ x="7.38477"
62
+ y="27.0781"
63
+ width="4.92308"
64
+ height="2.46154"
65
+ fill="#5F4CD9"
66
+ ></rect>
67
+ <rect
68
+ opacity="0.7"
69
+ y="22.1562"
70
+ width="14.7692"
71
+ height="2.46154"
72
+ fill="#5F4CD9"
73
+ ></rect>
74
+ <rect
75
+ opacity="0.5"
76
+ x="7.38477"
77
+ y="24.6133"
78
+ width="7.38462"
79
+ height="2.46154"
80
+ fill="#5F4CD9"
81
+ ></rect>
82
+ <rect
83
+ opacity="0.22"
84
+ x="7.38477"
85
+ y="12.3086"
86
+ width="2.46154"
87
+ height="2.46154"
88
+ fill="#5F4CD9"
89
+ ></rect>
90
+ <rect
91
+ opacity="0.85"
92
+ x="2.46094"
93
+ y="2.46094"
94
+ width="12.3077"
95
+ height="2.46154"
96
+ fill="#5F4CD9"
97
+ ></rect>
98
+ <rect x="4.92383" width="9.84615" height="2.46154" fill="#5F4CD9"></rect>
99
+ <rect
100
+ opacity="0.4"
101
+ x="7.38477"
102
+ y="9.84375"
103
+ width="4.92308"
104
+ height="2.46154"
105
+ fill="#5F4CD9"
106
+ ></rect>
107
+ <rect
108
+ opacity="0.7"
109
+ y="4.92188"
110
+ width="14.7692"
111
+ height="2.46154"
112
+ fill="#5F4CD9"
113
+ ></rect>
114
+ <rect
115
+ opacity="0.5"
116
+ x="7.38477"
117
+ y="7.38281"
118
+ width="7.38462"
119
+ height="2.46154"
120
+ fill="#5F4CD9"
121
+ ></rect>
122
+ <rect
123
+ opacity="0.22"
124
+ x="24.6152"
125
+ y="29.5391"
126
+ width="2.46154"
127
+ height="2.46154"
128
+ fill="#5F4CD9"
129
+ ></rect>
130
+ <rect
131
+ opacity="0.85"
132
+ x="19.6914"
133
+ y="19.6914"
134
+ width="12.3077"
135
+ height="2.46154"
136
+ fill="#5F4CD9"
137
+ ></rect>
138
+ <rect
139
+ x="22.1543"
140
+ y="17.2305"
141
+ width="9.84615"
142
+ height="2.46154"
143
+ fill="#5F4CD9"
144
+ ></rect>
145
+ <rect
146
+ opacity="0.4"
147
+ x="24.6152"
148
+ y="27.0781"
149
+ width="4.92308"
150
+ height="2.46154"
151
+ fill="#5F4CD9"
152
+ ></rect>
153
+ <rect
154
+ opacity="0.7"
155
+ x="17.2305"
156
+ y="22.1562"
157
+ width="14.7692"
158
+ height="2.46154"
159
+ fill="#5F4CD9"
160
+ ></rect>
161
+ <rect
162
+ opacity="0.5"
163
+ x="24.6152"
164
+ y="24.6133"
165
+ width="7.38462"
166
+ height="2.46154"
167
+ fill="#5F4CD9"
168
+ ></rect>
169
+ <rect
170
+ opacity="0.22"
171
+ x="24.6152"
172
+ y="12.3086"
173
+ width="2.46154"
174
+ height="2.46154"
175
+ fill="#5F4CD9"
176
+ ></rect>
177
+ <rect
178
+ opacity="0.85"
179
+ x="19.6914"
180
+ y="2.46094"
181
+ width="12.3077"
182
+ height="2.46154"
183
+ fill="#5F4CD9"
184
+ ></rect>
185
+ <rect x="22.1543" width="9.84615" height="2.46154" fill="#5F4CD9"></rect>
186
+ <rect
187
+ opacity="0.4"
188
+ x="24.6152"
189
+ y="9.84375"
190
+ width="4.92308"
191
+ height="2.46154"
192
+ fill="#5F4CD9"
193
+ ></rect>
194
+ <rect
195
+ opacity="0.7"
196
+ x="17.2305"
197
+ y="4.92188"
198
+ width="14.7692"
199
+ height="2.46154"
200
+ fill="#5F4CD9"
201
+ ></rect>
202
+ <rect
203
+ opacity="0.5"
204
+ x="24.6152"
205
+ y="7.38281"
206
+ width="7.38462"
207
+ height="2.46154"
208
+ fill="#5F4CD9"
209
+ ></rect>
210
+ </svg>
211
+ """
model.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any
2
+
3
+
4
+ def get_pipeline():
5
+ import torch
6
+ from diffusers import AutoencoderTiny, AutoPipelineForImage2Image
7
+
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
10
+
11
+ pipe = AutoPipelineForImage2Image.from_pretrained(
12
+ "SimianLuo/LCM_Dreamshaper_v7",
13
+ use_safetensors=True,
14
+ )
15
+ pipe.vae = AutoencoderTiny.from_pretrained(
16
+ "madebyollin/taesd",
17
+ torch_dtype=torch_dtype,
18
+ use_safetensors=True,
19
+ )
20
+ pipe = pipe.to(device, dtype=torch_dtype)
21
+ pipe.unet.to(memory_format=torch.channels_last)
22
+ return pipe
23
+
24
+
25
+ def get_test_pipeline():
26
+ from PIL import Image
27
+ from dataclasses import dataclass
28
+ import random
29
+ import time
30
+
31
+ @dataclass
32
+ class Images:
33
+ images: list[Image.Image]
34
+
35
+ class Pipeline:
36
+ def __call__(self, *args: Any, **kwds: Any) -> Any:
37
+ r = random.randint(0, 255)
38
+ g = random.randint(0, 255)
39
+ b = random.randint(0, 255)
40
+
41
+ return Images(images=[Image.new("RGB", (512, 512), color=(r, g, b))])
42
+
43
+ return Pipeline()
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
- fastapi[all]
2
- uvicorn[standard]
 
 
 
1
+ accelerate
2
+ diffusers[torch]
3
+ transformers
4
+ xformers
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import numpy as np
3
+
4
+
5
+ def replace_background(image: Image.Image, new_background_color=(0, 255, 255)):
6
+ image_np = np.array(image)
7
+
8
+ white_threshold = 255 * 3
9
+ white_pixels = np.sum(image_np, axis=-1) == white_threshold
10
+
11
+ image_np[white_pixels] = new_background_color
12
+
13
+ result = Image.fromarray(image_np)
14
+
15
+ return result