File size: 5,955 Bytes
681078d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random
import uuid
import gradio as gr
import numpy as np
from PIL import Image
import torch
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
from typing import Tuple

# CSS for Gradio Interface
css = '''
.gradio-container{max-width: 575px !important}
h1{text-align:center}
footer {
    visibility: hidden
}
'''

DESCRIPTION = """
## Text-to-Image Generator 🚀
Create stunning images from text prompts using Stable Diffusion XL. Explore high-quality styles and customizable options.
"""

# Example Prompts
examples = [
    "A beautiful sunset over the ocean, ultra-realistic, high resolution",
    "A futuristic cityscape with flying cars, cyberpunk theme, vibrant colors",
    "A cozy cabin in the woods during winter, detailed and realistic",
    "A magical forest with glowing plants and creatures, fantasy art",
]

# Model Configurations
MODEL_OPTIONS = {
    "LIGHTNING V5.0": "SG161222/RealVisXL_V5.0_Lightning",
    "LIGHTNING V4.0": "SG161222/RealVisXL_V4.0_Lightning",
}

# Define Styles
style_list = [
    {
        "name": "Ultra HD",
        "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": "4K Realistic",
        "prompt": "realistic 4K image of {prompt}. sharp, detailed, vibrant colors, photorealistic",
        "negative_prompt": "cartoonish, blurry, low resolution",
    },
    {
        "name": "Minimal Style",
        "prompt": "{prompt}, clean, minimalistic",
        "negative_prompt": "",
    },
]

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

# Define Global Variables
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
MAX_IMAGE_SIZE = 4096
MAX_SEED = np.iinfo(np.int32).max

# Load Model Function
def load_and_prepare_model(model_id):
    pipe = StableDiffusionXLPipeline.from_pretrained(
        model_id,
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    ).to(device)
    pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
    return pipe

# Load Models
models = {key: load_and_prepare_model(value) for key, value in MODEL_OPTIONS.items()}

# Generate Function
def generate_image(
    model_choice: str,
    prompt: str,
    negative_prompt: str,
    style_name: str,
    width: int,
    height: int,
    guidance_scale: float,
    num_steps: int,
    num_images: int,
    randomize_seed: bool,
    seed: int,
):
    # Apply Style
    positive_style, negative_style = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
    styled_prompt = positive_style.replace("{prompt}", prompt)
    styled_negative_prompt = negative_style + (negative_prompt if negative_prompt else "")

    # Randomize Seed if Enabled
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device=device).manual_seed(seed)

    # Generate Images
    pipe = models[model_choice]
    images = pipe(
        prompt=[styled_prompt] * num_images,
        negative_prompt=[styled_negative_prompt] * num_images,
        width=width,
        height=height,
        guidance_scale=guidance_scale,
        num_inference_steps=num_steps,
        generator=generator,
        output_type="pil",
    ).images

    # Save and Return Images
    image_paths = []
    for img in images:
        unique_name = f"{uuid.uuid4()}.png"
        img.save(unique_name)
        image_paths.append(unique_name)

    return image_paths, seed

# Gradio Interface
with gr.Blocks(css=css) as demo:
    gr.Markdown(DESCRIPTION)

    with gr.Row():
        model_choice = gr.Dropdown(
            label="Select Model",
            choices=list(MODEL_OPTIONS.keys()),
            value="LIGHTNING V5.0",
        )

    prompt = gr.Textbox(
        label="Prompt",
        placeholder="Enter your creative prompt here...",
    )
    
    negative_prompt = gr.Textbox(
        label="Negative Prompt",
        placeholder="Optional: Add details you want to avoid...",
        value="blurry, deformed, low-quality, cartoonish",
    )
    
    style_name = gr.Radio(
        label="Style",
        choices=list(styles.keys()),
        value=DEFAULT_STYLE_NAME,
    )

    with gr.Accordion("Advanced Options", open=False):
        width = gr.Slider(label="Width", minimum=512, maximum=2048, step=8, value=1024)
        height = gr.Slider(label="Height", minimum=512, maximum=2048, step=8, value=1024)
        guidance_scale = gr.Slider(
            label="Guidance Scale",
            minimum=1,
            maximum=20,
            step=0.5,
            value=7.5,
        )
        num_steps = gr.Slider(
            label="Steps",
            minimum=1,
            maximum=50,
            step=1,
            value=25,
        )
        num_images = gr.Slider(
            label="Number of Images",
            minimum=1,
            maximum=5,
            step=1,
            value=1,
        )
        randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
        seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)

    with gr.Row():
        run_button = gr.Button("Generate Images")
        result_gallery = gr.Gallery(label="Generated Images", show_label=False)

    run_button.click(
        generate_image,
        inputs=[
            model_choice,
            prompt,
            negative_prompt,
            style_name,
            width,
            height,
            guidance_scale,
            num_steps,
            num_images,
            randomize_seed,
            seed,
        ],
        outputs=[result_gallery, seed],
    )

    gr.Examples(
        examples=examples,
        inputs=prompt,
    )

if __name__ == "__main__":
    demo.queue(max_size=50).launch()