anzorq commited on
Commit
f899110
1 Parent(s): 040beda

testing offloading with 2 models

Browse files
Files changed (1) hide show
  1. app.py +62 -55
app.py CHANGED
@@ -20,16 +20,16 @@ models = [
20
  Model("Custom model", "", ""),
21
  Model("Arcane", "nitrosocke/Arcane-Diffusion", "arcane style "),
22
  Model("Archer", "nitrosocke/archer-diffusion", "archer style "),
23
- Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
24
- Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
25
- Model("Modern Disney", "nitrosocke/modern-disney-diffusion", "modern disney style "),
26
- Model("Classic Disney", "nitrosocke/classic-anim-diffusion", ""),
27
- Model("Waifu", "hakurei/waifu-diffusion", ""),
28
- Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
29
- Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
30
- Model("Robo Diffusion", "nousr/robo-diffusion", ""),
31
- Model("Cyberpunk Anime", "DGSpitzer/Cyberpunk-Anime-Diffusion", "dgs illustration style "),
32
- Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy")
33
  ]
34
 
35
  last_mode = "txt2img"
@@ -43,9 +43,12 @@ if is_colab:
43
  else: # download all models
44
  vae = AutoencoderKL.from_pretrained(current_model.path, subfolder="vae", torch_dtype=torch.float16)
45
  for model in models[1:]:
46
- unet = UNet2DConditionModel.from_pretrained(model.path, subfolder="unet", torch_dtype=torch.float16)
47
- model.pipe_t2i = StableDiffusionPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
48
- model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
 
 
 
49
  pipe = models[1].pipe_t2i
50
 
51
  if torch.cuda.is_available():
@@ -81,10 +84,11 @@ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, g
81
  if model_path != current_model_path or last_mode != "txt2img":
82
  current_model_path = model_path
83
 
84
- if is_colab:
85
  pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
86
  else:
87
- pipe = pipe.to("cpu")
 
88
  pipe = current_model.pipe_t2i
89
 
90
  if torch.cuda.is_available():
@@ -112,14 +116,15 @@ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, w
112
  if model_path != current_model_path or last_mode != "img2img":
113
  current_model_path = model_path
114
 
115
- if is_colab:
116
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
117
  else:
118
- pipe = pipe.to("cpu")
 
119
  pipe = current_model.pipe_i2i
120
 
121
  if torch.cuda.is_available():
122
- pipe = pipe.to("cuda")
123
  last_mode = "img2img"
124
 
125
  prompt = current_model.prefix + prompt
@@ -190,47 +195,49 @@ with gr.Blocks(css=css) as demo:
190
  <a href="https://huggingface.co/nitrosocke/Arcane-Diffusion">Arcane</a>, <a href="https://huggingface.co/nitrosocke/archer-diffusion">Archer</a>, <a href="https://huggingface.co/nitrosocke/elden-ring-diffusion">Elden Ring</a>, <a href="https://huggingface.co/nitrosocke/spider-verse-diffusion">Spiderverse</a>, <a href="https://huggingface.co/nitrosocke/modern-disney-diffusion">Modern Disney</a>, <a href="https://huggingface.co/hakurei/waifu-diffusion">Waifu</a>, <a href="https://huggingface.co/lambdalabs/sd-pokemon-diffusers">Pokemon</a>, <a href="https://huggingface.co/yuk/fuyuko-waifu-diffusion">Fuyuko Waifu</a>, <a href="https://huggingface.co/AstraliteHeart/pony-diffusion">Pony</a>, <a href="https://huggingface.co/sd-dreambooth-library/herge-style">Hergé (Tintin)</a>, <a href="https://huggingface.co/nousr/robo-diffusion">Robo</a>, <a href="https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion">Cyberpunk Anime</a> + any other custom Diffusers 🧨 SD model hosted on HuggingFace 🤗.
191
  </p>
192
  <p>Don't want to wait in queue? <a href="https://colab.research.google.com/gist/qunash/42112fb104509c24fd3aa6d1c11dd6e0/copy-of-fine-tuned-diffusion-gradio.ipynb"><img data-canonical-src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667"></a></p>
193
- Running on <b>{device}</b>
194
  </p>
195
  </div>
