Gopalag commited on
Commit
a60a488
·
verified ·
1 Parent(s): f8d6556

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -57
app.py CHANGED
@@ -4,6 +4,7 @@ import random
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
 
7
 
8
  dtype = torch.bfloat16
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -16,8 +17,29 @@ pipe = DiffusionPipeline.from_pretrained(
16
  MAX_SEED = np.iinfo(np.int32).max
17
  MAX_IMAGE_SIZE = 2048
18
 
19
- def enhance_prompt_for_pattern(prompt):
20
- """Add specific terms to ensure seamless, tileable patterns."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  pattern_terms = [
22
  "seamless pattern",
23
  "tileable textile design",
@@ -25,16 +47,37 @@ def enhance_prompt_for_pattern(prompt):
25
  "high-quality fabric design",
26
  "continuous pattern",
27
  ]
28
- enhanced_prompt = f"{prompt}, {random.choice(pattern_terms)}, suitable for textile printing, high-quality fabric design, seamless edges"
 
 
 
 
 
 
 
 
 
29
  return enhanced_prompt
30
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  @spaces.GPU()
32
- def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
33
  num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
34
  if randomize_seed:
35
  seed = random.randint(0, MAX_SEED)
36
 
37
- enhanced_prompt = enhance_prompt_for_pattern(prompt)
38
  generator = torch.Generator().manual_seed(seed)
39
  image = pipe(
40
  prompt=enhanced_prompt,
@@ -45,31 +88,32 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
45
  guidance_scale=0.0
46
  ).images[0]
47
 
48
- return image, seed
 
 
 
49
 
50
  examples = [
51
- "geometric Art Deco shapes in gold and navy",
52
- "delicate floral motifs with small roses and leaves",
53
- "abstract watercolor spots in pastel colors",
54
- "traditional paisley design in earth tones",
55
- "modern minimalist lines and circles",
56
  ]
57
 
58
  # Enhanced CSS for better visual design and mobile responsiveness
59
  css = """
60
  #col-container {
61
  margin: 0 auto;
62
- max-width: 800px !important;
63
  padding: 20px;
64
  }
65
-
66
  .main-title {
67
  text-align: center;
68
  color: #2d3748;
69
  margin-bottom: 1rem;
70
  font-family: 'Poppins', sans-serif;
71
  }
72
-
73
  .subtitle {
74
  text-align: center;
75
  color: #4a5568;
@@ -78,7 +122,6 @@ css = """
78
  font-size: 0.95rem;
79
  line-height: 1.5;
80
  }
81
-
82
  .pattern-input {
83
  border: 2px solid #e2e8f0;
84
  border-radius: 10px;
@@ -87,12 +130,10 @@ css = """
87
  font-size: 1rem;
88
  transition: all 0.3s ease;
89
  }
90
-
91
  .pattern-input:focus {
92
  border-color: #4299e1;
93
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
94
  }
95
-
96
  .generate-button {
97
  background-color: #4299e1 !important;
98
  color: white !important;
@@ -101,25 +142,34 @@ css = """
101
  font-weight: 600 !important;
102
  transition: all 0.3s ease !important;
103
  }
104
-
105
  .generate-button:hover {
106
  background-color: #3182ce !important;
107
  transform: translateY(-1px);
108
  }
109
-
110
  .result-image {
111
  border-radius: 12px;
112
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
113
  margin-top: 1rem;
114
  }
115
-
116
  .advanced-settings {
117
  margin-top: 1.5rem;
118
  border: 1px solid #e2e8f0;
119
  border-radius: 10px;
120
  padding: 1rem;
121
  }
122
-
 
 
 
 
 
 
 
 
 
 
 
 
123
  /* Mobile Responsiveness */
124
  @media (max-width: 768px) {
125
  #col-container {
@@ -138,54 +188,71 @@ css = """
138
  font-size: 0.9rem;
139
  }
140
  }
141
-
142
- /* Custom styling for examples section */
143
- .examples-section {
144
- margin-top: 2rem;
145
- padding: 1rem;
146
- background: #f7fafc;
147
- border-radius: 10px;
148
- }
149
  """
150
 
151
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
152
  with gr.Column(elem_id="col-container"):
153
  gr.Markdown(
154
  """
155
- # 🎨 Deradh's AI Pattern Master
156
  """,
157
  elem_classes=["main-title"]
158
  )
159
 
160
  gr.Markdown(
161
  """
