prithivMLmods commited on
Commit
40e0695
·
verified ·
1 Parent(s): f4d7114

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -173
app.py CHANGED
@@ -1,4 +1,5 @@
1
- #path1.0398
 
2
  import os
3
  import random
4
  import uuid
@@ -12,7 +13,8 @@ import torch
12
  from diffusers import DiffusionPipeline
13
  from typing import Tuple
14
 
15
- # BaseConditions
 
16
  bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
17
  bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
18
  default_negative = os.getenv("default_negative","")
@@ -27,121 +29,49 @@ def check_text(prompt, negative=""):
27
  return False
28
 
29
  style_list = [
30
- {
31
- "name": "3840 x 2160",
32
- "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
33
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
34
- },
35
  {
36
  "name": "2560 x 1440",
37
  "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
38
  "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
39
  },
40
  {
41
- "name": "3D Model",
42
- "prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
43
- "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
44
- },
45
- ]
46
-
47
- collage_style_list = [
48
-
49
-
50
- {
51
- "name": "B & W",
52
- "prompt": "black and white collage of {prompt}. monochromatic, timeless, classic, dramatic contrast",
53
- "negative_prompt": "colorful, vibrant, bright, flashy",
54
- },
55
-
56
- {
57
- "name": "Polaroid",
58
- "prompt": "collage of polaroid photos featuring {prompt}. vintage style, high contrast, nostalgic, instant film aesthetic",
59
- "negative_prompt": "digital, modern, low quality, blurry",
60
- },
61
-
62
- {
63
- "name": "Watercolor",
64
- "prompt": "watercolor collage of {prompt}. soft edges, translucent colors, painterly effects",
65
- "negative_prompt": "digital, sharp lines, solid colors",
66
  },
67
-
68
  {
69
  "name": "Cinematic",
70
- "prompt": "cinematic collage of {prompt}. film stills, movie posters, dramatic lighting",
71
- "negative_prompt": "static, lifeless, mundane",
72
  },
73
-
74
  {
75
- "name": "Nostalgic",
76
- "prompt": "nostalgic collage of {prompt}. retro imagery, vintage objects, sentimental journey",
77
- "negative_prompt": "contemporary, futuristic, forward-looking",
78
  },
79
-
80
  {
81
- "name": "Vintage",
82
- "prompt": "vintage collage of {prompt}. aged paper, sepia tones, retro imagery, antique vibes",
83
- "negative_prompt": "modern, contemporary, futuristic, high-tech",
84
- },
85
-
86
- {
87
- "name": "StoryBook",
88
- "prompt": "storybook collage of {prompt}. whimsical illustrations, handwritten text, fairy tale motifs",
89
- "negative_prompt": "technical, sterile, devoid of imagination",
90
- },
91
-
92
-
93
- {
94
- "name": "NeoNGlow",
95
- "prompt": "neon glow collage of {prompt}. vibrant colors, glowing effects, futuristic vibes",
96
- "negative_prompt": "dull, muted colors, vintage, retro",
97
- },
98
-
99
- {
100
- "name": "Geometric",
101
- "prompt": "geometric collage of {prompt}. abstract shapes, colorful, sharp edges, modern design, high quality",
102
- "negative_prompt": "blurry, low quality, traditional, dull",
103
- },
104
-
105
- {
106
- "name": "ComicBook",
107
- "prompt": "comic book-style collage of {prompt}. dynamic panels, speech bubbles, bold lines, vibrant colors",
108
- "negative_prompt": "static, monotonous, muted colors",
109
  },
110
-
111
  {
112
- "name": "Retro Pop",
113
- "prompt": "retro pop art collage of {prompt}. bold colors, comic book style, halftone dots, vintage ads",
114
- "negative_prompt": "subdued colors, minimalist, modern, subtle",
115
- },
116
-
117
-
118
- {
119
- "name": "No Style",
120
  "prompt": "{prompt}",
121
  "negative_prompt": "",
122
  },
123
  ]
124
 
125
  styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
126
- collage_styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in collage_style_list}
127
  STYLE_NAMES = list(styles.keys())
