File size: 23,522 Bytes
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a36cc0
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f071d5c
660c6ca
 
 
 
029730c
660c6ca
 
 
 
 
 
 
 
 
 
 
 
8391927
660c6ca
 
8391927
 
 
660c6ca
8391927
660c6ca
 
 
 
8391927
 
 
 
 
 
 
660c6ca
 
 
 
 
 
 
 
 
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import spaces
import gradio as gr
import os
import random
import re
import numpy as np
import torch
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer

# Import the custom tiling pipeline and its components
from pipeline_z_image_mod import ZImageMoDTilingPipeline
from diffusers import (
    AutoencoderKL,
    FlowMatchEulerDiscreteScheduler,
    GGUFQuantizationConfig,
)
from diffusers.models import ZImageTransformer2DModel
from huggingface_hub import hf_hub_download

# Import the safety checker function
try:
    from prompt_check import is_unsafe_prompt

    UNSAFE_CHECK_AVAILABLE = True
except ImportError:
    print("Warning: 'prompt_check.py' not found. NSFW prompt check will be disabled.")
    UNSAFE_CHECK_AVAILABLE = False

    def is_unsafe_prompt(*args, **kwargs):
        return False


# 1. Environment Variables for Model Paths
# Base model for VAE, text_encoder, tokenizer
BASE_MODEL_ID = os.getenv("BASE_MODEL_ID", "Tongyi-MAI/Z-Image-Turbo")
# GGUF model for the transformer
GGUF_REPO_ID = os.getenv("GGUF_REPO_ID", "jayn7/Z-Image-Turbo-GGUF")
GGUF_FILENAME = os.getenv("GGUF_FILENAME", "z_image_turbo-Q4_K_M.gguf")
GGUF_LOCAL_DIR = os.getenv(
    "GGUF_LOCAL_DIR", None
)  # Set this path for local use, e.g., "F:\\models\\Z-Image-Turbo"
USE_SPACES_ENV = os.getenv("USE_SPACES", "false").lower()
USE_SPACES = USE_SPACES_ENV not in ("false", "0", "no", "none")

# System prompt for the safety checker
UNSAFE_PROMPT_CHECK = os.getenv("UNSAFE_PROMPT_CHECK")
MAX_SEED = np.iinfo(np.int32).max

# 2. Load Models (GGUF + Standard Components)
print("--- Loading Models ---")
device = "cuda" if torch.cuda.is_available() else "cpu"

print("Loading VAE...")
if USE_SPACES:
    vae = AutoencoderKL.from_pretrained(
        BASE_MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16
    ).to(device)
else:
    vae = AutoencoderKL.from_pretrained(
        BASE_MODEL_ID, subfolder="vae", torch_dtype=torch.bfloat16
    )

print("Loading Text Encoder and Tokenizer...")
if USE_SPACES:
    text_encoder = AutoModelForCausalLM.from_pretrained(
        BASE_MODEL_ID, subfolder="text_encoder", dtype=torch.bfloat16
    ).to(device)
    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, subfolder="tokenizer")
else:
    text_encoder = AutoModelForCausalLM.from_pretrained(
        BASE_MODEL_ID, subfolder="text_encoder", dtype=torch.bfloat16
    )
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, subfolder="tokenizer")

print(f"Loading Transformer from GGUF: {GGUF_REPO_ID}/{GGUF_FILENAME}...")
transformer_path = (
    hf_hub_download(
        GGUF_REPO_ID,
        GGUF_FILENAME,
        local_dir=GGUF_LOCAL_DIR,
        local_dir_use_symlinks=False,
    )
    if GGUF_LOCAL_DIR
    else hf_hub_download(GGUF_REPO_ID, GGUF_FILENAME)
)

if USE_SPACES:
    transformer = ZImageTransformer2DModel.from_single_file(
        transformer_path,
        quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
        dtype=torch.bfloat16,
    ).to(device)
else:
    transformer = ZImageTransformer2DModel.from_single_file(
        transformer_path,
        quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
        dtype=torch.bfloat16,
    )


# 3. Assemble the Tiling Pipeline
print("\nAssembling the ZImageMoDTilingPipeline...")
scheduler = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000, shift=3.0)
if USE_SPACES:
    pipe = ZImageMoDTilingPipeline(
        vae=vae,
        text_encoder=text_encoder,
        tokenizer=tokenizer,  
        scheduler=scheduler,      
        transformer=transformer,
    ).to(device)
