Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,132 +1,154 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
4 |
import torch
|
5 |
-
from diffusers import
|
6 |
from PIL import Image
|
7 |
-
import
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
""
|
14 |
-
|
15 |
-
|
16 |
-
bottom_edge = img_array[-1, :, :]
|
17 |
-
left_edge = img_array[:, 0, :]
|
18 |
-
right_edge = img_array[:, -1, :]
|
19 |
-
edge_pixels = np.concatenate([top_edge, bottom_edge, left_edge, right_edge])
|
20 |
-
random_edge_color = tuple(edge_pixels[random.randint(0, len(edge_pixels)-1)])
|
21 |
-
return random_edge_color
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
img_array = np.array(tshirt_image)
|
26 |
-
white_mask = np.all(np.abs(img_array - [255, 255, 255]) < threshold, axis=2)
|
27 |
-
img_array[white_mask] = target_color
|
28 |
-
return Image.fromarray(img_array)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
try:
|
33 |
-
if not os.path.exists(logo_path):
|
34 |
-
return image
|
35 |
-
|
36 |
-
logo = Image.open(logo_path).convert('RGBA')
|
37 |
-
main_width, main_height = image.size
|
38 |
-
logo_width = int(main_width * size_percentage / 100)
|
39 |
-
logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
|
40 |
-
logo = logo.resize((logo_width, logo_height), Image.Resampling.LANCZOS)
|
41 |
-
|
42 |
-
if image.mode != 'RGBA':
|
43 |
-
image = image.convert('RGBA')
|
44 |
-
|
45 |
-
watermarked = Image.new('RGBA', image.size, (0, 0, 0, 0))
|
46 |
-
watermarked.paste(image, (0, 0))
|
47 |
-
|
48 |
-
if position == 'bottom-right':
|
49 |
-
pos = (main_width - logo_width - 10, main_height - logo_height - 10)
|
50 |
-
elif position == 'bottom-left':
|
51 |
-
pos = (10, main_height - logo_height - 10)
|
52 |
-
elif position == 'top-right':
|
53 |
-
pos = (main_width - logo_width - 10, 10)
|
54 |
-
else: # top-left
|
55 |
-
pos = (10, 10)
|
56 |
-
|
57 |
-
watermarked.paste(logo, pos, logo)
|
58 |
-
return watermarked.convert('RGB')
|
59 |
-
except Exception as e:
|
60 |
-
print(f"Failed to add watermark: {str(e)}")
|
61 |
-
return image
|
62 |
|
63 |
-
def
|
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 |
-
def
|
91 |
-
"""
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
"realistic": "photorealistic, detailed illustration"
|
102 |
-
}
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
def
|
107 |
-
"""
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
117 |
generator = torch.Generator().manual_seed(seed)
|
118 |
|
119 |
-
|
|
|
120 |
prompt=enhanced_prompt,
|
121 |
width=width,
|
122 |
height=height,
|
123 |
num_inference_steps=num_inference_steps,
|
124 |
generator=generator,
|
|
|
125 |
).images[0]
|
126 |
|
127 |
-
|
|
|
|
|
|
|
128 |
|
129 |
-
#
|
130 |
TSHIRT_COLORS = {
|
131 |
"White": "#FFFFFF",
|
132 |
"Black": "#000000",
|
@@ -134,7 +156,14 @@ TSHIRT_COLORS = {
|
|
134 |
"Gray": "#808080"
|
135 |
}
|
136 |
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
"minimal",
|
139 |
"vintage",
|
140 |
"artistic",
|
@@ -143,67 +172,153 @@ STYLES = [
|
|
143 |
"realistic"
|
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 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
)
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
outputs=[result, preview, seed]
|
203 |
)
|
204 |
-
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
-
|
208 |
-
demo = create_interface()
|
209 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
+
import spaces
|
5 |
import torch
|
6 |
+
from diffusers import DiffusionPipeline
|
7 |
from PIL import Image
|
8 |
+
import io
|
9 |
|
10 |
+
dtype = torch.bfloat16
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
+
pipe = DiffusionPipeline.from_pretrained(
|
14 |
+
"black-forest-labs/FLUX.1-schnell",
|
15 |
+
torch_dtype=dtype
|
16 |
+
).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
MAX_SEED = np.iinfo(np.int32).max
|
19 |
+
MAX_IMAGE_SIZE = 2048
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
import numpy as np
|
22 |
+
from collections import Counter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
def get_prominent_colors(image, num_colors=5):
|
25 |
+
"""
|
26 |
+
Get the most prominent colors from an image, focusing on edges
|
27 |
+
"""
|
28 |
+
# Convert to numpy array
|
29 |
+
img_array = np.array(image)
|
30 |
+
|
31 |
+
# Create a simple edge mask using gradient magnitude
|
32 |
+
gradient_x = np.gradient(img_array.mean(axis=2))[1]
|
33 |
+
gradient_y = np.gradient(img_array.mean(axis=2))[0]
|
34 |
+
gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2)
|
35 |
+
|
36 |
+
# Threshold to get edge pixels
|
37 |
+
edge_threshold = np.percentile(gradient_magnitude, 90) # Adjust percentile as needed
|
38 |
+
edge_mask = gradient_magnitude > edge_threshold
|
39 |
+
|
40 |
+
# Get colors from edge pixels
|
41 |
+
edge_colors = img_array[edge_mask]
|
42 |
+
|
43 |
+
# Convert colors to tuples for counting
|
44 |
+
colors = [tuple(color) for color in edge_colors]
|
45 |
+
|
46 |
+
# Count occurrences of each color
|
47 |
+
color_counts = Counter(colors)
|
48 |
+
|
49 |
+
# Get most common colors
|
50 |
+
prominent_colors = color_counts.most_common(num_colors)
|
51 |
+
|
52 |
+
return prominent_colors
|
53 |
|
54 |
+
def create_tshirt_preview(design_image, tshirt_color="white"):
|
55 |
+
"""
|
56 |
+
Overlay the design onto the existing t-shirt template and color match
|
57 |
+
"""
|
58 |
+
# Load the template t-shirt image
|
59 |
+
tshirt = Image.open('image.jpeg')
|
60 |
+
tshirt_width, tshirt_height = tshirt.size
|
61 |
+
|
62 |
+
# Convert design to PIL Image if it's not already
|
63 |
+
if not isinstance(design_image, Image.Image):
|
64 |
+
design_image = Image.fromarray(design_image)
|
|
|
|
|
65 |
|
66 |
+
# Get prominent colors from the design
|
67 |
+
prominent_colors = get_prominent_colors(design_image)
|
68 |
+
if prominent_colors:
|
69 |
+
# Use the most prominent color for the t-shirt
|
70 |
+
main_color = prominent_colors[0][0] # RGB tuple of most common color
|
71 |
+
else:
|
72 |
+
# Fallback to white if no colors found
|
73 |
+
main_color = (255, 255, 255)
|
74 |
+
|
75 |
+
# Convert design to PIL Image if it's not already
|
76 |
+
if not isinstance(design_image, Image.Image):
|
77 |
+
design_image = Image.fromarray(design_image)
|
78 |
+
|
79 |
+
# Resize design to fit nicely on shirt (40% of shirt width)
|
80 |
+
design_width = int(tshirt_width * 0.35) # Adjust this percentage as needed
|
81 |
+
design_height = int(design_width * design_image.size[1] / design_image.size[0])
|
82 |
+
design_image = design_image.resize((design_width, design_height), Image.Resampling.LANCZOS)
|
83 |
+
|
84 |
+
# Calculate position to center design on shirt
|
85 |
+
x = (tshirt_width - design_width) // 2
|
86 |
+
y = int(tshirt_height * 0.2) # Adjust this value based on your template
|
87 |
+
|
88 |
+
# If design has transparency (RGBA), create mask
|
89 |
+
if design_image.mode == 'RGBA':
|
90 |
+
mask = design_image.split()[3]
|
91 |
+
else:
|
92 |
+
mask = None
|
93 |
+
|
94 |
+
# Paste design onto shirt
|
95 |
+
tshirt.paste(design_image, (x, y), mask)
|
96 |
+
|
97 |
+
return tshirt
|
98 |
|
99 |
+
def enhance_prompt_for_tshirt(prompt, style=None):
|
100 |
+
"""Add specific terms to ensure good t-shirt designs."""
|
101 |
+
style_terms = {
|
102 |
+
"minimal": ["simple geometric shapes", "clean lines", "minimalist illustration"],
|
103 |
+
"vintage": ["distressed effect", "retro typography", "vintage illustration"],
|
104 |
+
"artistic": ["hand-drawn style", "watercolor effect", "artistic illustration"],
|
105 |
+
"geometric": ["abstract shapes", "geometric patterns", "modern design"],
|
106 |
+
"typography": ["bold typography", "creative lettering", "text-based design"],
|
107 |
+
"realistic": ["realistic", "cinematic", "photograph"]
|
108 |
+
}
|
109 |
+
|
110 |
+
base_terms = [
|
111 |
+
"create t-shirt design",
|
112 |
+
"with centered composition",
|
113 |
+
"high quality",
|
114 |
+
"professional design",
|
115 |
+
"clear background"
|
116 |
+
]
|
117 |
+
|
118 |
+
enhanced_prompt = f"{prompt}, {', '.join(base_terms)}"
|
119 |
+
|
120 |
+
if style and style in style_terms:
|
121 |
+
style_specific_terms = style_terms[style]
|
122 |
+
enhanced_prompt = f"{enhanced_prompt}, {', '.join(style_specific_terms)}"
|
123 |
+
|
124 |
+
return enhanced_prompt
|
125 |
|
126 |
+
@spaces.GPU()
|
127 |
+
def infer(prompt, style=None, tshirt_color="white", seed=42, randomize_seed=False,
|
128 |
+
width=1024, height=1024, num_inference_steps=4,
|
129 |
+
progress=gr.Progress(track_tqdm=True)):
|
130 |
+
if randomize_seed:
|
131 |
+
seed = random.randint(0, MAX_SEED)
|
132 |
+
|
133 |
+
enhanced_prompt = enhance_prompt_for_tshirt(prompt, style)
|
134 |
generator = torch.Generator().manual_seed(seed)
|
135 |
|
136 |
+
# Generate the design
|
137 |
+
design_image = pipe(
|
138 |
prompt=enhanced_prompt,
|
139 |
width=width,
|
140 |
height=height,
|
141 |
num_inference_steps=num_inference_steps,
|
142 |
generator=generator,
|
143 |
+
guidance_scale=0.0
|
144 |
).images[0]
|
145 |
|
146 |
+
# Create t-shirt preview
|
147 |
+
tshirt_preview = create_tshirt_preview(design_image, tshirt_color)
|
148 |
+
|
149 |
+
return design_image, tshirt_preview, seed
|
150 |
|
151 |
+
# Available t-shirt colors
|
152 |
TSHIRT_COLORS = {
|
153 |
"White": "#FFFFFF",
|
154 |
"Black": "#000000",
|
|
|
156 |
"Gray": "#808080"
|
157 |
}
|
158 |
|
159 |
+
examples = [
|
160 |
+
["Cool geometric mountain landscape", "minimal", "White"],
|
161 |
+
["Vintage motorcycle with flames", "vintage", "Black"],
|
162 |
+
["flamingo in scenic forset", "realistic", "White"],
|
163 |
+
["Adventure Starts typography", "typography", "White"]
|
164 |
+
]
|
165 |
+
|
166 |
+
styles = [
|
167 |
"minimal",
|
168 |
"vintage",
|
169 |
"artistic",
|
|
|
172 |
"realistic"
|
173 |
]
|
174 |
|
175 |
+
css = """
|
176 |
+
#col-container {
|
177 |
+
margin: 0 auto;
|
178 |
+
max-width: 1200px !important;
|
179 |
+
padding: 20px;
|
180 |
+
}
|
181 |
+
.main-title {
|
182 |
+
text-align: center;
|
183 |
+
color: #2d3748;
|
184 |
+
margin-bottom: 1rem;
|
185 |
+
font-family: 'Poppins', sans-serif;
|
186 |
+
}
|
187 |
+
.subtitle {
|
188 |
+
text-align: center;
|
189 |
+
color: #4a5568;
|
190 |
+
margin-bottom: 2rem;
|
191 |
+
font-family: 'Inter', sans-serif;
|
192 |
+
font-size: 0.95rem;
|
193 |
+
line-height: 1.5;
|
194 |
+
}
|
195 |
+
.design-input {
|
196 |
+
border: 2px solid #e2e8f0;
|
197 |
+
border-radius: 10px;
|
198 |
+
padding: 12px !important;
|
199 |
+
margin-bottom: 1rem !important;
|
200 |
+
font-size: 1rem;
|
201 |
+
transition: all 0.3s ease;
|
202 |
+
}
|
203 |
+
.results-row {
|
204 |
+
display: grid;
|
205 |
+
grid-template-columns: 1fr 1fr;
|
206 |
+
gap: 20px;
|
207 |
+
margin-top: 20px;
|
208 |
+
}
|
209 |
+
"""
|
210 |
|
211 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
212 |
+
with gr.Column(elem_id="col-container"):
|
213 |
+
gr.Markdown(
|
214 |
+
"""
|
215 |
+
# 👕Deradh's T-Shirt Design Generator
|
216 |
+
""",
|
217 |
+
elem_classes=["main-title"]
|
218 |
+
)
|
219 |
+
|
220 |
+
gr.Markdown(
|
221 |
+
"""
|
222 |
+
Create unique t-shirt designs using Deradh's AI.
|
223 |
+
Describe your design idea and select a style to generate professional-quality artwork
|
224 |
+
perfect for custom t-shirts.
|
225 |
+
""",
|
226 |
+
elem_classes=["subtitle"]
|
227 |
+
)
|
228 |
+
|
229 |
+
with gr.Row():
|
230 |
+
with gr.Column(scale=2):
|
231 |
+
prompt = gr.Text(
|
232 |
+
label="Design Description",
|
233 |
+
show_label=False,
|
234 |
+
max_lines=1,
|
235 |
+
placeholder="Describe your t-shirt design idea",
|
236 |
+
container=False,
|
237 |
+
elem_classes=["design-input"]
|
238 |
+
)
|
239 |
+
with gr.Column(scale=1):
|
240 |
+
style = gr.Dropdown(
|
241 |
+
choices=[""] + styles,
|
242 |
+
value="",
|
243 |
+
label="Style",
|
244 |
+
container=False
|
245 |
+
)
|
246 |
+
with gr.Column(scale=1):
|
247 |
+
tshirt_color = gr.Dropdown(
|
248 |
+
choices=list(TSHIRT_COLORS.keys()),
|
249 |
+
value="White",
|
250 |
+
label="T-Shirt Color",
|
251 |
+
container=False
|
252 |
+
)
|
253 |
+
run_button = gr.Button(
|
254 |
+
"✨ Generate",
|
255 |
+
scale=0,
|
256 |
+
elem_classes=["generate-button"]
|
257 |
+
)
|
258 |
+
|
259 |
+
with gr.Row(elem_classes=["results-row"]):
|
260 |
+
result = gr.Image(
|
261 |
+
label="Generated Design",
|
262 |
+
show_label=True,
|
263 |
+
elem_classes=["result-image"]
|
264 |
)
|
265 |
+
preview = gr.Image(
|
266 |
+
label="T-Shirt Preview",
|
267 |
+
show_label=True,
|
268 |
+
elem_classes=["preview-image"]
|
|
|
269 |
)
|
270 |
+
|
271 |
+
with gr.Accordion("🔧 Advanced Settings", open=False):
|
272 |
+
with gr.Group():
|
273 |
+
seed = gr.Slider(
|
274 |
+
label="Design Seed",
|
275 |
+
minimum=0,
|
276 |
+
maximum=MAX_SEED,
|
277 |
+
step=1,
|
278 |
+
value=0,
|
279 |
+
)
|
280 |
+
randomize_seed = gr.Checkbox(
|
281 |
+
label="Randomize Design",
|
282 |
+
value=True
|
283 |
+
)
|
284 |
+
|
285 |
+
with gr.Row():
|
286 |
+
width = gr.Slider(
|
287 |
+
label="Width",
|
288 |
+
minimum=256,
|
289 |
+
maximum=MAX_IMAGE_SIZE,
|
290 |
+
step=32,
|
291 |
+
value=1024,
|
292 |
+
)
|
293 |
+
height = gr.Slider(
|
294 |
+
label="Height",
|
295 |
+
minimum=256,
|
296 |
+
maximum=MAX_IMAGE_SIZE,
|
297 |
+
step=32,
|
298 |
+
value=1024,
|
299 |
+
)
|
300 |
+
|
301 |
+
num_inference_steps = gr.Slider(
|
302 |
+
label="Generation Quality (Steps)",
|
303 |
+
minimum=1,
|
304 |
+
maximum=50,
|
305 |
+
step=1,
|
306 |
+
value=4,
|
307 |
+
)
|
308 |
+
|
309 |
+
gr.Examples(
|
310 |
+
examples=examples,
|
311 |
+
fn=infer,
|
312 |
+
inputs=[prompt, style, tshirt_color],
|
313 |
+
outputs=[result, preview, seed],
|
314 |
+
cache_examples=True
|
315 |
+
)
|
316 |
+
|
317 |
+
gr.on(
|
318 |
+
triggers=[run_button.click, prompt.submit],
|
319 |
+
fn=infer,
|
320 |
+
inputs=[prompt, style, tshirt_color, seed, randomize_seed, width, height, num_inference_steps],
|
321 |
+
outputs=[result, preview, seed]
|
322 |
+
)
|
323 |
|
324 |
+
demo.launch()
|
|
|
|