128
- COLLAGE_STYLE_NAMES = list(collage_styles.keys())
129
- DEFAULT_STYLE_NAME = "3840 x 2160"
130
- DEFAULT_COLLAGE_STYLE_NAME = "Cinematic"
131
 
132
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
133
- if style_name in styles:
134
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
135
- elif style_name in collage_styles:
136
- p, n = collage_styles.get(style_name, collage_styles[DEFAULT_COLLAGE_STYLE_NAME])
137
- else:
138
- p, n = styles[DEFAULT_STYLE_NAME]
139
-
140
  if not negative:
141
  negative = ""
142
  return p.replace("{prompt}", positive), n + negative
143
 
144
- DESCRIPTION = """"""
145
  if not torch.cuda.is_available():
146
  DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
147
 
@@ -153,27 +83,38 @@ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
153
 
154
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
155
 
 
 
156
  if torch.cuda.is_available():
157
  pipe = DiffusionPipeline.from_pretrained(
158
  "Corcelio/mobius",
159
  torch_dtype=torch.float16,
160
  use_safetensors=True,
161
- add_watermarker=False,
162
- variant="fp16"
163
- ).to(device)
164
-
 
 
 
 
165
  if ENABLE_CPU_OFFLOAD:
166
  pipe.enable_model_cpu_offload()
 
167
  else:
168
- pipe.to(device)
 
169
  print("Loaded on Device!")
170
-
171
  if USE_TORCH_COMPILE:
172
  pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
 
173
  print("Model Compiled!")
174
 
175
- def save_image(img, path):
176
- img.save(path)
 
 
177
 
178
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
179
  if randomize_seed:
