prithivMLmods commited on
Commit
764dc74
·
verified ·
1 Parent(s): 357786a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +311 -167
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import random
3
  import uuid
4
- from typing import Tuple
5
  import gradio as gr
6
  import numpy as np
7
  from PIL import Image
@@ -9,38 +9,31 @@ import spaces
9
  import torch
10
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
11
 
12
- DESCRIPTIONz = """## SDXL-LoRA-DLC ⚡
 
13
  """
14
 
15
- # Define model options
16
- MODEL_OPTIONS = {
 
 
 
 
 
 
 
17
  "RealVisXL V4.0 Lightning": "SG161222/RealVisXL_V4.0_Lightning",
18
  "RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
 
 
19
  }
20
 
21
- # Dictionary to cache pipelines
22
- pipelines = {}
23
-
24
- def save_image(img):
25
- unique_name = str(uuid.uuid4()) + ".png"
26
- img.save(unique_name)
27
- return unique_name
28
-
29
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
30
- if randomize_seed:
31
- seed = random.randint(0, MAX_SEED)
32
- return seed
33
-
34
- MAX_SEED = np.iinfo(np.int32).max
35
-
36
- if not torch.cuda.is_available():
37
- 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>"
38
-
39
- USE_TORCH_COMPILE = 0
40
- ENABLE_CPU_OFFLOAD = 0
41
 
