File size: 4,976 Bytes
6a07ce1
 
 
 
 
 
 
 
 
 
570384a
6a07ce1
 
 
 
 
 
 
 
 
 
570384a
6a07ce1
 
 
 
 
570384a
6a07ce1
 
 
 
 
570384a
6a07ce1
 
 
 
 
 
 
570384a
6a07ce1
 
 
 
 
570384a
6a07ce1
 
 
 
 
570384a
6a07ce1
 
 
 
570384a
6a07ce1
 
 
570384a
6a07ce1
 
 
 
 
 
 
570384a
6a07ce1
570384a
6a07ce1
570384a
 
 
 
 
 
 
 
6a07ce1
 
 
 
 
 
 
 
 
 
570384a
6a07ce1
 
570384a
6a07ce1
 
 
 
 
570384a
6a07ce1
 
570384a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a07ce1
570384a
 
 
 
6a07ce1
 
570384a
 
 
 
 
6a07ce1
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
"""Image generator tab for SDXL Model Merger."""

import gradio as gr

from ..config import DEFAULT_PROMPT, DEFAULT_NEGATIVE_PROMPT
from ..generator import generate_image


def create_generator_tab():
    """Create the image generation tab with all input controls."""

    with gr.Accordion("🎨 2. Generate Image", open=True, elem_classes=["feature-card"]):
        # Prompts section
        with gr.Row():
            with gr.Column(scale=1):
                prompt = gr.Textbox(
                    label="Positive Prompt",
                    value=DEFAULT_PROMPT,
                    lines=3,
                    placeholder="Describe the image you want to generate..."
                )

                cfg = gr.Slider(
                    minimum=1.0, maximum=20.0, value=7.5, step=0.5,
                    label="CFG Scale",
                    info="Higher values make outputs match prompt more strictly"
                )

                height = gr.Number(
                    value=1024, precision=0,
                    label="Height (pixels)",
                    info="Output image height"
                )

            with gr.Column(scale=1):
                negative_prompt = gr.Textbox(
                    label="Negative Prompt",
                    value=DEFAULT_NEGATIVE_PROMPT,
                    lines=3,
                    placeholder="Elements to avoid in generation..."
                )

                steps = gr.Slider(
                    minimum=1, maximum=100, value=25, step=1,
                    label="Inference Steps",
                    info="More steps = better quality but slower"
                )

                width = gr.Number(
                    value=2048, precision=0,
                    label="Width (pixels)",
                    info="Output image width"
                )

        # Tiling options
        with gr.Row():
            tile_x = gr.Checkbox(True, label="X-axis Seamless Tiling")
            tile_y = gr.Checkbox(False, label="Y-axis Seamless Tiling")

        # Generate button and outputs
        with gr.Row():
            gen_btn = gr.Button("✨ Generate Image", variant="secondary", size="lg")

        with gr.Row():
            image_output = gr.Image(
                label="Result",
                height=400,
                show_label=True
            )
            with gr.Column():
                gen_status = gr.HTML(
                    label="Generation Status",
                    value='<div class="status-success">✅ Ready to generate</div>',
                )

                gen_progress = gr.Textbox(
                    label="Generation Progress",
                    placeholder="Ready to generate...",
                    show_label=True,
                    visible=False
                )

                gr.HTML("""
                    <div style="margin-top: 16px; padding: 12px; background: #f3f4f6; border-radius: 8px;">
                        <strong>💡 Tips:</strong>
                        <ul style="margin: 8px 0; padding-left: 20px; font-size: 0.9em;">
                            <li>Use wide aspect ratios (e.g., 1024x2048) for panoramas</li>
                            <li>Enable seamless tiling for texture-like outputs</li>
                            <li>Lower CFG (3-5) for more creative results</li>
                        </ul>
                    </div>
                """)

    return (
        prompt, negative_prompt, cfg, steps, height, width,
        tile_x, tile_y, gen_btn, image_output, gen_status, gen_progress
    )


def setup_generator_events(
    prompt, negative_prompt, cfg, steps, height, width,
    tile_x, tile_y, gen_btn, image_output, gen_status, gen_progress
):
    """Setup event handlers for the generator tab."""

    def on_generate_start():
        return (
            '<div class="status-warning">⏳ Generating image...</div>',
            "Starting generation...",
            gr.update(interactive=False),
        )

    def on_generate_complete(img, msg):
        """Read both image and last progress message to determine success/failure."""
        if img is None:
            return (
                f'<div class="status-error">{msg}</div>',
                "",
                gr.update(interactive=True),
                gr.update(),
            )
        return (
            '<div class="status-success">✅ Generation complete!</div>',
            "Done",
            gr.update(interactive=True),
            gr.update(value=img),
        )

    gen_btn.click(
        fn=on_generate_start,
        inputs=[],
        outputs=[gen_status, gen_progress, gen_btn],
    ).then(
        fn=generate_image,
        inputs=[prompt, negative_prompt, cfg, steps, height, width, tile_x, tile_y],
        outputs=[image_output, gen_progress],
    ).then(
        fn=on_generate_complete,
        inputs=[image_output, gen_progress],
        outputs=[gen_status, gen_progress, gen_btn, image_output],
    )