File size: 13,199 Bytes
176edce
06d2a79
b4de204
 
 
06d2a79
b4de204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb7274c
 
67818cd
 
fb7274c
67818cd
 
b4de204
fb7274c
06d2a79
 
 
67818cd
 
fb7274c
 
 
 
67818cd
 
b4de204
 
67818cd
 
06d2a79
 
fb7274c
67818cd
fb7274c
 
67818cd
fb7274c
7f00770
67818cd
fb7274c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f00770
 
67818cd
7f00770
 
67818cd
7f00770
 
 
fb7274c
 
 
67818cd
fb7274c
 
 
 
 
 
7f00770
 
 
fb7274c
 
 
 
7f00770
fb7274c
7f00770
 
fb7274c
 
 
 
 
7f00770
fb7274c
 
7f00770
 
 
67818cd
fb7274c
 
 
06d2a79
 
fb7274c
67818cd
fb7274c
 
 
 
 
 
67818cd
 
fb7274c
 
06d2a79
 
67818cd
fb7274c
 
 
06d2a79
 
67818cd
fb7274c
 
 
 
 
 
 
 
 
 
 
 
 
 
67818cd
fb7274c
 
 
 
7f00770
 
fb7274c
 
 
 
67818cd
 
fb7274c
 
 
7f00770
 
fb7274c
67818cd
fb7274c
 
 
67818cd
fb7274c
67818cd
fb7274c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f00770
 
67818cd
fb7274c
 
b4de204
 
fb7274c
7f00770
fb7274c
 
 
 
67818cd
fb7274c
06d2a79
 
7f00770
fb7274c
 
67818cd
 
fb7274c
 
06d2a79
 
7f00770
67818cd
fb7274c
 
67818cd
fb7274c
67818cd
 
 
fb7274c
 
06d2a79
 
67818cd
 
fb7274c
 
 
 
06d2a79
 
67818cd
b4de204
fb7274c
 
06d2a79
 
67818cd
fb7274c
 
 
 
 
 
 
 
 
 
7f00770
 
67818cd
7f00770
fb7274c
 
 
 
 
 
06d2a79
 
7f00770
 
fb7274c
 
 
 
7f00770
 
 
b4de204
fb7274c
06d2a79
 
b4de204
fb7274c
 
06d2a79
 
b4de204
fb7274c
 
 
06d2a79
 
fb7274c
7f00770
fb7274c
67818cd
7f00770
 
67818cd
7f00770
 
 
67818cd
 
 
 
fb7274c
 
 
 
 
67818cd
 
fb7274c
 
 
67818cd
fb7274c
 
 
 
 
 
 
 
06d2a79
 
 
fb7274c
 
 
 
b4de204
 
fb7274c
7f00770
67818cd
 
7f00770
fb7274c
7f00770
 
fb7274c
 
7f00770
 
06d2a79
7f00770
67818cd
 
fb7274c
7f00770
 
 
 
fb7274c
7f00770
67818cd
7f00770
 
fb7274c
b4de204
fb7274c
b4de204
67818cd
b4de204
 
 
67818cd
06d2a79
b4de204
fb7274c
 
 
06d2a79
b4de204
fb7274c
 
06d2a79
b4de204
fb7274c
 
7f00770
67818cd
 
 
b4de204
 
 
 
 
 
06d2a79
b4de204
 
 
 
 
18f2392
343fdaf
7f00770
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import os
import gradio as gr
import numpy as np
import random
import spaces
import torch
from diffusers.pipelines.glm_image import GlmImagePipeline
from PIL import Image

dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048

pipe = GlmImagePipeline.from_pretrained(
    "zai-org/GLM-Image",
    torch_dtype=torch.bfloat16,
).to("cuda")


