File size: 16,951 Bytes
2e94d15
 
 
f06faee
2e94d15
 
 
 
 
 
 
6091fe1
2e94d15
 
6091fe1
 
 
 
 
f06faee
6091fe1
 
 
 
 
f06faee
 
 
 
 
 
 
 
764dc74
6091fe1
764dc74
f06faee
 
6091fe1
 
 
 
f06faee
6091fe1
7f9076e
 
 
 
 
 
58b79f9
7f9076e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
58b79f9
2e94d15
 
 
 
f06faee
2e94d15
 
 
 
f06faee
2e94d15
 
 
 
f06faee
2e94d15
 
 
 
f06faee
2e94d15
 
f06faee
2e94d15
7f9076e
f06faee
2e94d15
 
 
6091fe1
7f9076e
f06faee
 
6091fe1
764dc74
2e94d15
 
 
 
 
 
 
 
 
 
 
f06faee
2e94d15
 
6091fe1
 
 
f06faee
7f9076e
6091fe1
 
 
 
 
 
 
 
 
 
 
 
7f9076e
6091fe1
 
 
 
 
 
764dc74
7f9076e
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e94d15
f06faee
6091fe1
 
 
f06faee
 
2e94d15
6091fe1
2e94d15
6091fe1
 
2e94d15
7f9076e
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f06faee
 
6091fe1
 
 
2e94d15
6091fe1
 
 
 
 
 
 
 
 
 
 
 
c6f8f53
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e94d15
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e94d15
7f9076e
6091fe1
2e94d15
 
 
6091fe1
 
2e94d15
 
 
 
 
6091fe1
 
2e94d15
 
 
6091fe1
 
 
2e94d15
f06faee
2e94d15
7f9076e
6091fe1
2e94d15
6091fe1
2e94d15
 
 
 
 
 
 
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f06faee
6091fe1
764dc74
6091fe1
 
 
764dc74
 
6091fe1
 
7f9076e
6091fe1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f06faee
6091fe1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import os
import random
import uuid
from typing import Tuple
import gradio as gr
import numpy as np
from PIL import Image
import spaces
import torch
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler

DESCRIPTIONz = """## SDXL-LoRA-DLC ⚑
"""

# Ensure assets directory exists if needed for predefined images
if not os.path.exists("assets"):
    print("Warning: 'assets' directory not found. Predefined gallery might be empty.")
    # Optionally create it: os.makedirs("assets")

def save_image(img):
    # Ensure an 'outputs' directory exists to save generated images (optional, good practice)
    output_dir = "outputs"
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    unique_name = os.path.join(output_dir, str(uuid.uuid4()) + ".png")
    img.save(unique_name)
    return unique_name

def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed

MAX_SEED = np.iinfo(np.int32).max
pipe = None # Initialize pipe to None

if not torch.cuda.is_available():
    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>"
    # Optionally, you could add a placeholder or disable functionality here
