Not-Grim-Refer commited on
Commit
f4a6d93
·
verified ·
1 Parent(s): 01377f9
Files changed (1) hide show
  1. app.py +44 -397
app.py CHANGED
@@ -15,400 +15,47 @@ from typing import Tuple
15
  import user_history
16
  from share_btn import community_icon_html, loading_icon_html, share_js
17
 
18
-
19
- style_list = [
20
- {
21
- "name": "(No style)",
22
- "prompt": "{prompt}",
23
- "negative_prompt": "",
24
- },
25
- {
26
- "name": "Cinematic",
27
- "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
28
- "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
29
- },
30
- {
31
- "name": "Photographic",
32
- "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
33
- "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
34
- },
35
- {
36
- "name": "Anime",
37
- "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
38
- "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
39
- },
40
- {
41
- "name": "Manga",
42
- "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
43
- "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
44
- },
45
- {
46
- "name": "Digital Art",
47
- "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
48
- "negative_prompt": "photo, photorealistic, realism, ugly",
49
- },
50
- {
51
- "name": "Pixel art",
52
- "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
53
- "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
54
- },
55
- {
56
- "name": "Fantasy art",
57
- "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
58
- "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
59
- },
60
- {
61
- "name": "Neonpunk",
62
- "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
63
- "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
64
- },
65
- {
66
- "name": "3D Model",
67
- "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
68
- "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
69
- },
70
- ]
71
-
72
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
73
- STYLE_NAMES = list(styles.keys())
74
- DEFAULT_STYLE_NAME = "(No style)"
75
-
76
-
77
- def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
78
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
79
- return p.replace("{prompt}", positive), n + negative
80
-
81
-
82
- word_list_dataset = load_dataset("google/word-list-sd", data_files="list.txt",
83
- word_list = word_list_dataset["train"]['text']
84
-
85
- #gradio.helpers.CACHED_FOLDER="/data/cache"
86
-
87
- def infer(prompt, negative="low_quality", scale=7, style_name=None, profile: gr.OAuthProfile | None = None):
88
- for filter in word_list:
89
- if re.search(rf"\b{filter}\b", prompt):
90
- raise gr.Error("Please try again with a different prompt")
91
-
92
- seed = random.randint(0,4294967295)
93
- prompt, negative = apply_style(style_name, prompt, negative)
94
- images = []
95
- url = os.getenv('JAX_BACKEND_URL')
96
- payload = {'instances': [{ 'prompt': prompt, 'negative_prompt': negative, 'parameters':{ 'guidance_scale': scale, 'seed': seed } }] }
97
- start_time = time.time()
98
- images_request = requests.post(url, json = payload)
99
- print(time.time() - start_time)
100
- try:
101
- json_data = images_request.json()
102
- except requests.exceptions.JSONDecodeError:
103
- raise gr.Error("SDXL did not return a valid result, try again")
104
-
105
- for prediction in json_data["predictions"]:
106
- for image in prediction["images"]:
107
- image_b64 = (f"data:image/jpeg;base64,{image}")
108
- images.append(image_b64)
109
-
110
- if profile is not None: # avoid conversion on non-logged-in users
111
- pil_image = Image.open(BytesIO(base64.b64decode(image)))
112
- user_history.save_image( # save images + metadata to user history
113
- label=prompt,
114
- image=pil_image,
115
- profile=profile,
116
- metadata={
117
- "prompt": prompt,
118
- "negative_prompt": negative,
119
- "guidance_scale": scale,
120
- },
121
- )
122
-
123
- return images, gr.update(visible=True)
124
-
125
-
126
- css = """
127
- .gradio-container {
128
- font-family: 'IBM Plex Sans', sans-serif;
129
- }
130
- .gr-button {
131
- color: white;
132
- border-color: black;
133
- background: black;
134
- }
135
- input[type='range'] {
136
- accent-color: black;
137
- }
138
- .dark input[type='range'] {
139
- accent-color: #dfdfdf;
140
- }
141
- .gradio-container {
142
- max-width: 730px !important;
143
- margin: auto;
144
- padding-top: 1.5rem;
145
- }
146
- #gallery {
147
- min-height: 22rem;
148
- margin-bottom: 15px;
149
- margin-left: auto;
150
- margin-right: auto;
151
- border-bottom-right-radius: .5rem !important;
152
- border-bottom-left-radius: .5rem !important;
153
- }
154
- #gallery>div>.h-full {
155
- min-height: 20rem;
156
- }
157
- .details:hover {
158
- text-decoration: underline;
159
- }
160
- .gr-button {
161
- white-space: nowrap;
162
- }
163
- .gr-button:focus {
164
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
165
- outline: none;
166
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
167
- --tw-border-opacity: 1;
168
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
169
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
170
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
171
- --tw-ring-opacity: .5;
172
- }
173
- #advanced-btn {
174
- font-size: .7rem !important;
175
- line-height: 19px;
176
- margin-top: 12px;
177
- margin-bottom: 12px;
178
- padding: 2px 8px;
179
- border-radius: 14px !important;
180
- }
181
- #advanced-options {
182
- display: none;
183
- margin-bottom: 20px;
184
- }
185
- .footer {
186
- margin-bottom: 45px;
187
- margin-top: 35px;
188
- text-align: center;
189
- border-bottom: 1px solid #e5e5e5;
190
- }
191
- .footer>p {
192
- font-size: .8rem;
193
- display: inline-block;
194
- padding: 0 10px;
195
- transform: translateY(10px);
196
- background: white;
197
- }
198
- .dark .footer {
199
- border-color: #303030;
200
- }
201
- .dark .footer>p {
202
- background: #0b0f19;
203
- }
204
- .acknowledgments h4{
205
- margin: 1.25em 0 .25em 0;
206
- font-weight: bold;
207
- font-size: 115%;
208
- }
209
- .animate-spin {
210
- animation: spin 1s linear infinite;
211
- }
212
- @keyframes spin {
213
- from {
214
- transform: rotate(0deg);
215
- }
216
- to {
217
- transform: rotate(360deg);
218
- }
219
- }
220
- #share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
221
- div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
222
- #share-btn-container:hover {background-color: #060606}
223
- #share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
224
- #share-btn * {all: unset}
225
- #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
226
- #share-btn-container .wrap {display: none !important}
227
- #share-btn-container.hidden {display: none!important}
228
-
229
- .gr-form{
230
- flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
231
- }
232
- #prompt-container{
233
- gap: 0;
234
- }
235
- #prompt-container .form{
236
- border-top-right-radius: 0;
237
- border-bottom-right-radius: 0;
238
- }
239
- #gen-button{
240
- border-top-left-radius:0;
241
- border-bottom-left-radius:0;
242
- }
243
- #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
244
- #component-16{border-top-width: 1px!important;margin-top: 1em}
245
- .image_duplication{position: absolute; width: 100px; left: 50px}
246
- .tabitem{border: 0 !important}
247
- """
248
-
249
- block = gr.Blocks()
250
-
251
- examples = [
252
- [
253
- "A serious capybara at work, wearing a suit",
254
- None,
255
- None
256
- ],
257
- [
258
- 'A Squirtle fine dining with a view to the London Eye',
259
- None,
260
- None
261
- ],
262
- [
263
- 'A tamale food cart in front of a Japanese Castle',
264
- None,
265
- None
266
- ],
267
- [
268
- 'a graffiti of a robot serving meals to people',
269
- None,
270
- None
271
- ],
272
- [
273
- 'a beautiful cabin in Attersee, Austria, 3d animation style',
274
- None,
275
- None
276
- ],
277
-
278
- ]
279
-
280
-
281
- with block:
282
- gr.HTML(
283
- """
284
- <div style="text-align: center; margin: 0 auto;">
285
- <div
286
- style="
287
- display: inline-flex;
288
- align-items: center;
289
- gap: 0.8rem;
290
- font-size: 1.75rem;
291
- "
292
- >
293
- <svg
294
- width="0.65em"
295
- height="0.65em"
296
- viewBox="0 0 115 115"
297
- fill="none"
298
- xmlns="http://www.w3.org/2000/svg"
299
- >
300
- <rect width="23" height="23" fill="white"></rect>
301
- <rect y="69" width="23" height="23" fill="white"></rect>
302
- <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
303
- <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
304
- <rect x="46" width="23" height="23" fill="white"></rect>
305
- <rect x="46" y="69" width="23" height="23" fill="white"></rect>
306
- <rect x="69" width="23" height="23" fill="black"></rect>
307
- <rect x="69" y="69" width="23" height="23" fill="black"></rect>
308
- <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
309
- <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
310
- <rect x="115" y="46" width="23" height="23" fill="white"></rect>
311
- <rect x="115" y="115" width="23" height="23" fill="white"></rect>
312
- <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
313
- <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
314
- <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
315
- <rect x="92" y="69" width="23" height="23" fill="white"></rect>
316
- <rect x="69" y="46" width="23" height="23" fill="white"></rect>
317
- <rect x="69" y="115" width="23" height="23" fill="white"></rect>
318
- <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
319
- <rect x="46" y="46" width="23" height="23" fill="black"></rect>
320
- <rect x="46" y="115" width="23" height="23" fill="black"></rect>
321
- <rect x="46" y="69" width="23" height="23" fill="black"></rect>
322
- <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
323
- <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
324
- <rect x="23" y="69" width="23" height="23" fill="black"></rect>
325
- </svg>
326
- <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
327
- Fast Stable Diffusion XL on TPU v5e ⚡
328
- </h1>
329
- </div>
330
- <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
331
- SDXL is a high quality text-to-image model from Stability AI. This demo is running on <a style="text-decoration: underline;" href="https://cloud.google.com/blog/products/compute/announcing-cloud-tpu-v5e-and-a3-gpus-in-ga">Google Cloud TPU v5e</a>, to achieve efficient and cost-effective inference of 1024×1024 images. <a href="https://hf.co/blog/sdxl_jax" target="_blank">How does it work?</a>
332
- </p>
333
- </div>
334
- """
335
- )
336
-
337
- with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
338
- text = gr.Textbox(
339
- label="Enter your prompt",
340
- show_label=False,
341
- max_lines=1,
342
- placeholder="Enter your prompt",
343
- elem_id="prompt-text-input",
344
- )
345
- btn = gr.Button("Generate", scale=0, elem_id="gen-button")
346
-
347
- gallery = gr.Gallery(
348
- label="Generated images", show_label=False, elem_id="gallery", grid=[2]
349
- )
350
-
351
-
352
- with gr.Group(elem_id="share-btn-container", visible=False) as community_group:
353
- community_icon = gr.HTML(community_icon_html)
354
- loading_icon = gr.HTML(loading_icon_html)
355
- share_button = gr.Button("Share to community", elem_id="share-btn")
356
-
357
- with gr.Accordion("Advanced settings", open=False):
358
- style_selection = gr.Radio(
359
- show_label=True, container=True, interactive=True,
360
- choices=STYLE_NAMES,
361
- value=DEFAULT_STYLE_NAME,
362
- label='Image Style'
363
- )
364
- negative = gr.Textbox(
365
- label="Enter your negative prompt",
366
- show_label=False,
367
- max_lines=1,
368
- placeholder="Enter a negative prompt",
369
- elem_id="negative-prompt-text-input",
370
- )
371
- guidance_scale = gr.Slider(
372
- label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
373
- )
374
-
375
- ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_group], cache_examples=True, postprocess=False)
376
- negative.submit(infer, inputs=[text, negative, guidance_scale, style_selection], outputs=[gallery, community_group], postprocess=False)
377
- text.submit(infer, inputs=[text, negative, guidance_scale, style_selection], outputs=[gallery, community_group], postprocess=False)
378
- btn.click(infer, inputs=[text, negative, guidance_scale, style_selection], outputs=[gallery, community_group], postprocess=False)
379
-
380
- share_button.click(
381
- None,
382
- [],
383
- [],
384
- _js=share_js,
385
- )
386
- gr.HTML(
387
- """
388
- <div class="footer">
389
- <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 - this is not an official Google Product
390
- </p>
391
- </div>
392
- """
393
- )
394
- with gr.Accordion(label="License", open=True):
395
- gr.HTML(
396
- """<div class="acknowledgments">
397
- <p><h4>LICENSE</h4>
398
- The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/LICENSE.md" style="text-decoration: underline;" target="_blank">Stability AI CreativeML Open RAIL++-M</a> license. The License allows users to take advantage of the model in a wide range of settings (including free use and redistribution) as long as they respect the specific use case restrictions outlined, which correspond to model applications the licensor deems ill-suited for the model or are likely to cause harm. For the full list of restrictions please <a href="https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/LICENSE.md" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
399
- <p><h4>Biases and content acknowledgment</h4>
400
- Despite how impressive being able to turn text into image is, beware that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. You can read more in the <a href="https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0" style="text-decoration: underline;" target="_blank">model card</a></p>
401
- </div>
402
- """
403
- )
404
-
405
- with gr.Blocks(css=css) as block_with_history:
406
- with gr.Tab("Demo"):
407
- block.render()
408
- with gr.Tab("Past generations"):
409
- user_history.render()
410
- gr.LoginButton
411
-
412
-
413
- block_with_history.queue(concurrency_count=8, max_size=10, api_open=False).launch(show_api=False)
414
- #block_with_history.launch(server_name="0.0.0.0")
 