@spaces.GPU(duration=120)
def infer(prompt, input_images=None, seed=42, randomize_seed=False, width=1024, height=1024, 
          num_inference_steps=50, guidance_scale=1.5, progress=gr.Progress(track_tqdm=True)):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    
    width = (width // 32) * 32
    height = (height // 32) * 32
    
    generator = torch.Generator(device="cuda").manual_seed(seed)

    image_list = None
    if input_images is not None and len(input_images) > 0:
        image_list = []
        for item in input_images:
            img = item[0] if isinstance(item, tuple) else item
            if isinstance(img, str):
                img = Image.open(img).convert("RGB")
            elif isinstance(img, Image.Image):
                img = img.convert("RGB")
            image_list.append(img)

    pipe_kwargs = {
        "prompt": prompt,
        "height": height,
        "width": width,
        "num_inference_steps": num_inference_steps,
        "guidance_scale": guidance_scale,
        "generator": generator,
    }

    if image_list is not None:
        pipe_kwargs["image"] = image_list

    image = pipe(**pipe_kwargs).images[0]
    
    return image, seed


def update_dimensions_from_image(image_list):
    if image_list is None or len(image_list) == 0:
        return 1024, 1024
    
    item = image_list[0]
    img = item[0] if isinstance(item, tuple) else item
    
    if isinstance(img, str):
        img = Image.open(img)
    
    img_width, img_height = img.size
    aspect_ratio = img_width / img_height
    
    if aspect_ratio >= 1:
        new_width = 1024
        new_height = int(1024 / aspect_ratio)
    else:
        new_height = 1024
        new_width = int(1024 * aspect_ratio)
    
    new_width = round(new_width / 32) * 32
    new_height = round(new_height / 32) * 32
    
    new_width = max(256, min(MAX_IMAGE_SIZE, new_width))
    new_height = max(256, min(MAX_IMAGE_SIZE, new_height))
    
    return new_width, new_height

css = """
/* POP ART Style */
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@700&display=swap');

* {
    font-family: 'Comic Neue', cursive !important;
}

body, .gradio-container {
    background: #ffeb3b !important;
    min-height: 100vh;
}

.gradio-container {
    background: 
        radial-gradient(circle at 10px 10px, #ff1744 4px, transparent 4px),
        radial-gradient(circle at 30px 30px, #ff1744 3px, transparent 3px),
        linear-gradient(135deg, #ffeb3b 0%, #fff176 100%) !important;
    background-size: 40px 40px, 40px 40px, 100% 100% !important;
}

#col-container {
    margin: 0 auto;
    max-width: 1150px;
    padding: 25px 20px;
}

/* Header - Comic Speech Bubble */
.header-box {
    background: #ffffff;
    border: 5px solid #000000;
    border-radius: 30px;
    padding: 25px 30px;
    text-align: center;
    margin-bottom: 25px;
    box-shadow: 8px 8px 0 #000000;
    position: relative;
}

.header-box::after {
    content: "";
    position: absolute;
    bottom: -25px;
    left: 50px;
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-top-color: #000000;
    border-bottom: 0;
}

.header-box::before {
    content: "";
    position: absolute;
    bottom: -17px;
    left: 53px;
    width: 0;
    height: 0;
    border: 16px solid transparent;
    border-top-color: #ffffff;
    border-bottom: 0;
    z-index: 1;
}

.badge-row {
    display: flex;
    justify-content: center;
    margin-bottom: 12px;
}

.title-text {
    font-family: 'Bangers', cursive !important;
    font-size: 3rem !important;
    color: #ff1744 !important;
    margin: 8px 0 !important;
    text-shadow: 
        3px 3px 0 #000000,
        -1px -1px 0 #000000,
        1px -1px 0 #000000,
        -1px 1px 0 #000000;
    letter-spacing: 3px;
}

.subtitle-text {
    font-family: 'Comic Neue', cursive !important;
    color: #1565c0 !important;
    font-size: 1.1rem !important;
    font-weight: 700 !important;
    margin: 0 !important;
    text-transform: uppercase;
}

/* Cards - Bold Comic Style */
.pop-card {
    background: #ffffff !important;
    border: 4px solid #000000 !important;
    border-radius: 20px !important;
    padding: 20px !important;
    box-shadow: 6px 6px 0 #000000 !important;
    position: relative;
}

.gr-panel, .gr-box, .gr-form, .block, .form {
    background: #ffffff !important;
    border: 3px solid #000000 !important;
    border-radius: 15px !important;
    box-shadow: 4px 4px 0 #000000 !important;
}

/* Prompt Input - Speech Bubble */
.prompt-box textarea {
    background: #ffffff !important;
    border: 4px solid #000000 !important;
    border-radius: 20px !important;
    color: #000000 !important;
    font-size: 1.1rem !important;
    font-weight: 700 !important;
    padding: 18px !important;
    min-height: 90px !important;
    transition: all 0.2s ease !important;
    box-shadow: 4px 4px 0 #000000 !important;
}

.prompt-box textarea:focus {
    border-color: #ff1744 !important;
    box-shadow: 6px 6px 0 #ff1744 !important;
    transform: translate(-2px, -2px);
}

.prompt-box textarea::placeholder {
    color: #757575 !important;
    font-weight: 600 !important;
}

/* Generate Button - POW! Style */
.pow-btn {
    background: linear-gradient(180deg, #ff1744 0%, #d50000 100%) !important;
    border: 4px solid #000000 !important;
    border-radius: 15px !important;
    color: #ffffff !important;
    font-family: 'Bangers', cursive !important;
    font-weight: 400 !important;
    font-size: 1.5rem !important;
    letter-spacing: 2px;
    padding: 16px 35px !important;
    transition: all 0.15s ease !important;
    box-shadow: 5px 5px 0 #000000 !important;
    text-shadow: 2px 2px 0 #000000;
    text-transform: uppercase;
}

.pow-btn:hover {
    transform: translate(-3px, -3px) !important;
    box-shadow: 8px 8px 0 #000000 !important;
    background: linear-gradient(180deg, #ff5252 0%, #ff1744 100%) !important;
}

.pow-btn:active {
    transform: translate(2px, 2px) !important;
    box-shadow: 2px 2px 0 #000000 !important;
}

/* Result Image - Comic Panel */
.result-box {
    background: #ffffff !important;
    border: 5px solid #000000 !important;
    border-radius: 20px !important;
    padding: 15px !important;
    box-shadow: 8px 8px 0 #000000 !important;
    min-height: 450px !important;
    position: relative;
}

.result-box::before {
    content: "★ OUTPUT ★";
    position: absolute;
    top: -15px;
    left: 20px;
    background: #ffeb3b;
    border: 3px solid #000000;
    padding: 3px 15px;
    font-family: 'Bangers', cursive !important;
    font-size: 1rem;
    color: #000000;
    z-index: 10;
}

.result-box img {
    border-radius: 12px !important;
    border: 3px solid #000000 !important;
}

/* Accordion - Comic Panels */
.gr-accordion {
    background: #e3f2fd !important;
    border: 3px solid #000000 !important;
    border-radius: 15px !important;
    margin-top: 20px !important;
    overflow: hidden !important;
    box-shadow: 4px 4px 0 #000000 !important;
}

.gr-accordion-header {
    background: #2196f3 !important;
    color: #ffffff !important;
    font-weight: 700 !important;
    padding: 12px 18px !important;
    border-bottom: 3px solid #000000 !important;
    text-transform: uppercase;
}

/* Gallery */
.gallery-box {
    background: #fff9c4 !important;
    border-radius: 12px !important;
    padding: 12px !important;
    border: 3px dashed #000000 !important;
}

.gallery-box img {
    border-radius: 10px !important;
    border: 3px solid #000000 !important;
}

/* Labels */
label, .label-wrap, span {
    color: #000000 !important;
    font-weight: 700 !important;
    text-transform: uppercase;
    font-size: 0.9rem !important;
}

/* Sliders */
input[type="range"] {
    accent-color: #ff1744 !important;
    height: 8px !important;
}

input[type="range"]::-webkit-slider-track {
    background: #000000 !important;
    border-radius: 5px !important;
    height: 8px !important;
}

input[type="range"]::-webkit-slider-thumb {
    background: #ffeb3b !important;
    border: 3px solid #000000 !important;
    width: 20px !important;
    height: 20px !important;
}

/* Number Input */
input[type="number"] {
    background: #ffffff !important;
    border: 3px solid #000000 !important;
    border-radius: 10px !important;
    color: #000000 !important;
    font-weight: 700 !important;
    box-shadow: 3px 3px 0 #000000 !important;
}

/* Checkbox */
input[type="checkbox"] {
    accent-color: #ff1744 !important;
    width: 20px !important;
    height: 20px !important;
    border: 3px solid #000000 !important;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 12px;
}

::-webkit-scrollbar-track {
    background: #ffeb3b;
    border: 2px solid #000000;
}

::-webkit-scrollbar-thumb {
    background: #ff1744;
    border: 2px solid #000000;
    border-radius: 0;
}

/* Layout */
.main-row {
    gap: 30px !important;
    align-items: flex-start !important;
}

.input-col {
    flex: 1 !important;
}

.output-col {
    flex: 1.1 !important;
}

/* Halftone Pattern Overlay */
.halftone-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: radial-gradient(#000000 1px, transparent 1px);
    background-size: 4px 4px;
    opacity: 0.03;
    pointer-events: none;
    z-index: 9999;
}

/* Action Words */
.action-word {
    font-family: 'Bangers', cursive !important;
    color: #ff1744;
    text-shadow: 2px 2px 0 #000000;
}
"""

with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
    
    # Halftone overlay
    gr.HTML('<div class="halftone-overlay"></div>')
    
    with gr.Column(elem_id="col-container"):
        # Header - Comic Speech Bubble
        gr.HTML("""
            <div class="header-box">
                <div class="badge-row">
                    <a href="https://www.humangen.ai" target="_blank">
                        <img src="https://img.shields.io/static/v1?label=Free%20AI%20HUB&message=humangen.ai&color=%23ff1744&labelColor=%23000000&logo=huggingface&logoColor=white&style=for-the-badge" alt="badge">
                    </a>
                </div>
                <h1 class="title-text">💥 GLM-IMAGE GENERATOR</h1>
                <p class="subtitle-text">⚡ 9B hybrid AI model for text-accurate image generation ⚡</p>
            </div>
        """)
        
        with gr.Row(elem_classes="main-row"):
            # Left - Input
            with gr.Column(elem_classes="input-col"):
                with gr.Group(elem_classes="pop-card"):
                    prompt = gr.Text(
                        label="Prompt",
                        show_label=False,
                        max_lines=4,
                        placeholder="💬 Type your image idea here...",
                        container=False,
                        elem_classes="prompt-box"
                    )
                    
                    run_button = gr.Button("⚡ POW! GENERATE ⚡", variant="primary", elem_classes="pow-btn")
                
                with gr.Accordion("📸 INPUT IMAGES", open=False):
                    input_images = gr.Gallery(
                        label="Upload images",
                        type="pil",
                        columns=3,
                        rows=1,
                        elem_classes="gallery-box"
                    )
                
                with gr.Accordion("🔧 SETTINGS", open=False):
                    seed = gr.Slider(label="SEED", minimum=0, maximum=MAX_SEED, step=1, value=42)
                    randomize_seed = gr.Checkbox(label="RANDOMIZE", value=True)
                    
                    with gr.Row():
                        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)
                    
                    with gr.Row():
                        num_inference_steps = gr.Slider(label="STEPS", minimum=1, maximum=100, step=1, value=50)
                        guidance_scale = gr.Slider(label="GUIDANCE", minimum=0.0, maximum=10.0, step=0.1, value=1.5)
            
            # Right - Output
            with gr.Column(elem_classes="output-col"):
                result = gr.Image(label="Result", show_label=False, elem_classes="result-box")

    input_images.upload(
        fn=update_dimensions_from_image,
        inputs=[input_images],
        outputs=[width, height]
    )

    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[prompt, input_images, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
        outputs=[result, seed]
    )

demo.launch()