FilipeR commited on
Commit
e104a78
1 Parent(s): b96b1cd
Files changed (2) hide show
  1. app-ok.py +294 -0
  2. app.py +135 -115
app-ok.py ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import json
4
+ import os
5
+ import random
6
+ from typing import Tuple
7
+ import uuid
8
+
9
+ from diffusers import DiffusionPipeline
10
+ import gradio as gr
11
+ import numpy as np
12
+ from PIL import Image
13
+ import spaces
14
+ import torch
15
+
16
+ from gradio_imagefeed import ImageFeed
17
+
18
+
19
+ DEFAULT_STYLE = "Photograph"
20
+ DEFAULT_NEGATIVE = (
21
+ "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon,"
22
+ " drawing, anime, asian, bad anatomy:1.4), text, close up, cropped, out of frame,"
23
+ " worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated,"
24
+ " extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation,"
25
+ " deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned"
26
+ " face, disfigured, gross proportions, malformed limbs, missing arms, missing legs,"
27
+ " extra arms, extra legs, fused fingers, too many fingers, long neck, {negative}"
28
+ )
29
+ STYLES = {
30
+ "Photograph": (
31
+ (
32
+ "realistic photograph of {positive}, ultra fine detail, lifelike,"
33
+ " high-resolution, sharp, realistic colors, photorealistic, Nikon, 35mm"
34
+ ),
35
+ DEFAULT_NEGATIVE,
36
+ ),
37
+ "Cinematic": (
38
+ (
39
+ "cinematic photograph of {positive}, 35mm photograph, film, bokeh,"
40
+ " professional, 4k, highly detailed"
41
+ ),
42
+ DEFAULT_NEGATIVE,
43
+ ),
44
+ "Still Photo": (
45
+ (
46
+ "cinematic still photograph of {positive}, emotional, harmonious, vignette,"
47
+ " highly detailed, bokeh, cinemascope, moody, epic, gorgeous, film grain,"
48
+ " grainy, high resolution"
49
+ ),
50
+ DEFAULT_NEGATIVE,
51
+ ),
52
+ }
53
+
54
+
55
+ def apply_style(name: str, pos: str, neg: str) -> Tuple[str, str]:
56
+ try:
57
+ def_pos, def_neg = STYLES[name]
58
+ except KeyError:
59
+ def_pos, def_neg = "{positive}", "{negative}"
60
+ finally:
61
+ pos = def_pos.replace("{positive}", pos).strip().strip(",")
62
+ neg = def_neg.replace("{negative}", ", " + neg).strip().strip(",")
63
+ return (pos, neg)
64
+
65
+
66
+ DESCRIPTION = ""
67
+ MAX_SEED = np.iinfo(np.int32).max
68
+ CACHE_EXAMPLES = "lazy"
69
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
70
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "1") == "1"
71
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "1") == "1"
72
+
73
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
74
+
75
+ NUM_IMAGES_PER_PROMPT = 2
76
+
77
+ if torch.cuda.is_available():
78
+ pipe = DiffusionPipeline.from_pretrained(
79
+ "SG161222/RealVisXL_V3.0_Turbo",
80
+ torch_dtype=torch.float16,
81
+ use_safetensors=True,
82
+ add_watermarker=False,
83
+ variant="fp16",
84
+ )
85
+ pipe2 = DiffusionPipeline.from_pretrained(
86
+ "SG161222/RealVisXL_V2.02_Turbo",
87
+ torch_dtype=torch.float16,
88
+ use_safetensors=True,
89
+ add_watermarker=False,
90
+ variant="fp16",
91
+ )
92
+ if ENABLE_CPU_OFFLOAD:
93
+ pipe.enable_model_cpu_offload()
94
+ pipe2.enable_model_cpu_offload()
95
+ else:
96
+ pipe.to(device)
97
+ pipe2.to(device)
98
+ print("Loaded on Device!")
99
+
100
+ if USE_TORCH_COMPILE:
101
+ pipe.unet = torch.compile(pipe.unet, mode="max-autotune", fullgraph=True)
102
+ pipe2.unet = torch.compile(pipe2.unet, mode="max-autotune", fullgraph=True)
103
+ print("Model Compiled!")
104
+
105
+
106
+ def save_image(img):
107
+ unique_name = str(uuid.uuid4()) + ".png"
108
+ img.save(unique_name)
109
+ return unique_name
110
+
111
+
112
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
113
+ if randomize_seed:
114
+ seed = random.randint(0, MAX_SEED)
115
+ return seed
116
+
117
+
118
+ @spaces.GPU(enable_queue=True)
119
+ def generate(
120
+ prompt: str,
121
+ negative_prompt: str = "",
122
+ use_negative_prompt: bool = False,
123
+ style: str = DEFAULT_STYLE,
124
+ seed: int = 0,
125
+ width: int = 896,
126
+ height: int = 1152,
127
+ guidance_scale: float = 3,
128
+ randomize_seed: bool = False,
129
+ use_resolution_binning: bool = True,
130
+ progress=gr.Progress(track_tqdm=True),
131
+ ):
132
+
133
+
134
+ print("prompt1 = ", prompt)
135
+ print("negative_prompt1 = ", negative_prompt)
136
+
137
+ prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
138
+ seed = int(randomize_seed_fn(seed, randomize_seed))
139
+ generator = torch.Generator().manual_seed(seed)
140
+
141
+ print("prompt2 = ", prompt)
142
+ print("negative_prompt2 = ", negative_prompt)
143
+
144
+ print("seed = ", seed)
145
+
146
+ options = {
147
+ "prompt": prompt,
148
+ "negative_prompt": negative_prompt,
149
+ "width": width,
150
+ "height": height,
151
+ "guidance_scale": guidance_scale,
152
+ "num_inference_steps": 25,
153
+ "generator": generator,
154
+ "num_images_per_prompt": NUM_IMAGES_PER_PROMPT,
155
+ "use_resolution_binning": use_resolution_binning,
156
+ "output_type": "pil",
157
+ }
158
+
159
+ print("options = ")
160
+ print(options)
161
+
162
+ images = pipe(**options).images + pipe2(**options).images
163
+
164
+ image_paths = [save_image(img) for img in images]
165
+ return image_paths, seed
166
+
167
+
168
+ examples = [
169
+ (
170
+ "college life of 21 year old, depth of field, bokeh, shallow"
171
+ " focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128"
172
+ " --v 6.0 --style raw"
173
+ ),
174
+ ]
175
+
176
+ css = """
177
+ """
178
+
179
+ with gr.Blocks(css=css, theme="rawrsor1/Everforest") as demo:
180
+ with gr.Group():
181
+ with gr.Row():
182
+ prompt = gr.Text(
183
+ label="Prompt",
184
+ show_label=False,
185
+ max_lines=4,
186
+ placeholder="Enter a Prompt",
187
+ container=False,
188
+ )
189
+ run_button = gr.Button("Run")
190
+ result = ImageFeed(label="Result")
191
+ # result = gr.Gallery(label="Result", columns=1, preview=True)
192
+
193
+ with gr.Accordion("Advanced", open=False):
194
+ use_negative_prompt = gr.Checkbox(
195
+ label="Use Negative", value=True, visible=True
196
+ )
197
+ negative_prompt = gr.Text(
198
+ label="Negative Prompt",
199
+ max_lines=4,
200
+ placeholder="",
201
+ value="",
202
+ visible=True,
203
+ )
204
+ with gr.Row():
205
+ num_inference_steps = gr.Slider(
206
+ label="Steps",
207
+ minimum=10,
208
+ maximum=60,
209
+ step=1,
210
+ value=30,
211
+ )
212
+ with gr.Row():
213
+ num_images_per_prompt = gr.Slider(
214
+ label="Image Count",
215
+ minimum=1,
216
+ maximum=5,
217
+ step=1,
218
+ value=2,
219
+ )
220
+ seed = gr.Slider(
221
+ label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, visible=True
222
+ )
223
+ randomize_seed = gr.Checkbox(label="New Seed", value=True)
224
+ with gr.Row(visible=True):
225
+ width = gr.Slider(
226
+ label="Width",
227
+ minimum=512,
228
+ maximum=2048,
229
+ step=16,
230
+ value=896,
231
+ )
232
+ height = gr.Slider(
233
+ label="Height",
234
+ minimum=512,
235
+ maximum=2048,
236
+ step=16,
237
+ value=1152,
238
+ )
239
+ with gr.Row():
240
+ guidance_scale = gr.Slider(
241
+ label="Guidance",
242
+ minimum=0.1,
243
+ maximum=20.0,
244
+ step=0.1,
245
+ value=6,
246
+ )
247
+ with gr.Row(visible=True):
248
+ style_selection = gr.Radio(
249
+ show_label=True,
250
+ container=True,
251
+ interactive=True,
252
+ choices=list(STYLES.keys()),
253
+ value=DEFAULT_STYLE,
254
+ label="Style",
255
+ )
256
+ gr.Examples(
257
+ examples=examples,
258
+ inputs=prompt,
259
+ outputs=[result, seed],
260
+ fn=generate,
261
+ cache_examples=CACHE_EXAMPLES,
262
+ )
263
+
264
+ use_negative_prompt.change(
265
+ fn=lambda x: gr.update(visible=x),
266
+ inputs=use_negative_prompt,
267
+ outputs=negative_prompt,
268
+ api_name=False,
269
+ )
270
+
271
+ gr.on(
272
+ triggers=[
273
+ prompt.submit,
274
+ negative_prompt.submit,
275
+ run_button.click,
276
+ ],
277
+ fn=generate,
278
+ inputs=[
279
+ prompt,
280
+ negative_prompt,
281
+ use_negative_prompt,
282
+ style_selection,
283
+ seed,
284
+ width,
285
+ height,
286
+ guidance_scale,
287
+ randomize_seed,
288
+ ],
289
+ outputs=[result, seed],
290
+ api_name="run",
291
+ )
292
+
293
+ if __name__ == "__main__":
294
+ demo.queue(max_size=20).launch()
app.py CHANGED
@@ -1,78 +1,101 @@
1
  #!/usr/bin/env python