196
  """
197
  )
198
  with gr.Row():
199
 
200
- with gr.Group():
201
- model_name = gr.Dropdown(label="Model", choices=[m.name for m in models], value=current_model.name)
202
- custom_model_path = gr.Textbox(label="Custom model path", placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion", visible=False, interactive=True)
203
-
204
- with gr.Row():
205
- prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="Enter prompt. Style applied automatically").style(container=False)
206
- generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
 
207
 
208
 
209
- image_out = gr.Image(height=512)
210
- # gallery = gr.Gallery(
211
- # label="Generated images", show_label=False, elem_id="gallery"
212
- # ).style(grid=[1], height="auto")
213
 
214
- with gr.Tab("Options"):
215
- with gr.Group():
216
- neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
 
217
 
218
- # n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
219
 
220
- with gr.Row():
221
- guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
222
- steps = gr.Slider(label="Steps", value=50, minimum=2, maximum=100, step=1)
223
 
224
- with gr.Row():
225
- width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
226
- height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
227
 
228
- seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
229
 
230
- with gr.Tab("Image to image"):
231
- with gr.Group():
232
- image = gr.Image(label="Image", height=256, tool="editor", type="pil")
233
- strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
234
 
235
  model_name.change(lambda x: gr.update(visible = x == models[0].name), inputs=model_name, outputs=custom_model_path)
236
  custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
@@ -240,13 +247,13 @@ with gr.Blocks(css=css) as demo:
240
  prompt.submit(inference, inputs=inputs, outputs=image_out)
241
  generate.click(inference, inputs=inputs, outputs=image_out)
242
 
243
- ex = gr.Examples([
244
- [models[1].name, "jason bateman disassembling the demon core", 7.5, 50],
245
- [models[4].name, "portrait of dwayne johnson", 7.0, 75],
246
- [models[5].name, "portrait of a beautiful alyx vance half life", 10, 50],
247
- [models[6].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0, 45],
248
- [models[5].name, "fantasy portrait painting, digital art", 4.0, 30],
249
- ], [model_name, prompt, guidance, steps, seed], image_out, inference, cache_examples=False)# not is_colab and torch.cuda.is_available())
250
  # ex.dataset.headers = [""]
251
 
252
  gr.Markdown('''
@@ -256,6 +263,6 @@ with gr.Blocks(css=css) as demo:
256
  ![visitors](https://visitor-badge.glitch.me/badge?page_id=anzorq.finetuned_diffusion)
257
  ''')
258
 
259
- #if not is_colab:
260
- demo.queue(concurrency_count=4)
261
- demo.launch()
 
20
  Model("Custom model", "", ""),
21
  Model("Arcane", "nitrosocke/Arcane-Diffusion", "arcane style "),
22
  Model("Archer", "nitrosocke/archer-diffusion", "archer style "),
23
+ # Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
24
+ # Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
25
+ # Model("Modern Disney", "nitrosocke/modern-disney-diffusion", "modern disney style "),
26
+ # Model("Classic Disney", "nitrosocke/classic-anim-diffusion", ""),
27
+ # Model("Waifu", "hakurei/waifu-diffusion", ""),
28
+ # Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
29
+ # Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
30
+ # Model("Robo Diffusion", "nousr/robo-diffusion", ""),
31
+ # Model("Cyberpunk Anime", "DGSpitzer/Cyberpunk-Anime-Diffusion", "dgs illustration style "),
32
+ # Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy")
33
  ]
34
 
35
  last_mode = "txt2img"
 
43
  else: # download all models
44
  vae = AutoencoderKL.from_pretrained(current_model.path, subfolder="vae", torch_dtype=torch.float16)
45
  for model in models[1:]:
46
+ try:
47
+ unet = UNet2DConditionModel.from_pretrained(model.path, subfolder="unet", torch_dtype=torch.float16)
48
+ model.pipe_t2i = StableDiffusionPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
49
+ model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
50
+ except:
51
+ models.remove(model)
52
  pipe = models[1].pipe_t2i
53
 
54
  if torch.cuda.is_available():
 
84
  if model_path != current_model_path or last_mode != "txt2img":
85
  current_model_path = model_path
86
 
87
+ if is_colab or current_model == models[0]:
88
  pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
89
  else:
90
+ # pipe = pipe.to("cpu")
91
+ pipe.to("cpu")
92
  pipe = current_model.pipe_t2i
93
 
94
  if torch.cuda.is_available():
 
116
  if model_path != current_model_path or last_mode != "img2img":
117
  current_model_path = model_path
118
 
119
+ if is_colab or current_model == models[0]:
120
  pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
121
  else:
122
+ # pipe = pipe.to("cpu")
123
+ pipe.to("cpu")
124
  pipe = current_model.pipe_i2i
125
 
126
  if torch.cuda.is_available():
127
+ pipe = pipe.to("cuda")
128
  last_mode = "img2img"
129
 
130
  prompt = current_model.prefix + prompt
 
195
  <a href="https://huggingface.co/nitrosocke/Arcane-Diffusion">Arcane</a>, <a href="https://huggingface.co/nitrosocke/archer-diffusion">Archer</a>, <a href="https://huggingface.co/nitrosocke/elden-ring-diffusion">Elden Ring</a>, <a href="https://huggingface.co/nitrosocke/spider-verse-diffusion">Spiderverse</a>, <a href="https://huggingface.co/nitrosocke/modern-disney-diffusion">Modern Disney</a>, <a href="https://huggingface.co/hakurei/waifu-diffusion">Waifu</a>, <a href="https://huggingface.co/lambdalabs/sd-pokemon-diffusers">Pokemon</a>, <a href="https://huggingface.co/yuk/fuyuko-waifu-diffusion">Fuyuko Waifu</a>, <a href="https://huggingface.co/AstraliteHeart/pony-diffusion">Pony</a>, <a href="https://huggingface.co/sd-dreambooth-library/herge-style">Hergé (Tintin)</a>, <a href="https://huggingface.co/nousr/robo-diffusion">Robo</a>, <a href="https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion">Cyberpunk Anime</a> + any other custom Diffusers 🧨 SD model hosted on HuggingFace 🤗.
196
  </p>
197
  <p>Don't want to wait in queue? <a href="https://colab.research.google.com/gist/qunash/42112fb104509c24fd3aa6d1c11dd6e0/copy-of-fine-tuned-diffusion-gradio.ipynb"><img data-canonical-src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667"></a></p>
198
+ Running on <b>{device}</b>{(" in a <b>Google Colab</b>." if is_colab else "")}
199
  </p>
200
  </div>
201
  """
