File size: 4,791 Bytes
6558358
bd38e6d
 
 
 
 
 
 
 
 
 
 
 
7374f55
bd38e6d
 
 
7374f55
bd38e6d
 
 
 
 
 
 
7374f55
bd38e6d
 
 
 
 
 
 
 
 
 
 
 
7374f55
bd38e6d
 
 
 
 
 
 
 
7374f55
 
 
bd38e6d
7374f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd38e6d
 
7374f55
 
 
 
 
 
 
 
 
 
 
 
 
 
bd38e6d
 
7374f55
bd38e6d
7374f55
 
 
 
 
 
 
bd38e6d
7374f55
 
 
 
bd38e6d
 
7374f55
 
bd38e6d
 
 
 
 
 
 
 
 
 
 
 
 
7374f55
bd38e6d
 
 
7374f55
bd38e6d
 
7374f55
 
 
 
 
bd38e6d
 
 
 
 
 
 
7374f55
bd38e6d
 
 
 
 
 
 
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
import gradio as gr
import os
from datetime import datetime

def generate_image(prompt, style, num_images=1, width=512, height=512):
    """
    Generate images based on the given prompt and parameters.
    Returns list of generated image paths.
    """
    try:
        model = gr.load("models/LAYEK-143/FLUX_V0")
        images = []
        for i in range(num_images):
            # Generate unique filename with timestamp
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"generated_{timestamp}_{i}.png"
            
            # Generate the image
            result = model.predict(
                prompt=prompt,
                style=style,
                width=width,
                height=height
            )
            
            # Save the image
            os.makedirs("generated_images", exist_ok=True)
            save_path = os.path.join("generated_images", filename)
            result.save(save_path)
            images.append(save_path)
            
        return images
    except Exception as e:
        return str(e)

def create_interface():
    """Create and configure the Gradio interface"""
    
    # Define available style options
    STYLES = [
        "Realistic",
        "Artistic",
        "Abstract",
        "Cartoon",
        "Sketch"
    ]
    
    # Create the interface
    with gr.Blocks(title="Image Generator") as interface:
        gr.Markdown("# 🎨 Image Generator")
        
        with gr.Row():
            with gr.Column():
                # Input components
                prompt = gr.Textbox(
                    label="Prompt",
                    placeholder="Describe the image you want to generate...",
                    lines=3
                )
                
                style = gr.Dropdown(
                    choices=STYLES,
                    label="Style",
                    value="Realistic"
                )
                
                with gr.Row():
                    num_images = gr.Slider(
                        minimum=1,
                        maximum=4,
                        value=1,
                        step=1,
                        label="Number of Images"
                    )
                    
                with gr.Row():
                    width = gr.Slider(
                        minimum=256,
                        maximum=1024,
                        value=512,
                        step=64,
                        label="Width"
                    )
                    height = gr.Slider(
                        minimum=256,
                        maximum=1024,
                        value=512,
                        step=64,
                        label="Height"
                    )
                
                generate_btn = gr.Button("Generate Images", variant="primary")
            
            with gr.Column():
                # Output gallery
                output_gallery = gr.Gallery(
                    label="Generated Images",
                    show_label=True,
                    elem_id="gallery"
                ).style(grid=2, height="auto")
        
        # Error message display
        error_message = gr.Textbox(
            label="Status",
            visible=False
        )
        
        # Handle generation
        def on_generate(prompt, style, num_images, width, height):
            if not prompt.strip():
                return None, "Please enter a prompt."
            
            try:
                images = generate_image(prompt, style, num_images, width, height)
                if isinstance(images, str):  # Error occurred
                    return None, images
                return images, "Generation successful!"
            except Exception as e:
                return None, f"Error: {str(e)}"
        
        generate_btn.click(
            fn=on_generate,
            inputs=[prompt, style, num_images, width, height],
            outputs=[output_gallery, error_message]
        )
        
        # Add usage instructions
        gr.Markdown("""
        ## How to Use
        1. Enter a detailed description of the image you want to generate
        2. Select a style from the dropdown menu
        3. Adjust the number of images and dimensions as needed
        4. Click 'Generate Images' and wait for the results
        5. Generated images will be saved in the 'generated_images' folder
        
        Note: Generation time may vary depending on the complexity of your prompt.
        """)
    
    return interface

if __name__ == "__main__":
    # Create and launch the interface
    app = create_interface()
    app.launch(
        share=True,
        enable_queue=True,
        server_name="0.0.0.0",
        server_port=7860
    )