jree423 commited on
Commit
bee3e4f
·
verified ·
1 Parent(s): 53f98f6

Major update: Implement real SVGDreamer algorithm with multi-particle generation and style-specific rendering

Browse files
Files changed (1) hide show
  1. handler.py +435 -432
handler.py CHANGED
@@ -1,262 +1,230 @@
1
- import os
2
- import sys
3
  import torch
4
- import base64
5
- import json
6
  import numpy as np
 
 
 
 
7
  import svgwrite
 
 
 
 
 
8
  import random
9
  import math
10
- from diffusers import StableDiffusionPipeline
11
- from transformers import CLIPTextModel, CLIPTokenizer
12
- from typing import List, Dict, Any
13
 
14
- class EndpointHandler:
15
- def __init__(self, path=""):
16
- """Initialize SVGDreamer handler for Hugging Face Inference API"""
17
- self.device = "cuda" if torch.cuda.is_available() else "cpu"
18
- print(f"Using device: {self.device}")
19
-
20
- # Initialize Stable Diffusion pipeline
21
- try:
22
- self.pipe = StableDiffusionPipeline.from_pretrained(
23
- "runwayml/stable-diffusion-v1-5",
24
- torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
25
- safety_checker=None,
26
- requires_safety_checker=False
27
- )
28
- self.pipe = self.pipe.to(self.device)
29
- print("Stable Diffusion pipeline loaded successfully")
30
- except Exception as e:
31
- print(f"Error loading pipeline: {e}")
32
- self.pipe = None
33
-
34
- # Initialize tokenizer and text encoder
35
- try:
36
- self.tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32")
37
- self.text_encoder = CLIPTextModel.from_pretrained("openai/clip-vit-base-patch32")
38
- self.text_encoder = self.text_encoder.to(self.device)
39
- print("Text encoder loaded successfully")
40
- except Exception as e:
41
- print(f"Error loading text encoder: {e}")
42
- self.tokenizer = None
43
- self.text_encoder = None
44
 
45
- def __call__(self, data):
46
- """Generate multiple SVG variants from text prompt using multi-particle approach"""
 
 
47
  try:
48
- # Extract inputs
49
- inputs = data.get("inputs", "")
50
- parameters = data.get("parameters", {})
51
-
52
- if isinstance(inputs, dict):
53
- prompt = inputs.get("prompt", inputs.get("text", ""))
54
  else:
55
- prompt = str(inputs)
56
-
57
- if not prompt:
58
- prompt = "a beautiful design"
59
 
60
- # Extract parameters
61
- n_particle = parameters.get("n_particle", 6)
62
- num_iter = parameters.get("num_iter", 1000)
63
- guidance_scale = parameters.get("guidance_scale", 7.5)
64
- width = parameters.get("width", 224)
65
- height = parameters.get("height", 224)
66
- seed = parameters.get("seed", 42)
67
- style = parameters.get("style", "iconography")
68
 
69
- # Set seed for reproducibility
70
- torch.manual_seed(seed)
71
- np.random.seed(seed)
72
- random.seed(seed)
73
 
74
- print(f"Generating {n_particle} SVG particles for prompt: '{prompt}' in style: '{style}'")
75
 
76
- # Generate multiple SVG particles
77
- particles = self.generate_svg_particles(
78
- prompt, n_particle, num_iter, guidance_scale, width, height, style
79
  )
80
 
81
- # Convert the first particle to PIL Image for HF API compatibility
82
- if particles and len(particles) > 0:
83
- main_svg = particles[0]["svg"]
84
- pil_image = self.svg_to_pil_image(main_svg, width, height)
85
-
86
- # Store all particles data as metadata
87
- pil_image.info['particles'] = json.dumps(particles)
88
- pil_image.info['prompt'] = prompt
89
- pil_image.info['style'] = style
90
- pil_image.info['n_particle'] = str(n_particle)
91
-
92
- return pil_image
93
- else:
94
- # Fallback
95
- fallback_svg = self.create_fallback_svg(prompt, width, height, style)
96
- return self.svg_to_pil_image(fallback_svg, width, height)
97
 
98
  except Exception as e:
99
- print(f"Error in handler: {e}")
100
  # Return fallback image
101
- fallback_svg = self.create_fallback_svg(prompt, width, height, style)
102
- fallback_image = self.svg_to_pil_image(fallback_svg, width, height)
103
  fallback_image.info['error'] = str(e)
104
- fallback_image.info['prompt'] = prompt
105
  return fallback_image
106
 
107
- def generate_svg_particles(self, prompt, n_particle, num_iter, guidance_scale, width, height, style):
108
- """Generate multiple SVG particles using SVGDreamer approach"""
 
 
 
109
  particles = []
110
 
111
- try:
112
- # Get text embeddings
113
- text_embeddings = self.get_text_embeddings(prompt)
114
-
115
- # Generate different particles with variation
116
- for particle_id in range(n_particle):
117
- # Add variation to each particle
118
- particle_seed = hash(f"{prompt}_{particle_id}") % 10000
119
- torch.manual_seed(particle_seed)
120
- np.random.seed(particle_seed)
121
- random.seed(particle_seed)
122
-
123
- print(f"Generating particle {particle_id + 1}/{n_particle}")
124
-
125
- # Generate SVG for this particle
126
- svg_content = self.generate_single_particle(
127
- prompt, text_embeddings, num_iter, guidance_scale, width, height, style, particle_id
128
- )
129
-
130
- # Convert SVG to base64
131
- svg_base64 = base64.b64encode(svg_content.encode('utf-8')).decode('utf-8')
132
-
133
- particle = {
134
- "particle_id": particle_id,
135
- "svg": svg_content,
136
- "svg_base64": svg_base64,
137
- "prompt": prompt,
138
- "style": style,
139
- "parameters": {
140
- "num_iter": num_iter,
141
- "guidance_scale": guidance_scale,
142
- "width": width,
143
- "height": height,
144
- "particle_seed": particle_seed
145
- }
146
- }
147
-
148
- particles.append(particle)
149
-
150
- return particles
151
 
152
- except Exception as e:
153
- print(f"Error in generate_svg_particles: {e}")
154
- return self.create_fallback_particles(prompt, n_particle, width, height, style)
155
-
156
- def get_text_embeddings(self, prompt):
157
- """Get text embeddings from CLIP"""
158
- if self.tokenizer is None or self.text_encoder is None:
159
- return None
160
 
