Download app.py from MLSolutions/VisualMultidimensionalMarkovChainsGenerator: direct link, hf CLI and curl.
- Browser
- Download file 4.94 kB
-
https://huggingface.co/spaces/MLSolutions/VisualMultidimensionalMarkovChainsGenerator/resolve/main/app.py
- Command line
-
hf download hf://spaces/MLSolutions/VisualMultidimensionalMarkovChainsGenerator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MLSolutions/VisualMultidimensionalMarkovChainsGenerator/resolve/main/app.py
4.94 kB
| #!/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) | |