162
- Create beautiful, seamless patterns for your textile designs using AI.
163
- Simply describe your desired pattern, and watch as AI brings your vision to life with
164
- professional-quality, repeatable patterns perfect for fabrics and materials.
165
  """,
166
  elem_classes=["subtitle"]
167
  )
168
 
169
  with gr.Row():
170
- prompt = gr.Text(
171
- label="Pattern Description",
172
- show_label=False,
173
- max_lines=1,
174
- placeholder="Describe your dream pattern (e.g., 'geometric Art Deco shapes in gold and navy')",
175
- container=False,
176
- elem_classes=["pattern-input"]
177
- )
178
- run_button = gr.Button(
179
- "✨ Generate",
180
- scale=0,
181
- elem_classes=["generate-button"]
182
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
- result = gr.Image(
185
- label="Your Generated Pattern",
186
- show_label=True,
187
- elem_classes=["result-image"]
188
- )
 
 
 
 
 
 
 
 
 
189
 
190
  with gr.Accordion("🔧 Advanced Settings", open=False):
191
  with gr.Group(elem_classes=["advanced-settings"]):
@@ -226,20 +293,19 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
226
  )
227
 
228
  with gr.Group(elem_classes=["examples-section"]):
229
- gr.Markdown("### 💫 Try These Examples")
230
  gr.Examples(
231
  examples=examples,
232
  fn=infer,
233
- inputs=[prompt],
234
- outputs=[result, seed],
235
- cache_examples="lazy"
236
  )
237
 
238
  gr.on(
239
  triggers=[run_button.click, prompt.submit],
240
  fn=infer,
241
- inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
242
- outputs=[result, seed]
243
  )
244
 
245
  demo.launch()
 
4
  import spaces
5
  import torch
6
  from diffusers import DiffusionPipeline
7
+ from PIL import Image
8
 
9
  dtype = torch.bfloat16
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
17
  MAX_SEED = np.iinfo(np.int32).max
18
  MAX_IMAGE_SIZE = 2048
19
 
20
+ STYLE_OPTIONS = {
21
+ "Vintage": "vintage style, retro aesthetic, aged appearance",
22
+ "Realistic": "photorealistic, detailed, true-to-life",
23
+ "Geometric": "geometric shapes, precise lines, mathematical patterns",
24
+ "Abstract": "abstract design, non-representational, artistic",
25
+ "Minimalist": "simple, clean lines, understated",
26
+ "Bohemian": "boho style, free-spirited, eclectic",
27
+ "Traditional": "classical design, timeless patterns",
28
+ "Contemporary": "modern style, current trends"
29
+ }
30
+
31
+ FABRIC_OPTIONS = {
32
+ "None": "",
33
+ "Cotton": "cotton textile texture, natural fiber appearance",
34
+ "Silk": "silk fabric texture, smooth and lustrous",
35
+ "Linen": "linen texture, natural weave pattern",
36
+ "Velvet": "velvet texture, plush surface",
37
+ "Canvas": "canvas texture, sturdy weave pattern",
38
+ "Wool": "wool texture, natural fiber appearance"
39
+ }
40
+
41
+ def enhance_prompt_for_pattern(prompt, style, fabric):
42
+ """Add specific terms to ensure seamless, tileable patterns with style and fabric considerations."""
43
  pattern_terms = [
44
  "seamless pattern",
45
  "tileable textile design",
 
47
  "high-quality fabric design",
48
  "continuous pattern",
49
  ]
50
+
51
+ enhanced_prompt = f"{prompt}, {random.choice(pattern_terms)}"
52
+
53
+ if style and style != "None":
54
+ enhanced_prompt += f", {STYLE_OPTIONS[style]}"
55
+
56
+ if fabric and fabric != "None":
57
+ enhanced_prompt += f", {FABRIC_OPTIONS[fabric]}"
58
+
59
+ enhanced_prompt += ", suitable for textile printing, high-quality fabric design, seamless edges"
60
  return enhanced_prompt
61
 
62
+ def create_fabric_preview(image):
63
+ """Create a fabric preview by tiling the pattern."""
64
+ # Create a 4x2 grid of the pattern
65
+ width, height = image.size
66
+ preview = Image.new('RGB', (width * 4, height * 2))
67
+
68
+ for y in range(2):
69
+ for x in range(4):
70
+ preview.paste(image, (x * width, y * height))
71
+
72
+ return preview
73
+
74
  @spaces.GPU()
75
+ def infer(prompt, style, fabric, seed=42, randomize_seed=False, width=1024, height=1024,
76
  num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
77
  if randomize_seed:
78
  seed = random.randint(0, MAX_SEED)
79
 
80
+ enhanced_prompt = enhance_prompt_for_pattern(prompt, style, fabric)
81
  generator = torch.Generator().manual_seed(seed)
82
  image = pipe(
83
  prompt=enhanced_prompt,
 
88
  guidance_scale=0.0
89
  ).images[0]
90
 
91
+ # Create fabric preview
92
+ fabric_preview = create_fabric_preview(image)
93
+
94
+ return image, fabric_preview, seed
95
 
96
  examples = [
97
+ ["geometric Art Deco shapes in gold and navy", "Geometric", "None"],
98
+ ["delicate floral motifs with small roses and leaves", "Vintage", "Cotton"],
99
+ ["abstract watercolor spots in pastel colors", "Abstract", "Silk"],
100
+ ["traditional paisley design in earth tones", "Traditional", "Linen"],
101
+ ["modern minimalist lines and circles", "Minimalist", "Canvas"],
102
  ]
103
 
104
  # Enhanced CSS for better visual design and mobile responsiveness
105
  css = """