202
  )
203
  with gr.Row():
204
 
205
+ with gr.Column(scale=55):
206
+ with gr.Group():
207
+ model_name = gr.Dropdown(label="Model", choices=[m.name for m in models], value=current_model.name)
208
+ custom_model_path = gr.Textbox(label="Custom model path", placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion", visible=False, interactive=True)
209
+
210
+ with gr.Row():
211
+ prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="Enter prompt. Style applied automatically").style(container=False)
212
+ generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
213
 
214
 
215
+ image_out = gr.Image(height=512)
216
+ # gallery = gr.Gallery(
217
+ # label="Generated images", show_label=False, elem_id="gallery"
218
+ # ).style(grid=[1], height="auto")
219
 
220
+ with gr.Column(scale=45):
221
+ with gr.Tab("Options"):
222
+ with gr.Group():
223
+ neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
224
 
225
+ # n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
226
 
227
+ with gr.Row():
228
+ guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
229
+ steps = gr.Slider(label="Steps", value=50, minimum=2, maximum=100, step=1)
230
 
231
+ with gr.Row():
232
+ width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
233
+ height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
234
 
235
+ seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
236
 
237
+ with gr.Tab("Image to image"):
238
+ with gr.Group():
239
+ image = gr.Image(label="Image", height=256, tool="editor", type="pil")
240
+ strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
241
 
242
  model_name.change(lambda x: gr.update(visible = x == models[0].name), inputs=model_name, outputs=custom_model_path)
243
  custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
 
247
  prompt.submit(inference, inputs=inputs, outputs=image_out)
248
  generate.click(inference, inputs=inputs, outputs=image_out)
249
 
250
+ # ex = gr.Examples([
251
+ # [models[1].name, "jason bateman disassembling the demon core", 7.5, 50],
252
+ # [models[4].name, "portrait of dwayne johnson", 7.0, 75],
253
+ # [models[5].name, "portrait of a beautiful alyx vance half life", 10, 50],
254
+ # [models[6].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0, 45],
255
+ # [models[5].name, "fantasy portrait painting, digital art", 4.0, 30],
256
+ # ], [model_name, prompt, guidance, steps, seed], image_out, inference, cache_examples=False)#not is_colab and torch.cuda.is_available())
257
  # ex.dataset.headers = [""]
258
 
259
  gr.Markdown('''
 
263
  ![visitors](https://visitor-badge.glitch.me/badge?page_id=anzorq.finetuned_diffusion)
264
  ''')
265
 
266
+ if not is_colab:
267
+ demo.queue(concurrency_count=4)
268
+ demo.launch(debug=True, share=True)