craftgamesnetwork commited on
Commit
2c34318
·
verified ·
1 Parent(s): d5cdaf8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -9
app.py CHANGED
@@ -12,7 +12,7 @@ import spaces
12
  import torch
13
  from PIL import Image
14
  from io import BytesIO
15
- from diffusers import AutoencoderKL, DiffusionPipeline, AutoPipelineForImage2Image
16
 
17
  DESCRIPTION = "# Run any LoRA or SD Model"
18
  if not torch.cuda.is_available():
@@ -25,7 +25,8 @@ USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
25
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
26
  ENABLE_USE_LORA = os.getenv("ENABLE_USE_LORA", "1") == "1"
27
  ENABLE_USE_VAE = os.getenv("ENABLE_USE_VAE", "1") == "1"
28
- ENABLE_USE_IMG2IMG = os.getenv("ENABLE_USE_VAE", "1") == "1"
 
29
 
30
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
31
 
@@ -57,7 +58,10 @@ def generate(
57
  lora = '',
58
  lora_scale: float = 0.7,
59
  use_img2img: bool = False,
 
60
  url = '',
 
 
61
  ):
62
  if torch.cuda.is_available():
63
 
@@ -74,11 +78,17 @@ def generate(
74
  if use_vae:
75
  vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
76
  pipe = AutoPipelineForImage2Image.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
77
-
 
 
 
78
  response = requests.get(url)
79
  init_image = Image.open(BytesIO(response.content)).convert("RGB")
80
  init_image = init_image.resize((width, height))
81
 
 
 
 
82
  if use_lora:
83
  pipe.load_lora_weights(lora)
84
  pipe.fuse_lora(lora_scale)
@@ -101,20 +111,21 @@ def generate(
101
  if not use_negative_prompt_2:
102
  negative_prompt_2 = None # type: ignore
103
 
104
- if not use_img2img:
105
- return pipe(
106
  prompt=prompt,
 
 
 
107
  negative_prompt=negative_prompt,
108
  prompt_2=prompt_2,
109
  negative_prompt_2=negative_prompt_2,
110
- width=width,
111
- height=height,
112
  guidance_scale=guidance_scale_base,
113
  num_inference_steps=num_inference_steps_base,
114
  generator=generator,
115
- output_type="pil",
116
  ).images[0]
117
- else:
 
118
  images = pipe(
119
  prompt=prompt,
120
  image=init_image,
@@ -130,6 +141,19 @@ def generate(
130
  output_type="pil",
131
  ).images[0]
132
  return images
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  examples = [
135
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
@@ -166,6 +190,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css="style.css") as demo:
166
  result = gr.Image(label="Result", show_label=False)
167
  with gr.Accordion("Advanced options", open=False):
168
  with gr.Row():
 
169
  use_img2img = gr.Checkbox(label='Use Img2Img', value=False, visible=ENABLE_USE_IMG2IMG)
170
  use_vae = gr.Checkbox(label='Use VAE', value=False, visible=ENABLE_USE_VAE)
171
  use_lora = gr.Checkbox(label='Use Lora', value=False, visible=ENABLE_USE_LORA)
@@ -293,6 +318,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css="style.css") as demo:
293
  queue=False,
294
  api_name=False,
295
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
 
297
  gr.on(
298
  triggers=[
@@ -330,6 +369,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css="style.css") as demo:
330
  lora,
331
  lora_scale,
332
  use_img2img,
 
333
  url,
334
  ],
335
  outputs=result,
 
12
  import torch
13
  from PIL import Image
14
  from io import BytesIO
15
+ from diffusers import AutoencoderKL, DiffusionPipeline, AutoPipelineForImage2Image, AutoPipelineForInpainting
16
 
17
  DESCRIPTION = "# Run any LoRA or SD Model"
18
  if not torch.cuda.is_available():
 
25
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
26
  ENABLE_USE_LORA = os.getenv("ENABLE_USE_LORA", "1") == "1"
27
  ENABLE_USE_VAE = os.getenv("ENABLE_USE_VAE", "1") == "1"
28
+ ENABLE_USE_IMG2IMG = os.getenv("ENABLE_USE_IMG2IMG", "1") == "1"
29
+ ENABLE_USE_INPAINTING = os.getenv("ENABLE_USE_INPAINTING", "1") == "1"
30
 
31
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
32
 
 
58
  lora = '',
59
  lora_scale: float = 0.7,
60
  use_img2img: bool = False,
61
+ use_inpainting: bool = False,
62
  url = '',
63
+ img_url = '',
64
+ mask_url = '',
65
  ):
66
  if torch.cuda.is_available():
67
 
 
78
  if use_vae:
79
  vae = AutoencoderKL.from_pretrained(vaecall, torch_dtype=torch.float16)
80
  pipe = AutoPipelineForImage2Image.from_pretrained(model, vae=vae, torch_dtype=torch.float16)
81
+
82
+ if use_inpainting:
83
+ pipe = AutoPipelineForInpainting.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16)
84
+
85
  response = requests.get(url)
