Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,195 @@
|
|
1 |
import gradio as gr
|
2 |
-
from diffusers import StableDiffusionXLImg2ImgPipeline
|
3 |
import torch
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
def
|
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 |
-
negative_prompt=negative_prompt,
|
42 |
-
num_inference_steps=num_inference_steps,
|
43 |
-
guidance_scale=guidance_scale,
|
44 |
-
strength=strength,
|
45 |
-
image=default_init_image
|
46 |
-
).images[0]
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
print(f"Error generating image: {e}")
|
51 |
-
return None
|
52 |
-
|
53 |
-
# Create Gradio interface
|
54 |
-
def create_gradio_interface():
|
55 |
-
with gr.Blocks() as demo:
|
56 |
-
gr.Markdown("# 🤖 Chatbot Icon Generator")
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
label="Icon Description",
|
63 |
-
value="Cute minimalist chatbot avatar, clean design, friendly expression, cartoon style"
|
64 |
-
)
|
65 |
-
|
66 |
-
# Generate button
|
67 |
-
generate_btn = gr.Button("Generate Icon")
|
68 |
-
|
69 |
-
with gr.Column():
|
70 |
-
# Output image
|
71 |
-
output_image = gr.Image(label="Generated Chatbot Icon")
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Launch the app
|
|
|
|
|
|
|
|
|
|
|
83 |
if __name__ == "__main__":
|
84 |
-
|
85 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline
|
4 |
+
from PIL import Image, ImageOps
|
5 |
|
6 |
+
class ChatbotIconGenerator:
|
7 |
+
def __init__(self):
|
8 |
+
# Predefined size options
|
9 |
+
self.SIZE_OPTIONS = {
|
10 |
+
"Small (128x128)": 128,
|
11 |
+
"Medium (256x256)": 256,
|
12 |
+
"Large (512x512)": 512,
|
13 |
+
"Extra Large (1024x1024)": 1024
|
14 |
+
}
|
15 |
+
|
16 |
+
# Predefined prompt templates
|
17 |
+
self.PROMPT_TEMPLATES = [
|
18 |
+
# Professional/Corporate
|
19 |
+
"Professional AI chatbot avatar, minimalist design, sleek geometric shapes, corporate blue and white color palette",
|
20 |
+
"Elegant corporate chatbot icon, modern flat design, clean lines, subtle technology motif",
|
21 |
+
|
22 |
+
# Cute/Friendly
|
23 |
+
"Cute cartoon chatbot mascot, big eyes, friendly smile, pastel colors, kawaii style",
|
24 |
+
"Adorable robot character avatar, round shape, soft colors, playful expression",
|
25 |
+
|
26 |
+
# Sci-Fi/Tech
|
27 |
+
"Futuristic AI chatbot icon, glowing circuit patterns, metallic blue and silver, high-tech aesthetic",
|
28 |
+
"Cyberpunk chatbot avatar, neon accents, digital glitch effects, modern tech design",
|
29 |
+
|
30 |
+
# Minimalist
|
31 |
+
"Ultra-minimalist chatbot icon, simple geometric face, monochrome color scheme",
|
32 |
+
"Abstract geometric chatbot avatar, clean lines, single color gradient background",
|
33 |
+
|
34 |
+
# Artistic
|
35 |
+
"Watercolor style chatbot icon, soft brush strokes, dreamy color blend, artistic interpretation",
|
36 |
+
"Sketch-style chatbot avatar, hand-drawn look, pencil texture, artistic rendering"
|
37 |
+
]
|
38 |
+
|
39 |
+
# Rounding options
|
40 |
+
self.CORNER_OPTIONS = {
|
41 |
+
"No Rounding": 0,
|
42 |
+
"Slight Rounding": 20,
|
43 |
+
"Medium Rounding": 50,
|
44 |
+
"Full Rounded": 100
|
45 |
+
}
|
46 |
+
|
47 |
+
# Load the model
|
48 |
+
self.model = self.load_image_generator()
|
49 |
|
50 |
+
def load_image_generator(self):
|
51 |
+
try:
|
52 |
+
model = StableDiffusionXLImg2ImgPipeline.from_pretrained(
|
53 |
+
"stabilityai/stable-diffusion-2-1",
|
54 |
+
torch_dtype=torch.float16,
|
55 |
+
variant="fp16",
|
56 |
+
use_safetensors=True
|
57 |
+
)
|
58 |
+
return model.to("cpu")
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error loading model: {e}")
|
61 |
+
return None
|
62 |
|
63 |
+
def round_image_corners(self, image, corner_radius):
|
64 |
+
# Create a rounded corner mask
|
65 |
+
if corner_radius == 0:
|
66 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Create a new image with an alpha channel
|
69 |
+
rounded_image = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
# Create a mask for rounded corners
|
72 |
+
mask = Image.new('L', image.size, 255)
|
73 |
+
from PIL import ImageDraw
|
74 |
+
draw = ImageDraw.Draw(mask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
# Draw rounded rectangle
|
77 |
+
draw.rounded_rectangle(
|
78 |
+
[0, 0, image.width-1, image.height-1],
|
79 |
+
radius=corner_radius,
|
80 |
+
fill=255
|
81 |
)
|
82 |
+
|
83 |
+
# Paste the original image with the mask
|
84 |
+
rounded_image.paste(image, mask=mask)
|
85 |
+
return rounded_image
|
86 |
+
|
87 |
+
def generate_chatbot_icon(
|
88 |
+
self,
|
89 |
+
prompt,
|
90 |
+
size,
|
91 |
+
corner_rounding,
|
92 |
+
negative_prompt="low quality, bad composition, blurry, ugly",
|
93 |
+
num_inference_steps=25,
|
94 |
+
guidance_scale=8.0,
|
95 |
+
strength=0.75
|
96 |
+
):
|
97 |
+
if self.model is None:
|
98 |
+
return None
|
99 |
+
|
100 |
+
try:
|
101 |
+
# Create a random initial image of specified size
|
102 |
+
default_init_image = torch.randn((1, 3, size, size))
|
103 |
+
|
104 |
+
# Generate the image
|
105 |
+
generated_image = self.model(
|
106 |
+
prompt=prompt,
|
107 |
+
negative_prompt=negative_prompt,
|
108 |
+
num_inference_steps=num_inference_steps,
|
109 |
+
guidance_scale=guidance_scale,
|
110 |
+
strength=strength,
|
111 |
+
image=default_init_image
|
112 |
+
).images[0]
|
113 |
+
|
114 |
+
# Resize and round corners
|
115 |
+
generated_image = generated_image.resize((size, size))
|
116 |
+
rounded_image = self.round_image_corners(generated_image,
|
117 |
+
self.CORNER_OPTIONS[corner_rounding])
|
118 |
+
|
119 |
+
return rounded_image
|
120 |
+
|
121 |
+
except Exception as e:
|
122 |
+
print(f"Error generating image: {e}")
|
123 |
+
return None
|
124 |
|
125 |
+
def create_gradio_interface(self):
|
126 |
+
with gr.Blocks(title="🤖 Chatbot Icon Generator") as demo:
|
127 |
+
gr.Markdown("# 🤖 Chatbot Icon Generator")
|
128 |
+
|
129 |
+
with gr.Row():
|
130 |
+
with gr.Column():
|
131 |
+
# Prompt selection
|
132 |
+
prompt_dropdown = gr.Dropdown(
|
133 |
+
label="Quick Templates",
|
134 |
+
choices=self.PROMPT_TEMPLATES,
|
135 |
+
allow_custom_value=True
|
136 |
+
)
|
137 |
+
|
138 |
+
# Custom prompt input
|
139 |
+
custom_prompt = gr.Textbox(
|
140 |
+
label="Custom Prompt (Optional)",
|
141 |
+
placeholder="Enter your own detailed description..."
|
142 |
+
)
|
143 |
+
|
144 |
+
# Size selection
|
145 |
+
size_dropdown = gr.Dropdown(
|
146 |
+
label="Icon Size",
|
147 |
+
choices=list(self.SIZE_OPTIONS.keys()),
|
148 |
+
value="Medium (256x256)"
|
149 |
+
)
|
150 |
+
|
151 |
+
# Corner rounding
|
152 |
+
corner_dropdown = gr.Dropdown(
|
153 |
+
label="Corner Rounding",
|
154 |
+
choices=list(self.CORNER_OPTIONS.keys()),
|
155 |
+
value="Slight Rounding"
|
156 |
+
)
|
157 |
+
|
158 |
+
# Generate button
|
159 |
+
generate_btn = gr.Button("Generate Icon", variant="primary")
|
160 |
+
|
161 |
+
with gr.Column():
|
162 |
+
# Output image
|
163 |
+
output_image = gr.Image(label="Generated Chatbot Icon")
|
164 |
+
|
165 |
+
# Logic for prompt selection
|
166 |
+
def update_prompt(template):
|
167 |
+
return template
|
168 |
+
|
169 |
+
prompt_dropdown.change(
|
170 |
+
fn=update_prompt,
|
171 |
+
inputs=[prompt_dropdown],
|
172 |
+
outputs=[custom_prompt]
|
173 |
+
)
|
174 |
+
|
175 |
+
# Generate button logic
|
176 |
+
generate_btn.click(
|
177 |
+
fn=lambda prompt, size, corners: self.generate_chatbot_icon(
|
178 |
+
prompt or "Cute minimalist chatbot avatar, clean design, friendly expression",
|
179 |
+
self.SIZE_OPTIONS[size],
|
180 |
+
corners
|
181 |
+
),
|
182 |
+
inputs=[custom_prompt, size_dropdown, corner_dropdown],
|
183 |
+
outputs=[output_image]
|
184 |
+
)
|
185 |
+
|
186 |
+
return demo
|
187 |
|
188 |
# Launch the app
|
189 |
+
def main():
|
190 |
+
generator = ChatbotIconGenerator()
|
191 |
+
demo = generator.create_gradio_interface()
|
192 |
+
demo.launch()
|
193 |
+
|
194 |
if __name__ == "__main__":
|
195 |
+
main()
|
|