else:
    pipe = ZImageMoDTilingPipeline(
        vae=vae,
        text_encoder=text_encoder,
        tokenizer=tokenizer,        
        scheduler=scheduler,
        transformer=transformer,
    )

    print("Enabling model CPU offload...")
    pipe.enable_model_cpu_offload()

# Load Translation Models
print("Loading translation models...")
try:
    ko_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
    zh_en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
except Exception as e:
    ko_en_translator, zh_en_translator = None, None
    print(f"Warning: Could not load translation models: {e}")

print("Pipeline loaded and ready.")


# Helper Functions
def translate_prompt(text: str, language: str) -> str:
    """Translates text to English if the selected language is not English."""
    if language == "English" or not text.strip():
        return text
    translated = text
    if (
        language == "Korean"
        and ko_en_translator
        and any("\uac00" <= char <= "\ud7a3" for char in text)
    ):
        translated = ko_en_translator(text)[0]["translation_text"]
    elif (
        language == "Chinese"
        and zh_en_translator
        and any("\u4e00" <= char <= "\u9fff" for char in text)
    ):
        translated = zh_en_translator(text)[0]["translation_text"]
    return translated


def create_hdr_effect(image, hdr_strength):
    if hdr_strength == 0:
        return image
    from PIL import ImageEnhance, Image

    if isinstance(image, Image.Image):
        image = np.array(image)
    from scipy.ndimage import gaussian_filter

    blurred = gaussian_filter(image, sigma=5)
    sharpened = np.clip(image + hdr_strength * (image - blurred), 0, 255).astype(
        np.uint8
    )
    pil_img = Image.fromarray(sharpened)
    converter = ImageEnhance.Color(pil_img)
    return converter.enhance(1 + hdr_strength)


@spaces.GPU(duration=120)
def generate_z_image_panorama(
    left_prompt,
    center_prompt,
    right_prompt,
    left_gs,
    center_gs,
    right_gs,
    overlap_pixels,
    steps,
    shift,
    generation_seed,
    tile_weighting_method,
    prompt_language,
    target_height,
    target_width,
    hdr,
    randomize_seed,
    progress=gr.Progress(track_tqdm=True),
):
    """
    Generate a panoramic image using the Z-Image Turbo model with tiling and composition.
    Args:
        left_prompt (str): Text prompt for the left section of the panorama.
        center_prompt (str): Text prompt for the center section of the panorama.
        right_prompt (str): Text prompt for the right section of the panorama.
        left_gs (float): Guidance scale for the left tile.
        center_gs (float): Guidance scale for the center tile.
        right_gs (float): Guidance scale for the right tile.
        overlap_pixels (int): Number of pixels to overlap between tiles.
        steps (int): Number of inference steps for generation.
        shift (float): Time Shift.
        generation_seed (int): Random seed for reproducibility.
        tile_weighting_method (str): Method for weighting overlapping tile regions.
        prompt_language (str): Language code for prompt translation.
        target_height (int): Height of the generated panorama in pixels.
        target_width (int): Width of the generated panorama in pixels.
        hdr (float): HDR effect intensity.
        randomize_seed (boolean): Not used.
        progress (gr.Progress): Gradio progress tracker.
    Returns:
        PIL.Image: The generated panoramic image with optional HDR effect applied.
    """
    if not left_prompt or not center_prompt or not right_prompt:
        gr.Info("⚡️ Prompts must be provided!")
        return gr.skip()
    # Safety Check    
    prompts_to_check = [left_prompt, center_prompt, right_prompt]
    for p in prompts_to_check:
        if UNSAFE_CHECK_AVAILABLE and is_unsafe_prompt(
            pipe.text_encoder,
            device,
            pipe.tokenizer,
            system_prompt=UNSAFE_PROMPT_CHECK,
            user_prompt=p,
        ):
            raise gr.Error(
                f"Unsafe prompt detected. Please modify your prompt and try again."
            )

    generator = torch.Generator("cuda").manual_seed(generation_seed)
    pipe.scheduler = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000, shift=shift)
    
    final_height, final_width = int(target_height), int(target_width)

    translated_left = translate_prompt(left_prompt, prompt_language)
    translated_center = translate_prompt(center_prompt, prompt_language)
    translated_right = translate_prompt(right_prompt, prompt_language)

    image = pipe(
        prompt=[[translated_left, translated_center, translated_right]],
        height=final_height,
        width=final_width,
        num_inference_steps=steps,
        guidance_scale_tiles=[[left_gs, center_gs, right_gs]],
        tile_overlap=overlap_pixels,
        tile_weighting_method=tile_weighting_method,
        generator=generator,
    ).images[0]

    return create_hdr_effect(image, hdr)


