tmblack commited on
Commit
883fe51
·
verified ·
1 Parent(s): ef427c2

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +27 -0
  2. app.py +361 -0
  3. gitattributes.txt +31 -0
  4. requirements.txt +4 -0
README.md ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Stable Diffusion 2.1
3
+ emoji: 🎨
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.44.1
8
+ app_file: app.py
9
+ pinned: true
10
+ license: mit
11
+ ---
12
+
13
+ # Stable Diffusion 2.1 Image Generator
14
+
15
+ This space demonstrates Stable Diffusion 2.1 for text-to-image generation.
16
+
17
+ ## Features
18
+ - Generate images from text prompts
19
+ - Safety filtering for inappropriate content
20
+ - Share generated images to Hugging Face community
21
+ - Mobile responsive interface
22
+
23
+ ## How to Use
24
+ 1. Enter a text prompt in the input box
25
+ 2. (Optional) Add negative prompts
26
+ 3. Click "Generate image" button
27
+ 4. Share your creations with the community
app.py ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import re
4
+ import os
5
+ import requests
6
+ import uuid
7
+ import base64
8
+
9
+ from share_btn import community_icon_html, loading_icon_html, share_js
10
+
11
+ # Word list directly loaded from URL
12
+ word_list_url = "https://huggingface.co/spaces/stabilityai/stable-diffusion/raw/main/list.txt"
13
+ response = requests.get(word_list_url)
14
+ word_list = response.text.splitlines()
15
+
16
+ is_gpu_busy = False
17
+ def infer(prompt, negative, scale):
18
+ global is_gpu_busy
19
+ for filter in word_list:
20
+ if re.search(rf"\b{filter}\b", prompt, re.IGNORECASE):
21
+ print(f"Filtered word detected: {filter}")
22
+ print(f"Prompt: {prompt}")
23
+ raise gr.Error("Unsafe content found. Please try again with different prompts.")
24
+
25
+ images = []
26
+ url = os.getenv('JAX_BACKEND_URL')
27
+ if not url:
28
+ raise gr.Error("Backend URL is not configured. Please set JAX_BACKEND_URL environment variable.")
29
+
30
+ print(f"Using backend URL: {url}")
31
+ payload = {
32
+ 'prompt': prompt,
33
+ 'negative_prompt': negative,
34
+ 'guidance_scale': scale
35
+ }
36
+
37
+ try:
38
+ images_request = requests.post(url, json=payload, timeout=60)
39
+ images_request.raise_for_status()
40
+ response_data = images_request.json()
41
+
42
+ for image in response_data["images"]:
43
+ file_path = f"{uuid.uuid4()}.jpg"
44
+ with open(file_path, "wb") as f:
45
+ f.write(base64.b64decode(image))
46
+ images.append(file_path)
47
+
48
+ except requests.exceptions.RequestException as e:
49
+ print(f"Backend request failed: {str(e)}")
50
+ raise gr.Error("Failed to connect to the image generation backend. Please try again later.")
51
+ except (KeyError, ValueError) as e:
52
+ print(f"Invalid response from backend: {str(e)}")
53
+ print(f"Response content: {images_request.text}")
54
+ raise gr.Error("Invalid response from image generation service. Please try again.")
55
+
56
+ return images
57
+
58
+ css = """
59
+ /* CSS remains unchanged from your original file */
60
+ .gradio-container {
61
+ max-width: 768px !important;
62
+ }
63
+ .gradio-container {
64
+ font-family: 'IBM Plex Sans', sans-serif;
65
+ }
66
+ .gr-button {
67
+ color: white;
68
+ border-color: black;
69
+ background: black;
70
+ }
71
+ input[type='range'] {
72
+ accent-color: black;
73
+ }
74
+ .dark input[type='range'] {
75
+ accent-color: #dfdfdf;
76
+ }
77
+ .container {
78
+ max-width: 730px;
79
+ margin: auto;
80
+ }
81
+ #gallery {
82
+ min-height: 22rem;
83
+ margin-bottom: 15px;
84
+ margin-left: auto;
85
+ margin-right: auto;
86
+ border-bottom-right-radius: .5rem !important;
87
+ border-bottom-left-radius: .5rem !important;
88
+ }
89
+ #gallery>div>.h-full {
90
+ min-height: 20rem;
91
+ }
92
+ .details:hover {
93
+ text-decoration: underline;
94
+ }
95
+ .gr-button {
96
+ white-space: nowrap;
97
+ }
98
+ .gr-button:focus {
99
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
100
+ outline: none;
101
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
102
+ --tw-border-opacity: 1;
103
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
104
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
105
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
106
+ --tw-ring-opacity: .5;
107
+ }
108
+ #advanced-btn {
109
+ font-size: .7rem !important;
110
+ line-height: 19px;
111
+ margin-top: 12px;
112
+ margin-bottom: 12px;
113
+ padding: 2px 8px;
114
+ border-radius: 14px !important;
115
+ }
116
+ #advanced-options {
117
+ display: none;
118
+ margin-bottom: 20px;
119
+ }
120
+ .footer {
121
+ margin-bottom: 45px;
122
+ margin-top: 35px;
123
+ text-align: center;
124
+ border-bottom: 1px solid #e5e5e5;
125
+ }
126
+ .footer>p {
127
+ font-size: .8rem;
128
+ display: inline-block;
129
+ padding: 0 10px;
130
+ transform: translateY(10px);
131
+ background: white;
132
+ }
133
+ .dark .footer {
134
+ border-color: #303030;
135
+ }
136
+ .dark .footer>p {
137
+ background: #0b0f19;
138
+ }
139
+ .acknowledgments h4{
140
+ margin: 1.25em 0 .25em 0;
141
+ font-weight: bold;
142
+ font-size: 115%;
143
+ }
144
+ .animate-spin {
145
+ animation: spin 1s linear infinite;
146
+ }
147
+ @keyframes spin {
148
+ from {
149
+ transform: rotate(0deg);
150
+ }
151
+ to {
152
+ transform: rotate(360deg);
153
+ }
154
+ }
155
+ #share-btn-container {
156
+ 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;
157
+ margin-top: 10px;
158
+ margin-left: auto;
159
+ }
160
+ #share-btn-container .styler{
161
+ background-color: #000000;
162
+ }
163
+ #share-btn {
164
+ 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;right:0;
165
+ }
166
+ #share-btn * {
167
+ all: unset;
168
+ }
169
+ #share-btn-container div:nth-child(-n+2){
170
+ width: auto !important;
171
+ min-height: 0px !important;
172
+ }
173
+ #share-btn-container .wrap {
174
+ display: none !important;
175
+ }
176
+ .gr-form{
177
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
178
+ }
179
+ #prompt-container{
180
+ gap: 0;
181
+ }
182
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
183
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
184
+ .image_duplication{position: absolute; width: 100px; left: 50px}
185
+ button{height: 100%}
186
+ """
187
+
188
+ block = gr.Blocks(css=css)
189
+
190
+ examples = [
191
+ [
192
+ 'A high tech solarpunk utopia in the Amazon rainforest',
193
+ 'low quality',
194
+ 9
195
+ ],
196
+ [
197
+ 'A pikachu fine dining with a view to the Eiffel Tower',
198
+ 'low quality',
199
+ 9
200
+ ],
201
+ [
202
+ 'A mecha robot in a favela in expressionist style',
203
+ 'low quality, 3d, photorealistic',
204
+ 9
205
+ ],
206
+ [
207
+ 'an insect robot preparing a delicious meal',
208
+ 'low quality, illustration',
209
+ 9
210
+ ],
211
+ [
212
+ "A small cabin on top of a snowy mountain in the style of Disney, artstation",
213
+ 'low quality, ugly',
214
+ 9
215
+ ],
216
+ ]
217
+
218
+ with block:
219
+ gr.HTML(
220
+ """
221
+ <div style="text-align: center; margin: 0 auto;">
222
+ <div
223
+ style="
224
+ display: inline-flex;
225
+ align-items: center;
226
+ gap: 0.8rem;
227
+ font-size: 1.75rem;
228
+ "
229
+ >
230
+ <svg
231
+ width="0.65em"
232
+ height="0.65em"
233
+ viewBox="0 0 115 115"
234
+ fill="none"
235
+ xmlns="http://www.w3.org/2000/svg"
236
+ >
237
+ <rect width="23" height="23" fill="white"></rect>
238
+ <rect y="69" width="23" height="23" fill="white"></rect>
239
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
240
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
241
+ <rect x="46" width="23" height="23" fill="white"></rect>
242
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
243
+ <rect x="69" width="23" height="23" fill="black"></rect>
244
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
245
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
246
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
247
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
248
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
249
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
250
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
251
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
252
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
253
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
254
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
255
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
256
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
257
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
258
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
259
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
260
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
261
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
262
+ </svg>
263
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
264
+ Stable Diffusion 2.1 Demo
265
+ </h1>
266
+ </div>
267
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
268
+ Stable Diffusion 2.1 is the latest text-to-image model from StabilityAI. <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion-1">Access Stable Diffusion 1 Space here</a><br>For faster generation and API
269
+ access you can try
270
+ <a
271
+ href="http://beta.dreamstudio.ai/"
272
+ style="text-decoration: underline;"
273
+ target="_blank"
274
+ >DreamStudio Beta</a
275
+ >.</a>
276
+ </p>
277
+ </div>
278
+ """
279
+ )
280
+ with gr.Group():
281
+ with gr.Row(elem_id="prompt-container"):
282
+ with gr.Column(scale=3):
283
+ text = gr.Textbox(
284
+ label="Enter your prompt",
285
+ show_label=False,
286
+ max_lines=1,
287
+ placeholder="Enter your prompt",
288
+ elem_id="prompt-text-input",
289
+ )
290
+ negative = gr.Textbox(
291
+ label="Enter your negative prompt",
292
+ show_label=False,
293
+ max_lines=1,
294
+ placeholder="Enter a negative prompt",
295
+ elem_id="negative-prompt-text-input",
296
+ )
297
+ with gr.Column(scale=1, min_width=150):
298
+ btn = gr.Button("Generate image")
299
+
300
+ gallery = gr.Gallery(
301
+ label="Generated images",
302
+ show_label=False,
303
+ elem_id="gallery",
304
+ columns=[2],
305
+ height="auto"
306
+ )
307
+
308
+ with gr.Group(elem_id="container-advanced-btns"):
309
+ with gr.Group(elem_id="share-btn-container"):
310
+ community_icon = gr.HTML(community_icon_html)
311
+ loading_icon = gr.HTML(loading_icon_html)
312
+ share_button = gr.Button("Share to community", elem_id="share-btn")
313
+
314
+ with gr.Accordion("Advanced settings", open=False):
315
+ guidance_scale = gr.Slider(
316
+ label="Guidance Scale",
317
+ minimum=0,
318
+ maximum=50,
319
+ value=9,
320
+ step=0.1
321
+ )
322
+
323
+ ex = gr.Examples(
324
+ examples=examples,
325
+ fn=infer,
326
+ inputs=[text, negative, guidance_scale],
327
+ outputs=[gallery],
328
+ cache_examples=False
329
+ )
330
+ ex.dataset.headers = [""]
331
+
332
+ negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery])
333
+ text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery])
334
+ btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery])
335
+
336
+ share_button.click(
337
+ None,
338
+ [],
339
+ [],
340
+ js=share_js,
341
+ )
342
+ gr.HTML(
343
+ """
344
+ <div class="footer">
345
+ <p>Model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a> - backend running JAX on TPUs due to generous support of <a href="https://sites.research.google/trc/about/" style="text-decoration: underline;" target="_blank">Google TRC program</a> - Gradio Demo by 🤗 Hugging Face
346
+ </p>
347
+ </div>
348
+ """
349
+ )
350
+ with gr.Accordion(label="License", open=False):
351
+ gr.HTML(
352
+ """<div class="acknowledgments">
353
+ <p><h4>LICENSE</h4>
354
+ The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-2/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</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>
355
+ <p><h4>Biases and content acknowledgment</h4>
356
+ 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>
357
+ </div>
358
+ """
359
+ )
360
+
361
+ block.queue(concurrency_count=5).launch(show_error=True, server_name="0.0.0.0", server_port=7860)
gitattributes.txt ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ requests
3
+ Pillow
4
+ python-dotenv