patrickvonplaten commited on
Commit
d27799d
1 Parent(s): 11a9900

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -139
app.py CHANGED
@@ -1,8 +1,4 @@
1
- from diffusers import (
2
- StableDiffusionPipeline,
3
- StableDiffusionImg2ImgPipeline,
4
- DPMSolverMultistepScheduler,
5
- )
6
  import gradio as gr
7
  import torch
8
  from PIL import Image
@@ -14,40 +10,8 @@ from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionS
14
 
15
  start_time = time.time()
16
  current_steps = 25
17
-
18
- SAFETY_CHECKER = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker", torch_dtype=torch.float16)
19
-
20
-
21
- class Model:
22
- def __init__(self, name, path=""):
23
- self.name = name
24
- self.path = path
25
-
26
- if path != "":
27
- self.pipe_t2i = StableDiffusionPipeline.from_pretrained(
28
- path, torch_dtype=torch.float16, safety_checker=SAFETY_CHECKER
29
- )
30
- self.pipe_t2i.scheduler = DPMSolverMultistepScheduler.from_config(
31
- self.pipe_t2i.scheduler.config
32
- )
33
- self.pipe_i2i = StableDiffusionImg2ImgPipeline(**self.pipe_t2i.components)
34
- else:
35
- self.pipe_t2i = None
36
- self.pipe_i2i = None
37
-
38
-
39
- models = [
40
- Model("Protogen v2.2 (Anime)", "darkstorm2150/Protogen_v2.2_Official_Release"),
41
- Model("Protogen x3.4 (Photorealism)", "darkstorm2150/Protogen_x3.4_Official_Release"),
42
- Model("Protogen x5.3 (Photorealism)", "darkstorm2150/Protogen_x5.3_Official_Release"),
43
- Model("Protogen x5.8 Rebuilt (Scifi+Anime)", "darkstorm2150/Protogen_x5.8_Official_Release"),
44
- Model("Protogen Dragon (RPG Model)", "darkstorm2150/Protogen_Dragon_Official_Release"),
45
- Model("Protogen Nova", "darkstorm2150/Protogen_Nova_Official_Release"),
46
- Model("Protogen Eclipse", "darkstorm2150/Protogen_Eclipse_Official_Release"),
47
- Model("Protogen Infinity", "darkstorm2150/Protogen_Infinity_Official_Release"),
48
- ]
49
-
50
- MODELS = {m.name: m for m in models}
51
 
52
  device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
53
 
