Add a slide to select the batch size
Browse files
app.py
CHANGED
|
@@ -1,118 +1,162 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from PIL import Image
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import math
|
| 6 |
+
from visual_markov_patterns_generator import create_generator
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def create_image_grid(images, rows=None, cols=None):
|
| 10 |
+
"""Images grid generation"""
|
| 11 |
+
n_images = len(images)
|
| 12 |
+
|
| 13 |
+
if rows is None and cols is None:
|
| 14 |
+
cols = min(4, n_images)
|
| 15 |
+
rows = math.ceil(n_images / cols)
|
| 16 |
+
elif rows is None:
|
| 17 |
+
rows = math.ceil(n_images / cols)
|
| 18 |
+
elif cols is None:
|
| 19 |
+
cols = math.ceil(n_images / rows)
|
| 20 |
+
|
| 21 |
+
w, h = images[0].size
|
| 22 |
+
grid = Image.new("RGB", size=(cols * w, rows * h))
|
| 23 |
+
|
| 24 |
+
for i, img in enumerate(images):
|
| 25 |
+
if i >= rows * cols:
|
| 26 |
+
break
|
| 27 |
+
|
| 28 |
+
row = i // cols
|
| 29 |
+
col = i % cols
|
| 30 |
+
grid.paste(img, box=(col * w, row * h))
|
| 31 |
+
|
| 32 |
+
return grid
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def generate_image_interface(temperature, seed, use_seed, batch_size):
|
| 36 |
+
"""Interface function for image generation"""
|
| 37 |
+
try:
|
| 38 |
+
images = []
|
| 39 |
+
for i in range(batch_size):
|
| 40 |
+
actual_seed = (seed + i) if use_seed else None
|
| 41 |
+
image = generator.generate_image(temperature=temperature, seed=actual_seed)
|
| 42 |
+
|
| 43 |
+
scale_factor = 4
|
| 44 |
+
new_size = (image.width * scale_factor, image.height * scale_factor)
|
| 45 |
+
image = image.resize(new_size, Image.BICUBIC)
|
| 46 |
+
images.append(image)
|
| 47 |
+
|
| 48 |
+
if batch_size == 1:
|
| 49 |
+
result_image = images[0]
|
| 50 |
+
else:
|
| 51 |
+
cols = min(4, batch_size)
|
| 52 |
+
rows = math.ceil(batch_size / cols)
|
| 53 |
+
result_image = create_image_grid(images, rows, cols)
|
| 54 |
+
|
| 55 |
+
info = f"""
|
| 56 |
+
**Generation Parameters:**
|
| 57 |
+
- Temperature: {temperature}
|
| 58 |
+
- Starting Seed: {seed if use_seed else 'random'}
|
| 59 |
+
- Batch Size: {batch_size}
|
| 60 |
+
- Image size: {images[0].size}
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
return result_image, info
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
error_info = f"Generation error: {str(e)}"
|
| 67 |
+
empty_img = generator._create_random_image()
|
| 68 |
+
scale_factor = 4
|
| 69 |
+
empty_img = empty_img.resize(
|
| 70 |
+
(empty_img.width * scale_factor, empty_img.height * scale_factor),
|
| 71 |
+
Image.BICUBIC,
|
| 72 |
+
)
|
| 73 |
+
return empty_img, error_info
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def get_model_info():
|
| 77 |
+
"""Get model information"""
|
| 78 |
+
info = """
|
| 79 |
+
## How to Use:
|
| 80 |
+
1. **Temperature (0.1-3.0):** Controls generation creativity
|
| 81 |
+
- Low values (0.1-0.5): More conservative generation
|
| 82 |
+
- Medium values (0.5-1.5): Balanced generation
|
| 83 |
+
- High values (1.5-3.0): More creative generation
|
| 84 |
+
|
| 85 |
+
2. **Seed:** For reproducible results (optional)
|
| 86 |
+
|
| 87 |
+
3. **Batch Size:** Controls the number of generated images
|
| 88 |
+
|
| 89 |
+
3. Click "Generate Image" to create a new image
|
| 90 |
+
|
| 91 |
+
## Pattern Types:
|
| 92 |
+
- haar
|
| 93 |
+
- gradient
|
| 94 |
+
- texture
|
| 95 |
+
- color
|
| 96 |
+
- frequency
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
return info
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
with gr.Blocks(title="Visual Markov Pattern Generator", theme=gr.themes.Soft()) as demo:
|
| 103 |
+
gr.Markdown("# Visual Markov Mosaic Generator")
|
| 104 |
+
gr.Markdown(
|
| 105 |
+
"Image generator based on multi-dimensional Markov chains and neural network"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column(scale=1):
|
| 110 |
+
gr.Markdown("## Generation Parameters")
|
| 111 |
+
|
| 112 |
+
temperature = gr.Slider(
|
| 113 |
+
minimum=0.1,
|
| 114 |
+
maximum=3.0,
|
| 115 |
+
value=1.0,
|
| 116 |
+
step=0.1,
|
| 117 |
+
label="Temperature",
|
| 118 |
+
info="Controls generation creativity",
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
with gr.Row():
|
| 122 |
+
use_seed = gr.Checkbox(
|
| 123 |
+
label="Use seed",
|
| 124 |
+
value=False,
|
| 125 |
+
info="For reproducible results",
|
| 126 |
+
)
|
| 127 |
+
seed = gr.Number(value=42, precision=0, label="Seed", interactive=True)
|
| 128 |
+
|
| 129 |
+
batch_size = gr.Slider(
|
| 130 |
+
minimum=1,
|
| 131 |
+
maximum=20,
|
| 132 |
+
value=1,
|
| 133 |
+
step=1,
|
| 134 |
+
label="Batch Size",
|
| 135 |
+
info="Number of images to generate",
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
generate_btn = gr.Button("Generate Images", variant="primary", size="lg")
|
| 139 |
+
|
| 140 |
+
gr.Markdown("## Information")
|
| 141 |
+
info_output = gr.Markdown(value=get_model_info())
|
| 142 |
+
|
| 143 |
+
with gr.Column(scale=2):
|
| 144 |
+
gr.Markdown("## Result")
|
| 145 |
+
|
| 146 |
+
image_output = gr.Image(
|
| 147 |
+
label="Generated Images", type="pil", show_label=True, container=True
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
generation_info = gr.Markdown(label="Generation Information")
|
| 151 |
+
|
| 152 |
+
generate_btn.click(
|
| 153 |
+
fn=generate_image_interface,
|
| 154 |
+
inputs=[temperature, seed, use_seed, batch_size],
|
| 155 |
+
outputs=[image_output, generation_info],
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
+
print("Initializing generator...")
|
| 161 |
+
generator = create_generator()
|
| 162 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|