else:
    USE_TORCH_COMPILE = False # Set to False as 0 is not standard boolean
    ENABLE_CPU_OFFLOAD = False # Set to False as 0 is not standard boolean

    # Moved pipe initialization inside the CUDA check
    pipe = StableDiffusionXLPipeline.from_pretrained(
        "SG161222/RealVisXL_V4.0_Lightning",
        torch_dtype=torch.float16,
        use_safetensors=True,
    )
    pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)

    LORA_OPTIONS = {
        "Realism (face/character)πŸ‘¦πŸ»": ("prithivMLmods/Canopus-Realism-LoRA", "Canopus-Realism-LoRA.safetensors", "rlms"),
        "Pixar (art/toons)πŸ™€": ("prithivMLmods/Canopus-Pixar-Art", "Canopus-Pixar-Art.safetensors", "pixar"),
        "Photoshoot (camera/film)πŸ“Έ": ("prithivMLmods/Canopus-Photo-Shoot-Mini-LoRA", "Canopus-Photo-Shoot-Mini-LoRA.safetensors", "photo"),
        "Clothing (hoodies/pant/shirts)πŸ‘”": ("prithivMLmods/Canopus-Clothing-Adp-LoRA", "Canopus-Dress-Clothing-LoRA.safetensors", "clth"),
        "Interior Architecture (house/hotel)🏠": ("prithivMLmods/Canopus-Interior-Architecture-0.1", "Canopus-Interior-Architecture-0.1δ.safetensors", "arch"),
        "Fashion Product (wearing/usable)πŸ‘œ": ("prithivMLmods/Canopus-Fashion-Product-Dilation", "Canopus-Fashion-Product-Dilation.safetensors", "fashion"),
        "Minimalistic Image (minimal/detailed)🏞️": ("prithivMLmods/Pegasi-Minimalist-Image-Style", "Pegasi-Minimalist-Image-Style.safetensors", "minimalist"),
        "Modern Clothing (trend/new)πŸ‘•": ("prithivMLmods/Canopus-Modern-Clothing-Design", "Canopus-Modern-Clothing-Design.safetensors", "mdrnclth"),
        "Animaliea (farm/wild)🫎": ("prithivMLmods/Canopus-Animaliea-Artism", "Canopus-Animaliea-Artism.safetensors", "Animaliea"),
        "Liquid Wallpaper (minimal/illustration)πŸ–ΌοΈ": ("prithivMLmods/Canopus-Liquid-Wallpaper-Art", "Canopus-Liquid-Wallpaper-Minimalize-LoRA.safetensors", "liquid"),
        "Canes Cars (realistic/futurecars)🚘": ("prithivMLmods/Canes-Cars-Model-LoRA", "Canes-Cars-Model-LoRA.safetensors", "car"),
        "Pencil Art (characteristic/creative)✏️": ("prithivMLmods/Canopus-Pencil-Art-LoRA", "Canopus-Pencil-Art-LoRA.safetensors", "Pencil Art"),
        "Art Minimalistic (paint/semireal)🎨": ("prithivMLmods/Canopus-Art-Medium-LoRA", "Canopus-Art-Medium-LoRA.safetensors", "mdm"),
    }

    # Load LoRAs only if pipe is initialized
    if pipe:
        for model_name, weight_name, adapter_name in LORA_OPTIONS.values():
            try:
                pipe.load_lora_weights(model_name, weight_name=weight_name, adapter_name=adapter_name)
                print(f"Loaded LoRA: {adapter_name}")
            except Exception as e:
                print(f"Warning: Could not load LoRA {adapter_name} from {model_name}. Error: {e}")
        pipe.to("cuda")
        print("Pipeline and LoRAs loaded to CUDA.")
    else:
        print("Pipeline not initialized (likely no CUDA available).")


style_list = [
    {
        "name": "3840 x 2160",
        "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
        "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
    },
    {
        "name": "2560 x 1440",
        "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
        "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
    },
    {
        "name": "HD+",
        "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
        "negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
    },
    {
        "name": "Style Zero",
        "prompt": "{prompt}",
        "negative_prompt": "",
    },
]

styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}

DEFAULT_STYLE_NAME = "3840 x 2160"
STYLE_NAMES = list(styles.keys())

def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
    p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME]) # Use .get for safety

    if not negative:
        negative = ""
    return p.replace("{prompt}", positive), n + " " + negative # Add space for clarity

