Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,14 @@ import spaces
|
|
| 9 |
import torch
|
| 10 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 11 |
|
| 12 |
-
|
| 13 |
-
DESCRIPTIONz = """## SDXL-LoRA-DLC β‘
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
# Function to save generated images
|
| 17 |
def save_image(img):
|
| 18 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 19 |
img.save(unique_name)
|
| 20 |
return unique_name
|
| 21 |
|
| 22 |
-
# Function to handle seed randomization
|
| 23 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 24 |
if randomize_seed:
|
| 25 |
seed = random.randint(0, MAX_SEED)
|
|
@@ -27,42 +24,41 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
| 27 |
|
| 28 |
MAX_SEED = np.iinfo(np.int32).max
|
| 29 |
|
| 30 |
-
# Warning if running on CPU
|
| 31 |
if not torch.cuda.is_available():
|
| 32 |
DESCRIPTIONz += "\n<p>β οΈRunning on CPU, This may not work on CPU. If it runs for an extended time or if you encounter errors, try running it on a GPU by duplicating the space using @spaces.GPU(). +import spaces.π</p>"
|
| 33 |
|
| 34 |
-
# Configuration flags (unchanged)
|
| 35 |
USE_TORCH_COMPILE = 0
|
| 36 |
ENABLE_CPU_OFFLOAD = 0
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
# Define style options
|
| 66 |
style_list = [
|
| 67 |
{
|
| 68 |
"name": "3840 x 2160",
|
|
@@ -87,20 +83,20 @@ style_list = [
|
|
| 87 |
]
|
| 88 |
|
| 89 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
|
|
|
| 90 |
DEFAULT_STYLE_NAME = "3840 x 2160"
|
| 91 |
STYLE_NAMES = list(styles.keys())
|
| 92 |
|
| 93 |
-
# Function to apply selected style
|
| 94 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 95 |
if style_name in styles:
|
| 96 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 97 |
else:
|
| 98 |
p, n = styles[DEFAULT_STYLE_NAME]
|
|
|
|
| 99 |
if not negative:
|
| 100 |
negative = ""
|
| 101 |
return p.replace("{prompt}", positive), n + negative
|
| 102 |
|
| 103 |
-
# Generation function with model selection
|
| 104 |
@spaces.GPU(duration=180, enable_queue=True)
|
| 105 |
def generate(
|
| 106 |
prompt: str,
|
|
@@ -113,39 +109,19 @@ def generate(
|
|
| 113 |
randomize_seed: bool = False,
|
| 114 |
style_name: str = DEFAULT_STYLE_NAME,
|
| 115 |
lora_model: str = "Realism (face/character)π¦π»",
|
| 116 |
-
base_model: str = "RealVisXL V5.0 Lightning",
|
| 117 |
progress=gr.Progress(track_tqdm=True),
|
| 118 |
):
|
| 119 |
-
global current_base_model, current_pipeline
|
| 120 |
-
|
| 121 |
-
# Load the pipeline if the base model has changed
|
| 122 |
-
if base_model != current_base_model:
|
| 123 |
-
model_id = base_models[base_model]
|
| 124 |
-
current_pipeline = StableDiffusionXLPipeline.from_pretrained(
|
| 125 |
-
model_id, torch_dtype=torch.float16, use_safetensors=True
|
| 126 |
-
)
|
| 127 |
-
current_pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(
|
| 128 |
-
current_pipeline.scheduler.config
|
| 129 |
-
)
|
| 130 |
-
for lora_display_name, (lora_model, lora_weight, adapter_name) in LORA_OPTIONS.items():
|
| 131 |
-
current_pipeline.load_lora_weights(
|
| 132 |
-
lora_model, weight_name=lora_weight, adapter_name=adapter_name
|
| 133 |
-
)
|
| 134 |
-
current_pipeline.to("cuda")
|
| 135 |
-
current_base_model = base_model
|
| 136 |
-
|
| 137 |
-
# Handle seed and prompts
|
| 138 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
|
|
|
| 139 |
positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
|
|
|
| 140 |
if not use_negative_prompt:
|
| 141 |
-
effective_negative_prompt = ""
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
current_pipeline.set_adapters(adapter_name)
|
| 146 |
|
| 147 |
-
|
| 148 |
-
images = current_pipeline(
|
| 149 |
prompt=positive_prompt,
|
| 150 |
negative_prompt=effective_negative_prompt,
|
| 151 |
width=width,
|
|
@@ -159,14 +135,12 @@ def generate(
|
|
| 159 |
image_paths = [save_image(img) for img in images]
|
| 160 |
return image_paths, seed
|
| 161 |
|
| 162 |
-
# Example prompts
|
| 163 |
examples = [
|
| 164 |
"Realism: Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational ",
|
| 165 |
"Pixar: A young man with light brown wavy hair and light brown eyes sitting in an armchair and looking directly at the camera, pixar style, disney pixar, office background, ultra detailed, 1 man",
|
| 166 |
"Hoodie: Front view, capture a urban style, Superman Hoodie, technical materials, fabric small point label on text Blue theory, the design is minimal, with a raised collar, fabric is a Light yellow, low angle to capture the Hoodies form and detailing, f/5.6 to focus on the hoodies craftsmanship, solid grey background, studio light setting, with batman logo in the chest region of the t-shirt",
|
| 167 |
]
|
| 168 |
|
| 169 |
-
# CSS styling
|
| 170 |
css = '''
|
| 171 |
.gradio-container{max-width: 545px !important}
|
| 172 |
h1{text-align:center}
|
|
@@ -174,10 +148,10 @@ footer {
|
|
| 174 |
visibility: hidden
|
| 175 |
}
|
| 176 |
'''
|
| 177 |
-
|
| 178 |
-
# Function to load predefined images
|
| 179 |
def load_predefined_images():
|
|
|
|
| 180 |
predefined_images = [
|
|
|
|
| 181 |
"assets/1.png",
|
| 182 |
"assets/2.png",
|
| 183 |
"assets/3.png",
|
|
@@ -187,10 +161,10 @@ def load_predefined_images():
|
|
| 187 |
"assets/7.png",
|
| 188 |
"assets/8.png",
|
| 189 |
"assets/9.png",
|
|
|
|
| 190 |
]
|
| 191 |
return predefined_images
|
| 192 |
|
| 193 |
-
# Gradio interface
|
| 194 |
with gr.Blocks(css=css) as demo:
|
| 195 |
gr.Markdown(DESCRIPTIONz)
|
| 196 |
with gr.Group():
|
|
@@ -224,6 +198,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 224 |
visible=True
|
| 225 |
)
|
| 226 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 227 |
with gr.Row(visible=True):
|
| 228 |
width = gr.Slider(
|
| 229 |
label="Width",
|
|
@@ -239,6 +214,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 239 |
step=8,
|
| 240 |
value=1024,
|
| 241 |
)
|
|
|
|
| 242 |
with gr.Row():
|
| 243 |
guidance_scale = gr.Slider(
|
| 244 |
label="Guidance Scale",
|
|
@@ -247,6 +223,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 247 |
step=0.1,
|
| 248 |
value=3.0,
|
| 249 |
)
|
|
|
|
| 250 |
style_selection = gr.Radio(
|
| 251 |
show_label=True,
|
| 252 |
container=True,
|
|
@@ -256,13 +233,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 256 |
label="Quality Style",
|
| 257 |
)
|
| 258 |
|
| 259 |
-
|
| 260 |
-
with gr.Row():
|
| 261 |
-
base_model_choice = gr.Dropdown(
|
| 262 |
-
label="Base Model",
|
| 263 |
-
choices=list(base_models.keys()),
|
| 264 |
-
value="RealVisXL V5.0 Lightning"
|
| 265 |
-
)
|
| 266 |
model_choice = gr.Dropdown(
|
| 267 |
label="LoRA Selection",
|
| 268 |
choices=list(LORA_OPTIONS.keys()),
|
|
@@ -302,20 +273,15 @@ with gr.Blocks(css=css) as demo:
|
|
| 302 |
randomize_seed,
|
| 303 |
style_selection,
|
| 304 |
model_choice,
|
| 305 |
-
base_model_choice,
|
| 306 |
],
|
| 307 |
outputs=[result, seed],
|
| 308 |
api_name="run",
|
| 309 |
)
|
| 310 |
|
|
|
|
| 311 |
with gr.Column(scale=3):
|
| 312 |
gr.Markdown("### Image Gallery")
|
| 313 |
-
predefined_gallery = gr.Gallery(
|
| 314 |
-
|
| 315 |
-
columns=3,
|
| 316 |
-
show_label=False,
|
| 317 |
-
value=load_predefined_images()
|
| 318 |
-
)
|
| 319 |
-
|
| 320 |
if __name__ == "__main__":
|
| 321 |
demo.queue(max_size=30).launch()
|
|
|
|
| 9 |
import torch
|
| 10 |
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 11 |
|
| 12 |
+
DESCRIPTIONz= """## SDXL-LoRA-DLC β‘
|
|
|
|
| 13 |
"""
|
| 14 |
|
|
|
|
| 15 |
def save_image(img):
|
| 16 |
unique_name = str(uuid.uuid4()) + ".png"
|
| 17 |
img.save(unique_name)
|
| 18 |
return unique_name
|
| 19 |
|
|
|
|
| 20 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 21 |
if randomize_seed:
|
| 22 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 24 |
|
| 25 |
MAX_SEED = np.iinfo(np.int32).max
|
| 26 |
|
|
|
|
| 27 |
if not torch.cuda.is_available():
|
| 28 |
DESCRIPTIONz += "\n<p>β οΈRunning on CPU, This may not work on CPU. If it runs for an extended time or if you encounter errors, try running it on a GPU by duplicating the space using @spaces.GPU(). +import spaces.π</p>"
|
| 29 |
|
|
|
|
| 30 |
USE_TORCH_COMPILE = 0
|
| 31 |
ENABLE_CPU_OFFLOAD = 0
|
| 32 |
|
| 33 |
+
if torch.cuda.is_available():
|
| 34 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 35 |
+
"SG161222/RealVisXL_V4.0_Lightning",
|
| 36 |
+
torch_dtype=torch.float16,
|
| 37 |
+
use_safetensors=True,
|
| 38 |
+
)
|
| 39 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 40 |
|
| 41 |
+
LORA_OPTIONS = {
|
| 42 |
+
"Realism (face/character)π¦π»": ("prithivMLmods/Canopus-Realism-LoRA", "Canopus-Realism-LoRA.safetensors", "rlms"),
|
| 43 |
+
"Pixar (art/toons)π": ("prithivMLmods/Canopus-Pixar-Art", "Canopus-Pixar-Art.safetensors", "pixar"),
|
| 44 |
+
"Photoshoot (camera/film)πΈ": ("prithivMLmods/Canopus-Photo-Shoot-Mini-LoRA", "Canopus-Photo-Shoot-Mini-LoRA.safetensors", "photo"),
|
| 45 |
+
"Clothing (hoodies/pant/shirts)π": ("prithivMLmods/Canopus-Clothing-Adp-LoRA", "Canopus-Dress-Clothing-LoRA.safetensors", "clth"),
|
| 46 |
+
"Interior Architecture (house/hotel)π ": ("prithivMLmods/Canopus-Interior-Architecture-0.1", "Canopus-Interior-Architecture-0.1Ξ΄.safetensors", "arch"),
|
| 47 |
+
"Fashion Product (wearing/usable)π": ("prithivMLmods/Canopus-Fashion-Product-Dilation", "Canopus-Fashion-Product-Dilation.safetensors", "fashion"),
|
| 48 |
+
"Minimalistic Image (minimal/detailed)ποΈ": ("prithivMLmods/Pegasi-Minimalist-Image-Style", "Pegasi-Minimalist-Image-Style.safetensors", "minimalist"),
|
| 49 |
+
"Modern Clothing (trend/new)π": ("prithivMLmods/Canopus-Modern-Clothing-Design", "Canopus-Modern-Clothing-Design.safetensors", "mdrnclth"),
|
| 50 |
+
"Animaliea (farm/wild)π«": ("prithivMLmods/Canopus-Animaliea-Artism", "Canopus-Animaliea-Artism.safetensors", "Animaliea"),
|
| 51 |
+
"Liquid Wallpaper (minimal/illustration)πΌοΈ": ("prithivMLmods/Canopus-Liquid-Wallpaper-Art", "Canopus-Liquid-Wallpaper-Minimalize-LoRA.safetensors", "liquid"),
|
| 52 |
+
"Canes Cars (realistic/futurecars)π": ("prithivMLmods/Canes-Cars-Model-LoRA", "Canes-Cars-Model-LoRA.safetensors", "car"),
|
| 53 |
+
"Pencil Art (characteristic/creative)βοΈ": ("prithivMLmods/Canopus-Pencil-Art-LoRA", "Canopus-Pencil-Art-LoRA.safetensors", "Pencil Art"),
|
| 54 |
+
"Art Minimalistic (paint/semireal)π¨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),
|
| 55 |
+
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
for model_name, weight_name, adapter_name in LORA_OPTIONS.values():
|
| 59 |
+
pipe.load_lora_weights(model_name, weight_name=weight_name, adapter_name=adapter_name)
|
| 60 |
+
pipe.to("cuda")
|
| 61 |
|
|
|
|
| 62 |
style_list = [
|
| 63 |
{
|
| 64 |
"name": "3840 x 2160",
|
|
|
|
| 83 |
]
|
| 84 |
|
| 85 |
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
| 86 |
+
|
| 87 |
DEFAULT_STYLE_NAME = "3840 x 2160"
|
| 88 |
STYLE_NAMES = list(styles.keys())
|
| 89 |
|
|
|
|
| 90 |
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
| 91 |
if style_name in styles:
|
| 92 |
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
| 93 |
else:
|
| 94 |
p, n = styles[DEFAULT_STYLE_NAME]
|
| 95 |
+
|
| 96 |
if not negative:
|
| 97 |
negative = ""
|
| 98 |
return p.replace("{prompt}", positive), n + negative
|
| 99 |
|
|
|
|
| 100 |
@spaces.GPU(duration=180, enable_queue=True)
|
| 101 |
def generate(
|
| 102 |
prompt: str,
|
|
|
|
| 109 |
randomize_seed: bool = False,
|
| 110 |
style_name: str = DEFAULT_STYLE_NAME,
|
| 111 |
lora_model: str = "Realism (face/character)π¦π»",
|
|
|
|
| 112 |
progress=gr.Progress(track_tqdm=True),
|
| 113 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 115 |
+
|
| 116 |
positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
| 117 |
+
|
| 118 |
if not use_negative_prompt:
|
| 119 |
+
effective_negative_prompt = "" # type: ignore
|
| 120 |
|
| 121 |
+
model_name, weight_name, adapter_name = LORA_OPTIONS[lora_model]
|
| 122 |
+
pipe.set_adapters(adapter_name)
|
|
|
|
| 123 |
|
| 124 |
+
images = pipe(
|
|
|
|
| 125 |
prompt=positive_prompt,
|
| 126 |
negative_prompt=effective_negative_prompt,
|
| 127 |
width=width,
|
|
|
|
| 135 |
image_paths = [save_image(img) for img in images]
|
| 136 |
return image_paths, seed
|
| 137 |
|
|
|
|
| 138 |
examples = [
|
| 139 |
"Realism: Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational ",
|
| 140 |
"Pixar: A young man with light brown wavy hair and light brown eyes sitting in an armchair and looking directly at the camera, pixar style, disney pixar, office background, ultra detailed, 1 man",
|
| 141 |
"Hoodie: Front view, capture a urban style, Superman Hoodie, technical materials, fabric small point label on text Blue theory, the design is minimal, with a raised collar, fabric is a Light yellow, low angle to capture the Hoodies form and detailing, f/5.6 to focus on the hoodies craftsmanship, solid grey background, studio light setting, with batman logo in the chest region of the t-shirt",
|
| 142 |
]
|
| 143 |
|
|
|
|
| 144 |
css = '''
|
| 145 |
.gradio-container{max-width: 545px !important}
|
| 146 |
h1{text-align:center}
|
|
|
|
| 148 |
visibility: hidden
|
| 149 |
}
|
| 150 |
'''
|
|
|
|
|
|
|
| 151 |
def load_predefined_images():
|
| 152 |
+
|
| 153 |
predefined_images = [
|
| 154 |
+
|
| 155 |
"assets/1.png",
|
| 156 |
"assets/2.png",
|
| 157 |
"assets/3.png",
|
|
|
|
| 161 |
"assets/7.png",
|
| 162 |
"assets/8.png",
|
| 163 |
"assets/9.png",
|
| 164 |
+
|
| 165 |
]
|
| 166 |
return predefined_images
|
| 167 |
|
|
|
|
| 168 |
with gr.Blocks(css=css) as demo:
|
| 169 |
gr.Markdown(DESCRIPTIONz)
|
| 170 |
with gr.Group():
|
|
|
|
| 198 |
visible=True
|
| 199 |
)
|
| 200 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 201 |
+
|
| 202 |
with gr.Row(visible=True):
|
| 203 |
width = gr.Slider(
|
| 204 |
label="Width",
|
|
|
|
| 214 |
step=8,
|
| 215 |
value=1024,
|
| 216 |
)
|
| 217 |
+
|
| 218 |
with gr.Row():
|
| 219 |
guidance_scale = gr.Slider(
|
| 220 |
label="Guidance Scale",
|
|
|
|
| 223 |
step=0.1,
|
| 224 |
value=3.0,
|
| 225 |
)
|
| 226 |
+
|
| 227 |
style_selection = gr.Radio(
|
| 228 |
show_label=True,
|
| 229 |
container=True,
|
|
|
|
| 233 |
label="Quality Style",
|
| 234 |
)
|
| 235 |
|
| 236 |
+
with gr.Row(visible=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
model_choice = gr.Dropdown(
|
| 238 |
label="LoRA Selection",
|
| 239 |
choices=list(LORA_OPTIONS.keys()),
|
|
|
|
| 273 |
randomize_seed,
|
| 274 |
style_selection,
|
| 275 |
model_choice,
|
|
|
|
| 276 |
],
|
| 277 |
outputs=[result, seed],
|
| 278 |
api_name="run",
|
| 279 |
)
|
| 280 |
|
| 281 |
+
|
| 282 |
with gr.Column(scale=3):
|
| 283 |
gr.Markdown("### Image Gallery")
|
| 284 |
+
predefined_gallery = gr.Gallery(label="Image Gallery", columns=3, show_label=False, value=load_predefined_images())
|
| 285 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
if __name__ == "__main__":
|
| 287 |
demo.queue(max_size=30).launch()
|