Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import math
|
| 3 |
+
import random
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline, EDMEulerScheduler, AutoencoderKL, DPMSolverMultistepScheduler
|
| 9 |
+
from huggingface_hub import hf_hub_download, InferenceClient
|
| 10 |
+
|
| 11 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 12 |
+
pipe_edit = StableDiffusionXLImg2ImgPipeline.from_single_file(
|
| 13 |
+
hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors"),
|
| 14 |
+
num_in_channels=8,
|
| 15 |
+
is_cosxl_edit=True,
|
| 16 |
+
vae=vae,
|
| 17 |
+
torch_dtype=torch.float16,
|
| 18 |
+
)
|
| 19 |
+
pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
|
| 20 |
+
pipe_edit.to("cuda")
|
| 21 |
+
|
| 22 |
+
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
| 23 |
+
refiner.to("cuda")
|
| 24 |
+
|
| 25 |
+
def set_timesteps_patched(self, num_inference_steps: int, device=None):
|
| 26 |
+
self.num_inference_steps = num_inference_steps
|
| 27 |
+
|
| 28 |
+
ramp = np.linspace(0, 1, self.num_inference_steps)
|
| 29 |
+
sigmas = torch.linspace(math.log(self.config.sigma_min), math.log(self.config.sigma_max), len(ramp)).exp().flip(0)
|
| 30 |
+
|
| 31 |
+
sigmas = (sigmas).to(dtype=torch.float32, device=device)
|
| 32 |
+
self.timesteps = self.precondition_noise(sigmas)
|
| 33 |
+
|
| 34 |
+
self.sigmas = torch.cat([sigmas, torch.zeros(1, device=sigmas.device)])
|
| 35 |
+
self._step_index = None
|
| 36 |
+
self._begin_index = None
|
| 37 |
+
self.sigmas = self.sigmas.to("cpu")
|
| 38 |
+
|
| 39 |
+
EDMEulerScheduler.set_timesteps = set_timesteps_patched
|
| 40 |
+
|
| 41 |
+
def king(
|
| 42 |
+
input_image,
|
| 43 |
+
instruction: str,
|
| 44 |
+
negative_prompt: str = "",
|
| 45 |
+
steps: int = 25,
|
| 46 |
+
randomize_seed: bool = True,
|
| 47 |
+
seed: int = 2404,
|
| 48 |
+
guidance_scale: float = 6,
|
| 49 |
+
progress=gr.Progress(track_tqdm=True)
|
| 50 |
+
):
|
| 51 |
+
input_image = Image.open(input_image).convert('RGB')
|
| 52 |
+
if randomize_seed:
|
| 53 |
+
seed = random.randint(0, 999999)
|
| 54 |
+
generator = torch.manual_seed(seed)
|
| 55 |
+
output_image = pipe_edit(
|
| 56 |
+
instruction,
|
| 57 |
+
negative_prompt=negative_prompt,
|
| 58 |
+
image=input_image,
|
| 59 |
+
guidance_scale=guidance_scale,
|
| 60 |
+
image_guidance_scale=1.5,
|
| 61 |
+
width=input_image.width,
|
| 62 |
+
height=input_image.height,
|
| 63 |
+
num_inference_steps=steps,
|
| 64 |
+
generator=generator,
|
| 65 |
+
output_type="latent",
|
| 66 |
+
).images
|
| 67 |
+
refine = refiner(
|
| 68 |
+
prompt=f"{instruction}, 4k, hd, high quality, masterpiece",
|
| 69 |
+
negative_prompt=negative_prompt,
|
| 70 |
+
guidance_scale=7.5,
|
| 71 |
+
num_inference_steps=steps,
|
| 72 |
+
image=output_image,
|
| 73 |
+
generator=generator,
|
| 74 |
+
).images[0]
|
| 75 |
+
return seed, refine
|
| 76 |
+
|
| 77 |
+
css = '''
|
| 78 |
+
.gradio-container{max-width: 700px !important}
|
| 79 |
+
h1{text-align:center}
|
| 80 |
+
footer {
|
| 81 |
+
visibility: hidden
|
| 82 |
+
}
|
| 83 |
+
'''
|
| 84 |
+
|
| 85 |
+
examples = [
|
| 86 |
+
[
|
| 87 |
+
"./supercar.png",
|
| 88 |
+
"make it red",
|
| 89 |
+
],
|
| 90 |
+
[
|
| 91 |
+
"./red_car.png",
|
| 92 |
+
"add some snow",
|
| 93 |
+
],
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(css=css) as demo:
|
| 97 |
+
gr.Markdown("# Image Editing\n### Note: First image generation takes time")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
instruction = gr.Textbox(lines=1, label="Instruction", interactive=True)
|
| 100 |
+
generate_button = gr.Button("Run", scale=0)
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
input_image = gr.Image(label="Image", type='filepath', interactive=True)
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
guidance_scale = gr.Number(value=6.0, step=0.1, label="Guidance Scale", interactive=True)
|
| 107 |
+
steps = gr.Number(value=25, step=1, label="Steps", interactive=True)
|
| 108 |
+
|
| 109 |
+
with gr.Accordion("Advanced options", open=False):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
negative_prompt = gr.Text(
|
| 112 |
+
label="Negative prompt",
|
| 113 |
+
max_lines=1,
|
| 114 |
+
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, ugly, disgusting, blurry, amputation,(face asymmetry, eyes asymmetry, deformed eyes, open mouth)",
|
| 115 |
+
visible=True
|
| 116 |
+
)
|
| 117 |
+
with gr.Row():
|
| 118 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True, interactive=True)
|
| 119 |
+
seed = gr.Number(value=2404, step=1, label="Seed", interactive=True)
|
| 120 |
+
|
| 121 |
+
gr.Examples(
|
| 122 |
+
examples=examples,
|
| 123 |
+
inputs=[input_image, instruction],
|
| 124 |
+
outputs=[input_image],
|
| 125 |
+
cache_examples=False,
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
generate_button.click(
|
| 129 |
+
king,
|
| 130 |
+
inputs=[input_image, instruction, negative_prompt, steps, randomize_seed, seed, guidance_scale],
|
| 131 |
+
outputs=[seed, input_image],
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
demo.queue(max_size=500).launch()
|