@spaces.GPU(duration=180, enable_queue=True)
def generate(
    prompt: str,
    negative_prompt: str = "",
    use_negative_prompt: bool = False,
    seed: int = 0,
    width: int = 1024,
    height: int = 1024,
    guidance_scale: float = 3,
    randomize_seed: bool = False,
    style_name: str = DEFAULT_STYLE_NAME,
    lora_model: str = "Realism (face/character)πŸ‘¦πŸ»",
    progress=gr.Progress(track_tqdm=True),
):
    if pipe is None:
        raise gr.Error("Pipeline not initialized. Check if CUDA is available and drivers are installed.")
    
    seed = int(randomize_seed_fn(seed, randomize_seed))

    # Apply style first
    positive_prompt, base_negative_prompt = apply_style(style_name, prompt, negative_prompt if use_negative_prompt else "")

    # If user explicitly provided a negative prompt and wants to use it, append it
    # (apply_style already incorporates the style's negative prompt)
    # This logic might need adjustment depending on desired behavior: replace or append?
    # Current: Style neg prompt + user neg prompt
    effective_negative_prompt = base_negative_prompt
    if use_negative_prompt and negative_prompt:
         # Check if the negative prompt from apply_style is already there to avoid duplication
        if not negative_prompt in effective_negative_prompt:
             effective_negative_prompt = (effective_negative_prompt + " " + negative_prompt).strip()


    # Ensure LoRA selection is valid
    if lora_model not in LORA_OPTIONS:
        print(f"Warning: Invalid LoRA selection '{lora_model}'. Using default or first available.")
        # Fallback logic could be added here, e.g., use the first key
        lora_model = next(iter(LORA_OPTIONS)) # Get the first key as a fallback

    model_name, weight_name, adapter_name = LORA_OPTIONS[lora_model]

    try:
        print(f"Setting adapter: {adapter_name}")
        pipe.set_adapters(adapter_name)
        # Optional: Add LoRA scale if needed, often done via cross_attention_kwargs
        # Example: cross_attention_kwargs={"scale": lora_scale}
        # Note: RealVisXL Lightning might not need explicit scale adjustments like older models.
        # Using 0.65 as hardcoded before. Keeping it.
        lora_scale = 0.65

        print(f"Generating with prompt: '{positive_prompt}'")
        print(f"Negative prompt: '{effective_negative_prompt}'")
        print(f"Seed: {seed}, W: {width}, H: {height}, Scale: {guidance_scale}, Steps: 20")

        images = pipe(
            prompt=positive_prompt,
            negative_prompt=effective_negative_prompt,
            width=width,
            height=height,
            guidance_scale=guidance_scale,
            num_inference_steps=20, # Lightning models use fewer steps
            num_images_per_prompt=1,
            generator=torch.Generator("cuda").manual_seed(seed), # Ensure reproducibility
            cross_attention_kwargs={"scale": lora_scale}, # Apply LoRA scale if needed
            output_type="pil",
        ).images
        
        image_paths = [save_image(img) for img in images]
        print(f"Generated {len(image_paths)} image(s).")
        return image_paths, seed

    except Exception as e:
        print(f"Error during generation: {e}")
        # Raise a Gradio error to display it in the UI
        import traceback
        traceback.print_exc()
        raise gr.Error(f"Generation failed: {e}")


examples = [
    ["Realism: Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational"],
    ["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"],
    ["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"],
]

css = '''
.gradio-container{max-width: 780px !important; margin: auto;}
h1{text-align:center}
#gallery { min-height: 400px; }
footer { display: none !important; visibility: hidden !important; }
'''

def load_predefined_images():
    predefined_images = []
    asset_dir = "assets"
    if os.path.exists(asset_dir):
        valid_extensions = {".png", ".jpg", ".jpeg", ".webp"}
        try:
            for i in range(1, 10): # Try loading 1.png to 9.png
                 for ext in valid_extensions:
                    img_path = os.path.join(asset_dir, f"{i}{ext}")
                    if os.path.exists(img_path):
                        predefined_images.append(img_path)
                        break # Found image for this number, move to next
        except Exception as e:
             print(f"Error loading predefined images: {e}")
    if not predefined_images:
        print("No predefined images found in assets folder (e.g., assets/1.png, assets/2.jpg).")
    return predefined_images


