Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,13 @@ import gradio as gr
|
|
| 5 |
import numpy as np
|
| 6 |
import torch
|
| 7 |
from PIL import Image
|
| 8 |
-
from diffusers import StableDiffusionXLImg2ImgPipeline, EDMEulerScheduler, AutoencoderKL
|
| 9 |
-
from huggingface_hub import hf_hub_download
|
| 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,
|
|
@@ -16,21 +19,28 @@ pipe_edit = StableDiffusionXLImg2ImgPipeline.from_single_file(
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -38,16 +48,8 @@ def set_timesteps_patched(self, num_inference_steps: int, device=None):
|
|
| 38 |
|
| 39 |
EDMEulerScheduler.set_timesteps = set_timesteps_patched
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 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)
|
|
@@ -74,6 +76,7 @@ def king(
|
|
| 74 |
).images[0]
|
| 75 |
return seed, refine
|
| 76 |
|
|
|
|
| 77 |
css = '''
|
| 78 |
.gradio-container{max-width: 700px !important}
|
| 79 |
h1{text-align:center}
|
|
@@ -82,17 +85,13 @@ footer {
|
|
| 82 |
}
|
| 83 |
'''
|
| 84 |
|
|
|
|
| 85 |
examples = [
|
| 86 |
-
[
|
| 87 |
-
|
| 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():
|
|
@@ -131,4 +130,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 131 |
outputs=[seed, input_image],
|
| 132 |
)
|
| 133 |
|
| 134 |
-
demo.queue(max_size=500).launch()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import torch
|
| 7 |
from PIL import Image
|
| 8 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline, EDMEulerScheduler, AutoencoderKL
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
|
| 11 |
+
# Load the VAE
|
| 12 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 13 |
+
|
| 14 |
+
# Download and load the model
|
| 15 |
pipe_edit = StableDiffusionXLImg2ImgPipeline.from_single_file(
|
| 16 |
hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors"),
|
| 17 |
num_in_channels=8,
|
|
|
|
| 19 |
vae=vae,
|
| 20 |
torch_dtype=torch.float16,
|
| 21 |
)
|
| 22 |
+
|
| 23 |
+
# Set the scheduler
|
| 24 |
pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
|
| 25 |
pipe_edit.to("cuda")
|
| 26 |
|
| 27 |
+
# Load the refiner
|
| 28 |
+
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
| 29 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
| 30 |
+
vae=vae,
|
| 31 |
+
torch_dtype=torch.float16,
|
| 32 |
+
use_safetensors=True,
|
| 33 |
+
variant="fp16"
|
| 34 |
+
)
|
| 35 |
refiner.to("cuda")
|
| 36 |
|
| 37 |
+
# Patch for the scheduler
|
| 38 |
def set_timesteps_patched(self, num_inference_steps: int, device=None):
|
| 39 |
self.num_inference_steps = num_inference_steps
|
|
|
|
| 40 |
ramp = np.linspace(0, 1, self.num_inference_steps)
|
| 41 |
sigmas = torch.linspace(math.log(self.config.sigma_min), math.log(self.config.sigma_max), len(ramp)).exp().flip(0)
|
| 42 |
+
sigmas = sigmas.to(dtype=torch.float32, device=device)
|
|
|
|
| 43 |
self.timesteps = self.precondition_noise(sigmas)
|
|
|
|
| 44 |
self.sigmas = torch.cat([sigmas, torch.zeros(1, device=sigmas.device)])
|
| 45 |
self._step_index = None
|
| 46 |
self._begin_index = None
|
|
|
|
| 48 |
|
| 49 |
EDMEulerScheduler.set_timesteps = set_timesteps_patched
|
| 50 |
|
| 51 |
+
# Function to perform image editing
|
| 52 |
+
def king(input_image, instruction: str, negative_prompt: str = "", steps: int = 25, randomize_seed: bool = True, seed: int = 2404, guidance_scale: float = 6, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
input_image = Image.open(input_image).convert('RGB')
|
| 54 |
if randomize_seed:
|
| 55 |
seed = random.randint(0, 999999)
|
|
|
|
| 76 |
).images[0]
|
| 77 |
return seed, refine
|
| 78 |
|
| 79 |
+
# CSS for the Gradio interface
|
| 80 |
css = '''
|
| 81 |
.gradio-container{max-width: 700px !important}
|
| 82 |
h1{text-align:center}
|
|
|
|
| 85 |
}
|
| 86 |
'''
|
| 87 |
|
| 88 |
+
# Examples for the Gradio interface
|
| 89 |
examples = [
|
| 90 |
+
["./supercar.png", "make it red"],
|
| 91 |
+
["./red_car.png", "add some snow"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
]
|
| 93 |
|
| 94 |
+
# Creating the Gradio interface
|
| 95 |
with gr.Blocks(css=css) as demo:
|
| 96 |
gr.Markdown("# Image Editing\n### Note: First image generation takes time")
|
| 97 |
with gr.Row():
|
|
|
|
| 130 |
outputs=[seed, input_image],
|
| 131 |
)
|
| 132 |
|
| 133 |
+
demo.queue(max_size=500).launch()
|