@@ -186,8 +127,6 @@ def generate(
186
  negative_prompt: str = "",
187
  use_negative_prompt: bool = False,
188
  style: str = DEFAULT_STYLE_NAME,
189
- collage_style: str = DEFAULT_COLLAGE_STYLE_NAME,
190
- grid_size: str = "2x2",
191
  seed: int = 0,
192
  width: int = 1024,
193
  height: int = 1024,
@@ -199,11 +138,7 @@ def generate(
199
  if check_text(prompt, negative_prompt):
200
  raise ValueError("Prompt contains restricted words.")
201
 
202
- if collage_style != "No Style":
203
- prompt, negative_prompt = apply_style(collage_style, prompt, negative_prompt)
204
- else:
205
- prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
206
-
207
  seed = int(randomize_seed_fn(seed, randomize_seed))
208
  generator = torch.Generator().manual_seed(seed)
209
 
@@ -211,18 +146,6 @@ def generate(
211
  negative_prompt = "" # type: ignore
212
  negative_prompt += default_negative
213
 
214
- grid_sizes = {
215
- "2x1": (2, 1),
216
- "1x2": (1, 2),
217
- "2x2": (2, 2),
218
- "2x3": (2, 3),
219
- "3x2": (3, 2),
220
- "1x1": (1, 1)
221
- }
222
-
223
- grid_size_x, grid_size_y = grid_sizes.get(grid_size, (2, 2))
224
- num_images = grid_size_x * grid_size_y
225
-
226
  options = {
227
  "prompt": prompt,
228
  "negative_prompt": negative_prompt,
@@ -231,35 +154,28 @@ def generate(
231
  "guidance_scale": guidance_scale,
232
  "num_inference_steps": 25,
233
  "generator": generator,
234
- "num_images_per_prompt": num_images,
235
  "use_resolution_binning": use_resolution_binning,
236
  "output_type": "pil",
237
  }
238
 
239
- torch.cuda.empty_cache() # Clear GPU memory
240
- images = pipe(**options).images
241
-
242
- grid_img = Image.new('RGB', (width * grid_size_x, height * grid_size_y))
243
-
244
- for i, img in enumerate(images[:num_images]):
245
- grid_img.paste(img, (i % grid_size_x * width, i // grid_size_x * height))
246
 
247
- unique_name = str(uuid.uuid4()) + ".png"
248
- save_image(grid_img, unique_name)
249
- return [unique_name], seed
250
 
251
  examples = [
252
-
253
- "Portrait of a beautiful woman in a hat, summer outfit, with freckles on her face, in a close up shot, with sunlight, outdoors, in soft light, with a beach background, looking at the camera, with high resolution photography, in the style of Hasselblad X2D50c --ar 85:128 --v 6.0 --style raw",
254
- "Dragon ball, portrait of dr goku, in the style of street art aesthetic, cute cartoonish designs, photo-realistic techniques, dark red, childhood arcadias, anime aesthetic, cartoon-like figures --ar 73:98 --stylize 750 --v 6"
255
-
256
  ]
257
 
258
  css = '''
259
- .gradio-container{max-width: 530px !important}
260
  h1{text-align:center}
261
  '''
262
- with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
263
  gr.Markdown(DESCRIPTION)
264
  gr.DuplicateButton(
265
  value="Duplicate Space for private use",
@@ -276,49 +192,23 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
276
  container=False,
277
  )
278
  run_button = gr.Button("Run")
279
- result = gr.Gallery(label="Grid", columns=1, preview=True)
280
-
281
- with gr.Row(visible=True):
282
- collage_style_selection = gr.Radio(
283
- show_label=True,
284
- container=True,
285
- interactive=True,
286
- choices=COLLAGE_STYLE_NAMES,
287
- value=DEFAULT_COLLAGE_STYLE_NAME,
288
- label="Collage Template",
289
- )
290
- with gr.Row(visible=True):
291
- grid_size_selection = gr.Dropdown(
292
- choices=["2x1", "1x2", "2x2", "2x3", "3x2", "1x1"],
293
- value="2x3",
294
- label="Grid Size"
295
- )
296
- with gr.Row(visible=True):
297
- style_selection = gr.Radio(
298
- show_label=True,
299
- container=True,
300
- interactive=True,
301
- choices=STYLE_NAMES,
302
- value=DEFAULT_STYLE_NAME,
303
- label="Style",
304
- )
305
-
306
  with gr.Accordion("Advanced options", open=False):
307
  use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
308
  negative_prompt = gr.Text(
309
  label="Negative prompt",
310
  max_lines=1,
311
  placeholder="Enter a negative prompt",
312
- value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
313
  visible=True,
314
  )
315
  with gr.Row():
316
  num_inference_steps = gr.Slider(
317
  label="Steps",
318
  minimum=10,
319
- maximum=30,
320
  step=1,
321
- value=15,
322
  )
323
  with gr.Row():
324
  num_images_per_prompt = gr.Slider(
@@ -337,8 +227,6 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
337
  visible=True
338
  )
339
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
340
-
341
-
342
  with gr.Row(visible=True):
343
  width = gr.Slider(
344
  label="Width",
@@ -354,9 +242,6 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
354
  step=8,
355
  value=1024,
356
  )
357
-
358
-
359
-
360
  with gr.Row():
361
  guidance_scale = gr.Slider(
362
  label="Guidance Scale",
@@ -365,9 +250,15 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
365
  step=0.1,
366
  value=6,
367
  )
368
-
369
-
370
-
 
 
 
 
 
 
371
  gr.Examples(
372
  examples=examples,
373
  inputs=prompt,
@@ -395,8 +286,6 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
395
  negative_prompt,
396
  use_negative_prompt,
397
  style_selection,
398
- collage_style_selection,
399
- grid_size_selection,
400
  seed,
401
  width,
402
  height,
 
1
+ #!/usr/bin/env python
2
+
3
  import os
4
  import random
5
  import uuid
 
13
  from diffusers import DiffusionPipeline
14
  from typing import Tuple
15
 
16
+ # Check for the Model Base
17
+
18
  bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
19
  bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
20
  default_negative = os.getenv("default_negative","")
 
29
  return False
30
 
31
  style_list = [
 
 
 
 
 
32
  {
33
  "name": "2560 x 1440",
34
  "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
35
  "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
36
  },
37
  {
38
+ "name": "Photo",
39
+ "prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
40
+ "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  },
 
42
  {
43
  "name": "Cinematic",
44
+ "prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
45
+ "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
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
+ DESCRIPTION = """ """
75
  if not torch.cuda.is_available():
76
  DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
77
 
 
83
 
84
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
85
 
86
+ NUM_IMAGES_PER_PROMPT = 1
87
+
88
  if torch.cuda.is_available():
89
  pipe = DiffusionPipeline.from_pretrained(
90
  "Corcelio/mobius",
91
  torch_dtype=torch.float16,
92
  use_safetensors=True,
93
+ add_watermarker=False
94
+ )
95
+ pipe2 = DiffusionPipeline.from_pretrained(
96
+ "ByteDance/SDXL-Lightning",
97
+ torch_dtype=torch.float16,
98
+ use_safetensors=True,
99
+ add_watermarker=False
100
+ )
101
  if ENABLE_CPU_OFFLOAD:
102
  pipe.enable_model_cpu_offload()
103
+ pipe2.enable_model_cpu_offload()
104
  else:
105
+ pipe.to(device)
106
+ pipe2.to(device)
107
  print("Loaded on Device!")
108
+
109
  if USE_TORCH_COMPILE:
110
  pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
111
+ pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
112
  print("Model Compiled!")
113
 
114
+ def save_image(img):
115
+ unique_name = str(uuid.uuid4()) + ".png"
116
+ img.save(unique_name)
117
+ return unique_name
118
 
119
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
120
  if randomize_seed:
 
127
  negative_prompt: str = "",
128
  use_negative_prompt: bool = False,
129
  style: str = DEFAULT_STYLE_NAME,
 
 
130
  seed: int = 0,
131
  width: int = 1024,
132
  height: int = 1024,
 
138
  if check_text(prompt, negative_prompt):
139
  raise ValueError("Prompt contains restricted words.")
140
 
141
+ prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
 
 
 
 
142
  seed = int(randomize_seed_fn(seed, randomize_seed))
143
  generator = torch.Generator().manual_seed(seed)
144
 
 
146
  negative_prompt = "" # type: ignore
147
  negative_prompt += default_negative
148
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  options = {
150
  "prompt": prompt,
151
  "negative_prompt": negative_prompt,
 
154
  "guidance_scale": guidance_scale,
155
  "num_inference_steps": 25,
156
  "generator": generator,
157
+ "num_images_per_prompt": NUM_IMAGES_PER_PROMPT,
158
  "use_resolution_binning": use_resolution_binning,
159
  "output_type": "pil",
160
  }
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
  examples = [
168
+ "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",
169
+ "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",
170
+ "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",
171
+ "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"
172
  ]
173
 
174
  css = '''
175
+ .gradio-container{max-width: 700px !important}
176
  h1{text-align:center}
177
  '''
178
+ with gr.Blocks(css=css, theme="finlaymacklon/smooth_slate") as demo:
179
  gr.Markdown(DESCRIPTION)
180
  gr.DuplicateButton(
181
  value="Duplicate Space for private use",
 
192
  container=False,
193
  )
194
  run_button = gr.Button("Run")
195
+ result = gr.Gallery(label="Result", columns=1, preview=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  with gr.Accordion("Advanced options", open=False):
197
  use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
198
  negative_prompt = gr.Text(
199
  label="Negative prompt",
200
  max_lines=1,
201
  placeholder="Enter a negative prompt",
202
+ 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",
203
  visible=True,
204
  )
205
  with gr.Row():
206
  num_inference_steps = gr.Slider(
207
  label="Steps",
208
  minimum=10,
209
+ maximum=60,
210
  step=1,
211
+ value=30,
212
  )
213
  with gr.Row():
214
  num_images_per_prompt = gr.Slider(
 
227
  visible=True
228
  )
229
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
 
230
  with gr.Row(visible=True):
231
  width = gr.Slider(
232
  label="Width",
 
242
  step=8,
243
  value=1024,
244
  )
 
 
 
245
  with gr.Row():
246
  guidance_scale = gr.Slider(
247
  label="Guidance Scale",
 
250
  step=0.1,
251
  value=6,
252
  )
253
+ with gr.Row(visible=True):
254
+ style_selection = gr.Radio(
255
+ show_label=True,
256
+ container=True,
257
+ interactive=True,
258
+ choices=STYLE_NAMES,
259
+ value=DEFAULT_STYLE_NAME,
260
+ label="Image Style",
261
+ )
262
  gr.Examples(
263
  examples=examples,
264
  inputs=prompt,
 
286
  negative_prompt,
287
  use_negative_prompt,
288
  style_selection,
 
 
289
  seed,
290
  width,
291
  height,