2
 
3
- import json
4
  import os
5
  import random
6
- from typing import Tuple
7
  import uuid
 
8
 
9
- from diffusers import DiffusionPipeline
10
  import gradio as gr
11
  import numpy as np
12
  from PIL import Image
13
  import spaces
14
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- from gradio_imagefeed import ImageFeed
17
-
18
-
19
- DEFAULT_STYLE = "Photograph"
20
- DEFAULT_NEGATIVE = (
21
- "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon,"
22
- " drawing, anime, asian, bad anatomy:1.4), text, close up, cropped, out of frame,"
23
- " worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated,"
24
- " extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation,"
25
- " deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned"
26
- " face, disfigured, gross proportions, malformed limbs, missing arms, missing legs,"
27
- " extra arms, extra legs, fused fingers, too many fingers, long neck, {negative}"
28
- )
29
- STYLES = {
30
- "Photograph": (
31
- (
32
- "realistic photograph of {positive}, ultra fine detail, lifelike,"
33
- " high-resolution, sharp, realistic colors, photorealistic, Nikon, 35mm"
34
- ),
35
- DEFAULT_NEGATIVE,
36
- ),
37
- "Cinematic": (
38
- (
39
- "cinematic photograph of {positive}, 35mm photograph, film, bokeh,"
40
- " professional, 4k, highly detailed"
41
- ),
42
- DEFAULT_NEGATIVE,
43
- ),
44
- "Still Photo": (
45
- (
46
- "cinematic still photograph of {positive}, emotional, harmonious, vignette,"
47
- " highly detailed, bokeh, cinemascope, moody, epic, gorgeous, film grain,"
48
- " grainy, high resolution"
49
- ),
50
- DEFAULT_NEGATIVE,
51
- ),
52
- }
53
-
54
-
55
- def apply_style(name: str, pos: str, neg: str) -> Tuple[str, str]:
56
- try:
57
- def_pos, def_neg = STYLES[name]
58
- except KeyError:
59
- def_pos, def_neg = "{positive}", "{negative}"
60
- finally:
61
- pos = def_pos.replace("{positive}", pos).strip().strip(",")
62
- neg = def_neg.replace("{negative}", ", " + neg).strip().strip(",")
63
- return (pos, neg)
64
-
65
-
66
- DESCRIPTION = ""
67
  MAX_SEED = np.iinfo(np.int32).max