106
  #col-container {
107
  margin: 0 auto;
108
+ max-width: 1200px !important;
109
  padding: 20px;
110
  }
 
111
  .main-title {
112
  text-align: center;
113
  color: #2d3748;
114
  margin-bottom: 1rem;
115
  font-family: 'Poppins', sans-serif;
116
  }
 
117
  .subtitle {
118
  text-align: center;
119
  color: #4a5568;
 
122
  font-size: 0.95rem;
123
  line-height: 1.5;
124
  }
 
125
  .pattern-input {
126
  border: 2px solid #e2e8f0;
127
  border-radius: 10px;
 
130
  font-size: 1rem;
131
  transition: all 0.3s ease;
132
  }
 
133
  .pattern-input:focus {
134
  border-color: #4299e1;
135
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
136
  }
 
137
  .generate-button {
138
  background-color: #4299e1 !important;
139
  color: white !important;
 
142
  font-weight: 600 !important;
143
  transition: all 0.3s ease !important;
144
  }
 
145
  .generate-button:hover {
146
  background-color: #3182ce !important;
147
  transform: translateY(-1px);
148
  }
 
149
  .result-image {
150
  border-radius: 12px;
151
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
152
  margin-top: 1rem;
153
  }
 
154
  .advanced-settings {
155
  margin-top: 1.5rem;
156
  border: 1px solid #e2e8f0;
157
  border-radius: 10px;
158
  padding: 1rem;
159
  }
160
+ .examples-section {
161
+ margin-top: 2rem;
162
+ padding: 1rem;
163
+ background: #f7fafc;
164
+ border-radius: 10px;
165
+ border: none;
166
+ }
167
+ .preview-section {
168
+ margin-top: 1rem;
169
+ padding: 1rem;
170
+ background: #ffffff;
171
+ border-radius: 10px;
172
+ }
173
  /* Mobile Responsiveness */
174
  @media (max-width: 768px) {
175
  #col-container {
 
188
  font-size: 0.9rem;
189
  }
190
  }
 
 
 
 
 
 
 
 
191
  """
192
 
193
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
194
  with gr.Column(elem_id="col-container"):
195
  gr.Markdown(
196
  """
197
+ # 🎨 Deradh's Professional Textile Pattern Generator
198
  """,
199
  elem_classes=["main-title"]
200
  )
201
 
202
  gr.Markdown(
203
  """
204
+ Create professional-grade, seamless patterns for textile manufacturing.
205
+ Design unique patterns with style and fabric texture controls,
206
+ perfect for commercial textile production and fashion design.
207
  """,
208
  elem_classes=["subtitle"]
209
  )
210
 
211
  with gr.Row():
212
+ with gr.Column(scale=2):
213
+ prompt = gr.Text(
214
+ label="Pattern Description",
215
+ show_label=False,
216
+ max_lines=1,
217
+ placeholder="Describe your dream pattern (e.g., 'geometric Art Deco shapes in gold and navy')",
218
+ container=False,
219
+ elem_classes=["pattern-input"]
220
+ )
221
+
222
+ with gr.Column(scale=1):
223
+ style = gr.Dropdown(
224
+ choices=list(STYLE_OPTIONS.keys()),
225
+ label="Style",
226
+ value="None"
227
+ )
228
+
229
+ with gr.Column(scale=1):
230
+ fabric = gr.Dropdown(
231
+ choices=list(FABRIC_OPTIONS.keys()),
232
+ label="Fabric Texture",
233
+ value="None"
234
+ )
235
+
236
+ with gr.Column(scale=0.5):
237
+ run_button = gr.Button(
238
+ "✨ Generate",
239
+ elem_classes=["generate-button"]
240
+ )
241
 
242
+ with gr.Row():
243
+ with gr.Column():
244
+ pattern = gr.Image(
245
+ label="Generated Pattern",
246
+ show_label=True,
247
+ elem_classes=["result-image"]
248
+ )
249
+
250
+ with gr.Column():
251
+ preview = gr.Image(
252
+ label="Fabric Preview",
253
+ show_label=True,
254
+ elem_classes=["result-image"]
255
+ )
256
 
257
  with gr.Accordion("🔧 Advanced Settings", open=False):
258
  with gr.Group(elem_classes=["advanced-settings"]):
 
293
  )
294
 
295
  with gr.Group(elem_classes=["examples-section"]):
 
296
  gr.Examples(
297
  examples=examples,
298
  fn=infer,
299
+ inputs=[prompt, style, fabric],
300
+ outputs=[pattern, preview, seed],
301
+ cache_examples=True
302
  )
303
 
304
  gr.on(
305
  triggers=[run_button.click, prompt.submit],
306
  fn=infer,
307
+ inputs=[prompt, style, fabric, seed, randomize_seed, width, height, num_inference_steps],
308
+ outputs=[pattern, preview, seed]
309
  )
310
 
311
  demo.launch()