Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,11 @@ 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
|
| 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,6 +40,7 @@ normal_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl.safet
|
|
| 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,21 +49,15 @@ 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")
|
|
@@ -103,10 +97,9 @@ def king(type = "Image Generation",
|
|
| 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,
|
|
@@ -118,14 +111,14 @@ def king(type = "Image Generation",
|
|
| 118 |
"output_type":"pil",
|
| 119 |
}
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
return seed, output_image
|
| 123 |
|
| 124 |
# Prompt classifier
|
| 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 |
|
|
|
|
| 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 |
|
| 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 |
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.enable_xformers_memory_efficient_attention() # Enable memory efficient attention for less VRAM usage
|
| 61 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 62 |
pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
|
| 63 |
pipe.set_adapters("dalle")
|
|
|
|
| 97 |
num_inference_steps=steps, generator=generator).images[0]
|
| 98 |
return seed, output_image
|
| 99 |
else :
|
|
|
|
| 100 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 101 |
+
generator = torch.Generator(device="cuda").manual_seed(seed) # Move generator to cuda for speed
|
| 102 |
+
|
| 103 |
options = {
|
| 104 |
"prompt":instruction,
|
| 105 |
"width":width,
|
|
|
|
| 111 |
"output_type":"pil",
|
| 112 |
}
|
| 113 |
|
| 114 |
+
with torch.autocast("cuda"): # Use autocast for faster inference
|
| 115 |
+
output_image = pipe(**options).images[0]
|
| 116 |
return seed, output_image
|
| 117 |
|
| 118 |
# Prompt classifier
|
| 119 |
def response(instruction, input_image=None):
|
| 120 |
if input_image is None:
|
| 121 |
output="Image Generation"
|
|
|
|
| 122 |
else:
|
| 123 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 124 |
|