42
- # Define LoRA options
43
  LORA_OPTIONS = {
 
44
  "Realism (face/character)👦🏻": ("prithivMLmods/Canopus-Realism-LoRA", "Canopus-Realism-LoRA.safetensors", "rlms"),
45
  "Pixar (art/toons)🙀": ("prithivMLmods/Canopus-Pixar-Art", "Canopus-Pixar-Art.safetensors", "pixar"),
46
  "Photoshoot (camera/film)📸": ("prithivMLmods/Canopus-Photo-Shoot-Mini-LoRA", "Canopus-Photo-Shoot-Mini-LoRA.safetensors", "photo"),
@@ -56,60 +49,88 @@ LORA_OPTIONS = {
56
  "Art Minimalistic (paint/semireal)🎨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),
57
  }
58
 
59
- # Function to load or retrieve a pipeline
60
- def get_pipeline(model_name):
61
- if model_name not in pipelines:
62
- pipelines[model_name] = StableDiffusionXLPipeline.from_pretrained(
63
- MODEL_OPTIONS[model_name],
64
- torch_dtype=torch.float16,
65
- use_safetensors=True,
66
- )
67
- pipelines[model_name].scheduler = EulerAncestralDiscreteScheduler.from_config(pipelines[model_name].scheduler.config)
68
- for lora_model_name, lora_weight_name, lora_adapter_name in LORA_OPTIONS.values():
69
- pipelines[model_name].load_lora_weights(lora_model_name, weight_name=lora_weight_name, adapter_name=lora_adapter_name)
70
- pipelines[model_name].to("cuda")
71
- return pipelines[model_name]
72
-
73
  style_list = [
74
  {
75
  "name": "3840 x 2160",
76
  "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
77
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
78
  },
79
  {
80
  "name": "2560 x 1440",
81
  "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
82
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
83
  },
84
  {
85
  "name": "HD+",
86
  "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
87
- "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
88
  },
89
  {
90
  "name": "Style Zero",
91
  "prompt": "{prompt}",
92
- "negative_prompt": "",
93
  },
94
  ]
95
-
96
  styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
97
-
98
- DEFAULT_STYLE_NAME = "3840 x 2160"
99
  STYLE_NAMES = list(styles.keys())
100
 
 
 
 
 
 
 
 
 
 
 
 
101
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
102
- if style_name in styles:
103
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
 
 
 
 
 
 
 
104
  else:
105
- p, n = styles[DEFAULT_STYLE_NAME]
 
 
 
106
 
107
- if not negative:
108
- negative = ""
109
- return p.replace("{prompt}", positive), n + negative
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  @spaces.GPU(duration=180, enable_queue=True)
112
  def generate(
 
113
  prompt: str,
114
  negative_prompt: str = "",
115
  use_negative_prompt: bool = False,
@@ -117,88 +138,212 @@ def generate(
117
  width: int = 1024,
118
  height: int = 1024,
119
  guidance_scale: float = 3,
 
120
  randomize_seed: bool = False,
121
  style_name: str = DEFAULT_STYLE_NAME,
122
- lora_model: str = "Realism (face/character)👦🏻",
123
- base_model: str = "RealVisXL V4.0 Lightning",
124
  progress=gr.Progress(track_tqdm=True),
125
  ):
126
- seed = int(randomize_seed_fn(seed, randomize_seed))
127
-
128
- positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt)
129
 
130
- if not use_negative_prompt:
131
- effective_negative_prompt = ""
132
 
133
- pipe = get_pipeline(base_model)
134
- model_name, weight_name, adapter_name = LORA_OPTIONS[lora_model]
135
- pipe.set_adapters(adapter_name)
 
 
 
 
 
 
 
 
 
 
 
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  images = pipe(
138
  prompt=positive_prompt,
139
  negative_prompt=effective_negative_prompt,
140
  width=width,
141
  height=height,
142
  guidance_scale=guidance_scale,
143
- num_inference_steps=20,
 
144
  num_images_per_prompt=1,
145
- cross_attention_kwargs={"scale": 0.65},
146
  output_type="pil",
147
  ).images
 
148
  image_paths = [save_image(img) for img in images]
 
149
  return image_paths, seed
150
 
151
- examples = [
152
- "Realism: Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational ",
153
- "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",
154
- "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",
155
- ]
156
-
157
  css = '''
158
- .gradio-container{max-width: 545px !important}
159
  h1{text-align:center}
160
- footer {
161
- visibility: hidden
 
 
 
162
  }
 
 
 
 
163
  '''
164
 
165
- def load_predefined_images():
166
- predefined_images = [
167
- "assets/1.png",
168
- "assets/2.png",
169
- "assets/3.png",
170
- "assets/4.png",
171
- "assets/5.png",
172
- "assets/6.png",
173
- "assets/7.png",
174
- "assets/8.png",
175
- "assets/9.png",
176
- ]
177
- return predefined_images
178
-
179
  with gr.Blocks(css=css) as demo:
180
  gr.Markdown(DESCRIPTIONz)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  with gr.Group():
182
  with gr.Row():
183
  prompt = gr.Text(
184
  label="Prompt",
185
  show_label=False,
186
- max_lines=1,
187
- placeholder="Enter your prompt with resp. tag!",
188
  container=False,
 
189
  )
190
- run_button = gr.Button("Run", scale=0)
191
- result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
- with gr.Accordion("Advanced options", open=False, visible=False):
194
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
195
  negative_prompt = gr.Text(
196
- label="Negative prompt",
197
- lines=4,
198
- max_lines=6,
199
- value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
200
- placeholder="Enter a negative prompt",
201
- visible=True,
202
  )
203
  seed = gr.Slider(
204
  label="Seed",
@@ -206,67 +351,45 @@ with gr.Blocks(css=css) as demo:
206
  maximum=MAX_SEED,
207
  step=1,
208
  value=0,
209
- visible=True
 
210
  )
211
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
212
 
213
- with gr.Row(visible=True):
214
  width = gr.Slider(
215
  label="Width",
216
  minimum=512,
217
- maximum=2048,
218
- step=8,
219
  value=1024,
220
  )
221
  height = gr.Slider(
222
  label="Height",
223
  minimum=512,
224
- maximum=2048,
225
- step=8,
226
  value=1024,
227
  )
228
 
229
  with gr.Row():
230
  guidance_scale = gr.Slider(
231
- label="Guidance Scale",
232
- minimum=0.1,
233
- maximum=20.0,
234
  step=0.1,
235
- value=3.0,
 
 
 
 
 
 
 
236
  )
237
 
238
- style_selection = gr.Radio(
239
- show_label=True,
240
- container=True,
241
- interactive=True,
242
- choices=STYLE_NAMES,
243
- value=DEFAULT_STYLE_NAME,
244
- label="Quality Style",
245
- )
246
-
247
- # Add base model selection dropdown
248
- with gr.Row():
249
- base_model_choice = gr.Dropdown(
250
- label="Base Model",
251
- choices=list(MODEL_OPTIONS.keys()),
252
- value="RealVisXL V4.0 Lightning",
253
- )
254
-
255
- with gr.Row(visible=True):
256
- model_choice = gr.Dropdown(
257
- label="LoRA Selection",
258
- choices=list(LORA_OPTIONS.keys()),
259
- value="Realism (face/character)👦🏻"
260
- )
261
-
262
- gr.Examples(
263
- examples=examples,
264
- inputs=prompt,
265
- outputs=[result, seed],
266
- fn=generate,
267
- cache_examples=False,
268
- )
269
 
 
270
  use_negative_prompt.change(
271
  fn=lambda x: gr.update(visible=x),
272
  inputs=use_negative_prompt,
@@ -274,33 +397,54 @@ with gr.Blocks(css=css) as demo:
274
  api_name=False,
275
  )
276
 
277
- gr.on(
278
- triggers=[
279
- prompt.submit,
280
- negative_prompt.submit,
281
- run_button.click,
282
- ],
283
- fn=generate,
284
- inputs=[
285
- prompt,
286
- negative_prompt,
287
- use_negative_prompt,
288
- seed,
289
- width,
290
- height,
291
- guidance_scale,
292
- randomize_seed,
293
- style_selection,
294
- model_choice,
295
- base_model_choice,
296
- ],
297
- outputs=[result, seed],
298
- api_name="run",
299
  )
300
 
301
- with gr.Column(scale=3):
302
- gr.Markdown("### Image Gallery")
303
- predefined_gallery = gr.Gallery(label="Image Gallery", columns=3, show_label=False, value=load_predefined_images())
 
 
 
 
 
 
 
 
 
 
 
 
 
304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  if __name__ == "__main__":
306
- demo.queue(max_size=30).launch()
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import random
3
  import uuid
4
+ from typing import Tuple, Dict
5
  import gradio as gr
6
  import numpy as np
7
  from PIL import Image
 
9
  import torch
10
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
11
 
12
+ DESCRIPTIONz= """## SDXL-LoRA-DLC ⚡
13
+ Select a base model, choose a LoRA, and generate images!
14
  """
15
 
16
+ # --- Constants ---
17
+ MAX_SEED = np.iinfo(np.int32).max
18
+ DEFAULT_STYLE_NAME = "3840 x 2160"
19
+ USE_TORCH_COMPILE = False # Set to True if you want to try torch compile (might be faster but requires compatible hardware/drivers)
20
+ ENABLE_CPU_OFFLOAD = False # Set to True to offload parts of the model to CPU (saves VRAM but slower)
21
+
22
+ # --- Model Definitions ---
23
+ # Dictionary mapping user-friendly names to Hugging Face model IDs
24
+ pipelines_info = {
25
  "RealVisXL V4.0 Lightning": "SG161222/RealVisXL_V4.0_Lightning",
26
  "RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
27
+ # Add more SDXL base models here if desired
28
+ # "Another SDXL Model": "stabilityai/stable-diffusion-xl-base-1.0", # Example
29
  }
30
 
31
+ # Dictionary to cache loaded pipelines
32
+ loaded_pipelines: Dict[str, StableDiffusionXLPipeline] = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # --- LoRA Definitions ---
35
  LORA_OPTIONS = {
36
+ # Name: (HuggingFace Repo ID, Weight Filename, Adapter Name)
37
  "Realism (face/character)👦🏻": ("prithivMLmods/Canopus-Realism-LoRA", "Canopus-Realism-LoRA.safetensors", "rlms"),
38
  "Pixar (art/toons)🙀": ("prithivMLmods/Canopus-Pixar-Art", "Canopus-Pixar-Art.safetensors", "pixar"),
39
  "Photoshoot (camera/film)📸": ("prithivMLmods/Canopus-Photo-Shoot-Mini-LoRA", "Canopus-Photo-Shoot-Mini-LoRA.safetensors", "photo"),
 
49
  "Art Minimalistic (paint/semireal)🎨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),
50
  }
51
 
52
+ # --- Style Definitions ---
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  style_list = [
54
  {
55
  "name": "3840 x 2160",
56
  "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
57
+ "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly, bad anatomy, worst quality, low quality",
58
  },
59
  {
60
  "name": "2560 x 1440",
61
  "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
62
+ "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly, bad anatomy, worst quality, low quality",
63
  },
64
  {
65
  "name": "HD+",
66
  "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
67
+ "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly, bad anatomy, worst quality, low quality",
68
  },
69
  {
70
  "name": "Style Zero",
71
  "prompt": "{prompt}",
72
+ "negative_prompt": "worst quality, low quality", # Added basic negative prompt
73
  },
74
  ]
 
75
  styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
 
 
76
  STYLE_NAMES = list(styles.keys())
77
 
78
+ # --- Utility Functions ---
79
+ def save_image(img):
80
+ unique_name = str(uuid.uuid4()) + ".png"
81
+ img.save(unique_name)
82
+ return unique_name
83
+
84
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
85
+ if randomize_seed:
86
+ seed = random.randint(0, MAX_SEED)
87
+ return seed
88
+
89
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
90
+ # Get the base style prompt and negative prompt
91
+ base_p, base_n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
92
+
93
+ # Combine the base negative prompt with the user's negative prompt
94
+ # Ensure user's negative prompt is appended correctly
95
+ if negative and base_n:
96
+ combined_n = f"{base_n}, {negative}"
97
+ elif negative:
98
+ combined_n = negative
99
  else:
100
+ combined_n = base_n
101
+
102
+ # Apply the positive prompt template
103
+ final_p = base_p.replace("{prompt}", positive)
104
 
105
+ return final_p, combined_n
 
 
106
 
107
+ def load_predefined_images():
108
+ # Ensure the assets directory and images exist
109
+ asset_dir = "assets"
110
+ image_files = [
111
+ "1.png", "2.png", "3.png",
112
+ "4.png", "5.png", "6.png",
113
+ "7.png", "8.png", "9.png",
114
+ ]
115
+ predefined_images = []
116
+ if os.path.exists(asset_dir):
117
+ for img_file in image_files:
118
+ img_path = os.path.join(asset_dir, img_file)
119
+ if os.path.exists(img_path):
120
+ predefined_images.append(img_path)
121
+ else:
122
+ print(f"Warning: Predefined image not found: {img_path}")
123
+ else:
124
+ print(f"Warning: Asset directory not found: {asset_dir}")
125
+ # If no images were found, return None or an empty list
126
+ # to avoid errors in gr.Gallery
127
+ return predefined_images if predefined_images else None
128
+
129
+
130
+ # --- Core Generation Function ---
131
  @spaces.GPU(duration=180, enable_queue=True)
132
  def generate(
133
+ selected_base_model_name: str, # New input for base model selection
134
  prompt: str,
135
  negative_prompt: str = "",
136
  use_negative_prompt: bool = False,
 
138
  width: int = 1024,
139
  height: int = 1024,
140
  guidance_scale: float = 3,
141
+ num_inference_steps: int = 4, # Lightning models use fewer steps
142
  randomize_seed: bool = False,
143
  style_name: str = DEFAULT_STYLE_NAME,
144
+ lora_choice: str = "Realism (face/character)👦🏻",
 
145
  progress=gr.Progress(track_tqdm=True),
146
  ):
147
+ if not torch.cuda.is_available():
148
+ raise gr.Error("GPU not available. This Space requires a GPU to run.")
 
149
 
150
+ seed = int(randomize_seed_fn(seed, randomize_seed))
151
+ torch.manual_seed(seed) # Ensure reproducibility if seed is fixed
152
 
153
+ # --- Pipeline Loading and Caching ---
154
+ pipe = None
155
+ if selected_base_model_name in loaded_pipelines:
156
+ print(f"Using cached pipeline: {selected_base_model_name}")
157
+ pipe = loaded_pipelines[selected_base_model_name]
158
+ else:
159
+ print(f"Loading pipeline: {selected_base_model_name}")
160
+ model_id = pipelines_info[selected_base_model_name]
161
+ pipe = StableDiffusionXLPipeline.from_pretrained(
162
+ model_id,
163
+ torch_dtype=torch.float16,
164
+ use_safetensors=True,
165
+ variant="fp16" if torch.cuda.is_available() else None # Use fp16 variant if available on GPU
166
+ )
167
 
168
+ # Apply optimizations based on flags
169
+ if ENABLE_CPU_OFFLOAD:
170
+ print("Enabling CPU Offload")
171
+ pipe.enable_model_cpu_offload()
172
+ else:
173
+ pipe.to("cuda") # Default: move entire pipeline to GPU
174
+
175
+ # Configure scheduler (important for Lightning models)
176
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
177
+
178
+ # Load ALL LoRAs onto this newly loaded pipeline instance
179
+ print(f"Loading LoRAs for {selected_base_model_name}...")
180
+ for lora_name, (model_repo, weight_file, adapter_tag) in LORA_OPTIONS.items():
181
+ try:
182
+ print(f" Loading LoRA: {lora_name} ({adapter_tag})")
183
+ pipe.load_lora_weights(model_repo, weight_name=weight_file, adapter_name=adapter_tag)
184
+ except Exception as e:
185
+ print(f" Failed to load LoRA {lora_name}: {e}")
186
+ # Optionally raise an error or continue without this LoRA
187
+ # raise gr.Error(f"Failed to load LoRA {lora_name}. Check repo/file names.")
188
+
189
+ if USE_TORCH_COMPILE:
190
+ print("Attempting to compile UNet (may take time)...")
191
+ try:
192
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
193
+ print("UNet compiled successfully.")
194
+ except Exception as e:
195
+ print(f"Torch compile failed: {e}. Running without compilation.")
196
+
197
+ # Cache the fully loaded and configured pipeline
198
+ loaded_pipelines[selected_base_model_name] = pipe
199
+ print(f"Pipeline {selected_base_model_name} loaded and cached.")
200
+
201
+ # --- Prompt Styling ---
202
+ positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt if use_negative_prompt else "")
203
+
204
+ # --- LoRA Selection ---
205
+ if lora_choice not in LORA_OPTIONS:
206
+ raise gr.Error(f"Selected LoRA '{lora_choice}' not found in options.")
207
+
208
+ _lora_repo, _lora_weight, lora_adapter_name = LORA_OPTIONS[lora_choice]
209
+ print(f"Activating LoRA: {lora_choice} (Adapter: {lora_adapter_name})")
210
+ pipe.set_adapters(lora_adapter_name)
211
+ # Note: LoRA weight/scale is often handled within the pipeline or during loading.
212
+ # If you need adjustable LoRA scale, you might need `add_weighted_adapter` or similar.
213
+ # For simplicity here, we assume the default scale is used.
214
+ # cross_attention_kwargs={"scale": 0.8} # Example if you need to set scale explicitly
215
+
216
+ # --- Image Generation ---
217
+ print("Starting image generation...")
218
+ generator = torch.Generator("cuda").manual_seed(seed)
219
  images = pipe(
220
  prompt=positive_prompt,
221
  negative_prompt=effective_negative_prompt,
222
  width=width,
223
  height=height,
224
  guidance_scale=guidance_scale,
225
+ num_inference_steps=num_inference_steps, # Use steps suitable for Lightning
226
+ generator=generator,
227
  num_images_per_prompt=1,
228
+ # cross_attention_kwargs=cross_attention_kwargs, # Add if scale needed
229
  output_type="pil",
230
  ).images
