timpearce commited on
Commit
e518bda
·
1 Parent(s): 9545654

Setup space

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. app.py +339 -0
  3. requirements.txt +8 -0
  4. setup.py +15 -0
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: Gina The Cat
3
- emoji: 📉
4
- colorFrom: indigo
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 3.9.1
 
1
  ---
2
  title: Gina The Cat
3
+ emoji: 😻
4
+ colorFrom: white
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 3.9.1
app.py ADDED
@@ -0,0 +1,339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import os
4
+ from diffusers import StableDiffusionPipeline
5
+
6
+ def dummy(images, **kwargs):
7
+ return images, False
8
+
9
+ model_id = "timothepearce/gina-the-cat"
10
+
11
+ AUTH_TOKEN = os.environ.get('AUTH_TOKEN')
12
+ if not AUTH_TOKEN:
13
+ with open('/root/.huggingface/token') as f:
14
+ lines = f.readlines()
15
+ AUTH_TOKEN = lines[0]
16
+
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+ if device == "cuda":
19
+ print('Nvidia GPU detected!')
20
+ share = True
21
+ pipe = StableDiffusionPipeline.from_pretrained(
22
+ model_id,
23
+ use_auth_token=AUTH_TOKEN,
24
+ revision="fp16",
25
+ torch_dtype=torch.float16
26
+ )
27
+ else:
28
+ print('No Nvidia GPU in system!')
29
+ share = False
30
+ pipe = StableDiffusionPipeline.from_pretrained(
31
+ model_id,
32
+ use_auth_token=AUTH_TOKEN
33
+ )
34
+
35
+ pipe.to(device)
36
+ pipe.safety_checker = dummy
37
+ #torch.backends.cudnn.benchmark = True
38
+
39
+ def infer(prompt="", samples=4, steps=20, scale=7.5, seed=1437181781):
40
+ generator = torch.Generator(device=device).manual_seed(seed)
41
+ return pipe(
42
+ [prompt] * samples,
43
+ num_inference_steps=steps,
44
+ guidance_scale=scale,
45
+ generator=generator,
46
+ ).images
47
+
48
+
49
+ css = """
50
+ .gradio-container {
51
+ font-family: 'IBM Plex Sans', sans-serif;
52
+ }
53
+ .gr-button {
54
+ color: white;
55
+ border-color: black;
56
+ background: black;
57
+ }
58
+ input[type='range'] {
59
+ accent-color: black;
60
+ }
61
+ .dark input[type='range'] {
62
+ accent-color: #dfdfdf;
63
+ }
64
+ .container {
65
+ max-width: 730px;
66
+ margin: auto;
67
+ padding-top: 1.5rem;
68
+ }
69
+ #gallery {
70
+ min-height: 22rem;
71
+ margin-bottom: 15px;
72
+ margin-left: auto;
73
+ margin-right: auto;
74
+ border-bottom-right-radius: .5rem !important;
75
+ border-bottom-left-radius: .5rem !important;
76
+ }
77
+ #gallery>div>.h-full {
78
+ min-height: 20rem;
79
+ }
80
+ .details:hover {
81
+ text-decoration: underline;
82
+ }
83
+ .gr-button {
84
+ white-space: nowrap;
85
+ }
86
+ .gr-button:focus {
87
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
88
+ outline: none;
89
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
90
+ --tw-border-opacity: 1;
91
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
92
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
93
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
94
+ --tw-ring-opacity: .5;
95
+ }
96
+ #advanced-btn {
97
+ font-size: .7rem !important;
98
+ line-height: 19px;
99
+ margin-top: 12px;
100
+ margin-bottom: 12px;
101
+ padding: 2px 8px;
102
+ border-radius: 14px !important;
103
+ }
104
+ #advanced-options {
105
+ display: none;
106
+ margin-bottom: 20px;
107
+ }
108
+ .footer {
109
+ margin-bottom: 45px;
110
+ margin-top: 35px;
111
+ text-align: center;
112
+ border-bottom: 1px solid #e5e5e5;
113
+ }
114
+ .footer>p {
115
+ font-size: .8rem;
116
+ display: inline-block;
117
+ padding: 0 10px;
118
+ transform: translateY(10px);
119
+ background: white;
120
+ }
121
+ .dark .footer {
122
+ border-color: #303030;
123
+ }
124
+ .dark .footer>p {
125
+ background: #0b0f19;
126
+ }
127
+ .acknowledgments h4{
128
+ margin: 1.25em 0 .25em 0;
129
+ font-weight: bold;
130
+ font-size: 115%;
131
+ }
132
+ #container-advanced-btns{
133
+ display: flex;
134
+ flex-wrap: wrap;
135
+ justify-content: space-between;
136
+ align-items: center;
137
+ }
138
+ .animate-spin {
139
+ animation: spin 1s linear infinite;
140
+ }
141
+ @keyframes spin {
142
+ from {
143
+ transform: rotate(0deg);
144
+ }
145
+ to {
146
+ transform: rotate(360deg);
147
+ }
148
+ }
149
+ #share-btn-container {
150
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
151
+ }
152
+ #share-btn {
153
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
154
+ }
155
+ #share-btn * {
156
+ all: unset;
157
+ }
158
+ .gr-form{
159
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
160
+ }
161
+ #prompt-container{
162
+ gap: 0;
163
+ }
164
+ """
165
+
166
+ block = gr.Blocks(css=css)
167
+
168
+ examples = [
169
+ [
170
+ 'A high tech solarpunk utopia in the Amazon rainforest',
171
+ # 4,
172
+ # 45,
173
+ # 7.5,
174
+ # 1024,
175
+ ],
176
+ [
177
+ 'A pikachu fine dining with a view to the Eiffel Tower',
178
+ # 4,
179
+ # 45,
180
+ # 7,
181
+ # 1024,
182
+ ],
183
+ [
184
+ 'A mecha robot in a favela in expressionist style',
185
+ # 4,
186
+ # 45,
187
+ # 7,
188
+ # 1024,
189
+ ],
190
+ [
191
+ 'an insect robot preparing a delicious meal',
192
+ # 4,
193
+ # 45,
194
+ # 7,
195
+ # 1024,
196
+ ],
197
+ [
198
+ "A small cabin on top of a snowy mountain in the style of Disney, artstation",
199
+ # 4,
200
+ # 45,
201
+ # 7,
202
+ # 1024,
203
+ ],
204
+ ]
205
+
206
+
207
+ with block:
208
+ gr.HTML(
209
+ """
210
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
211
+ <div
212
+ style="
213
+ display: inline-flex;
214
+ align-items: center;
215
+ gap: 0.8rem;
216
+ font-size: 1.75rem;
217
+ "
218
+ >
219
+ <svg
220
+ width="0.65em"
221
+ height="0.65em"
222
+ viewBox="0 0 115 115"
223
+ fill="none"
224
+ xmlns="http://www.w3.org/2000/svg"
225
+ >
226
+ <rect width="23" height="23" fill="white"></rect>
227
+ <rect y="69" width="23" height="23" fill="white"></rect>
228
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
229
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
230
+ <rect x="46" width="23" height="23" fill="white"></rect>
231
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
232
+ <rect x="69" width="23" height="23" fill="black"></rect>
233
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
234
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
235
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
236
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
237
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
238
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
239
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
240
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
241
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
242
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
243
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
244
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
245
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
246
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
247
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
248
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
249
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
250
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
251
+ </svg>
252
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
253
+ Stable Diffusion Demo
254
+ </h1>
255
+ </div>
256
+ <p style="margin-bottom: 10px; font-size: 94%">
257
+ Stable Diffusion is a state of the art text-to-image model that generates
258
+ images from text.
259
+ </p>
260
+ </div>
261
+ """
262
+ )
263
+ with gr.Group():
264
+ with gr.Box():
265
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
266
+ text = gr.Textbox(
267
+ label="Enter your prompt",
268
+ show_label=False,
269
+ max_lines=1,
270
+ placeholder="Enter your prompt",
271
+ elem_id="prompt-text-input",
272
+ ).style(
273
+ border=(True, False, True, True),
274
+ rounded=(True, False, False, True),
275
+ container=False,
276
+ )
277
+ btn = gr.Button("Generate image").style(
278
+ margin=False,
279
+ rounded=(False, True, True, False),
280
+ full_width=False,
281
+ )
282
+
283
+ gallery = gr.Gallery(
284
+ label="Generated images",
285
+ show_label=False,
286
+ elem_id="gallery"
287
+ ).style(grid=[2], height="auto")
288
+
289
+ with gr.Group(elem_id="container-advanced-btns"):
290
+ advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
291
+
292
+ with gr.Row(elem_id="advanced-options"):
293
+ samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
294
+ steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
295
+ scale = gr.Slider(
296
+ label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
297
+ )
298
+ seed = gr.Slider(
299
+ label="Seed",
300
+ minimum=0,
301
+ maximum=2147483647,
302
+ step=1,
303
+ randomize=True,
304
+ )
305
+
306
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, samples, steps, scale, seed], outputs=[gallery], cache_examples=False)
307
+ ex.dataset.headers = [""]
308
+
309
+ text.submit(infer, inputs=[text, samples, steps, scale, seed], outputs=[gallery])
310
+ btn.click(infer, inputs=[text, samples, steps, scale, seed], outputs=[gallery])
311
+
312
+ advanced_button.click(
313
+ None,
314
+ [],
315
+ text,
316
+ _js="""
317
+ () => {
318
+ var appDom = document.querySelector("body > gradio-app");
319
+ var options = appDom.querySelector("#advanced-options")
320
+ if (options == null) {options = appDom.shadowRoot.querySelector("#advanced-options")}
321
+ options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
322
+ }""",
323
+ )
324
+ gr.HTML(
325
+ """
326
+ <div class="footer">
327
+ <p>Model by <a href="https://huggingface.co/timothepearce" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://huggingface.co/timothepearce" style="text-decoration: underline;" target="_blank">Timothé Pearce</a> - Gradio Demo by 🤗 Hugging Face
328
+ </p>
329
+ </div>
330
+ <div class="acknowledgments">
331
+ <p><h4>LICENSE</h4>
332
+ The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
333
+ <p><h4>Biases and content acknowledgment</h4>
334
+ Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
335
+ </div>
336
+ """
337
+ )
338
+
339
+ block.queue(max_size=10).launch(share=share, enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ --find-links https://download.pytorch.org/whl/torch_stable.html
2
+ python-dotenv
3
+ torch==1.12.1+cu113
4
+ diffusers
5
+ transformers
6
+ nvidia-ml-py3
7
+ ftfy
8
+ gradio==3.9.1
setup.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from setuptools import setup, find_packages
2
+ import os
3
+
4
+ def _read_reqs(relpath):
5
+ fullpath = os.path.join(os.path.dirname(__file__), relpath)
6
+ with open(fullpath) as f:
7
+ return [s.strip() for s in f.readlines() if (s.strip() and not s.startswith("#"))]
8
+
9
+ setup(
10
+ name='stable-diffusion',
11
+ version='0.0.1',
12
+ description='',
13
+ packages=find_packages(),
14
+ install_requires=_read_reqs("requirements.txt"),
15
+ )