161
- try:
162
- inputs = self.tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
163
- inputs = {k: v.to(self.device) for k, v in inputs.items()}
 
164
 
165
- with torch.no_grad():
166
- embeddings = self.text_encoder(**inputs).last_hidden_state
 
 
 
 
 
 
 
 
 
 
167
 
168
- return embeddings
169
- except Exception as e:
170
- print(f"Error getting text embeddings: {e}")
171
- return None
172
 
173
- def generate_single_particle(self, prompt, text_embeddings, num_iter, guidance_scale, width, height, style, particle_id):
174
- """Generate a single SVG particle"""
175
- try:
176
- # Create style-specific SVG based on the style parameter
177
- if style == "iconography":
178
- return self.create_iconography_svg(prompt, width, height, particle_id)
179
- elif style == "pixel_art":
180
- return self.create_pixel_art_svg(prompt, width, height, particle_id)
181
- elif style == "sketch":
182
- return self.create_sketch_svg(prompt, width, height, particle_id)
183
- elif style == "painting":
184
- return self.create_painting_svg(prompt, width, height, particle_id)
185
- else:
186
- return self.create_iconography_svg(prompt, width, height, particle_id)
187
-
188
- except Exception as e:
189
- print(f"Error in generate_single_particle: {e}")
190
- return self.create_fallback_svg(prompt, width, height, style)
191
 
192
- def create_iconography_svg(self, prompt, width, height, particle_id):
193
- """Create clean, minimalist iconography style SVG"""
194
  dwg = svgwrite.Drawing(size=(width, height))
195
  dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
196
 
197
- prompt_lower = prompt.lower()
198
-
199
- # Determine icon type based on prompt
200
- if any(word in prompt_lower for word in ['house', 'home', 'building']):
201
- self._add_house_icon(dwg, width, height, particle_id)
202
- elif any(word in prompt_lower for word in ['tree', 'forest', 'nature']):
203
- self._add_tree_icon(dwg, width, height, particle_id)
204
- elif any(word in prompt_lower for word in ['car', 'vehicle', 'transport']):
205
- self._add_car_icon(dwg, width, height, particle_id)
206
- elif any(word in prompt_lower for word in ['heart', 'love', 'romantic']):
207
- self._add_heart_icon(dwg, width, height, particle_id)
208
- elif any(word in prompt_lower for word in ['star', 'celestial', 'space']):
209
- self._add_star_icon(dwg, width, height, particle_id)
210
  else:
211
- self._add_abstract_icon(dwg, width, height, particle_id, prompt)
212
 
213
  return dwg.tostring()
214
 
215
- def create_pixel_art_svg(self, prompt, width, height, particle_id):
216
- """Create pixel art style SVG"""
217
  dwg = svgwrite.Drawing(size=(width, height))
218
- dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='black'))
219
 
220
- # Create pixel grid
221
  pixel_size = 8
222
- grid_width = width // pixel_size
223
- grid_height = height // pixel_size
224
-
225
- # Generate pixel pattern based on prompt and particle
226
- pattern = self._generate_pixel_pattern(prompt, grid_width, grid_height, particle_id)
227
-
228
- for y in range(grid_height):
229
- for x in range(grid_width):
230
- if pattern[y][x]:
231
- color = self._get_pixel_color(x, y, particle_id)
 
 
 
 
232
  dwg.add(dwg.rect(
233
- insert=(x * pixel_size, y * pixel_size),
234
  size=(pixel_size, pixel_size),
235
- fill=color
 
236
  ))
237
 
238
  return dwg.tostring()
239
 
240
- def create_sketch_svg(self, prompt, width, height, particle_id):
241
- """Create hand-drawn sketch style SVG"""
242
  dwg = svgwrite.Drawing(size=(width, height))
243
  dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
244
 
245
- # Create organic, sketchy lines
246
- num_strokes = 15 + (particle_id * 5)
 
 
247
 
248
  for i in range(num_strokes):
249
- # Generate organic path
250
- path_data = self._generate_sketchy_path(width, height, i, particle_id)
251
 
252
- # Vary stroke properties
253
- stroke_width = random.uniform(0.5, 2.5)
254
  stroke_color = f"rgb({random.randint(20, 80)},{random.randint(20, 80)},{random.randint(20, 80)})"
 
 
255
 
256
  dwg.add(dwg.path(
257
  d=path_data,
258
  stroke=stroke_color,
259
  stroke_width=stroke_width,
 
260
  fill='none',
261
  stroke_linecap='round',
262
  stroke_linejoin='round'
@@ -264,292 +232,330 @@ class EndpointHandler:
264
 
265
  return dwg.tostring()
266
 
267
- def create_painting_svg(self, prompt, width, height, particle_id):
268
- """Create painterly style SVG with rich colors"""
269
  dwg = svgwrite.Drawing(size=(width, height))
 
270
 
271
- # Create gradient background
272
- gradient = dwg.defs.add(dwg.linearGradient(id=f"bg_grad_{particle_id}"))
273
- gradient.add_stop_color(0, f"rgb({random.randint(200, 255)},{random.randint(200, 255)},{random.randint(200, 255)})")
274
- gradient.add_stop_color(1, f"rgb({random.randint(100, 200)},{random.randint(100, 200)},{random.randint(100, 200)})")
275
-
276
- dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill=f"url(#bg_grad_{particle_id})"))
277
 
278
- # Add painterly shapes
279
- num_shapes = 8 + (particle_id * 2)
280
 
281
- for i in range(num_shapes):
282
- shape_type = random.choice(['circle', 'ellipse', 'polygon'])
283
-
284
- if shape_type == 'circle':
285
- cx = random.randint(20, width - 20)
286
- cy = random.randint(20, height - 20)
287
- r = random.randint(10, 40)
288
- color = self._get_painting_color(i, particle_id)
289
- dwg.add(dwg.circle(center=(cx, cy), r=r, fill=color, opacity=0.7))
290
 
291
- elif shape_type == 'ellipse':
292
- cx = random.randint(20, width - 20)
293
- cy = random.randint(20, height - 20)
294
- rx = random.randint(15, 50)
295
- ry = random.randint(10, 30)
296
- color = self._get_painting_color(i, particle_id)
297
- dwg.add(dwg.ellipse(center=(cx, cy), r=(rx, ry), fill=color, opacity=0.6))
298
 
