ehristoforu commited on
Commit
54b21f3
β€’
1 Parent(s): fe3a594

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -4
app.py CHANGED
@@ -9,10 +9,11 @@ import numpy as np
9
  from PIL import Image
10
  import spaces
11
  import torch
12
- from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
 
13
 
14
  DESCRIPTION = """
15
- # [**Fluently** Playground](https://huggingface.co/fluently)
16
 
17
  """
18
  if not torch.cuda.is_available():
@@ -51,6 +52,14 @@ if torch.cuda.is_available():
51
  pipe_epic.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_epic.scheduler.config)
52
  pipe_epic.to(device)
53
 
 
 
 
 
 
 
 
 
54
  pipe_xl = StableDiffusionXLPipeline.from_pretrained(
55
  "fluently/Fluently-XL-v2",
56
  torch_dtype=torch.float16,
@@ -74,10 +83,20 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
74
  seed = random.randint(0, MAX_SEED)
75
  return seed
76
 
 
 
 
 
 
 
77
 
78
  @spaces.GPU(enable_queue=True)
79
  def generate(
80
  model,
 
 
 
 
81
  prompt: str,
82
  negative_prompt: str = "",
83
  use_negative_prompt: bool = False,
@@ -127,7 +146,7 @@ def generate(
127
  num_images_per_prompt=1,
128
  output_type="pil",
129
  ).images
130
- else:
131
  images = pipe_xl(
132
  prompt=prompt,
133
  negative_prompt=negative_prompt,
@@ -138,11 +157,28 @@ def generate(
138
  num_images_per_prompt=1,
139
  output_type="pil",
140
  ).images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  image_paths = [save_image(img) for img in images]
143
  print(image_paths)
144
  return image_paths, seed
145
 
 
 
146
 
147
 
148
 
@@ -173,10 +209,20 @@ with gr.Blocks(title="Fluently Playground", css=css) as demo:
173
  with gr.Row():
174
  model = gr.Radio(
175
  label="Model",
176
- choices=["Fluently XL v2", "Fluently v3.5", "Fluently Anime", "Fluently Epic"],
177
  value="Fluently v3.5",
178
  interactive=True,
179
  )
 
 
 
 
 
 
 
 
 
 
180
  with gr.Group():
181
  with gr.Row():
182
  prompt = gr.Text(
@@ -244,6 +290,13 @@ with gr.Blocks(title="Fluently Playground", css=css) as demo:
244
  outputs=negative_prompt,
245
  api_name=False,
246
  )
 
 
 
 
 
 
 
247
 
248
 
249
  gr.on(
@@ -255,6 +308,10 @@ with gr.Blocks(title="Fluently Playground", css=css) as demo:
255
  fn=generate,
256
  inputs=[
257
  model,
 
 
 
 
258
  prompt,
259
  negative_prompt,
260
  use_negative_prompt,
 
9
  from PIL import Image
10
  import spaces
11
  import torch
12
+ from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
13
+ from diffusers.utils import load_image
14
 
15
  DESCRIPTION = """
16
+ # [Fluently Playground](https://huggingface.co/fluently)
17
 
18
  """
19
  if not torch.cuda.is_available():
 
52
  pipe_epic.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_epic.scheduler.config)
53
  pipe_epic.to(device)
54
 
55
+ pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
56
+ "fluently/Fluently-v3-inpainting",
57
+ torch_dtype=torch.float16,
58
+ use_safetensors=True,
59
+ )
60
+ #pipe_inpaint.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_inpaint.scheduler.config)
61
+ pipe_inpaint.to(device)
62
+
63
  pipe_xl = StableDiffusionXLPipeline.from_pretrained(
64
  "fluently/Fluently-XL-v2",
65
  torch_dtype=torch.float16,
 
83
  seed = random.randint(0, MAX_SEED)
84
  return seed
85
 
86
+ def get_model(model):
87
+ if model == "Fluently v3 inpaint":
88
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
89
+ else:
90
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
91
+
92
 
93
  @spaces.GPU(enable_queue=True)
94
  def generate(
95
  model,
96
+ inpaint_image,
97
+ mask_image,
98
+ blur_factor,
99
+ strength,
100
  prompt: str,
101
  negative_prompt: str = "",
102
  use_negative_prompt: bool = False,
 
146
  num_images_per_prompt=1,
147
  output_type="pil",
148
  ).images
149
+ elif model == "Fluently XL v2":
150
  images = pipe_xl(
151
  prompt=prompt,
152
  negative_prompt=negative_prompt,
 
157
  num_images_per_prompt=1,
158
  output_type="pil",
159
  ).images
160
+ else:
161
+ blurred_mask = pipe_inpaint.mask_processor.blur(mask_image, blur_factor=blur_factor)
162
+ images = pipe_inpaint(
163
+ prompt=prompt,
164
+ image=inpaint_image,
165
+ mask_image=blurred_mask,
166
+ negative_prompt=negative_prompt,
167
+ width=width,
168
+ height=height,
169
+ guidance_scale=guidance_scale,
170
+ num_inference_steps=30,
171
+ strength=strength,
172
+ num_images_per_prompt=1,
173
+ output_type="pil",
174
+ ).images
175
 
176
  image_paths = [save_image(img) for img in images]
177
  print(image_paths)
178
  return image_paths, seed
179
 
180
+
181
+
182
 
183
 
184
 
 
209
  with gr.Row():
210
  model = gr.Radio(
211
  label="Model",
212
+ choices=["Fluently XL v2", "Fluently v3.5", "Fluently Anime", "Fluently Epic", "Fluently v3 inpaint"],
213
  value="Fluently v3.5",
214
  interactive=True,
215
  )
216
+
217
+ md_mask = gr.Markdown("""
218
+ ⚠️ To generate an inpaint mask, go [here](https://huggingface.co/spaces/stevhliu/inpaint-mask-maker).
219
+ """, visible=False)
220
+ inpaint_image = gr.Image(label="Inpaint Image", interactive=True, scale=5, visible=False, type="pil")
221
+ mask_image = gr.Image(label="Mask Image", interactive=True, scale=5, visible=False, type="pil")
222
+
223
+ blur_factor = gr.Slider(label="Mask Blur Factor", minimum=0, maximum=100, value=4, step=1, interactive=True, visible=False)
224
+ strength = gr.Slider(label="Denoising Strength", minimum=0.00, maximum=1.00, value=0.70, step=0.01, interactive=True, visible=False)
225
+
226
  with gr.Group():
227
  with gr.Row():
228
  prompt = gr.Text(
 
290
  outputs=negative_prompt,
291
  api_name=False,
292
  )
293
+
294
+ model.change(
295
+ fn=get_model,
296
+ inputs=model,
297
+ outputs=[md_mask, inpaint_image, mask_image, blur_factor, strength],
298
+ api_name=False,
299
+ )
300
 
301
 
302
  gr.on(
 
308
  fn=generate,
309
  inputs=[
310
  model,
311
+ inpaint_image,
312
+ mask_image,
313
+ blur_factor,
314
+ strength,
315
  prompt,
316
  negative_prompt,
317
  use_negative_prompt,