15
  import user_history
16
  from share_btn import community_icon_html, loading_icon_html, share_js
17
 
18
+ # Define constants for map region size and overlay shapes
19
+ MAP_REGION_SIZE = 64
20
+ OVERLAY_SHAPES = ["square", "circle", "triangle"]
21
+
22
+ # Load pre-trained AI model for map concept generation
23
+ model = load_model("path/to/pretrained/model")
24
+
25
+ # Define function for generating map layout concepts using AI model
26
+ def generate_map_concept(text_description):
27
+ # Preprocess text description
28
+ processed_text = preprocess_text(text_description)
29
+
30
+ # Generate map layout concept using AI model
31
+ map_concept = model.generate(processed_text)
32
+
33
+ return map_concept
34
+
35
+ # Define Gradio interface components
36
+ def gradio_ui():
37
+ with gr.Group():
38
+ with gr.Box():
39
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
40
+ with gr.Column():
41
+ num_regions = gr.Number(label="Number of Regions", value=1, min=1, max=10, step=1)
42
+ overlay_shape = gr.Radio(label="Overlay Shape", choices=OVERLAY_SHAPES, value=OVERLAY_SHAPES[0])
43
+ text_description = gr.Textbox(
44
+ label="Enter map description",
45
+ show_label=True,
46
+ max_lines=5,
47
+ placeholder="Enter a description for the map layout",
48
+ )
49
+ generate_btn = gr.Button("Generate Map Concept")
50
+
51
+ with gr.Column():
52
+ map_concept_output = gr.Image(label="Generated Map Concept", shape=(MAP_REGION_SIZE * 10, MAP_REGION_SIZE * 10))
53
+
54
+ # Set up event listeners
55
+ generate_btn.click(fn=generate_map_concept, inputs=text_description, outputs=map_concept_output)
56
+
57
+ # Launch Gradio interface
58
+ gr.Interface(gradio_ui, theme="darkdefault").launch()
59
+
60
+ # Run the Gradio interface
61
+ gradio_ui()