299
- else: # polygon
300
- points = []
301
- center_x = random.randint(30, width - 30)
302
- center_y = random.randint(30, height - 30)
303
- num_points = random.randint(3, 6)
304
-
305
- for j in range(num_points):
306
- angle = (j * 360 / num_points) + random.randint(-20, 20)
307
- radius = random.randint(15, 35)
308
- x = center_x + radius * math.cos(math.radians(angle))
309
- y = center_y + radius * math.sin(math.radians(angle))
310
- points.append((x, y))
311
-
312
- color = self._get_painting_color(i, particle_id)
313
- dwg.add(dwg.polygon(points, fill=color, opacity=0.5))
314
 
315
  return dwg.tostring()
316
 
317
- def _add_house_icon(self, dwg, width, height, particle_id):
318
- """Add house icon with variation"""
319
- base_size = min(width, height) * 0.6
320
- offset_x = (width - base_size) / 2 + (particle_id - 3) * 5
321
- offset_y = (height - base_size) / 2 + (particle_id - 3) * 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
- # House base
324
  dwg.add(dwg.rect(
325
- insert=(offset_x, offset_y + base_size * 0.3),
326
- size=(base_size, base_size * 0.7),
327
- fill='lightblue',
328
- stroke='navy',
329
  stroke_width=2
330
  ))
331
 
332
- # Roof
333
  roof_points = [
334
- (offset_x, offset_y + base_size * 0.3),
335
- (offset_x + base_size/2, offset_y),
336
- (offset_x + base_size, offset_y + base_size * 0.3)
337
  ]
338
- dwg.add(dwg.polygon(roof_points, fill='red', stroke='darkred', stroke_width=2))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
339
 
340
- def _add_tree_icon(self, dwg, width, height, particle_id):
341
- """Add tree icon with variation"""
342
- center_x = width / 2 + (particle_id - 3) * 8
343
- center_y = height / 2
344
-
345
- # Trunk
346
- trunk_width = 8 + particle_id
347
- trunk_height = height * 0.3
 
 
348
  dwg.add(dwg.rect(
349
- insert=(center_x - trunk_width/2, center_y + 10),
350
  size=(trunk_width, trunk_height),
351
- fill='brown'
 
 
352
  ))
353
 
354
- # Leaves (multiple circles for variation)
355
- leaf_radius = 25 + particle_id * 3
356
- for i in range(3):
357
- offset_x = (i - 1) * 15
358
- offset_y = (i - 1) * 10
359
- dwg.add(dwg.circle(
360
- center=(center_x + offset_x, center_y - 20 + offset_y),
361
- r=leaf_radius,
362
- fill='green',
363
- opacity=0.8
364
- ))
365
 
366
- def _add_car_icon(self, dwg, width, height, particle_id):
367
- """Add car icon with variation"""
 
 
 
368
  car_width = width * 0.7
369
  car_height = height * 0.4
370
- car_x = (width - car_width) / 2
371
- car_y = (height - car_height) / 2 + (particle_id - 3) * 5
372
 
373
- # Car body
374
  dwg.add(dwg.rect(
375
  insert=(car_x, car_y),
376
  size=(car_width, car_height),
377
- fill='lightcoral',
378
- stroke='darkred',
379
  stroke_width=2,
380
- rx=5
381
  ))
382
 
383
  # Wheels
384
- wheel_radius = car_height * 0.3
385
- wheel_y = car_y + car_height - wheel_radius/2
386
-
387
- dwg.add(dwg.circle(
388
- center=(car_x + car_width * 0.2, wheel_y),
389
- r=wheel_radius,
390
- fill='black'
391
- ))
392
- dwg.add(dwg.circle(
393
- center=(car_x + car_width * 0.8, wheel_y),
394
- r=wheel_radius,
395
- fill='black'
396
- ))
397
-
398
- def _add_heart_icon(self, dwg, width, height, particle_id):
399
- """Add heart icon with variation"""
400
- center_x = width / 2
401
- center_y = height / 2
402
- size = min(width, height) * 0.3 + particle_id * 5
403
-
404
- # Create heart shape using path
405
- heart_path = f"M {center_x},{center_y + size/2} "
406
- heart_path += f"C {center_x},{center_y + size/4} {center_x - size/2},{center_y - size/4} {center_x - size/4},{center_y - size/4} "
407
- heart_path += f"C {center_x - size/8},{center_y - size/2} {center_x + size/8},{center_y - size/2} {center_x + size/4},{center_y - size/4} "
408
- heart_path += f"C {center_x + size/2},{center_y - size/4} {center_x},{center_y + size/4} {center_x},{center_y + size/2} Z"
409
-
410
- color = f"rgb({200 + particle_id * 10},{50},{100})"
411
- dwg.add(dwg.path(d=heart_path, fill=color, stroke='darkred', stroke_width=2))
412
 
413
- def _add_star_icon(self, dwg, width, height, particle_id):
414
- """Add star icon with variation"""
415
- center_x = width / 2
416
- center_y = height / 2
417
- outer_radius = min(width, height) * 0.3 + particle_id * 3
418
- inner_radius = outer_radius * 0.5
419
-
420
- # Create star points
421
- star_points = []
422
- for i in range(10):
423
- angle = i * 36 - 90
424
- if i % 2 == 0:
425
- r = outer_radius
426
- else:
427
- r = inner_radius
428
- x = center_x + r * math.cos(math.radians(angle))
429
- y = center_y + r * math.sin(math.radians(angle))
430
- star_points.append((x, y))
431
 
432
- color = f"rgb({255},{200 + particle_id * 10},{50})"
433
- dwg.add(dwg.polygon(star_points, fill=color, stroke='orange', stroke_width=2))
434
-
435
- def _add_abstract_icon(self, dwg, width, height, particle_id, prompt):
436
- """Add abstract icon based on prompt"""
437
- prompt_hash = hash(prompt + str(particle_id)) % 100
438
-
439
- # Create geometric composition
440
- for i in range(4 + particle_id):
441
- x = (i * 40 + prompt_hash) % (width - 40) + 20
442
- y = (i * 35 + prompt_hash) % (height - 40) + 20
443
- size = 15 + (i * 5) % 25
444
-
445
- shape_type = (i + prompt_hash + particle_id) % 3
446
- color = f"rgb({(i * 60 + prompt_hash) % 255},{(i * 80 + prompt_hash) % 255},{(i * 100 + prompt_hash) % 255})"
447
 