231
+
232
  image_paths = [save_image(img) for img in images]
233
+ print("Image generation complete.")
234
  return image_paths, seed
235
 
236
+ # --- Gradio UI ---
 
 
 
 
 
237
  css = '''
238
+ .gradio-container{max-width: 860px !important; margin: auto;}
239
  h1{text-align:center}
240
+ .gr-prose { text-align: center; }
241
+ #model-select-row { justify-content: center; } /* Center dropdowns */
242
+ /* Make gallery taller */
243
+ #result_gallery .h-\[400px\] {
244
+ height: 600px !important; /* Adjust height as needed */
245
  }
246
+ #predefined_gallery .h-\[400px\] {
247
+ height: 300px !important; /* Adjust height as needed */
248
+ }
249
+ footer { visibility: hidden }
250
  '''
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  with gr.Blocks(css=css) as demo:
253
  gr.Markdown(DESCRIPTIONz)
254
+
255
+ with gr.Row(elem_id="model-select-row"):
256
+ model_selector = gr.Dropdown(
257
+ label="Select Base Model",
258
+ choices=list(pipelines_info.keys()),
259
+ value=list(pipelines_info.keys())[0], # Default to the first model
260
+ scale=1
261
+ )
262
+ model_choice = gr.Dropdown(
263
+ label="Select LoRA Style",
264
+ choices=list(LORA_OPTIONS.keys()),
265
+ value="Realism (face/character)👦🏻", # Default LoRA
266
+ scale=1
267
+ )
268
+
269
  with gr.Group():