68
- CACHE_EXAMPLES = "lazy"
69
  MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
70
- USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "1") == "1"
71
- ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "1") == "1"
72
 
73
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
74
 
75
- NUM_IMAGES_PER_PROMPT = 2
76
 
77
  if torch.cuda.is_available():
78
  pipe = DiffusionPipeline.from_pretrained(
@@ -80,69 +103,60 @@ if torch.cuda.is_available():
80
  torch_dtype=torch.float16,
81
  use_safetensors=True,
82
  add_watermarker=False,
83
- variant="fp16",
84
  )
85
  pipe2 = DiffusionPipeline.from_pretrained(
86
  "SG161222/RealVisXL_V2.02_Turbo",
87
  torch_dtype=torch.float16,
88
  use_safetensors=True,
89
  add_watermarker=False,
90
- variant="fp16",
91
  )
92
  if ENABLE_CPU_OFFLOAD:
93
  pipe.enable_model_cpu_offload()
94
  pipe2.enable_model_cpu_offload()
95
  else:
96
- pipe.to(device)
97
- pipe2.to(device)
98
  print("Loaded on Device!")
99
-
100
  if USE_TORCH_COMPILE:
101
- pipe.unet = torch.compile(pipe.unet, mode="max-autotune", fullgraph=True)
102
- pipe2.unet = torch.compile(pipe2.unet, mode="max-autotune", fullgraph=True)
103
  print("Model Compiled!")