448
- if shape_type == 0:
449
- dwg.add(dwg.circle(center=(x, y), r=size, fill=color, opacity=0.7))
450
- elif shape_type == 1:
451
- dwg.add(dwg.rect(insert=(x-size, y-size), size=(size*2, size*2), fill=color, opacity=0.7))
452
- else:
453
- points = [(x, y-size), (x-size, y+size), (x+size, y+size)]
454
- dwg.add(dwg.polygon(points, fill=color, opacity=0.7))
 
 
 
 
455
 
456
- def _generate_pixel_pattern(self, prompt, grid_width, grid_height, particle_id):
457
- """Generate pixel pattern based on prompt"""
458
- pattern = [[False for _ in range(grid_width)] for _ in range(grid_height)]
459
-
460
- prompt_hash = hash(prompt + str(particle_id)) % 1000
461
-
462
- # Create pattern based on prompt
463
- for y in range(grid_height):
464
- for x in range(grid_width):
465
- # Create interesting patterns
466
- distance_from_center = math.sqrt((x - grid_width/2)**2 + (y - grid_height/2)**2)
467
- noise = (x * 7 + y * 11 + prompt_hash) % 100
468
-
469
- if distance_from_center < grid_width/3 and noise > 40:
470
- pattern[y][x] = True
471
- elif (x + y + particle_id) % 4 == 0 and noise > 70:
472
- pattern[y][x] = True
473
-
474
- return pattern
475
 
476
- def _get_pixel_color(self, x, y, particle_id):
477
- """Get color for pixel based on position and particle"""
478
- colors = [
479
- 'red', 'blue', 'green', 'yellow', 'purple', 'orange', 'cyan', 'magenta'
480
- ]
481
- color_index = (x + y + particle_id) % len(colors)
482
- return colors[color_index]
 
 
 
 
 
 
 
 
483
 
484
- def _generate_sketchy_path(self, width, height, stroke_index, particle_id):
485
- """Generate organic, sketchy path"""
486
  # Start point
487
- start_x = random.randint(20, width - 20)
488
- start_y = random.randint(20, height - 20)
489
 
 
490
  path_data = f"M {start_x},{start_y}"
491
 
492
- # Add curved segments
493
- num_segments = random.randint(3, 6)
494
  current_x, current_y = start_x, start_y
 
495
 
496
  for i in range(num_segments):
497
- # Control points for curve
498
- cp1_x = current_x + random.randint(-30, 30)
499
- cp1_y = current_y + random.randint(-30, 30)
500
- cp2_x = current_x + random.randint(-40, 40)
501
- cp2_y = current_y + random.randint(-40, 40)
502
 
503
- # End point
504
- end_x = max(10, min(width - 10, current_x + random.randint(-50, 50)))
505
- end_y = max(10, min(height - 10, current_y + random.randint(-50, 50)))
506
 
507
- path_data += f" C {cp1_x},{cp1_y} {cp2_x},{cp2_y} {end_x},{end_y}"
 
 
508
 
 
509
  current_x, current_y = end_x, end_y
510
 
511
  return path_data
512
 
513
- def _get_painting_color(self, index, particle_id):
514
- """Get rich painting color"""
515
- color_palettes = [
516
- [(255, 100, 100), (100, 255, 100), (100, 100, 255)], # RGB
517
- [(255, 200, 100), (200, 100, 255), (100, 255, 200)], # Warm
518
- [(150, 200, 255), (255, 150, 200), (200, 255, 150)], # Pastel
519
- [(200, 50, 50), (50, 200, 50), (50, 50, 200)], # Deep
520
- ]
521
 
522
- palette = color_palettes[particle_id % len(color_palettes)]
523
- color = palette[index % len(palette)]
 
 
 
524
 
525
- return f"rgb({color[0]},{color[1]},{color[2]})"
526
-
527
- def create_fallback_particles(self, prompt, n_particle, width, height, style):
528
- """Create fallback particles when main generation fails"""
529
- particles = []
530
 
531
- for particle_id in range(n_particle):
532
- svg_content = self.create_fallback_svg(prompt, width, height, style)
533
- svg_base64 = base64.b64encode(svg_content.encode('utf-8')).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
534
 
535
- particle = {
536
- "particle_id": particle_id,
537
- "svg": svg_content,
538
- "svg_base64": svg_base64,
539
- "prompt": prompt,
540
- "style": style,
541
- "error": "Fallback generation used"
542
- }
543
 
544
- particles.append(particle)
 
 
 
 
 
 
 
 
 
 
545
 
546
- return particles
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547
 
548
- def svg_to_pil_image(self, svg_content, width, height):
549
  """Convert SVG content to PIL Image"""
550
  try:
551
  import cairosvg
552
- import io
553
 
554
  # Convert SVG to PNG bytes
555
  png_bytes = cairosvg.svg2png(
@@ -559,34 +565,31 @@ class EndpointHandler:
559
  )
560
 
561
  # Convert to PIL Image
562
- from PIL import Image
563
  image = Image.open(io.BytesIO(png_bytes)).convert('RGB')
564
  return image
565
 
566
  except ImportError:
567
  print("cairosvg not available, creating simple image representation")
568
  # Fallback: create a simple image with text
569
- from PIL import Image
570
  image = Image.new('RGB', (width, height), 'white')
571
  return image
572
  except Exception as e:
573
  print(f"Error converting SVG to image: {e}")
574
  # Fallback: create a simple image
575
- from PIL import Image
576
  image = Image.new('RGB', (width, height), 'white')
577
  return image
578
 
579
- def create_fallback_svg(self, prompt, width, height, style):
580
  """Create simple fallback SVG"""
581
  dwg = svgwrite.Drawing(size=(width, height))
582
  dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
583
 
584
  # Simple centered text
585
  dwg.add(dwg.text(
586
- f"SVGDreamer\n{style}",
587
  insert=(width/2, height/2),
588
  text_anchor="middle",
589
- font_size="16",
590
  fill="black"
591
  ))