270
  with gr.Row():
271
  prompt = gr.Text(
272
  label="Prompt",
273
  show_label=False,
274
+ max_lines=2, # Allow slightly more room for prompt
275
+ placeholder="Enter your prompt (e.g., 'Astronaut riding a horse')",
276
  container=False,
277
+ scale=5, # Make prompt input wider
278
  )
279
+ run_button = gr.Button("Generate", scale=1, variant="primary") # Make button stand out
280
+
281
+ # Use Tabs for Main Result and Examples/Gallery
282
+ with gr.Tabs():
283
+ with gr.TabItem("Result", id="result_tab"):
284
+ result = gr.Gallery(
285
+ label="Generated Image", elem_id="result_gallery",
286
+ columns=1, preview=True, show_label=False, height=600 # Make gallery taller
287
+ )
288
+ # Display the seed used for the generated image
289
+ used_seed = gr.Number(label="Seed Used", interactive=False)
290
+
291
+ with gr.TabItem("Examples & Predefined Gallery", id="examples_tab"):
292
+ gr.Markdown("### Prompt Examples")
293
+ gr.Examples(
294
+ examples=[
295
+ "cinematic photo, a man sitting on a chair in a dark room, realistic", # Realism example
296
+ "pixar style 3d render of a cute cat astronaut exploring mars", # Pixar example
297
+ "studio photography, high fashion model wearing a futuristic silver hoodie, dramatic lighting", # Photoshoot/Clothing example
298
+ "minimalist vector art illustration of a mountain range at sunset, liquid style", # Minimalist/Liquid example
299
+ "pencil sketch drawing of an old wise wizard with a long beard", # Pencil Art example
300
+ ],
301
+ inputs=[prompt], # Only update the prompt field from examples
302
+ outputs=[result, used_seed], # Define outputs for example generation
303
+ fn=lambda p: generate( # Need a lambda to pass default values for other args
304
+ selected_base_model_name=list(pipelines_info.keys())[0], # Use default model for examples
305
+ prompt=p,
306
+ lora_choice="Realism (face/character)👦🏻", # Use default LoRA for examples
307
+ # Add other default args from 'generate' signature if needed
308
+ negative_prompt="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
309
+ use_negative_prompt=True,
310
+ seed=0, # Or make examples use random seed?
311
+ width=1024,
312
+ height=1024,
313
+ guidance_scale=3.0,
314
+ num_inference_steps=4,
315
+ randomize_seed=True, # Randomize seed for examples
316
+ style_name=DEFAULT_STYLE_NAME,
317
+ ),
318
+ cache_examples=False, # Recalculate examples if needed
319
+ label="Click an example to generate"
320
+ )
321
+ gr.Markdown("### Predefined Image Gallery")
322
+ predefined_gallery = gr.Gallery(
323
+ label="Image Gallery", elem_id="predefined_gallery",
324
+ columns=3, show_label=False, value=load_predefined_images(), height=300
325
+ )
326
+
327
+
328
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
329
+ style_selection = gr.Radio(
330
+ show_label=True,
331
+ container=True,
332
+ interactive=True,
333
+ choices=STYLE_NAMES,
334
+ value=DEFAULT_STYLE_NAME,
335
+ label="Image Quality Style",
336
+ )
337
+ with gr.Row():
338
+ use_negative_prompt = gr.Checkbox(label="Use Negative Prompt", value=True)
339
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
340
 
 
 
