File size: 12,654 Bytes
0cc374b 0243379 0cc374b c70fac3 c698ffc 17da47c c70fac3 c698ffc 0cc374b 6a93fc7 c70fac3 bd9036b 0cc374b e3aef47 0cc374b c70fac3 0cc374b d6e3245 0cc374b 4a53f08 6a93fc7 0cc374b d6e3245 0cc374b 0243379 0cc374b d6e3245 0cc374b 4a53f08 6a93fc7 0cc374b d6e3245 0cc374b 4a53f08 6a93fc7 0cc374b 4a53f08 c70fac3 0cc374b c70fac3 0cc374b 4a53f08 0cc374b 4a53f08 0cc374b 4a53f08 0cc374b 4a53f08 0cc374b f172e70 0cc374b f172e70 0cc374b 4a0ddbb 0cc374b 4a0ddbb 0cc374b 4a0ddbb d74a8de 0cc374b 6a93fc7 0cc374b 19dae6b 0cc374b 0243379 c698ffc 0cc374b c70fac3 6a93fc7 0cc374b 17da47c 0cc374b 4a53f08 17da47c 4a53f08 c70fac3 0cc374b 6a93fc7 0cc374b 4a0ddbb 0cc374b c70fac3 d6e3245 0cc374b 4a53f08 6a93fc7 0cc374b d6e3245 0cc374b 4a53f08 6a93fc7 0cc374b e77bb90 c70fac3 0cc374b d6e3245 0cc374b 4a53f08 6a93fc7 0cc374b c70fac3 0cc374b 17da47c 0cc374b 4a53f08 6a93fc7 0cc374b c70fac3 0cc374b 17da47c 0cc374b 4a53f08 6a93fc7 0cc374b c70fac3 0cc374b 17da47c 0cc374b 4a53f08 6a93fc7 0cc374b d74a8de c70fac3 d74a8de 17da47c d74a8de 6a93fc7 d74a8de c70fac3 d74a8de 17da47c d74a8de f7b2641 6a93fc7 d74a8de 0cc374b 5bb1f93 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
import gradio as gr
import numpy as np
import time
import math
import random
import torch
import spaces
from diffusers import (
ControlNetModel,
StableDiffusionControlNetPipeline,
)
from PIL import Image
from pillow_heif import register_heif_opener
register_heif_opener()
max_64_bit_int = np.iinfo(np.int32).max
if torch.cuda.is_available():
device = "cuda"
floatType = torch.float16
else:
device = "cpu"
floatType = torch.float32
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11e_sd15_ip2p", torch_dtype = floatType)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"botp/stable-diffusion-v1-5", safety_checker = None, controlnet = controlnet, torch_dtype = floatType
)
pipe = pipe.to(device)
def update_seed(is_randomize_seed, seed):
if is_randomize_seed:
return random.randint(0, max_64_bit_int)
return seed
def check(
input_image,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
is_randomize_seed,
seed,
progress = gr.Progress()):
if input_image is None:
raise gr.Error("Please provide an image.")
if prompt is None or prompt == "":
raise gr.Error("Please provide a prompt input.")
@spaces.GPU(duration=420)
def pix2pix(
input_image,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
is_randomize_seed,
seed,
progress = gr.Progress()):
check(
input_image,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
is_randomize_seed,
seed
)
start = time.time()
progress(0, desc = "Preparing data...")
if negative_prompt is None:
negative_prompt = ""
if denoising_steps is None:
denoising_steps = 0
if num_inference_steps is None:
num_inference_steps = 20
if guidance_scale is None:
guidance_scale = 5
if image_guidance_scale is None:
image_guidance_scale = 1.5
if seed is None:
seed = random.randint(0, max_64_bit_int)
random.seed(seed)
torch.manual_seed(seed)
original_height, original_width, dummy_channel = np.array(input_image).shape
output_width = original_width
output_height = original_height
mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = "white")
limitation = "";
# Limited to 1 million pixels
if 1024 * 1024 < output_width * output_height:
factor = ((1024 * 1024) / (output_width * output_height))**0.5
output_width = math.floor(output_width * factor)
output_height = math.floor(output_height * factor)
limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
# Width and height must be multiple of 8
output_width = output_width - (output_width % 8)
output_height = output_height - (output_height % 8)
progress(None, desc = "Processing...")
output_image = pipe(
seeds=[seed],
width = output_width,
height = output_height,
prompt = prompt,
negative_prompt = negative_prompt,
image = input_image,
mask_image = mask_image,
num_inference_steps = num_inference_steps,
guidance_scale = guidance_scale,
image_guidance_scale = image_guidance_scale,
denoising_steps = denoising_steps,
show_progress_bar = True
).images[0]
if limitation != "":
output_image = output_image.resize((original_width, original_height))
end = time.time()
secondes = int(end - start)
minutes = math.floor(secondes / 60)
secondes = secondes - (minutes * 60)
hours = math.floor(minutes / 60)
minutes = minutes - (hours * 60)
return [
output_image,
("Start again to get a different result. " if is_randomize_seed else "") + "The image has been generated in " + ((str(hours) + " h, ") if hours != 0 else "") + ((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + str(secondes) + " sec." + limitation
]
with gr.Blocks() as interface:
gr.HTML(
"""
<h1 style="text-align: center;">Instruct Pix2Pix demo</h1>
<p style="text-align: center;">Modifies your image using a textual instruction, freely, without account, without watermark, without installation, which can be downloaded</p>
<br/>
<br/>
โจ Powered by <i>SD 1.5</i> and <i>ControlNet</i>. The result quality extremely varies depending on what we ask.
<br/>
<ul>
<li>To change the <b>view angle</b> of your image, I recommend to use <i>Zero123</i>,</li>
<li>To <b>upscale</b> your image, I recommend to use <i><a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/SUPIR">SUPIR</a></i>,</li>
<li>To <b>slightly change</b> your image, I recommend to use <i>Image-to-Image SDXL</i>,</li>
<li>To change <b>one detail</b> on your image, I recommend to use <i>Inpaint SDXL</i>,</li>
<li>To remove the <b>background</b> of your image, I recommend to use <i>BRIA</i>,</li>
<li>To enlarge the <b>viewpoint</b> of your image, I recommend to use <i>Uncrop</i>,</li>
<li>To make a <b>tile</b> of your image, I recommend to use <i>Make My Image Tile</i>,</li>
</ul>
<br/>
""" + ("๐โโ๏ธ Estimated time: few minutes." if torch.cuda.is_available() else "๐ Slow process... ~1 hour.") + """
Your computer must not enter into standby mode. You can launch several generations in different browser tabs when you're gone. If this space does not work or you want a faster run, use <i>Instruct Pix2Pix</i> available on hysts's <i>ControlNet-v1-1</i> space (last tab) or on <i>Dezgo</i> site.<br>You can duplicate this space on a free account, it's designed to work on CPU, GPU and ZeroGPU.<br/>
<a href='https://huggingface.co/spaces/Fabrice-TIERCELIN/Instruct-Pix2Pix?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14'></a>
<br/>
โ๏ธ You can use, modify and share the generated images but not for commercial uses.
"""
)
with gr.Column():
input_image = gr.Image(label = "Your image", sources = ["upload", "webcam", "clipboard"], type = "pil")
prompt = gr.Textbox(label = "Prompt", info = "Instruct what to change in the image", placeholder = "Order the AI what to change in the image", lines = 2)
with gr.Accordion("Advanced options", open = False):
negative_prompt = gr.Textbox(label = "Negative prompt", placeholder = "Describe what you do NOT want to see in the image", value = ''
'blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, '
'worst quality, low quality, frames, watermark, signature, jpeg artifacts, '
'deformed, lowres, over-smooth')
denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 0, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
num_inference_steps = gr.Slider(minimum = 10, maximum = 500, value = 20, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 5, step = 0.1, label = "Guidance Scale", info = "lower=image quality, higher=follow the prompt")
image_guidance_scale = gr.Slider(minimum = 1, value = 1.5, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed", value = True, info = "If checked, result is always different")
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed")
submit = gr.Button("๐ Modify", variant = "primary")
modified_image = gr.Image(label = "Modified image")
information = gr.HTML()
submit.click(fn = update_seed, inputs = [
randomize_seed,
seed
], outputs = [
seed
], queue = False, show_progress = False).then(check, inputs = [
input_image,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
], outputs = [], queue = False, show_progress = False).success(pix2pix, inputs = [
input_image,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
], outputs = [
modified_image,
information
], scroll_to_output = True)
gr.Examples(
run_on_click = True,
fn = pix2pix,
inputs = [
input_image,
prompt,
negative_prompt,
denoising_steps,
num_inference_steps,
guidance_scale,
image_guidance_scale,
randomize_seed,
seed
],
outputs = [
modified_image,
information
],
examples = [
[
"./Examples/Example1.webp",
"What if it's snowing?",
"blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, "
"worst quality, low quality, frames, watermark, signature, jpeg artifacts, "
"deformed, lowres, over-smooth",
1,
20,
5,
1.5,
False,
42
],
[
"./Examples/Example2.png",
"What if this woman had brown hair?",
"blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, "
"worst quality, low quality, frames, watermark, signature, jpeg artifacts, "
"deformed, lowres, over-smooth",
1,
20,
5,
1.5,
False,
42
],
[
"./Examples/Example3.jpeg",
"Replace the house by a windmill",
"blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, "
"worst quality, low quality, frames, watermark, signature, jpeg artifacts, "
"deformed, lowres, over-smooth",
1,
20,
5,
1.5,
False,
42
],
[
"./Examples/Example4.gif",
"What if the camera was in opposite side?",
"blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, "
"worst quality, low quality, frames, watermark, signature, jpeg artifacts, "
"deformed, lowres, over-smooth",
1,
20,
5,
1.5,
False,
42
],
[
"./Examples/Example5.bmp",
"Turn him into cyborg",
"blurring, aliasing, unsharp, weird textures, ugly, dirty, messy, "
"worst quality, low quality, frames, watermark, signature, jpeg artifacts, "
"deformed, lowres, over-smooth",
1,
20,
5,
25,
False,
42
],
],
cache_examples = False,
)
interface.queue().launch() |