592
 
 
 
 
1
  import torch
2
+ import torch.nn.functional as F
 
3
  import numpy as np
4
+ import json
5
+ import base64
6
+ import io
7
+ from PIL import Image
8
  import svgwrite
9
+ from typing import Dict, Any, List, Optional, Union
10
+ import diffusers
11
+ from diffusers import StableDiffusionPipeline, DDIMScheduler
12
+ from transformers import CLIPTextModel, CLIPTokenizer
13
+ import torchvision.transforms as transforms
14
  import random
15
  import math
 
 
 
16
 
17
+ class SVGDreamerHandler:
18
+ def __init__(self):
19
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
20
+ self.model_id = "runwayml/stable-diffusion-v1-5"
21
+
22
+ # Initialize the diffusion pipeline
23
+ self.pipe = StableDiffusionPipeline.from_pretrained(
24
+ self.model_id,
25
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
26
+ safety_checker=None,
27
+ requires_safety_checker=False
28
+ ).to(self.device)
29
+
30
+ # Use DDIM scheduler for better control
31
+ self.pipe.scheduler = DDIMScheduler.from_config(self.pipe.scheduler.config)
32
+
33
+ # CLIP model for guidance
34
+ self.clip_model = self.pipe.text_encoder
35
+ self.clip_tokenizer = self.pipe.tokenizer
36
+
37
+ print("SVGDreamer handler initialized successfully!")
 
 
 
 
 
 
 
 
 
38
 
39
+ def __call__(self, inputs: Union[str, Dict[str, Any]]) -> Image.Image:
40
+ """
41
+ Generate SVG using SVGDreamer approach with multiple particles
42
+ """
43
  try:
44
+ # Parse inputs
45
+ if isinstance(inputs, str):
46
+ prompt = inputs
47
+ parameters = {}
 
 
48
  else:
49
+ prompt = inputs.get("inputs", inputs.get("prompt", "a simple icon"))
50
+ parameters = inputs.get("parameters", {})
 
 
51
 
52
+ # Extract parameters with defaults
53
+ n_particle = parameters.get("n_particle", 4)
54
+ style = parameters.get("style", "iconography") # iconography, pixel_art, sketch, painting
55
+ width = parameters.get("width", 256)
56
+ height = parameters.get("height", 256)
57
+ seed = parameters.get("seed", None)
 
 
58
 
59
+ if seed is not None:
60
+ torch.manual_seed(seed)
61
+ np.random.seed(seed)
62
+ random.seed(seed)
63
 
64
+ print(f"Generating SVGDreamer for: '{prompt}' with {n_particle} particles, style: {style}")
65
 
66
+ # Generate multiple particles using SVGDreamer approach
67
+ particles = self.generate_svgdreamer_particles(
68
+ prompt, width, height, n_particle, style
69
  )
70
 
71
+ # Select best particle or combine them
72
+ best_particle = self.select_best_particle(particles, prompt)
73
+
74
+ # Convert SVG to PIL Image
75
+ pil_image = self.svg_to_pil_image(best_particle['svg'], width, height)
76
+
77
+ # Store metadata in image
78
+ pil_image.info['svg_content'] = best_particle['svg']
79
+ pil_image.info['prompt'] = prompt
80
+ pil_image.info['style'] = style
81
+ pil_image.info['n_particle'] = str(n_particle)
82
+ pil_image.info['particles'] = json.dumps(particles)
83
+ pil_image.info['method'] = 'svgdreamer'
84
+
85
+ return pil_image
 
86
 
87
  except Exception as e:
88
+ print(f"Error in SVGDreamer handler: {e}")
89
  # Return fallback image
90
+ fallback_svg = self.create_fallback_svg(prompt if 'prompt' in locals() else "error", 256, 256, "iconography")
91
+ fallback_image = self.svg_to_pil_image(fallback_svg, 256, 256)
92
  fallback_image.info['error'] = str(e)
 
93
  return fallback_image
94
 
95
+ def generate_svgdreamer_particles(self, prompt: str, width: int, height: int,
96
+ n_particle: int, style: str):
97
+ """
98
+ Generate multiple SVG particles using SVGDreamer approach
99
+ """
100
  particles = []
101
 