341
  negative_prompt = gr.Text(
342
+ label="Negative Prompt",
343
+ max_lines=2,
344
+ value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, worst quality, low quality",
345
+ placeholder="Enter concepts to avoid...",
346
+ visible=True, # Initially visible, controlled by checkbox change
 
347
  )
348
  seed = gr.Slider(
349
  label="Seed",
 
351
  maximum=MAX_SEED,
352
  step=1,
353
  value=0,
354
+ visible=True, # Initially visible, maybe hide if randomize is checked?
355
+ interactive=True
356
  )
 
357
 
358
+ with gr.Row():
359
  width = gr.Slider(
360
  label="Width",
361
  minimum=512,
362
+ maximum=1536, # Adjusted max based on typical SDXL use
363
+ step=64,
364
  value=1024,
365
  )
366
  height = gr.Slider(
367
  label="Height",
368
  minimum=512,
369
+ maximum=1536, # Adjusted max based on typical SDXL use
370
+ step=64,
371
  value=1024,
372
  )
373
 
374
  with gr.Row():
375
  guidance_scale = gr.Slider(
376
+ label="Guidance Scale (CFG)",
377
+ minimum=0.0,
378
+ maximum=10.0, # Lightning models often use low CFG
379
  step=0.1,
380
+ value=1.5, # Default low CFG for Lightning
381
+ )
382
+ num_inference_steps = gr.Slider(
383
+ label="Inference Steps",
384
+ minimum=1,
385
+ maximum=20, # Lightning models need very few steps
386
+ step=1,
387
+ value=4, # Default steps for Lightning
388
  )