104
 
105
-
106
  def save_image(img):
107
  unique_name = str(uuid.uuid4()) + ".png"
108
  img.save(unique_name)
109
  return unique_name
110
 
111
-
112
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
113
  if randomize_seed:
114
  seed = random.randint(0, MAX_SEED)
115
  return seed
116
 
117
-
118
  @spaces.GPU(enable_queue=True)
119
  def generate(
120
  prompt: str,
121
  negative_prompt: str = "",
122
  use_negative_prompt: bool = False,
123
- style: str = DEFAULT_STYLE,
124
  seed: int = 0,
125
- width: int = 896,
126
- height: int = 1152,
127
  guidance_scale: float = 3,
128
  randomize_seed: bool = False,
129
  use_resolution_binning: bool = True,
130
  progress=gr.Progress(track_tqdm=True),
131
  ):
132
-
133
-
134
- print("prompt1 = ", prompt)
135
- print("negative_prompt1 = ", negative_prompt)
136
-
137
  prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
138
  seed = int(randomize_seed_fn(seed, randomize_seed))
139
  generator = torch.Generator().manual_seed(seed)
140
 
141
- print("prompt2 = ", prompt)
142
- print("negative_prompt2 = ", negative_prompt)
143
-
144
- print("seed = ", seed)
145
-
146
  options = {
147
  "prompt": prompt,
148
  "negative_prompt": negative_prompt,
@@ -155,50 +169,51 @@ def generate(
155
  "use_resolution_binning": use_resolution_binning,
156
  "output_type": "pil",
157
  }
 
 
158
 
159
- print("options = ")
160
- print(options)
161
 
162
  images = pipe(**options).images + pipe2(**options).images
163
 
164
  image_paths = [save_image(img) for img in images]
165
  return image_paths, seed
166
 
167
-
168
  examples = [
169
- (
170
- "college life of 21 year old, depth of field, bokeh, shallow"
171
- " focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128"
172
- " --v 6.0 --style raw"
173
- ),
174
  ]
175
 
176
- css = """
177
- """
178
-
179
- with gr.Blocks(css=css, theme="rawrsor1/Everforest") as demo:
 
 
 
 
 
 
 
180
  with gr.Group():
181
  with gr.Row():
182
  prompt = gr.Text(
183
  label="Prompt",
184
  show_label=False,
185
  max_lines=4,
186
- placeholder="Enter a Prompt",
187
  container=False,
188
  )
189
  run_button = gr.Button("Run")
190
- result = ImageFeed(label="Result")
191
- # result = gr.Gallery(label="Result", columns=1, preview=True)
192
-
193
- with gr.Accordion("Advanced", open=False):
194
- use_negative_prompt = gr.Checkbox(
195
- label="Use Negative", value=True, visible=True
196
- )
197
  negative_prompt = gr.Text(
198
- label="Negative Prompt",
199
  max_lines=4,
200
- placeholder="",
201
- value="",
202
  visible=True,
203
  )
204
  with gr.Row():
@@ -211,34 +226,39 @@ with gr.Blocks(css=css, theme="rawrsor1/Everforest") as demo:
211
  )
212
  with gr.Row():
213
  num_images_per_prompt = gr.Slider(
214
- label="Image Count",
215
  minimum=1,
216
  maximum=5,
217
  step=1,
218
  value=2,
219
  )
220
  seed = gr.Slider(
221
- label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, visible=True
 
 
 
 
 
222
  )
223
- randomize_seed = gr.Checkbox(label="New Seed", value=True)
224
  with gr.Row(visible=True):
225
  width = gr.Slider(
226
  label="Width",
227
  minimum=512,
228
  maximum=2048,
229
- step=16,
230
- value=896,
231
  )