@@ -83,79 +47,27 @@ def inference(
83
  generator = torch.Generator("cuda").manual_seed(seed)
84
 
85
  try:
86
- if img is not None:
87
- return (
88
- img_to_img(
89
- model_name,
90
- prompt,
91
- n_images,
92
- neg_prompt,
93
- img,
94
- strength,
95
- guidance,
96
- steps,
97
- width,
98
- height,
99
- generator,
100
- seed,
101
- ),
102
- f"Done. Seed: {seed}",
103
- )
104
- else:
105
- return (
106
- txt_to_img(
107
- model_name,
108
- prompt,
109
- n_images,
110
- neg_prompt,
111
- guidance,
112
- steps,
113
- width,
114
- height,
115
- generator,
116
- seed,
117
- ),
118
- f"Done. Seed: {seed}",
119
- )
120
  except Exception as e:
121
  return None, error_str(e)
122
 
123
 
124
- def txt_to_img(
125
- model_name,
126
- prompt,
127
- n_images,
128
- neg_prompt,
129
- guidance,
130
- steps,
131
- width,
132
- height,
133
- generator,
134
- seed,
135
- ):
136
- pipe = MODELS[model_name].pipe_t2i
137
-
138
- if torch.cuda.is_available():
139
- pipe = pipe.to("cuda")
140
- pipe.enable_xformers_memory_efficient_attention()
141
-
142
- result = pipe(
143
- prompt,
144
- negative_prompt=neg_prompt,
145
- num_images_per_prompt=n_images,
146
- num_inference_steps=int(steps),
147
- guidance_scale=guidance,
148
- width=width,
149
- height=height,
150
- generator=generator,
151
- )
152
-
153
- pipe.to("cpu")
154
- torch.cuda.empty_cache()
155
-
156
- return replace_nsfw_images(result)
157
-
158
-
159
  def img_to_img(
160
  model_name,
161
  prompt,
@@ -170,7 +82,7 @@ def img_to_img(
170
  generator,
171
  seed,
172
  ):
173
- pipe = MODELS[model_name].pipe_i2i
174
 
175
  if torch.cuda.is_available():
176
  pipe = pipe.to("cuda")
@@ -190,10 +102,8 @@ def img_to_img(
190
  generator=generator,
191
  )
192
 
193
- pipe.to("cpu")
194
- torch.cuda.empty_cache()
195
-
196
- return replace_nsfw_images(result)
197
 
198
 
199
  def replace_nsfw_images(results):
@@ -222,7 +132,6 @@ with gr.Blocks(css="style.css") as demo:
222
  """
223
  )
224
  with gr.Row():
225
-
226
  with gr.Column(scale=55):
227
  with gr.Group():
228
  model_name = gr.Dropdown(
@@ -231,11 +140,6 @@ with gr.Blocks(css="style.css") as demo:
231
  value=models[0].name,
232
  )
233
  with gr.Box(visible=False) as custom_model_group:
234
- custom_model_path = gr.Textbox(
235
- label="Custom model path",
236
- placeholder="Path to model, e.g. darkstorm2150/Protogen_x3.4_Official_Release",
237
- interactive=True,
238
- )
239
  gr.HTML(
240
  "<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>"
241
  )
@@ -285,19 +189,10 @@ with gr.Blocks(css="style.css") as demo:
285
  step=1,
286
  )
287
 
288
- with gr.Row():
289
- width = gr.Slider(
290
- label="Width", value=512, minimum=64, maximum=1024, step=8
291
- )
292
- height = gr.Slider(
293
- label="Height", value=512, minimum=64, maximum=1024, step=8
294
- )
295
-
296
  seed = gr.Slider(
297
  0, 2147483647, label="Seed (0 = random)", value=0, step=1
298
  )
299
 
300
- with gr.Tab("Image to image"):
301
  with gr.Group():
302
  image = gr.Image(
303
  label="Image", height=256, tool="editor", type="pil"
@@ -328,20 +223,11 @@ with gr.Blocks(css="style.css") as demo:
328
  generate.click(inference, inputs=inputs, outputs=outputs)
329
 
330
  ex = gr.Examples(
331
- [
332
- [models[0].name, "portrait of a beautiful alyx vance half life", 10, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
333
- [models[1].name, "Brad Pitt with sunglasses, highly realistic", 7.5, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
334
- [models[2].name, "(extremely detailed CG unity 8k wallpaper), the most beautiful artwork in the world", 7.5, 50, "human, people, canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
335
- [models[3].name, "(extremely detailed CG unity 8k wallpaper), full shot body photo star lord chris pratt posing in an outdoor spaceship, holding a gun, extremely detailed, trending on ArtStation, trending on CGSociety, Intricate, High Detail, dramatic, realism, beautiful and detailed lighting, shadows", 7.5, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
336
- [models[4].name, "(extremely detailed CG unity 8k wallpaper), full body portrait of (david:1.1), staring at us with a mysterious gaze, realistic, masterpiece, highest quality, ((scifi)), lens flare, ((light sparkles)), unreal engine, digital painting, trending on ArtStation, trending on CGSociety, Intricate, High Detail, dramatic, realism, beautiful and detailed lighting, shadows", 7.5, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
337
- [models[5].name, "(extremely detailed CG unity 8k wallpaper), full body portrait of (david:1.1), staring at us with a mysterious gaze, realistic, masterpiece, highest quality, ((scifi)), lens flare, ((light sparkles)), unreal engine, digital painting, trending on ArtStation, trending on CGSociety, Intricate, High Detail, dramatic, realism, beautiful and detailed lighting, shadows", 7.5, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
338
- [models[6].name, "(extremely detailed CG unity 8k wallpaper), full body portrait of (david:1.1), staring at us with a mysterious gaze, realistic, masterpiece, highest quality, ((scifi)), lens flare, ((light sparkles)), unreal engine, digital painting, trending on ArtStation, trending on CGSociety, Intricate, High Detail, dramatic, realism, beautiful and detailed lighting, shadows", 7.5, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
339
- [models[7].name, "(extremely detailed CG unity 8k wallpaper), full body portrait of (david:1.1), staring at us with a mysterious gaze, realistic, masterpiece, highest quality, ((scifi)), lens flare, ((light sparkles)), unreal engine, digital painting, trending on ArtStation, trending on CGSociety, Intricate, High Detail, dramatic, realism, beautiful and detailed lighting, shadows", 7.5, 50, "canvas frame, ((disfigured)), ((bad art)), ((deformed)),((extra limbs)),((close up)),((b&w)), weird colors, blurry, (((duplicate))), ((morbid)), ((mutilated)), [out of frame], extra fingers, mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), blurry, ((bad anatomy)), (((bad proportions))), ((extra limbs)), cloned face, (((disfigured))), out of frame, ugly, extra limbs, (bad anatomy), gross proportions, (malformed limbs), ((missing arms)), ((missing legs)), (((extra arms))), (((extra legs))), mutated hands, (fused fingers), (too many fingers), (((long neck))), Photoshop, video game, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, mutation, mutated, extra limbs, extra legs, extra arms, disfigured, deformed, cross-eye, body out of frame, blurry, bad art, bad anatomy"],
340
- ],
341
  inputs=[model_name, prompt, guidance, steps, neg_prompt],
342
  outputs=outputs,
343
  fn=inference,
344
- cache_examples=False,
345
  )
346
 
347
  gr.HTML(
 
1
+ from diffusers import DiffusionPipeline
 
 
 
 
2
  import gradio as gr
3
  import torch
4
  from PIL import Image
 
10
 
11
  start_time = time.time()
12
  current_steps = 25
13
+
14
+ PIPE = DiffusionPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
17
 
 
47
  generator = torch.Generator("cuda").manual_seed(seed)
48
 
49
  try:
50
+ return (
51
+ img_to_img(
52
+ model_name,
53
+ prompt,
54
+ n_images,
55
+ neg_prompt,
56
+ img,
57
+ strength,
58
+ guidance,
59
+ steps,
60
+ width,
61
+ height,
62
+ generator,
63
+ seed,
64
+ ),
65
+ f"Done. Seed: {seed}",
66
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  except Exception as e:
68
  return None, error_str(e)
69
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  def img_to_img(
72
  model_name,
73
  prompt,
 
82
  generator,
83
  seed,
84
  ):
85
+ pipe = PIPE
86
 
87
  if torch.cuda.is_available():
88
  pipe = pipe.to("cuda")
 
102
  generator=generator,
103
  )
104
 
105
+ # return replace_nsfw_images(result)
106
+ return result.images
 
 
107
 
108
 
109
  def replace_nsfw_images(results):
 
132
  """
133
  )
134
  with gr.Row():
 
135
  with gr.Column(scale=55):
136
  with gr.Group():
137
  model_name = gr.Dropdown(
 
140
  value=models[0].name,
141
  )
142
  with gr.Box(visible=False) as custom_model_group:
 
 
 
 
 
143
  gr.HTML(
144
  "<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>"
145
  )
 
189
  step=1,
190
  )
191
 
 
 
 
 
 
 
 
 
192
  seed = gr.Slider(
193
  0, 2147483647, label="Seed (0 = random)", value=0, step=1
194
  )
195
 
 
196
  with gr.Group():
197
  image = gr.Image(
198
  label="Image", height=256, tool="editor", type="pil"
 
223
  generate.click(inference, inputs=inputs, outputs=outputs)
224
 
225
  ex = gr.Examples(
226
+ [],
 
 
 
 
 
 
 
 
 
227
  inputs=[model_name, prompt, guidance, steps, neg_prompt],
228
  outputs=outputs,
229
  fn=inference,
230
+ cache_examples=True,
231
  )
232
 
233
  gr.HTML(