Commit
·
c4e3a54
1
Parent(s):
c644570
Better
Browse files- __pycache__/app.cpython-310.pyc +0 -0
- app.py +19 -5
__pycache__/app.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -2,9 +2,10 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
|
| 4 |
from diffusers import AutoPipelineForInpainting, UNet2DConditionModel
|
|
|
|
| 5 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 6 |
|
| 7 |
-
unet = UNet2DConditionModel.from_pretrained("valhalla/sdxl-inpaint-ema")
|
| 8 |
pipe = AutoPipelineForInpainting.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
| 9 |
|
| 10 |
def read_content(file_path: str) -> str:
|
|
@@ -15,12 +16,23 @@ def read_content(file_path: str) -> str:
|
|
| 15 |
|
| 16 |
return content
|
| 17 |
|
| 18 |
-
def predict(dict, prompt="", guidance_scale=7.5, steps=20, strength=1.0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
init_image = dict["image"].convert("RGB").resize((1024, 1024))
|
| 21 |
mask = dict["mask"].convert("RGB").resize((1024, 1024))
|
| 22 |
|
| 23 |
-
output = pipe(prompt = prompt, image=init_image, mask_image=mask, guidance_scale=guidance_scale,
|
| 24 |
|
| 25 |
return output.images[0], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 26 |
|
|
@@ -79,6 +91,8 @@ with image_blocks as demo:
|
|
| 79 |
guidance_scale = gr.Number(value=7.5, minimum=1.0, maximum=20.0, step=0.1, label="guidance_scale")
|
| 80 |
steps = gr.Number(value=20, minimum=10, maximum=50, step=0.1, label="steps")
|
| 81 |
strength = gr.Number(value=1.0, minimum=0.0, maximum=1.0, step=0.05, label="strength")
|
|
|
|
|
|
|
| 82 |
|
| 83 |
btn = gr.Button("Inpaint!").style(
|
| 84 |
margin=False,
|
|
@@ -93,13 +107,13 @@ with image_blocks as demo:
|
|
| 93 |
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
| 94 |
|
| 95 |
|
| 96 |
-
btn.click(fn=predict, inputs=[image, prompt, guidance_scale, steps, strength], outputs=[image_out, community_icon, loading_icon, share_button])
|
| 97 |
share_button.click(None, [], [], _js=share_js)
|
| 98 |
|
| 99 |
gr.HTML(
|
| 100 |
"""
|
| 101 |
<div class="footer">
|
| 102 |
-
<p>Model by <a href="https://huggingface.co/diffusers" style="text-decoration: underline;" target="_blank">
|
| 103 |
</p>
|
| 104 |
</div>
|
| 105 |
"""
|
|
|
|
| 2 |
import torch
|
| 3 |
|
| 4 |
from diffusers import AutoPipelineForInpainting, UNet2DConditionModel
|
| 5 |
+
import diffusers
|
| 6 |
from share_btn import community_icon_html, loading_icon_html, share_js
|
| 7 |
|
| 8 |
+
unet = UNet2DConditionModel.from_pretrained("valhalla/sdxl-inpaint-ema", torch_dtype=torch.float16)
|
| 9 |
pipe = AutoPipelineForInpainting.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
| 10 |
|
| 11 |
def read_content(file_path: str) -> str:
|
|
|
|
| 16 |
|
| 17 |
return content
|
| 18 |
|
| 19 |
+
def predict(dict, prompt="", guidance_scale=7.5, steps=20, strength=1.0, scheduler="EulerDiscreteScheduler"):
|
| 20 |
+
|
| 21 |
+
scheduler_class_name = scheduler.split("")[0]
|
| 22 |
+
scheduler = getattr(diffusers, scheduler_class_name)
|
| 23 |
+
|
| 24 |
+
add_kwargs = {}
|
| 25 |
+
if len(scheduler.split("")) > 1:
|
| 26 |
+
add_kwargs["use_karras"] = True
|
| 27 |
+
if len(scheduler.split("")) > 2:
|
| 28 |
+
add_kwargs["algorithm_type"] = "sde-dpmsolver++"
|
| 29 |
+
|
| 30 |
+
pipe.scheduler = scheduler.from_config(pipe.scheduler.config, **add_kwargs)
|
| 31 |
|
| 32 |
init_image = dict["image"].convert("RGB").resize((1024, 1024))
|
| 33 |
mask = dict["mask"].convert("RGB").resize((1024, 1024))
|
| 34 |
|
| 35 |
+
output = pipe(prompt = prompt, image=init_image, mask_image=mask, guidance_scale=guidance_scale, num_inference_steps=int(steps), strength=strength)
|
| 36 |
|
| 37 |
return output.images[0], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 38 |
|
|
|
|
| 91 |
guidance_scale = gr.Number(value=7.5, minimum=1.0, maximum=20.0, step=0.1, label="guidance_scale")
|
| 92 |
steps = gr.Number(value=20, minimum=10, maximum=50, step=0.1, label="steps")
|
| 93 |
strength = gr.Number(value=1.0, minimum=0.0, maximum=1.0, step=0.05, label="strength")
|
| 94 |
+
schedulers = ["DEISMultistepScheduler", "HeunDiscreteScheduler", "EulerDiscreteScheduler", "DPMSolverMultistepScheduler", "DPMSolverMultistepScheduler Karras", "DPMSolverMultistepScheduler Karras SDE"]
|
| 95 |
+
scheduler = gr.Dropdown(choices=schedulers, value="EulerDiscreteScheduler")
|
| 96 |
|
| 97 |
btn = gr.Button("Inpaint!").style(
|
| 98 |
margin=False,
|
|
|
|
| 107 |
share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
| 108 |
|
| 109 |
|
| 110 |
+
btn.click(fn=predict, inputs=[image, prompt, guidance_scale, steps, strength, scheduler], outputs=[image_out, community_icon, loading_icon, share_button])
|
| 111 |
share_button.click(None, [], [], _js=share_js)
|
| 112 |
|
| 113 |
gr.HTML(
|
| 114 |
"""
|
| 115 |
<div class="footer">
|
| 116 |
+
<p>Model by <a href="https://huggingface.co/diffusers" style="text-decoration: underline;" target="_blank">Diffusers</a> - Gradio Demo by 🤗 Hugging Face
|
| 117 |
</p>
|
| 118 |
</div>
|
| 119 |
"""
|