86
  init_image = Image.open(BytesIO(response.content)).convert("RGB")
87
  init_image = init_image.resize((width, height))
88
 
89
+ img_url = download_image(img_url)
90
+ mask_image = download_image(mask_url)
91
+
92
  if use_lora:
93
  pipe.load_lora_weights(lora)
94
  pipe.fuse_lora(lora_scale)
 
111
  if not use_negative_prompt_2:
112
  negative_prompt_2 = None # type: ignore
113
 
114
+ if use_inpainting:
115
+ images = pipe(
116
  prompt=prompt,
117
+ image=img_url,
118
+ mask_image=mask_url,
119
+ strength=strength_img2img,
120
  negative_prompt=negative_prompt,
121
  prompt_2=prompt_2,
122
  negative_prompt_2=negative_prompt_2,
 
 
123
  guidance_scale=guidance_scale_base,
124
  num_inference_steps=num_inference_steps_base,
125
  generator=generator,
 
126
  ).images[0]
127
+ return images
128
+ if use_img2img:
129
  images = pipe(
130
  prompt=prompt,
131
  image=init_image,
 
141
  output_type="pil",
142
  ).images[0]
143
  return images
144
+ else:
145
+ return pipe(
146
+ prompt=prompt,
147
+ negative_prompt=negative_prompt,
148
+ prompt_2=prompt_2,
149
+ negative_prompt_2=negative_prompt_2,
150
+ width=width,
151
+ height=height,
152
+ guidance_scale=guidance_scale_base,
153
+ num_inference_steps=num_inference_steps_base,
154
+ generator=generator,
155
+ output_type="pil",
156
+ ).images[0]
157
 
158
  examples = [
159
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
190
  result = gr.Image(label="Result", show_label=False)
191
  with gr.Accordion("Advanced options", open=False):
192
  with gr.Row():
193
+ use_inpainting = gr.Checkbox(label='Use Inpainting', value=False, visible=ENABLE_USE_INPAINTING)
194
  use_img2img = gr.Checkbox(label='Use Img2Img', value=False, visible=ENABLE_USE_IMG2IMG)
195
  use_vae = gr.Checkbox(label='Use VAE', value=False, visible=ENABLE_USE_VAE)
196
  use_lora = gr.Checkbox(label='Use Lora', value=False, visible=ENABLE_USE_LORA)
 
318
  queue=False,
319
  api_name=False,
320
  )
321
+ use_inpainting.change(
322
+ fn=lambda x: gr.update(visible=x),
323
+ inputs=use_inpainting,
324
+ outputs=img_url,
325
+ queue=False,
326
+ api_name=False,
327
+ )
328
+ use_inpainting.change(
329
+ fn=lambda x: gr.update(visible=x),
330
+ inputs=use_inpainting,
331
+ outputs=mask_url,
332
+ queue=False,
333
+ api_name=False,
334
+ )
335
 
336
  gr.on(
337
  triggers=[
 
369
  lora,
370
  lora_scale,
371
  use_img2img,
372
+ use_inpainting
373
  url,
374
  ],
375
  outputs=result,