File size: 7,209 Bytes
4e09337
92717ee
a1cb500
4ebc629
4e09337
 
 
 
92717ee
a1cb500
 
 
 
 
 
 
 
 
 
 
 
 
d2a27fd
4e09337
 
92717ee
d2a27fd
4e09337
92717ee
 
4e09337
 
d2a27fd
92717ee
 
 
a1cb500
4e09337
 
 
a1cb500
4e09337
 
a1cb500
 
 
 
 
4e09337
 
 
 
 
a1cb500
4e09337
 
 
 
a1cb500
4e09337
a1cb500
 
 
 
 
 
 
4e09337
 
4ebc629
4e09337
4ebc629
 
4e09337
4ebc629
a1cb500
4ebc629
 
 
 
4e09337
4ebc629
a1cb500
4ebc629
 
4e09337
4ebc629
4e09337
a1cb500
4ebc629
 
4e09337
4ebc629
a1cb500
4ebc629
 
4e09337
4ebc629
 
d2a27fd
4e09337
92717ee
 
 
 
 
 
 
 
 
4e09337
a1cb500
d2a27fd
4e09337
a1cb500
 
4e09337
 
92717ee
a1cb500
4e09337
 
 
d2a27fd
4e09337
92717ee
 
 
 
 
 
 
 
4e09337
 
92717ee
4e09337
 
 
4ebc629
 
 
 
 
 
 
4e09337
4ebc629
a1cb500
 
 
 
 
4ebc629
a1cb500
92717ee
4e09337
92717ee
 
4e09337
d2a27fd
4e09337
92717ee
4e09337
 
92717ee
d2a27fd
4e09337
92717ee
4e09337
 
d2a27fd
92717ee
4e09337
 
 
 
d2a27fd
92717ee
d2a27fd
4e09337
 
 
 
d2a27fd
92717ee
 
 
 
4e09337
 
 
92717ee
4e09337
 
 
92717ee
4e09337
 
 
 
92717ee
4e09337
 
92717ee
4e09337
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
# ---- Flags ----
run_api = False
SSD_1B = False  # True = use SSD-1B + LCM LoRA, False = SDXL Base + LCM (default)

# ---- Standard imports ----
import os
import subprocess
import numpy as np

# Optional: clear_output is nice in notebooks; ignore if not available
try:
    from IPython.display import clear_output  # noqa: F401
except Exception:
    def clear_output():  # no-op outside notebooks
        pass

# ---- Tame NVML noise in containers without GPU drivers (optional) ----
os.environ.setdefault("DEEPSPEED_DISABLE_NVML", "1")
import warnings
warnings.filterwarnings("ignore", message="Can't initialize NVML")

# ---- App imports (expect deps from requirements.txt already installed) ----
import torch
import gradio as gr
from PIL import Image
from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler

# ---- Config / constants ----
current_dir = os.getcwd()
cache_path = os.path.join(current_dir, "cache")
os.makedirs(cache_path, exist_ok=True)

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
SECRET_TOKEN = os.getenv("SECRET_TOKEN", "default_secret")

# ---- GPU visibility / info (for logs only) ----
def print_nvidia_smi():
    try:
        proc = subprocess.run(["nvidia-smi"], capture_output=True, text=True)
        if proc.returncode == 0 and proc.stdout.strip():
            print(proc.stdout)
        else:
            # Show stderr when present to help debugging; not used for logic
            if proc.stderr:
                print(proc.stderr)
            else:
                print("nvidia-smi not available or returned no output.")
    except FileNotFoundError:
        print("nvidia-smi not found on PATH.")

print_nvidia_smi()

# ---- Device + dtype selection (robust) ----
is_gpu = torch.cuda.is_available()
print(f"CUDA available: {is_gpu}")

device = torch.device("cuda") if is_gpu else torch.device("cpu")
dtype = torch.float16 if is_gpu else torch.float32

# ---- Helpers to only pass 'variant' when needed (Diffusers <=0.23 friendly) ----
def _add_variant(kwargs: dict) -> dict:
    """Only include 'variant' when running on GPU."""
    if is_gpu:
        kwargs = dict(kwargs)  # shallow copy
        kwargs["variant"] = "fp16"
    return kwargs