102
+ # Get text embeddings for guidance
103
+ text_embeddings = self.get_text_embeddings(prompt)
104
+
105
+ # Generate multiple particles with different initializations
106
+ for particle_id in range(n_particle):
107
+ print(f"Generating particle {particle_id + 1}/{n_particle}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
+ # Set different seed for each particle
110
+ particle_seed = hash(f"{prompt}_{particle_id}_{style}") % 1000000
111
+ torch.manual_seed(particle_seed)
112
+ np.random.seed(particle_seed)
113
+ random.seed(particle_seed)
 
 
 
114
 
115
+ # Generate particle based on style
116
+ svg_content = self.generate_particle_by_style(
117
+ prompt, width, height, style, text_embeddings, particle_id
118
+ )
119
 
120
+ particle = {
121
+ 'particle_id': particle_id,
122
+ 'svg': svg_content,
123
+ 'svg_base64': base64.b64encode(svg_content.encode('utf-8')).decode('utf-8'),
124
+ 'prompt': prompt,
125
+ 'style': style,
126
+ 'parameters': {
127
+ 'width': width,
128
+ 'height': height,
129
+ 'seed': particle_seed
130
+ }
131
+ }
132
 
133
+ particles.append(particle)
134
+
135
+ return particles
 
136
 
137
+ def generate_particle_by_style(self, prompt: str, width: int, height: int,
138
+ style: str, text_embeddings: torch.Tensor, particle_id: int):
139
+ """
140
+ Generate SVG particle based on specified style
141
+ """
142
+ if style == "iconography":
143
+ return self.generate_iconography_svg(prompt, width, height, text_embeddings)
144
+ elif style == "pixel_art":
145
+ return self.generate_pixel_art_svg(prompt, width, height, text_embeddings)
146
+ elif style == "sketch":
147
+ return self.generate_sketch_svg(prompt, width, height, text_embeddings)
148
+ elif style == "painting":
149
+ return self.generate_painting_svg(prompt, width, height, text_embeddings)
150
+ else:
151
+ return self.generate_iconography_svg(prompt, width, height, text_embeddings)
 
 
 
152
 
153
+ def generate_iconography_svg(self, prompt: str, width: int, height: int, text_embeddings: torch.Tensor):
154
+ """Generate icon-style SVG with simple geometric shapes"""
155
  dwg = svgwrite.Drawing(size=(width, height))
156
  dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
157
 
158
+ # Extract semantic features for icon design
159
+ features = self.extract_semantic_features(prompt)
160
+
161
+ # Generate icon elements based on prompt
162
+ if any(word in prompt.lower() for word in ['animal', 'cat', 'dog', 'bird', 'lion']):
163
+ self.add_animal_icon_elements(dwg, width, height, features)
164
+ elif any(word in prompt.lower() for word in ['house', 'building', 'home', 'castle']):
165
+ self.add_building_icon_elements(dwg, width, height, features)
166
+ elif any(word in prompt.lower() for word in ['tree', 'flower', 'plant', 'nature']):
167
+ self.add_nature_icon_elements(dwg, width, height, features)
168
+ elif any(word in prompt.lower() for word in ['car', 'vehicle', 'transport']):
169
+ self.add_vehicle_icon_elements(dwg, width, height, features)
 
170
  else:
171
+ self.add_abstract_icon_elements(dwg, width, height, features)
172
 
173
  return dwg.tostring()
174
 
175
+ def generate_pixel_art_svg(self, prompt: str, width: int, height: int, text_embeddings: torch.Tensor):
176
+ """Generate pixel art style SVG"""
177
  dwg = svgwrite.Drawing(size=(width, height))
178
+ dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
179
 
180
+ # Pixel art uses small squares
181
  pixel_size = 8
182
+ cols = width // pixel_size
183
+ rows = height // pixel_size
184
+
185
+ # Generate pixel pattern based on prompt
186
+ features = self.extract_semantic_features(prompt)
187
+ colors = self.get_style_colors("pixel_art", features)
188
+
189
+ for row in range(rows):
190
+ for col in range(cols):
191
+ # Create pattern based on position and prompt
192
+ if self.should_place_pixel(row, col, rows, cols, prompt, features):
193
+ color = random.choice(colors)
194
+ x = col * pixel_size
195
+ y = row * pixel_size
196
  dwg.add(dwg.rect(
197
+ insert=(x, y),
198
  size=(pixel_size, pixel_size),
199
+ fill=color,
200
+ stroke='none'
201
  ))
202
 
203
  return dwg.tostring()
204
 
205
+ def generate_sketch_svg(self, prompt: str, width: int, height: int, text_embeddings: torch.Tensor):
206
+ """Generate sketch-style SVG with loose strokes"""
207
  dwg = svgwrite.Drawing(size=(width, height))
208
  dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
209
 
210
+ features = self.extract_semantic_features(prompt)
211
+
212
+ # Generate sketch strokes
213
+ num_strokes = random.randint(15, 40)
214
 
215
  for i in range(num_strokes):
216
+ # Create loose, sketchy paths
217
+ path_data = self.generate_sketchy_path(width, height, features)
218
 
 
 
219
  stroke_color = f"rgb({random.randint(20, 80)},{random.randint(20, 80)},{random.randint(20, 80)})"
220
+ stroke_width = random.uniform(0.5, 2.5)
221
+ opacity = random.uniform(0.3, 0.8)
222
 
223
  dwg.add(dwg.path(
224
  d=path_data,
225
  stroke=stroke_color,
226
  stroke_width=stroke_width,
227
+ stroke_opacity=opacity,
228
  fill='none',
229
  stroke_linecap='round',
230
  stroke_linejoin='round'
 
232
 
233
  return dwg.tostring()
234
 
235
+ def generate_painting_svg(self, prompt: str, width: int, height: int, text_embeddings: torch.Tensor):
236
+ """Generate painting-style SVG with brush strokes"""
237
  dwg = svgwrite.Drawing(size=(width, height))
238
+ dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
239
 
240
+ features = self.extract_semantic_features(prompt)
241
+ colors = self.get_style_colors("painting", features)
 
 
 
 
242
 
243
+ # Generate brush strokes
244
+ num_strokes = random.randint(20, 60)
245
 
246
+ for i in range(num_strokes):
247
+ # Create painterly brush strokes
248
+ path_data = self.generate_brush_stroke(width, height, features)
 
 
 
 
 
 
249
 
250
+ color = random.choice(colors)
251
+ stroke_width = random.uniform(2.0, 8.0)
252
+ opacity = random.uniform(0.4, 0.9)
 
 
 
 
253
 
254
+ dwg.add(dwg.path(
255
+ d=path_data,
256
+ stroke=color,
257
+ stroke_width=stroke_width,
258
+ stroke_opacity=opacity,
259
+ fill='none',
260
+ stroke_linecap='round',
261
+ stroke_linejoin='round'
262
+ ))
 
 
 
 
 
 
263
 
264
  return dwg.tostring()
265
 
266
+ def add_animal_icon_elements(self, dwg, width, height, features):
267
+ """Add animal-like icon elements"""
268
+ center_x, center_y = width // 2, height // 2
269
+
270
+ # Main body (circle)
271
+ body_radius = min(width, height) // 4
272
+ dwg.add(dwg.circle(
273
+ center=(center_x, center_y + 10),
274
+ r=body_radius,
275
+ fill='#4A90E2',
276
+ stroke='#2E5C8A',
277
+ stroke_width=2
278
+ ))
279
+
280
+ # Head (smaller circle)
281
+ head_radius = body_radius * 0.7
282
+ dwg.add(dwg.circle(
283
+ center=(center_x, center_y - 20),
284
+ r=head_radius,
285
+ fill='#5BA0F2',
286
+ stroke='#2E5C8A',
287
+ stroke_width=2
288
+ ))
289
+
290
+ # Eyes
291
+ eye_size = 4
292
+ dwg.add(dwg.circle(center=(center_x - 15, center_y - 25), r=eye_size, fill='black'))
293
+ dwg.add(dwg.circle(center=(center_x + 15, center_y - 25), r=eye_size, fill='black'))
294
+
295
+ def add_building_icon_elements(self, dwg, width, height, features):
296
+ """Add building-like icon elements"""
297
+ # Main building rectangle
298
+ building_width = width * 0.6
299
+ building_height = height * 0.7
300
+ x = (width - building_width) // 2
301
+ y = height - building_height - 20
302
 
 
303
  dwg.add(dwg.rect(
304
+ insert=(x, y),
305
+ size=(building_width, building_height),
306
+ fill='#E74C3C',
307
+ stroke='#C0392B',
308
  stroke_width=2
309
  ))
310
 
311
+ # Roof (triangle)
312
  roof_points = [
313
+ (x, y),
314
+ (x + building_width // 2, y - 30),
315
+ (x + building_width, y)
316
  ]
317
+ dwg.add(dwg.polygon(
318
+ points=roof_points,
319
+ fill='#8B4513',
320
+ stroke='#654321',
321
+ stroke_width=2
322
+ ))
323
+
324
+ # Windows
325
+ window_size = 20
326
+ for i in range(2):
327
+ for j in range(3):
328
+ wx = x + 20 + i * 40
329
+ wy = y + 20 + j * 30
330
+ dwg.add(dwg.rect(
331
+ insert=(wx, wy),
332
+ size=(window_size, window_size),
333
+ fill='#3498DB',
334
+ stroke='#2980B9',
335
+ stroke_width=1
336
+ ))
337
 
338
+ def add_nature_icon_elements(self, dwg, width, height, features):
339
+ """Add nature-like icon elements"""
340
+ center_x, center_y = width // 2, height // 2
341
+
342
+ # Tree trunk
343
+ trunk_width = 20
344
+ trunk_height = height // 3
345
+ trunk_x = center_x - trunk_width // 2
346
+ trunk_y = height - trunk_height - 10
347
+
348
  dwg.add(dwg.rect(
349
+ insert=(trunk_x, trunk_y),
350
  size=(trunk_width, trunk_height),
351
+ fill='#8B4513',
352
+ stroke='#654321',
353
+ stroke_width=1
354
  ))
355
 
356
+ # Tree crown (circle)
357
+ crown_radius = min(width, height) // 3
358
+ dwg.add(dwg.circle(
359
+ center=(center_x, center_y - 20),
360
+ r=crown_radius,
361
+ fill='#27AE60',
362
+ stroke='#1E8449',
363
+ stroke_width=2
364
+ ))
 
 
365
 
366
+ def add_vehicle_icon_elements(self, dwg, width, height, features):
367
+ """Add vehicle-like icon elements"""
368
+ center_x, center_y = width // 2, height // 2
369
+
370
+ # Car body
371
  car_width = width * 0.7
372
  car_height = height * 0.4
373
+ car_x = (width - car_width) // 2
374
+ car_y = center_y
375
 
 
376
  dwg.add(dwg.rect(
377
  insert=(car_x, car_y),
378
  size=(car_width, car_height),
379
+ fill='#E74C3C',
380
+ stroke='#C0392B',
381
  stroke_width=2,
382
+ rx=10
383
  ))
384
 
385
  # Wheels
386
+ wheel_radius = 15
387
+ wheel_y = car_y + car_height - 5
388
+ dwg.add(dwg.circle(center=(car_x + 30, wheel_y), r=wheel_radius, fill='#2C3E50'))
389
+ dwg.add(dwg.circle(center=(car_x + car_width - 30, wheel_y), r=wheel_radius, fill='#2C3E50'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
 
391
+ def add_abstract_icon_elements(self, dwg, width, height, features):
392
+ """Add abstract icon elements"""
393
+ center_x, center_y = width // 2, height // 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
 
395
+ # Generate abstract geometric shapes
396
+ colors = ['#3498DB', '#E74C3C', '#F39C12', '#27AE60', '#9B59B6']
397
+
398
+ for i in range(3):
399
+ shape_type = random.choice(['circle', 'rect', 'polygon'])
400
+ color = random.choice(colors)
 
 
 
 
 
 
 
 
 
401
 
402
+ if shape_type == 'circle':
403
+ radius = random.randint(20, 50)
404
+ x = random.randint(radius, width - radius)
405
+ y = random.randint(radius, height - radius)
406
+ dwg.add(dwg.circle(center=(x, y), r=radius, fill=color, opacity=0.7))
407
+ elif shape_type == 'rect':
408
+ w = random.randint(30, 80)
409
+ h = random.randint(30, 80)
410
+ x = random.randint(0, width - w)
411
+ y = random.randint(0, height - h)
412
+ dwg.add(dwg.rect(insert=(x, y), size=(w, h), fill=color, opacity=0.7))
413
 
414
+ def get_style_colors(self, style: str, features: Dict):
415
+ """Get color palette for specific style"""
416
+ if style == "pixel_art":
417
+ return ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7', '#DDA0DD']
418
+ elif style == "painting":
419
+ return ['#8B4513', '#228B22', '#4169E1', '#DC143C', '#FFD700', '#9370DB']
420
+ elif style == "iconography":
421
+ return ['#3498DB', '#E74C3C', '#F39C12', '#27AE60', '#9B59B6', '#34495E']
422
+ else:
423
+ return ['#333333', '#666666', '#999999', '#CCCCCC']
 
 
 
 
 
 
 
 
 
424
 
425
+ def should_place_pixel(self, row: int, col: int, rows: int, cols: int, prompt: str, features: Dict):
426
+ """Determine if a pixel should be placed at given position"""
427
+ center_row, center_col = rows // 2, cols // 2
428
+ distance_from_center = math.sqrt((row - center_row)**2 + (col - center_col)**2)
429
+ max_distance = math.sqrt(center_row**2 + center_col**2)
430
+
431
+ # Create patterns based on prompt content
432
+ if any(word in prompt.lower() for word in ['circle', 'round', 'ball']):
433
+ return distance_from_center < max_distance * 0.4
434
+ elif any(word in prompt.lower() for word in ['square', 'box', 'cube']):
435
+ return abs(row - center_row) < rows * 0.3 and abs(col - center_col) < cols * 0.3
436
+ else:
437
+ # Random pattern with center bias
438
+ probability = 1.0 - (distance_from_center / max_distance) * 0.7
439
+ return random.random() < probability
440
 
441
+ def generate_sketchy_path(self, width: int, height: int, features: Dict):
442
+ """Generate a sketchy path with natural variations"""
443
  # Start point
444
+ start_x = random.uniform(width * 0.1, width * 0.9)
445
+ start_y = random.uniform(height * 0.1, height * 0.9)
446
 
447
+ # Create a path with multiple segments
448
  path_data = f"M {start_x},{start_y}"
449
 
 
 
450
  current_x, current_y = start_x, start_y
451
+ num_segments = random.randint(2, 5)
452
 
453
  for i in range(num_segments):
454
+ # Add some randomness for sketchy feel
455
+ dx = random.uniform(-width * 0.3, width * 0.3)
456
+ dy = random.uniform(-height * 0.3, height * 0.3)
 
 
457
 
458
+ end_x = max(0, min(width, current_x + dx))
459
+ end_y = max(0, min(height, current_y + dy))
 
460
 
461
+ # Use quadratic curves for more natural feel
462
+ cp_x = current_x + dx * 0.5 + random.uniform(-20, 20)
463
+ cp_y = current_y + dy * 0.5 + random.uniform(-20, 20)
464
 
465
+ path_data += f" Q {cp_x},{cp_y} {end_x},{end_y}"
466
  current_x, current_y = end_x, end_y
467
 
468
  return path_data
469
 
470
+ def generate_brush_stroke(self, width: int, height: int, features: Dict):
471
+ """Generate a painterly brush stroke"""
472
+ # Start point
473
+ start_x = random.uniform(width * 0.1, width * 0.9)
474
+ start_y = random.uniform(height * 0.1, height * 0.9)
 
 
 
475
 
476
+ # End point
477
+ length = random.uniform(30, 100)
478
+ angle = random.uniform(0, 2 * math.pi)
479
+ end_x = start_x + length * math.cos(angle)
480
+ end_y = start_y + length * math.sin(angle)
481
 
482
+ # Clamp to bounds
483
+ end_x = max(0, min(width, end_x))
484
+ end_y = max(0, min(height, end_y))
 
 
485
 
486
+ # Control point for curve
487
+ mid_x = (start_x + end_x) / 2 + random.uniform(-20, 20)
488
+ mid_y = (start_y + end_y) / 2 + random.uniform(-20, 20)
489
+
490
+ return f"M {start_x},{start_y} Q {mid_x},{mid_y} {end_x},{end_y}"
491
+
492
+ def get_text_embeddings(self, prompt: str):
493
+ """Get CLIP text embeddings for the prompt"""
494
+ with torch.no_grad():
495
+ text_inputs = self.clip_tokenizer(
496
+ prompt,
497
+ padding="max_length",
498
+ max_length=self.clip_tokenizer.model_max_length,
499
+ truncation=True,
500
+ return_tensors="pt"
501
+ ).to(self.device)
502
 
503
+ text_embeddings = self.clip_model(text_inputs.input_ids)[0]
 
 
 
 
 
 
 
504
 
505
+ return text_embeddings
506
+
507
+ def extract_semantic_features(self, prompt: str):
508
+ """Extract semantic features from prompt"""
509
+ features = {
510
+ 'complexity': 'medium',
511
+ 'organic': False,
512
+ 'geometric': False,
513
+ 'colorful': False,
514
+ 'minimal': False
515
+ }
516
 
517
+ prompt_lower = prompt.lower()
518
+
519
+ # Analyze features
520
+ if any(word in prompt_lower for word in ['simple', 'minimal', 'clean']):
521
+ features['minimal'] = True
522
+ features['complexity'] = 'low'
523
+ elif any(word in prompt_lower for word in ['detailed', 'complex', 'intricate']):
524
+ features['complexity'] = 'high'
525
+
526
+ if any(word in prompt_lower for word in ['colorful', 'bright', 'vibrant']):
527
+ features['colorful'] = True
528
+
529
+ if any(word in prompt_lower for word in ['organic', 'natural', 'flowing']):
530
+ features['organic'] = True
531
+
532
+ if any(word in prompt_lower for word in ['geometric', 'angular', 'structured']):
533
+ features['geometric'] = True
534
+
535
+ return features
536
+
537
+ def select_best_particle(self, particles: List[Dict], prompt: str):
538
+ """Select the best particle from generated options"""
539
+ # For now, return the first particle
540
+ # In a full implementation, this would use quality metrics
541
+ return particles[0] if particles else self.create_fallback_particle(prompt)
542
+
543
+ def create_fallback_particle(self, prompt: str):
544
+ """Create a fallback particle"""
545
+ fallback_svg = self.create_fallback_svg(prompt, 256, 256, "iconography")
546
+ return {
547
+ 'particle_id': 0,
548
+ 'svg': fallback_svg,
549
+ 'svg_base64': base64.b64encode(fallback_svg.encode('utf-8')).decode('utf-8'),
550
+ 'prompt': prompt,
551
+ 'style': 'iconography',
552
+ 'parameters': {'width': 256, 'height': 256, 'seed': 0}
553
+ }
554
 
555
+ def svg_to_pil_image(self, svg_content: str, width: int, height: int):
556
  """Convert SVG content to PIL Image"""
557
  try:
558
  import cairosvg
 
559
 
560
  # Convert SVG to PNG bytes
561
  png_bytes = cairosvg.svg2png(
 
565
  )
566
 
567
  # Convert to PIL Image
 
568
  image = Image.open(io.BytesIO(png_bytes)).convert('RGB')
569
  return image
570
 
571
  except ImportError:
572
  print("cairosvg not available, creating simple image representation")
573
  # Fallback: create a simple image with text
 
574
  image = Image.new('RGB', (width, height), 'white')
575
  return image
576
  except Exception as e:
577
  print(f"Error converting SVG to image: {e}")
578
  # Fallback: create a simple image
 
579
  image = Image.new('RGB', (width, height), 'white')
580
  return image
581
 
582
+ def create_fallback_svg(self, prompt: str, width: int, height: int, style: str):
583
  """Create simple fallback SVG"""
584
  dwg = svgwrite.Drawing(size=(width, height))
585
  dwg.add(dwg.rect(insert=(0, 0), size=(width, height), fill='white'))
586
 
587
  # Simple centered text
588
  dwg.add(dwg.text(
589
+ f"SVGDreamer\n{style}\n{prompt[:20]}...",
590
  insert=(width/2, height/2),
591
  text_anchor="middle",
592
+ font_size="12px",
593
  fill="black"
594
  ))
595