File size: 4,936 Bytes
be6fd48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

import gradio as gr
from PIL import Image
import math
from visual_markov_patterns_generator import create_generator


def create_image_grid(images, rows=None, cols=None):
    """Images grid generation"""
    n_images = len(images)

    if rows is None and cols is None:
        cols = min(4, n_images)
        rows = math.ceil(n_images / cols)
    elif rows is None:
        rows = math.ceil(n_images / cols)
    elif cols is None:
        cols = math.ceil(n_images / rows)

    w, h = images[0].size
    grid = Image.new("RGB", size=(cols * w, rows * h))

    for i, img in enumerate(images):
        if i >= rows * cols:
            break

        row = i // cols
        col = i % cols
        grid.paste(img, box=(col * w, row * h))

    return grid


def generate_image_interface(temperature, seed, use_seed, batch_size):
    """Interface function for image generation"""
    try:
        images = []
        for i in range(batch_size):
            actual_seed = (seed + i) if use_seed else None
            image = generator.generate_image(temperature=temperature, seed=actual_seed)

            scale_factor = 4
            new_size = (image.width * scale_factor, image.height * scale_factor)
            image = image.resize(new_size, Image.BICUBIC)
            images.append(image)

        if batch_size == 1:
            result_image = images[0]
        else:
            cols = min(4, batch_size)
            rows = math.ceil(batch_size / cols)
            result_image = create_image_grid(images, rows, cols)

        info = f"""

        **Generation Parameters:**

        - Temperature: {temperature}

        - Starting Seed: {seed if use_seed else 'random'}

        - Batch Size: {batch_size}

        - Image size: {images[0].size}

        """

        return result_image, info

    except Exception as e:
        error_info = f"Generation error: {str(e)}"
        empty_img = generator._create_random_image()
        scale_factor = 4
        empty_img = empty_img.resize(
            (empty_img.width * scale_factor, empty_img.height * scale_factor),
            Image.BICUBIC,
        )
        return empty_img, error_info


def get_model_info():
    """Get model information"""
    info = """

    ## How to Use:

    1. **Temperature (0.1-3.0):** Controls generation creativity

       - Low values (0.1-0.5): More conservative generation

       - Medium values (0.5-1.5): Balanced generation  

       - High values (1.5-3.0): More creative generation

    

    2. **Seed:** For reproducible results (optional)

    

    3. **Batch Size:** Controls the number of generated images

    

    3. Click "Generate Image" to create a new image

    

    ## Pattern Types:

    - haar

    - gradient

    - texture

    - color

    - frequency

    """

    return info


with gr.Blocks(title="Visual Markov Pattern Generator", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Visual Markov Mosaic Generator")
    gr.Markdown(
        "Image generator based on multi-dimensional Markov chains and neural network"
    )

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("## Generation Parameters")

            temperature = gr.Slider(
                minimum=0.1,
                maximum=3.0,
                value=1.0,
                step=0.1,
                label="Temperature",
                info="Controls generation creativity",
            )

            with gr.Row():
                use_seed = gr.Checkbox(
                    label="Use seed",
                    value=False,
                    info="For reproducible results",
                )
                seed = gr.Number(value=42, precision=0, label="Seed", interactive=True)

            batch_size = gr.Slider(
                minimum=1,
                maximum=20,
                value=1,
                step=1,
                label="Batch Size",
                info="Number of images to generate",
            )

            generate_btn = gr.Button("Generate Images", variant="primary", size="lg")

            gr.Markdown("## Information")
            info_output = gr.Markdown(value=get_model_info())

        with gr.Column(scale=2):
            gr.Markdown("## Result")

            image_output = gr.Image(
                label="Generated Images", type="pil", show_label=True, container=True
            )

            generation_info = gr.Markdown(label="Generation Information")

    generate_btn.click(
        fn=generate_image_interface,
        inputs=[temperature, seed, use_seed, batch_size],
        outputs=[image_output, generation_info],
    )


if __name__ == "__main__":
    print("Initializing generator...")
    generator = create_generator()
    demo.launch(share=False, server_name="0.0.0.0", server_port=7860)