389
 
390
+ # --- Event Listeners ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
 
392
+ # Show/hide negative prompt input based on checkbox
393
  use_negative_prompt.change(
394
  fn=lambda x: gr.update(visible=x),
395
  inputs=use_negative_prompt,
 
397
  api_name=False,
398
  )
399
 
400
+ # Show/hide seed slider based on randomize checkbox
401
+ randomize_seed.change(
402
+ fn=lambda x: gr.update(interactive=not x), # Make slider non-interactive if randomizing
403
+ inputs=randomize_seed,
404
+ outputs=seed,
405
+ api_name=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406
  )
407
 
408
+ # Main generation trigger
409
+ inputs_list = [
410
+ model_selector, # Add model selector
411
+ prompt,
412
+ negative_prompt,
413
+ use_negative_prompt,
414
+ seed,
415
+ width,
416
+ height,
417
+ guidance_scale,
418
+ num_inference_steps, # Add steps slider
419
+ randomize_seed,
420
+ style_selection,
421
+ model_choice, # This is the LoRA choice dropdown
422
+ ]
423
+ outputs_list = [result, used_seed] # Output gallery and the seed number
424
 
425
+ prompt.submit(
426
+ fn=generate,
427
+ inputs=inputs_list,
428
+ outputs=outputs_list,
429
+ api_name="run_prompt_submit" # Optional: Define API name
430
+ )
431
+ run_button.click(
432
+ fn=generate,
433
+ inputs=inputs_list,
434
+ outputs=outputs_list,
435
+ api_name="run_button_click" # Optional: Define API name
436
+ )
437
+
438
+ # --- Launch ---
439
  if __name__ == "__main__":
440
+ if not torch.cuda.is_available():
441
+ print("Warning: No CUDA GPU detected. Running on CPU will be extremely slow or may fail.")
442
+ DESCRIPTIONz += "\n<p>⚠️<b>WARNING: No GPU detected. Running on CPU is very slow and may not work reliably.</b> Consider using a GPU instance.</p>"
443
+ # Optionally disable parts of the UI or exit if CPU is unacceptable
444
+ # exit()
445
+
446
+ # Ensure asset directory exists for predefined images (optional but good practice)
447
+ if not os.path.exists("assets"):
448
+ print("Warning: 'assets' directory not found. Predefined images will not load.")
449
+
450
+ demo.queue(max_size=20).launch(debug=False) # Set debug=True for more logs if needed