KingNish commited on
Commit
da74abb
1 Parent(s): d9340e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -6,11 +6,12 @@ import gradio as gr
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
- from diffusers import StableDiffusionXLPipeline, EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
10
  from custom_pipeline import CosStableDiffusionXLInstructPix2PixPipeline
11
  from huggingface_hub import hf_hub_download
12
  from huggingface_hub import InferenceClient
13
 
 
14
  help_text = """
15
  To optimize image results:
16
  - Adjust the **Image CFG weight** if the image isn't changing enough or is changing too much. Lower it to allow bigger changes, or raise it to preserve original details.
@@ -40,7 +41,6 @@ normal_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl.safet
40
 
41
  EDMEulerScheduler.set_timesteps = set_timesteps_patched
42
 
43
- # Use a pre-converted fp16 VAE for faster loading and inference
44
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
45
 
46
  pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
@@ -49,14 +49,21 @@ pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
49
  pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
50
  pipe_edit.to("cuda")
51
 
 
 
 
 
 
 
 
 
52
  # Image Generator
53
- # Keep the models loaded globally for reuse
54
  if torch.cuda.is_available():
55
  pipe = StableDiffusionXLPipeline.from_pretrained(
56
  "fluently/Fluently-XL-v4",
57
  torch_dtype=torch.float16,
58
  use_safetensors=True,
59
- ).to("cuda")
60
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
61
  pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
62
  pipe.set_adapters("dalle")
@@ -87,6 +94,7 @@ def king(type = "Image Generation",
87
  text_cfg_scale = text_cfg_scale
88
  image_cfg_scale = image_cfg_scale
89
  input_image = input_image
 
90
  steps=steps
91
  generator = torch.manual_seed(seed)
92
  output_image = pipe_edit(
@@ -95,9 +103,10 @@ def king(type = "Image Generation",
95
  num_inference_steps=steps, generator=generator).images[0]
96
  return seed, output_image
97
  else :
 
98
  seed = int(randomize_seed_fn(seed, randomize_seed))
99
- generator = torch.Generator().manual_seed(seed) # Move generator to cuda for speed
100
-
101
  options = {
102
  "prompt":instruction,
103
  "width":width,
@@ -116,6 +125,7 @@ def king(type = "Image Generation",
116
  def response(instruction, input_image=None):
117
  if input_image is None:
118
  output="Image Generation"
 
119
  else:
120
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
121
 
 
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
+ from diffusers import 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
 
14
+
15
  help_text = """
16
  To optimize image results:
17
  - Adjust the **Image CFG weight** if the image isn't changing enough or is changing too much. Lower it to allow bigger changes, or raise it to preserve original details.
 
41
 
42
  EDMEulerScheduler.set_timesteps = set_timesteps_patched
43
 
 
44
  vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
45
 
46
  pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file(
 
49
  pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
50
  pipe_edit.to("cuda")
51
 
52
+ from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
53
+
54
+ if not torch.cuda.is_available():
55
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
56
+
57
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
58
+
59
+
60
  # Image Generator
 
61
  if torch.cuda.is_available():
62
  pipe = StableDiffusionXLPipeline.from_pretrained(
63
  "fluently/Fluently-XL-v4",
64
  torch_dtype=torch.float16,
65
  use_safetensors=True,
66
+ )
67
  pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
68
  pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
69
  pipe.set_adapters("dalle")
 
94
  text_cfg_scale = text_cfg_scale
95
  image_cfg_scale = image_cfg_scale
96
  input_image = input_image
97
+
98
  steps=steps
99
  generator = torch.manual_seed(seed)
100
  output_image = pipe_edit(
 
103
  num_inference_steps=steps, generator=generator).images[0]
104
  return seed, output_image
105
  else :
106
+ pipe.to(device)
107
  seed = int(randomize_seed_fn(seed, randomize_seed))
108
+ generator = torch.Generator().manual_seed(seed)
109
+
110
  options = {
111
  "prompt":instruction,
112
  "width":width,
 
125
  def response(instruction, input_image=None):
126
  if input_image is None:
127
  output="Image Generation"
128
+ yield output
129
  else:
130
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
131