232
  height = gr.Slider(
233
  label="Height",
234
  minimum=512,
235
  maximum=2048,
236
- step=16,
237
- value=1152,
238
  )
239
  with gr.Row():
240
  guidance_scale = gr.Slider(
241
- label="Guidance",
242
  minimum=0.1,
243
  maximum=20.0,
244
  step=0.1,
@@ -249,9 +269,9 @@ with gr.Blocks(css=css, theme="rawrsor1/Everforest") as demo:
249
  show_label=True,
250
  container=True,
251
  interactive=True,
252
- choices=list(STYLES.keys()),
253
- value=DEFAULT_STYLE,
254
- label="Style",
255
  )
256
  gr.Examples(
257
  examples=examples,
 
1
  #!/usr/bin/env python
2
 
 
3
  import os
4
  import random
 
5
  import uuid
6
+ import json
7
 
 
8
  import gradio as gr
9
  import numpy as np
10
  from PIL import Image
11
  import spaces
12
  import torch
13
+ from diffusers import DiffusionPipeline
14
+ from typing import Tuple
15
+
16
+ #Check for the Model Base..//
17
+
18
+
19
+
20
+ bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
21
+ bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
22
+ default_negative = os.getenv("default_negative","")
23
+
24
+ def check_text(prompt, negative=""):
25
+ return False
26
+
27
+ style_list = [
28
+
29
+ {
30
+ "name": "2560 x 1440",
31
+ "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
32
+ "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
33
+ },
34
+
35
+ {
36
+ "name": "Photo",
37
+ "prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
38
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
39
+ },
40
+
41
+ {
42
+ "name": "Cinematic",
43
+ "prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
44
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
45
+ },
46
+
47
+ {
48
+ "name": "Anime",
49
+ "prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
50
+ "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
51
+ },
52
+ {
53
+ "name": "3D Model",
54
+ "prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
55
+ "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
56
+ },
57
+ {
58
+ "name": "(No style)",
59
+ "prompt": "{prompt}",
60
+ "negative_prompt": "",
61
+ },
62
+ ]
63
+
64
+ styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
65
+ STYLE_NAMES = list(styles.keys())
66
+ DEFAULT_STYLE_NAME = "2560 x 1440"
67
+
68
+ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
69
+ p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
70
+ if not negative:
71
+ negative = ""
72
+ return p.replace("{prompt}", positive), n + negative
73
+
74
+
75
+
76
+
77
+
78
+ DESCRIPTION = """## MidJourney
79
+
80
+ Drop your best results in the community: [rb.gy/klkbs7](http://rb.gy/klkbs7), Have you tried the stable hamster space? [rb.gy/hfrm2f](http://rb.gy/hfrm2f)
81
+ """
82
+
83
+
84
+
85
+
86
+
87
+ if not torch.cuda.is_available():
88
+ DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  MAX_SEED = np.iinfo(np.int32).max
91
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
92
  MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
93
+ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
94
+ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
95
 
96
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
97
 
98
+ NUM_IMAGES_PER_PROMPT = 1
99
 
100
  if torch.cuda.is_available():
101
  pipe = DiffusionPipeline.from_pretrained(
 
103
  torch_dtype=torch.float16,
104
  use_safetensors=True,
105
  add_watermarker=False,
106
+ variant="fp16"
107
  )
108
  pipe2 = DiffusionPipeline.from_pretrained(
109
  "SG161222/RealVisXL_V2.02_Turbo",
110
  torch_dtype=torch.float16,
111
  use_safetensors=True,
112
  add_watermarker=False,
113
+ variant="fp16"
114
  )
115
  if ENABLE_CPU_OFFLOAD:
116
  pipe.enable_model_cpu_offload()
117
  pipe2.enable_model_cpu_offload()
118
  else:
119
+ pipe.to(device)
120
+ pipe2.to(device)
121
  print("Loaded on Device!")
122
+
123
  if USE_TORCH_COMPILE:
124
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
125
+ pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
126
  print("Model Compiled!")
127
 
 
128
  def save_image(img):
129
  unique_name = str(uuid.uuid4()) + ".png"
130
  img.save(unique_name)
131
  return unique_name
132
 
 
133
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
134
  if randomize_seed:
135
  seed = random.randint(0, MAX_SEED)
136
  return seed
137
 
 
138
  @spaces.GPU(enable_queue=True)
139
  def generate(
140
  prompt: str,
141
  negative_prompt: str = "",
142
  use_negative_prompt: bool = False,
143
+ style: str = DEFAULT_STYLE_NAME,
144
  seed: int = 0,
145
+ width: int = 1024,
146
+ height: int = 1024,
147
  guidance_scale: float = 3,
148
  randomize_seed: bool = False,
149
  use_resolution_binning: bool = True,
150
  progress=gr.Progress(track_tqdm=True),
151
  ):
 
 
 
 
 
152
  prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
153
  seed = int(randomize_seed_fn(seed, randomize_seed))
154
  generator = torch.Generator().manual_seed(seed)
155
 
156
+ if not use_negative_prompt:
157
+ negative_prompt = "" # type: ignore
158
+ negative_prompt += default_negative
159
+
 
160
  options = {
161
  "prompt": prompt,
162
  "negative_prompt": negative_prompt,
 
169
  "use_resolution_binning": use_resolution_binning,
170
  "output_type": "pil",
171
  }
172
+ print("options = ", options)
173
+
174
 
 
 
175
 
176
  images = pipe(**options).images + pipe2(**options).images
177
 
178
  image_paths = [save_image(img) for img in images]
179
  return image_paths, seed
180
 
 
181
  examples = [
182
+ "A closeup of a cat, a window, in a rustic cabin, close up, with a shallow depth of field, with a vintage film grain, in the style of Annie Leibovitz and in the style of Wes Anderson. --ar 85:128 --v 6.0 --style raw",
183
+ "Daria Morgendorffer the main character of the animated series Daria, serious expression, very excites sultry look, so hot girl, beautiful charismatic girl, so hot shot, a woman wearing eye glasses, gorgeous figure, interesting shapes, life-size figures",
184
+ "Dark green large leaves of anthurium, close up, photography, aerial view, in the style of unsplash, hasselblad h6d400c --ar 85:128 --v 6.0 --style raw",
185
+ "Closeup of blonde woman depth of field, bokeh, shallow focus, minimalism, fujifilm xh2s with Canon EF lens, cinematic --ar 85:128 --v 6.0 --style raw"
 
186
  ]
187
 
188
+ css = '''
189
+ .gradio-container{max-width: 700px !important}
190
+ h1{text-align:center}
191
+ '''
192
+ with gr.Blocks() as demo:
193
+ gr.Markdown(DESCRIPTION)
194
+ gr.DuplicateButton(
195
+ value="Duplicate Space for private use",
196
+ elem_id="duplicate-button",
197
+ visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
198
+ )
199
  with gr.Group():
200
  with gr.Row():
201
  prompt = gr.Text(
202
  label="Prompt",
203
  show_label=False,
204
  max_lines=4,
205
+ placeholder="Enter your prompt",
206
  container=False,
207
  )
208
  run_button = gr.Button("Run")
209
+ result = gr.Gallery(label="Result", columns=1, preview=True)
210
+ with gr.Accordion("Advanced options", open=False):
211
+ use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
 
 
 
 
212
  negative_prompt = gr.Text(
213
+ label="Negative prompt",
214
  max_lines=4,
215
+ placeholder="Enter a negative prompt",
216
+ value="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
217
  visible=True,
218
  )
219
  with gr.Row():
 
226
  )
227
  with gr.Row():
228
  num_images_per_prompt = gr.Slider(
229
+ label="Images",
230
  minimum=1,
231
  maximum=5,
232
  step=1,
233
  value=2,
234
  )
235
  seed = gr.Slider(
236
+ label="Seed",
237
+ minimum=0,
238
+ maximum=MAX_SEED,
239
+ step=1,
240
+ value=0,
241
+ visible=True
242
  )
243
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
244
  with gr.Row(visible=True):
245
  width = gr.Slider(
246
  label="Width",
247
  minimum=512,
248
  maximum=2048,
249
+ step=8,
250
+ value=1024,
251
  )
252
  height = gr.Slider(
253
  label="Height",
254
  minimum=512,
255
  maximum=2048,
256
+ step=8,
257
+ value=1024,
258
  )
259
  with gr.Row():
260
  guidance_scale = gr.Slider(
261
+ label="Guidance Scale",
262
  minimum=0.1,
263
  maximum=20.0,
264
  step=0.1,
 
269
  show_label=True,
270
  container=True,
271
  interactive=True,
272
+ choices=STYLE_NAMES,
273
+ value=DEFAULT_STYLE_NAME,
274
+ label="Image Style",
275
  )
276
  gr.Examples(
277
  examples=examples,