def calculate_tile_size(target_height, target_width, overlap_pixels):
    """
    Calculate tile dimensions for panoramic image generation.

    Args:
        target_height (int): The target height of the final panoramic image in pixels.
        target_width (int): The target width of the final panoramic image in pixels.
        overlap_pixels (int): The number of overlapping pixels between adjacent tiles.

    Returns:
        tuple: A tuple of 2 gr.update objects containing:
            - final_height: Final panorama height after tiling
            - final_width: Final panorama width after tiling
    """
    num_cols = 3
    num_rows = 1
    tile_width = (target_width + (num_cols - 1) * overlap_pixels) // num_cols
    tile_height = (target_height + (num_rows - 1) * overlap_pixels) // num_rows
    tile_width -= tile_width % 16
    tile_height -= tile_height % 16
    final_width = tile_width * num_cols - (num_cols - 1) * overlap_pixels
    final_height = tile_height * num_rows - (num_rows - 1) * overlap_pixels
    return (
        gr.update(value=final_height),
        gr.update(value=final_width),
    )


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


def run_for_examples(
    left_prompt,
    center_prompt,
    right_prompt,
    left_gs,
    center_gs,
    right_gs,
    overlap_pixels,
    steps,
    shift,
    generation_seed,
    tile_weighting_method,
    prompt_language,
    target_height,
    target_width,
    hdr,
    randomize_seed
):
    return generate_z_image_panorama(
        left_prompt,
        center_prompt,
        right_prompt,
        left_gs,
        center_gs,
        right_gs,
        overlap_pixels,
        steps,
        shift,
        generation_seed,
        tile_weighting_method,
        prompt_language,
        target_height,
        target_width,
        hdr,
        randomize_seed
    )


def clear_result():
    return gr.update(value=None)


def update_dimensions_from_preset(resolution_preset):
    """Updates the width and height sliders based on a preset string."""
    match = re.search(r"(\d+)\s*[x]\s*(\d+)", resolution_preset)
    if match:
        width, height = int(match.group(1)), int(match.group(2))
        return gr.update(value=width), gr.update(value=height)
    return gr.update(), gr.update()


# UI Layout
theme = gr.themes.Default(
    primary_hue="red", secondary_hue="orange", neutral_hue="gray"
).set(
    body_background_fill="*neutral_100",
    body_background_fill_dark="*neutral_900",
    body_text_color="*neutral_900",
    body_text_color_dark="*neutral_100",
    panel_background_fill="*neutral_800",
    panel_background_fill_dark="*neutral_900",
    input_background_fill="white",
    input_background_fill_dark="*neutral_800",
    button_primary_background_fill="*primary_500",
    button_primary_background_fill_dark="*primary_700",
    button_primary_text_color="white",
    button_primary_text_color_dark="white",
    button_secondary_background_fill="*secondary_500",
    button_secondary_background_fill_dark="*secondary_700",
    button_secondary_text_color="white",
    button_secondary_text_color_dark="white",
)

css_code = ""

try:
    with open("./style.css", "r", encoding="utf-8") as f:
        css_code += f.read() + "\n"
except FileNotFoundError:
    pass

title = """<h1 align="center">Z Image Turbo - Panorama 🏞️✨</h1>
           <div style="text-align: center;">
                <span>Create panoramic compositions with Z-Image-Turbo.</span>
           </div>
           """
# Adapted panoramic resolutions
PANORAMIC_RESOLUTIONS = [
    "2048x1024 (2:1)",
    "2560x1024 (2.5:1)",
    "3072x1024 (3:1)",
    "2304x896 ( ~2.5:1)",
    "2880x1152 (~2.5:1)",
    "3840x1536 (2.5:1)",
]

