KingNish commited on
Commit
089c3bd
1 Parent(s): dde6a3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -6
app.py CHANGED
@@ -6,11 +6,13 @@ import gradio as gr
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
- from diffusers import DiffusionPipeline, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, StableDiffusionInstructPix2PixPipeline, AutoencoderKL
10
  from custom_pipeline import CosStableDiffusionXLInstructPix2PixPipeline
11
  from huggingface_hub import hf_hub_download
12
  from huggingface_hub import InferenceClient
 
13
 
 
14
  dtype = torch.float16
15
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
16
 
@@ -21,6 +23,8 @@ pipe.set_adapters("lora")
21
  pipe.to("cuda")
22
 
23
  refiner = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
 
 
24
  refiner.to("cuda")
25
 
26
  help_text = """
@@ -30,11 +34,30 @@ To optimize image results:
30
  - Experiment with different **random seeds** and **CFG values** for varied outcomes.
31
  - **Rephrase your instructions** for potentially better results.
32
  - **Increase the number of steps** for enhanced edits.
33
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # Image Editor
36
- pipe_edit = StableDiffusionInstructPix2PixPipeline.from_pretrained( "vinesmsuic/magicbrush-jul7", torch_dtype=torch.float16 ).to("cuda")
37
- pipe_edit.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_edit.scheduler.config)
 
 
 
 
 
38
 
39
  # Generator
40
  @spaces.GPU(duration=30, queue=False)
@@ -45,7 +68,7 @@ def king(type ,
45
  randomize_seed: bool = False,
46
  seed: int = 25,
47
  text_cfg_scale: float = 7.3,
48
- image_cfg_scale: float = 1.5,
49
  width: int = 1024,
50
  height: int = 1024,
51
  guidance_scale: float = 6,
@@ -172,7 +195,7 @@ with gr.Blocks(css=css) as demo:
172
 
173
  with gr.Row():
174
  text_cfg_scale = gr.Number(value=7.3, step=0.1, label="Text CFG", interactive=True)
175
- image_cfg_scale = gr.Number(value=1.5, step=0.1,label="Image CFG", interactive=True)
176
  guidance_scale = gr.Number(value=6.0, step=0.1, label="Image Generation Guidance Scale", interactive=True)
177
  steps = gr.Number(value=25, step=1, label="Steps", interactive=True)
178
  randomize_seed = gr.Radio(
 
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
+ from diffusers import DiffusionPipeline, StableDiffusionXLPipeline, EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline, AutoencoderKL
10
  from custom_pipeline import CosStableDiffusionXLInstructPix2PixPipeline
11
  from huggingface_hub import hf_hub_download
12
  from huggingface_hub import InferenceClient
13
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
14
 
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
  dtype = torch.float16
17
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
18
 
 
23
  pipe.to("cuda")
24
 
25
  refiner = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
26
+ refiner.load_lora_weights("KingNish/Better-Image-XL-Lora", weight_name="example-03.safetensors", adapter_name="lora")
27
+ refiner.set_adapters("lora")
28
  refiner.to("cuda")
29
 
30
  help_text = """
 
34
  - Experiment with different **random seeds** and **CFG values** for varied outcomes.
35
  - **Rephrase your instructions** for potentially better results.
36
  - **Increase the number of steps** for enhanced edits.
37
+ """
38
+
39
+ def set_timesteps_patched(self, num_inference_steps: int, device = None):
40
+ self.num_inference_steps = num_inference_steps
41
+
42
+ ramp = np.linspace(0, 1, self.num_inference_steps)
43
+ sigmas = torch.linspace(math.log(self.config.sigma_min), math.log(self.config.sigma_max), len(ramp)).exp().flip(0)
44
+
45
+ sigmas = (sigmas).to(dtype=torch.float32, device=device)
46
+ self.timesteps = self.precondition_noise(sigmas)
47
+
48
+ self.sigmas = torch.cat([sigmas, torch.zeros(1, device=sigmas.device)])
49
+ self._step_index = None
50
+ self._begin_index = None
51
+ self.sigmas = self.sigmas.to("cpu")
52
 
53
  # Image Editor
54
+ edit_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors")
55
+ EDMEulerScheduler.set_timesteps = set_timesteps_patched
56
+ pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
57
+ edit_file, num_in_channels=8, is_cosxl_edit=True, vae=vae, torch_dtype=torch.float16,
58
+ )
59
+ pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
60
+ pipe_edit.to("cuda")
61
 
62
  # Generator
63
  @spaces.GPU(duration=30, queue=False)
 
68
  randomize_seed: bool = False,
69
  seed: int = 25,
70
  text_cfg_scale: float = 7.3,
71
+ image_cfg_scale: float = 1.7,
72
  width: int = 1024,
73
  height: int = 1024,
74
  guidance_scale: float = 6,
 
195
 
196
  with gr.Row():
197
  text_cfg_scale = gr.Number(value=7.3, step=0.1, label="Text CFG", interactive=True)
198
+ image_cfg_scale = gr.Number(value=1.7, step=0.1,label="Image CFG", interactive=True)
199
  guidance_scale = gr.Number(value=6.0, step=0.1, label="Image Generation Guidance Scale", interactive=True)
200
  steps = gr.Number(value=25, step=1, label="Steps", interactive=True)
201
  randomize_seed = gr.Radio(