# ---- Pipeline setup ----
if not SSD_1B:
    # SDXL base + LCM UNet
    unet = UNet2DConditionModel.from_pretrained(
        "latent-consistency/lcm-sdxl",
        torch_dtype=dtype,
        cache_dir=cache_path,
        **_add_variant({})
    )
    pipe = DiffusionPipeline.from_pretrained(
        "stabilityai/stable-diffusion-xl-base-1.0",
        unet=unet,
        torch_dtype=dtype,
        cache_dir=cache_path,
        **_add_variant({})
    )
    pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
    pipe.to(device)
else:
    # SSD-1B + LCM LoRA
    from diffusers import AutoPipelineForText2Image
    pipe = AutoPipelineForText2Image.from_pretrained(
        "segmind/SSD-1B",
        torch_dtype=dtype,
        cache_dir=cache_path,
        **_add_variant({})
    )
    pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
    pipe.to(device)
    pipe.load_lora_weights("latent-consistency/lcm-lora-ssd-1b")
    pipe.fuse_lora()

# ---- Core generate function ----
def generate(
    prompt: str,
    negative_prompt: str = "",
    seed: int = 0,
    width: int = 1024,
    height: int = 1024,
    guidance_scale: float = 0.0,
    num_inference_steps: int = 4,
    secret_token: str = "",
) -> Image.Image:
    # Token gate
    if secret_token != SECRET_TOKEN:
        raise gr.Error("Invalid secret token. Set SECRET_TOKEN on the server or pass the correct token.")

    # Clamp sizes (avoid OOM on CPU)
    width = int(np.clip(width, 256, MAX_IMAGE_SIZE))
    height = int(np.clip(height, 256, MAX_IMAGE_SIZE))

    # Deterministic generator on the active device
    generator = torch.Generator(device=device)
    if seed is not None:
        generator = generator.manual_seed(int(seed))

    out = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        width=width,
        height=height,
        guidance_scale=guidance_scale,
        num_inference_steps=num_inference_steps,
        generator=generator,
        output_type="pil",
    )
    return out.images[0]

# ---- Optional notebook helper ----
def generate_image(prompt="A scenic watercolor landscape, mountains at dawn"):
    img = generate(
        prompt=prompt,
        negative_prompt="",
        seed=0,
        width=1024,
        height=1024,
        guidance_scale=0.0,
        num_inference_steps=4,
        secret_token=SECRET_TOKEN,
    )
    try:
        from IPython.display import display
        display(img)
    except Exception:
        pass  # Non-notebook environment

# ---- UI (Gradio 3.39.0 components) ----
if not run_api:
    secret_token = gr.Textbox(
        label="Secret Token",
        placeholder="Enter your secret token",
        type="password",
    )
    prompt = gr.Textbox(
        label="Prompt",
        show_label=True,
        max_lines=2,
        placeholder="Enter your prompt",
    )
    negative_prompt = gr.Textbox(
        label="Negative prompt",
        max_lines=2,
        placeholder="Enter a negative prompt (optional)",
    )
    seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
    width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
    height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
    guidance_scale = gr.Slider(label="Guidance scale", minimum=0, maximum=2, step=0.1, value=0.0)
    num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=8, step=1, value=4)

    iface = gr.Interface(
        fn=generate,
        inputs=[prompt, negative_prompt, seed, width, height, guidance_scale, num_inference_steps, secret_token],
        outputs=gr.Image(label="Result"),
        title="Image Generator (LCM)",
        description="Fast SDXL/SSD-1B image generation with LCM. Uses CPU if CUDA is unavailable.",
    )
    iface.launch()

if run_api:
    with gr.Blocks() as demo:
        gr.Markdown(
            "### REST API for LCM Text-to-Image\n"
            "Use the `/run` endpoint programmatically with your secret."
        )
        secret_token = gr.Textbox(label="Secret Token", type="password")
        prompt = gr.Textbox(label="Prompt")
        negative_prompt = gr.Textbox(label="Negative prompt")
        seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
        width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
        height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
        guidance_scale = gr.Slider(label="Guidance scale", minimum=0, maximum=2, step=0.1, value=0.0)
        num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=8, step=1, value=4)

        inputs = [prompt, negative_prompt, seed, width, height, guidance_scale, num_inference_steps, secret_token]
        prompt.submit(fn=generate, inputs=inputs, outputs=gr.Image(), api_name="run")

    demo.queue(max_size=32).launch(debug=False)