with gr.Blocks(theme=theme, css=css_code, title="Panorama Z-Image") as app:
    gr.Markdown(title)
    with gr.Row():
        with gr.Column(scale=7):
            generate_button = gr.Button("Generate", variant="primary")
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("### Left Region")
                    left_prompt = gr.Textbox(lines=4, label="Prompt (Left)")
                    left_gs = gr.Slider(
                        minimum=0.0,
                        maximum=10.0,
                        value=0.0,
                        step=0.1,
                        label="Left Guidance",
                        visible=False,
                    )
                with gr.Column(scale=1):
                    gr.Markdown("### Center Region")
                    center_prompt = gr.Textbox(lines=4, label="Prompt (Center)")
                    center_gs = gr.Slider(
                        minimum=0.0,
                        maximum=10.0,
                        value=0.0,
                        step=0.1,
                        label="Center Guidance",
                        visible=False,
                    )
                with gr.Column(scale=1):
                    gr.Markdown("### Right Region")
                    right_prompt = gr.Textbox(lines=4, label="Prompt (Right)")
                    right_gs = gr.Slider(
                        minimum=0.0,
                        maximum=10.0,
                        value=0.0,
                        step=0.1,
                        label="Right Guidance",
                        visible=False,
                    )
            with gr.Row():
                result = gr.Image(
                    label="Generated Image",
                    show_label=True,
                    format="png",
                    interactive=False,
                )
        with gr.Column(scale=1, min_width=200):
            gr.Markdown(
                """
                ### Related Apps
                Check out these other great demos:
             
                - **[Official Z-Image Demo](https://huggingface.co/spaces/Tongyi-MAI/Z-Image-Turbo)**
                    *The official demo for single image generation.*
                
                - **[Panorama FLUX](https://huggingface.co/spaces/elismasilva/flux-1-panorama)**
                    *The Panorama app using the FLUX model.*
                """
            )
    with gr.Sidebar():
        gr.Markdown("### Generation Parameters")
        prompt_language = gr.Radio(
            choices=["English", "Korean", "Chinese"],
            value="English",
            label="Prompt Language",
        )

        resolution_preset = gr.Dropdown(
            choices=PANORAMIC_RESOLUTIONS,
            value=PANORAMIC_RESOLUTIONS[2],
            label="Panoramic Resolution Preset",
        )

        with gr.Row():
            height = gr.Slider(
                label="Target Height", value=1024, step=16, minimum=512, maximum=2048
            )
            width = gr.Slider(
                label="Target Width", value=3072, step=16, minimum=512, maximum=4096
            )
        with gr.Row():
            overlap = gr.Slider(
                minimum=0, maximum=512, value=256, step=16, label="Tile Overlap"
            )
            tile_weighting_method = gr.Dropdown(
                label="Blending Method", choices=["Cosine", "Gaussian"], value="Cosine"
            )
        with gr.Row():
            calc_tile = gr.Button("Calculate Final Dimensions", variant="primary")
        with gr.Row():
            new_target_height = gr.Textbox(
                label="Actual Height", value=1024, interactive=False
            )
            new_target_width = gr.Textbox(
                label="Actual Width", value=3072, interactive=False
            )
        with gr.Row():
            steps = gr.Slider(
                minimum=1, maximum=50, value=9, step=1, label="Inference Steps"
            )
            shift = gr.Slider(label="Time Shift", minimum=1.0, maximum=10.0, value=3.0, step=0.1)
        with gr.Row():
            generation_seed = gr.Slider(
                label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0
            )
            randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
        with gr.Row():
            hdr = gr.Slider(
                minimum=0.0, maximum=1.0, value=0.1, step=0.00, label="HDR Effect"
            )

    with gr.Row():
        gr.Examples(
            examples=[                
                [
                    "On the left side of a wide, wet stone courtyard at night, a traditional wooden temple building has a soft, warm glow from its illuminated windows, the environment is a deep, dark night sky.",
                    "Young Chinese woman in red Hanfu, standing gracefully in the center of the stone-paved temple courtyard, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp (⚡️), bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda (西安大雁塔), blurred colorful distant lights.",
                    "On the right side of the frame, a wooden temple corridor with a row of glowing paper lanterns recedes into the background. The lantern light reflects on the wet stone path below, and the distant pagoda is silhouetted against the same deep, dark night sky.",    
                    0.0,
                    0.0,
                    0.0,
                    256,
                    8,
                    3.0,
                    109320357,
                    "Cosine",
                    "English",
                    1024,
                    3072,
                    0.0,
                    False,
                ],
                [
                    "Street photography of a bustling, modern Chinese city square in the morning. On the left, people in business attire walk past gleaming glass and steel office buildings under a hazy, bright sun.",
                    "The center of the vast, modern plaza, where a large, elegant water fountain sprays water high into the air. In the background, through the mist of the fountain, the same modern glass and steel office buildings continue across the scene. The sun reflects off the wet plaza floor.",
                    "The right side of the plaza, where groups of fashionable teenagers are casually standing near the concrete pillars of a modern building, looking at their smartphones under the same hazy, bright sun.",
                    0.0,
                    0.0,
                    0.0,
                    256,
                    8,
                    6.0,
                    2024,
                    "Cosine",
                    "English",
                    1024,
                    3072,
                    0.0,
                    False
                ],
                [
                    "The edge of the green carpeted arena floor, with the blurred figures of spectators in the stands rising up in the background. A judge's table is partially visible on the far left.",
                    "In the center of the green carpeted arena floor, a proud man in a fine suit stands with his perfectly groomed white poodle, both looking forward. The poodle wears a matching tiny tuxedo.",
                    "Further down the green carpeted arena on the right, a line of other dog handlers and their breeds are waiting their turn in the distance, their forms slightly blurred by the depth of field.",
                    0.0,
                    0.0,
                    0.0,
                    200,
                    8,
                    3.0,
                    1146985307,
                    "Gaussian",
                    "English",
                    1024,
                    2672,
                    0.0,
                    False
                ],
                [
                    "Iron Man, repulsor rays blasting enemies in destroyed cityscape, cinematic lighting, photorealistic. Focus: Iron Man.",
                    "Captain America charging forward, vibranium shield deflecting energy blasts in destroyed cityscape, cinematic composition. Focus: Captain America.",
                    "Thor wielding Stormbreaker in destroyed cityscape, lightning crackling, powerful strike downwards, cinematic photography. Focus: Thor.",
                    0,
                    0,
                    0,
                    200,
                    8,
                    4.0,
                    85512926,
                    "Cosine",
                    "English",
                    1024,
                    3440,
                    0.0,
                    False
                ]
            ],
            inputs=[
                left_prompt,
                center_prompt,
                right_prompt,
                left_gs,
                center_gs,
                right_gs,
                overlap,
                steps,
                shift,
                generation_seed,
                tile_weighting_method,
                prompt_language,
                height,
                width,
                hdr,
                randomize_seed
            ],
            fn=run_for_examples,
            outputs=result,
            cache_examples=False,
            api_name=False,
        )

    # Event Handling
    predict_inputs = [
        left_prompt,
        center_prompt,
        right_prompt,
        left_gs,
        center_gs,
        right_gs,
        overlap,
        steps,
        shift,
        generation_seed,
        tile_weighting_method,        
        prompt_language,
        new_target_height,
        new_target_width,
        hdr,
        randomize_seed
    ]

    resolution_preset.change(
        fn=update_dimensions_from_preset,
        inputs=resolution_preset,
        outputs=[width, height],
        api_name=False,
    )
    calc_tile.click(
        fn=calculate_tile_size,
        inputs=[height, width, overlap],
        outputs=[new_target_height, new_target_width],
        api_name="calculate_tile_size"
    )
    generate_button.click(
        fn=clear_result,
        inputs=None,
        outputs=result,
        queue=False,
        api_name=False
    ).then(
        fn=calculate_tile_size,
        inputs=[height, width, overlap],
        outputs=[new_target_height, new_target_width],
        show_api=False
    ).then(
        fn=randomize_seed_fn,
        inputs=[generation_seed, randomize_seed],
        outputs=generation_seed,
        queue=False,
        api_name=False,    
    ).then(
        fn=generate_z_image_panorama,
        inputs=predict_inputs,
        outputs=result,
        show_progress="full",
        api_name="generate_z_image_panorama",
    )

app.queue().launch(mcp_server=True, share=True)