# --- Gradio UI Definition ---
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
    gr.Markdown(DESCRIPTIONz)

    # Define the output gallery component first
    result_gallery = gr.Gallery(
        label="Generated Images",
        show_label=False,
        elem_id="gallery", # For CSS styling
        columns=1, # Adjust as needed
        height="auto"
    )
    # Define the output seed component
    output_seed = gr.State(value=0) # Use gr.State for non-displayed outputs or values needing persistence

    with gr.Row():
         prompt = gr.Textbox(
            label="Prompt",
            show_label=False,
            max_lines=2,
            placeholder="Enter your prompt here...",
            container=False,
            scale=7 # Give more space to prompt
         )
         run_button = gr.Button("Generate", scale=1, variant="primary")

    with gr.Row():
        model_choice = gr.Dropdown(
            label="LoRA Selection",
            choices=list(LORA_OPTIONS.keys()),
            value="Realism (face/character)πŸ‘¦πŸ»", # Default selection
            scale=3
        )
        style_selection = gr.Radio(
            show_label=False, # Label provided by Row context or Accordion
            container=True,
            interactive=True,
            choices=STYLE_NAMES,
            value=DEFAULT_STYLE_NAME,
            label="Quality Style",
            scale=2
        )


    with gr.Accordion("Advanced options", open=False):
        with gr.Row():
            use_negative_prompt = gr.Checkbox(label="Use Negative Prompt", value=True, scale=1)
            randomize_seed = gr.Checkbox(label="Randomize Seed", value=True, scale=1)
            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0, # Initial value
                visible=True, # Controlled by randomize_seed logic later if needed
                scale=3
            )


        negative_prompt = gr.Textbox(
            label="Negative Prompt",
            lines=2,
            max_lines=4,
            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",
            placeholder="Enter things to avoid...",
            visible=True, # Controlled by use_negative_prompt checkbox
        )

        with gr.Row():
            width = gr.Slider(
                label="Width",
                minimum=512,
                maximum=1536, # Adjusted max for typical SDXL usage
                step=64,      # Step by 64 for common resolutions
                value=1024,
            )
            height = gr.Slider(
                label="Height",
                minimum=512,
                maximum=1536, # Adjusted max
                step=64,      # Step by 64
                value=1024,
            )
            guidance_scale = gr.Slider(
                label="Guidance Scale (CFG)",
                minimum=1.0, # Usually start CFG from 1
                maximum=10.0, # Lightning models often use low CFG
                step=0.1,
                value=3.0,
            )

    # --- Event Listeners ---

    # Toggle negative prompt visibility
    use_negative_prompt.change(
        fn=lambda x: gr.update(visible=x),
        inputs=use_negative_prompt,
        outputs=negative_prompt,
        api_name=False,
    )

    # Toggle seed slider visibility based on randomize checkbox
    # def toggle_seed_visibility(randomize):
    #     return gr.update(interactive=not randomize)
    # randomize_seed.change(
    #     fn=toggle_seed_visibility,
    #     inputs=randomize_seed,
    #     outputs=seed,
    #     api_name=False
    # )

    # --- Image Generation Trigger ---
    inputs = [
        prompt,
        negative_prompt,
        use_negative_prompt,
        seed,
        width,
        height,
        guidance_scale,
        randomize_seed,
        style_selection,
        model_choice,
    ]
    # Define outputs using the created components
    outputs = [
        result_gallery, # The gallery to display images
        output_seed      # The state to hold the used seed
    ]

    # Connect the generate function to the button click and prompt submit
    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=generate,
        inputs=inputs,
        outputs=outputs,
        api_name="run" # Keep API name if needed
    )

    # Update the seed slider display when a new seed is generated and returned via output_seed
    output_seed.change(fn=lambda x: x, inputs=output_seed, outputs=seed, api_name=False)


    # --- Examples ---
    gr.Examples(
        examples=examples,
        inputs=[prompt], # Only prompt needed for examples
        outputs=[result_gallery, output_seed], # Update example outputs as well
        fn=generate, # Function to run when example is clicked
        cache_examples=os.getenv("CACHE_EXAMPLES", "False").lower() == "true" # Cache examples in Spaces
    )

    # --- Predefined Image Gallery (Static) ---
    with gr.Column(): # Use column for better layout control if needed
        gr.Markdown("### Example Gallery (Predefined)")
        try:
            predefined_gallery_images = load_predefined_images()
            if predefined_gallery_images:
                 predefined_gallery = gr.Gallery(
                     label="Predefined Images",
                     value=predefined_gallery_images,
                     columns=3,
                     show_label=False
                 )
            else:
                 gr.Markdown("_(No predefined images found in 'assets' folder)_")
        except Exception as e:
            gr.Markdown(f"_Error loading predefined gallery: {e}_")


# --- Launch the App ---
if __name__ == "__main__":
    demo.queue(max_size=20).launch(debug=True) # Add debug=True for more detailed logs