File size: 6,739 Bytes
06ae90e
dd260ca
057604e
dd260ca
06ae90e
cfcc0d9
 
 
 
 
 
 
 
 
 
 
 
 
dd260ca
 
 
 
cfcc0d9
 
dd260ca
 
 
cfcc0d9
 
 
 
 
 
 
 
 
 
 
 
06ae90e
cfcc0d9
 
dd260ca
 
 
cfcc0d9
dd260ca
 
cfcc0d9
 
 
 
 
06ae90e
cfcc0d9
 
 
 
057604e
cfcc0d9
 
057604e
cfcc0d9
 
 
057604e
cfcc0d9
 
 
 
 
057604e
cfcc0d9
 
 
 
 
 
 
 
 
 
dd260ca
 
 
cfcc0d9
 
dd260ca
cfcc0d9
 
 
 
 
 
 
 
dd260ca
 
cfcc0d9
 
 
 
 
 
 
 
 
 
 
dd260ca
06ae90e
cfcc0d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06ae90e
057604e
cfcc0d9
 
 
 
 
057604e
cfcc0d9
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import gradio as gr
from diffusers import DiffusionPipeline
import torch
from PIL import Image, ImageDraw

class ChatbotIconGenerator:
    def __init__(self):
        # Predefined size options
        self.SIZE_OPTIONS = {
            "Small (128x128)": 128,
            "Medium (256x256)": 256,
            "Large (512x512)": 512,
            "Extra Large (1024x1024)": 1024
        }
        
        # Predefined prompt templates
        self.PROMPT_TEMPLATES = [
            "Cute cartoon chatbot mascot, big eyes, friendly smile, pastel colors, kawaii style",
            "Professional AI chatbot avatar, minimalist design, sleek geometric shapes, corporate blue and white",
            "Futuristic AI chatbot icon, glowing circuit patterns, metallic blue and silver",
            "Abstract geometric chatbot avatar, clean lines, single color gradient background",
            "Watercolor style chatbot icon, soft brush strokes, dreamy color blend",
            "Adorable robot character avatar, round shape, soft colors, playful expression",
            "Ultra-minimalist chatbot icon, simple geometric face, monochrome color scheme",
            "Cyberpunk chatbot avatar, neon accents, digital glitch effects",
            "Elegant corporate chatbot icon, modern flat design, clean lines",
            "Sketch-style chatbot avatar, hand-drawn look, pencil texture"
        ]
        
        # Rounding options
        self.CORNER_OPTIONS = {
            "No Rounding": 0,
            "Slight Rounding": 20,
            "Medium Rounding": 50,
            "Full Rounded": 100
        }
        
        # Load the model
        self.model = self.load_image_generator()
    
    def load_image_generator(self):
        try:
            # Use a more lightweight model
            model = DiffusionPipeline.from_pretrained(
                "runwayml/stable-diffusion-v1-5", # kopyl/ui-icons-256
                torch_dtype=torch.float16,
                safety_checker=None,  # Disable safety checker to reduce load
                requires_safety_checker=False
            )
            return model.to("cpu")
        except Exception as e:
            print(f"Error loading model: {e}")
            return None
    
    def round_image_corners(self, image, corner_radius):
        # Create a rounded corner mask
        if corner_radius == 0:
            return image
        
        # Create a new image with an alpha channel
        rounded_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
        
        # Create a mask for rounded corners
        mask = Image.new('L', image.size, 255)
        draw = ImageDraw.Draw(mask)
        
        # Draw rounded rectangle
        draw.rounded_rectangle(
            [0, 0, image.width-1, image.height-1], 
            radius=corner_radius, 
            fill=255
        )
        
        # Paste the original image with the mask
        rounded_image.paste(image, mask=mask)
        return rounded_image
    
    def generate_chatbot_icon(
        self, 
        prompt, 
        size, 
        corner_rounding,
        negative_prompt="low quality, bad composition, blurry, ugly, deformed",
        num_inference_steps=20,
        guidance_scale=7.5
    ):
        if self.model is None:
            raise ValueError("Model failed to load. Please check your dependencies.")
        
        try:
            # Generate the image
            generated_image = self.model(
                prompt=prompt,
                negative_prompt=negative_prompt,
                num_inference_steps=num_inference_steps,
                guidance_scale=guidance_scale,
                height=size,
                width=size
            ).images[0]
            
            # Resize and round corners
            generated_image = generated_image.resize((size, size))
            rounded_image = self.round_image_corners(generated_image, 
                self.CORNER_OPTIONS[corner_rounding])
            
            return rounded_image
        
        except Exception as e:
            print(f"Error generating image: {e}")
            raise
    
    def create_gradio_interface(self):
        with gr.Blocks(title="πŸ€– Chatbot Icon Generator") as demo:
            gr.Markdown("# πŸ€– Chatbot Icon Generator")
            
            with gr.Row():
                with gr.Column():
                    # Prompt selection
                    prompt_dropdown = gr.Dropdown(
                        label="Quick Templates", 
                        choices=self.PROMPT_TEMPLATES,
                        allow_custom_value=True
                    )
                    
                    # Custom prompt input
                    custom_prompt = gr.Textbox(
                        label="Custom Prompt (Optional)", 
                        placeholder="Enter your own detailed description..."
                    )
                    
                    # Size selection
                    size_dropdown = gr.Dropdown(
                        label="Icon Size", 
                        choices=list(self.SIZE_OPTIONS.keys()),
                        value="Medium (256x256)"
                    )
                    
                    # Corner rounding
                    corner_dropdown = gr.Dropdown(
                        label="Corner Rounding", 
                        choices=list(self.CORNER_OPTIONS.keys()),
                        value="Slight Rounding"
                    )
                    
                    # Generate button
                    generate_btn = gr.Button("Generate Icon", variant="primary")
                
                with gr.Column():
                    # Output image
                    output_image = gr.Image(label="Generated Chatbot Icon")
            
            # Logic for prompt selection
            def update_prompt(template):
                return template
            
            prompt_dropdown.change(
                fn=update_prompt, 
                inputs=[prompt_dropdown], 
                outputs=[custom_prompt]
            )
            
            # Generate button logic
            generate_btn.click(
                fn=lambda prompt, size, corners: self.generate_chatbot_icon(
                    prompt or "Cute minimalist chatbot avatar, clean design, friendly expression",
                    self.SIZE_OPTIONS[size],
                    corners
                ),
                inputs=[custom_prompt, size_dropdown, corner_dropdown], 
                outputs=[output_image]
            )
        
        return demo

# Launch the app
def main():
    generator = ChatbotIconGenerator()
    demo = generator.create_gradio_interface()
    demo.launch()

if __name__ == "__main__":
    main()