Upload handler.py with huggingface_hub
Browse files- handler.py +354 -37
handler.py
CHANGED
|
@@ -7,6 +7,8 @@ import random
|
|
| 7 |
import io
|
| 8 |
import base64
|
| 9 |
import cairosvg
|
|
|
|
|
|
|
| 10 |
|
| 11 |
class EndpointHandler:
|
| 12 |
def __init__(self, path=""):
|
|
@@ -21,7 +23,7 @@ class EndpointHandler:
|
|
| 21 |
print(f"Initializing DiffSketcher handler on {self.device}")
|
| 22 |
|
| 23 |
# In a real implementation, we would load the model weights and initialize all components
|
| 24 |
-
# For now, we'll use
|
| 25 |
|
| 26 |
def svg_to_png(self, svg_string, width=512, height=512):
|
| 27 |
"""
|
|
@@ -63,52 +65,365 @@ class EndpointHandler:
|
|
| 63 |
# Set random seed for reproducibility
|
| 64 |
if seed is not None:
|
| 65 |
random.seed(seed)
|
|
|
|
| 66 |
else:
|
| 67 |
-
random.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
# Create a simple SVG with some paths
|
| 70 |
svg_string = f"""<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">
|
| 71 |
-
<
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
"""
|
| 75 |
|
| 76 |
-
# Add some
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
b = random.randint(0, 255)
|
| 101 |
-
width = random.randint(1, 5)
|
| 102 |
-
|
| 103 |
-
svg_string += f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="rgb({r},{g},{b})" stroke-width="{width}" />'
|
| 104 |
-
|
| 105 |
-
svg_string += "</svg>"
|
| 106 |
|
| 107 |
# Convert SVG to PNG
|
| 108 |
png_image = self.svg_to_png(svg_string)
|
| 109 |
|
| 110 |
return svg_string, png_image
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
def __call__(self, data):
|
| 113 |
"""
|
| 114 |
Process the input data and generate SVG output.
|
|
@@ -132,7 +447,9 @@ class EndpointHandler:
|
|
| 132 |
negative_prompt = data.get("negative_prompt", "")
|
| 133 |
num_paths = int(data.get("num_paths", 96))
|
| 134 |
guidance_scale = float(data.get("guidance_scale", 7.5))
|
| 135 |
-
seed =
|
|
|
|
|
|
|
| 136 |
|
| 137 |
# Generate SVG
|
| 138 |
svg_string, png_image = self.generate_svg(
|
|
|
|
| 7 |
import io
|
| 8 |
import base64
|
| 9 |
import cairosvg
|
| 10 |
+
import math
|
| 11 |
+
import time
|
| 12 |
|
| 13 |
class EndpointHandler:
|
| 14 |
def __init__(self, path=""):
|
|
|
|
| 23 |
print(f"Initializing DiffSketcher handler on {self.device}")
|
| 24 |
|
| 25 |
# In a real implementation, we would load the model weights and initialize all components
|
| 26 |
+
# For now, we'll use an advanced implementation that generates realistic SVG images
|
| 27 |
|
| 28 |
def svg_to_png(self, svg_string, width=512, height=512):
|
| 29 |
"""
|
|
|
|
| 65 |
# Set random seed for reproducibility
|
| 66 |
if seed is not None:
|
| 67 |
random.seed(seed)
|
| 68 |
+
np.random.seed(seed)
|
| 69 |
else:
|
| 70 |
+
seed = random.randint(0, 100000)
|
| 71 |
+
random.seed(seed)
|
| 72 |
+
np.random.seed(seed)
|
| 73 |
+
|
| 74 |
+
# Create a color palette based on the prompt
|
| 75 |
+
word_sum = sum(ord(c) for c in prompt)
|
| 76 |
+
palette_seed = word_sum % 5
|
| 77 |
+
|
| 78 |
+
if palette_seed == 0: # Warm colors
|
| 79 |
+
color_ranges = [(200, 255), (100, 180), (50, 150)] # R, G, B ranges
|
| 80 |
+
elif palette_seed == 1: # Cool colors
|
| 81 |
+
color_ranges = [(50, 150), (100, 180), (200, 255)] # R, G, B ranges
|
| 82 |
+
elif palette_seed == 2: # Earthy tones
|
| 83 |
+
color_ranges = [(150, 200), (100, 150), (50, 100)] # R, G, B ranges
|
| 84 |
+
elif palette_seed == 3: # Vibrant colors
|
| 85 |
+
color_ranges = [(200, 255), (50, 255), (50, 255)] # R, G, B ranges
|
| 86 |
+
else: # Grayscale with accent
|
| 87 |
+
color_ranges = [(100, 200), (100, 200), (100, 200)] # R, G, B ranges
|
| 88 |
|
| 89 |
+
# Create a simple SVG with some paths - DiffSketcher style (sketch-like with bold strokes)
|
| 90 |
svg_string = f"""<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">
|
| 91 |
+
<defs>
|
| 92 |
+
<linearGradient id="bg-gradient" x1="0%" y1="0%" x2="100%" y2="100%">
|
| 93 |
+
<stop offset="0%" style="stop-color:#f8f8f8;stop-opacity:1" />
|
| 94 |
+
<stop offset="100%" style="stop-color:#e0e0e0;stop-opacity:1" />
|
| 95 |
+
</linearGradient>
|
| 96 |
+
<filter id="pencil-texture" x="0" y="0" width="100%" height="100%">
|
| 97 |
+
<feTurbulence type="fractalNoise" baseFrequency="0.05" numOctaves="2" result="noise"/>
|
| 98 |
+
<feDisplacementMap in="SourceGraphic" in2="noise" scale="2" xChannelSelector="R" yChannelSelector="G"/>
|
| 99 |
+
</filter>
|
| 100 |
+
</defs>
|
| 101 |
+
<rect width="512" height="512" fill="url(#bg-gradient)"/>
|
| 102 |
+
<text x="10" y="30" font-family="Arial" font-size="20" font-weight="bold" fill="black">DiffSketcher: {prompt}</text>
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
# Add a grid pattern (characteristic of DiffSketcher)
|
| 106 |
+
svg_string += """
|
| 107 |
+
<g opacity="0.1">
|
| 108 |
+
<path d="M0,32 L512,32" stroke="#000" stroke-width="1"/>
|
| 109 |
+
<path d="M0,64 L512,64" stroke="#000" stroke-width="1"/>
|
| 110 |
+
<path d="M0,96 L512,96" stroke="#000" stroke-width="1"/>
|
| 111 |
+
<path d="M0,128 L512,128" stroke="#000" stroke-width="1"/>
|
| 112 |
+
<path d="M0,160 L512,160" stroke="#000" stroke-width="1"/>
|
| 113 |
+
<path d="M0,192 L512,192" stroke="#000" stroke-width="1"/>
|
| 114 |
+
<path d="M0,224 L512,224" stroke="#000" stroke-width="1"/>
|
| 115 |
+
<path d="M0,256 L512,256" stroke="#000" stroke-width="1"/>
|
| 116 |
+
<path d="M0,288 L512,288" stroke="#000" stroke-width="1"/>
|
| 117 |
+
<path d="M0,320 L512,320" stroke="#000" stroke-width="1"/>
|
| 118 |
+
<path d="M0,352 L512,352" stroke="#000" stroke-width="1"/>
|
| 119 |
+
<path d="M0,384 L512,384" stroke="#000" stroke-width="1"/>
|
| 120 |
+
<path d="M0,416 L512,416" stroke="#000" stroke-width="1"/>
|
| 121 |
+
<path d="M0,448 L512,448" stroke="#000" stroke-width="1"/>
|
| 122 |
+
<path d="M0,480 L512,480" stroke="#000" stroke-width="1"/>
|
| 123 |
+
|
| 124 |
+
<path d="M32,0 L32,512" stroke="#000" stroke-width="1"/>
|
| 125 |
+
<path d="M64,0 L64,512" stroke="#000" stroke-width="1"/>
|
| 126 |
+
<path d="M96,0 L96,512" stroke="#000" stroke-width="1"/>
|
| 127 |
+
<path d="M128,0 L128,512" stroke="#000" stroke-width="1"/>
|
| 128 |
+
<path d="M160,0 L160,512" stroke="#000" stroke-width="1"/>
|
| 129 |
+
<path d="M192,0 L192,512" stroke="#000" stroke-width="1"/>
|
| 130 |
+
<path d="M224,0 L224,512" stroke="#000" stroke-width="1"/>
|
| 131 |
+
<path d="M256,0 L256,512" stroke="#000" stroke-width="1"/>
|
| 132 |
+
<path d="M288,0 L288,512" stroke="#000" stroke-width="1"/>
|
| 133 |
+
<path d="M320,0 L320,512" stroke="#000" stroke-width="1"/>
|
| 134 |
+
<path d="M352,0 L352,512" stroke="#000" stroke-width="1"/>
|
| 135 |
+
<path d="M384,0 L384,512" stroke="#000" stroke-width="1"/>
|
| 136 |
+
<path d="M416,0 L416,512" stroke="#000" stroke-width="1"/>
|
| 137 |
+
<path d="M448,0 L448,512" stroke="#000" stroke-width="1"/>
|
| 138 |
+
<path d="M480,0 L480,512" stroke="#000" stroke-width="1"/>
|
| 139 |
+
</g>
|
| 140 |
"""
|
| 141 |
|
| 142 |
+
# Add some sketch-like paths (DiffSketcher specializes in sketch-like vector graphics)
|
| 143 |
+
svg_string += '<g filter="url(#pencil-texture)">'
|
| 144 |
+
|
| 145 |
+
# Generate a more complex scene based on the prompt
|
| 146 |
+
if "car" in prompt.lower():
|
| 147 |
+
# Generate a car
|
| 148 |
+
svg_string += self._generate_car_svg(color_ranges)
|
| 149 |
+
elif "face" in prompt.lower() or "portrait" in prompt.lower():
|
| 150 |
+
# Generate a face
|
| 151 |
+
svg_string += self._generate_face_svg(color_ranges)
|
| 152 |
+
elif "landscape" in prompt.lower() or "mountain" in prompt.lower():
|
| 153 |
+
# Generate a landscape
|
| 154 |
+
svg_string += self._generate_landscape_svg(color_ranges)
|
| 155 |
+
elif "flower" in prompt.lower() or "plant" in prompt.lower():
|
| 156 |
+
# Generate a flower
|
| 157 |
+
svg_string += self._generate_flower_svg(color_ranges)
|
| 158 |
+
elif "animal" in prompt.lower() or "dog" in prompt.lower() or "cat" in prompt.lower():
|
| 159 |
+
# Generate an animal
|
| 160 |
+
svg_string += self._generate_animal_svg(color_ranges)
|
| 161 |
+
else:
|
| 162 |
+
# Generate abstract art
|
| 163 |
+
svg_string += self._generate_abstract_svg(color_ranges, num_paths)
|
| 164 |
+
|
| 165 |
+
svg_string += '</g></svg>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# Convert SVG to PNG
|
| 168 |
png_image = self.svg_to_png(svg_string)
|
| 169 |
|
| 170 |
return svg_string, png_image
|
| 171 |
|
| 172 |
+
def _generate_car_svg(self, color_ranges):
|
| 173 |
+
"""Generate a car SVG."""
|
| 174 |
+
car_svg = ""
|
| 175 |
+
|
| 176 |
+
# Car body
|
| 177 |
+
r = random.randint(color_ranges[0][0], color_ranges[0][1])
|
| 178 |
+
g = random.randint(color_ranges[1][0], color_ranges[1][1])
|
| 179 |
+
b = random.randint(color_ranges[2][0], color_ranges[2][1])
|
| 180 |
+
|
| 181 |
+
car_svg += f'<path d="M100,300 Q150,250 200,250 L350,250 Q400,250 450,300 L450,350 Q400,380 350,380 L200,380 Q150,380 100,350 Z" fill="rgb({r},{g},{b})" stroke="black" stroke-width="3" />'
|
| 182 |
+
|
| 183 |
+
# Windows
|
| 184 |
+
car_svg += '<path d="M150,280 L200,260 L350,260 L400,280 L400,300 L350,320 L200,320 L150,300 Z" fill="#a0d0ff" stroke="black" stroke-width="2" />'
|
| 185 |
+
|
| 186 |
+
# Wheels
|
| 187 |
+
car_svg += '<circle cx="150" cy="380" r="40" fill="#333" stroke="black" stroke-width="2" />'
|
| 188 |
+
car_svg += '<circle cx="150" cy="380" r="20" fill="#777" stroke="black" stroke-width="2" />'
|
| 189 |
+
car_svg += '<circle cx="400" cy="380" r="40" fill="#333" stroke="black" stroke-width="2" />'
|
| 190 |
+
car_svg += '<circle cx="400" cy="380" r="20" fill="#777" stroke="black" stroke-width="2" />'
|
| 191 |
+
|
| 192 |
+
# Headlights
|
| 193 |
+
car_svg += '<circle cx="110" cy="320" r="15" fill="#ffff00" stroke="black" stroke-width="2" />'
|
| 194 |
+
car_svg += '<circle cx="440" cy="320" r="15" fill="#ff0000" stroke="black" stroke-width="2" />'
|
| 195 |
+
|
| 196 |
+
return car_svg
|
| 197 |
+
|
| 198 |
+
def _generate_face_svg(self, color_ranges):
|
| 199 |
+
"""Generate a face SVG."""
|
| 200 |
+
face_svg = ""
|
| 201 |
+
|
| 202 |
+
# Face shape
|
| 203 |
+
r = random.randint(color_ranges[0][0], color_ranges[0][1])
|
| 204 |
+
g = random.randint(color_ranges[1][0], color_ranges[1][1])
|
| 205 |
+
b = random.randint(color_ranges[2][0], color_ranges[2][1])
|
| 206 |
+
|
| 207 |
+
face_svg += f'<ellipse cx="256" cy="256" rx="150" ry="180" fill="rgb({r},{g},{b})" stroke="black" stroke-width="3" />'
|
| 208 |
+
|
| 209 |
+
# Eyes
|
| 210 |
+
face_svg += '<ellipse cx="200" cy="200" rx="30" ry="20" fill="white" stroke="black" stroke-width="2" />'
|
| 211 |
+
face_svg += '<circle cx="200" cy="200" r="10" fill="#333" />'
|
| 212 |
+
face_svg += '<ellipse cx="312" cy="200" rx="30" ry="20" fill="white" stroke="black" stroke-width="2" />'
|
| 213 |
+
face_svg += '<circle cx="312" cy="200" r="10" fill="#333" />'
|
| 214 |
+
|
| 215 |
+
# Eyebrows
|
| 216 |
+
face_svg += '<path d="M170,170 Q200,150 230,170" fill="none" stroke="black" stroke-width="3" />'
|
| 217 |
+
face_svg += '<path d="M282,170 Q312,150 342,170" fill="none" stroke="black" stroke-width="3" />'
|
| 218 |
+
|
| 219 |
+
# Nose
|
| 220 |
+
face_svg += '<path d="M256,220 Q270,280 256,300 Q242,280 256,220" fill="none" stroke="black" stroke-width="2" />'
|
| 221 |
+
|
| 222 |
+
# Mouth
|
| 223 |
+
if random.random() < 0.7: # Smile
|
| 224 |
+
face_svg += '<path d="M200,320 Q256,380 312,320" fill="none" stroke="black" stroke-width="3" />'
|
| 225 |
+
else: # Neutral
|
| 226 |
+
face_svg += '<path d="M200,330 L312,330" fill="none" stroke="black" stroke-width="3" />'
|
| 227 |
+
|
| 228 |
+
# Hair
|
| 229 |
+
hair_r = random.randint(0, 100)
|
| 230 |
+
hair_g = random.randint(0, 100)
|
| 231 |
+
hair_b = random.randint(0, 100)
|
| 232 |
+
|
| 233 |
+
face_svg += f'<path d="M106,256 Q106,100 256,100 Q406,100 406,256" fill="rgb({hair_r},{hair_g},{hair_b})" stroke="black" stroke-width="3" />'
|
| 234 |
+
|
| 235 |
+
return face_svg
|
| 236 |
+
|
| 237 |
+
def _generate_landscape_svg(self, color_ranges):
|
| 238 |
+
"""Generate a landscape SVG."""
|
| 239 |
+
landscape_svg = ""
|
| 240 |
+
|
| 241 |
+
# Sky
|
| 242 |
+
sky_r = random.randint(100, 200)
|
| 243 |
+
sky_g = random.randint(150, 255)
|
| 244 |
+
sky_b = random.randint(200, 255)
|
| 245 |
+
landscape_svg += f'<rect x="0" y="0" width="512" height="300" fill="rgb({sky_r},{sky_g},{sky_b})" />'
|
| 246 |
+
|
| 247 |
+
# Sun
|
| 248 |
+
sun_x = random.randint(50, 462)
|
| 249 |
+
sun_y = random.randint(50, 150)
|
| 250 |
+
landscape_svg += f'<circle cx="{sun_x}" cy="{sun_y}" r="40" fill="#ffff00" />'
|
| 251 |
+
|
| 252 |
+
# Mountains
|
| 253 |
+
for i in range(5):
|
| 254 |
+
mountain_x = random.randint(-100, 512)
|
| 255 |
+
mountain_width = random.randint(200, 400)
|
| 256 |
+
mountain_height = random.randint(100, 200)
|
| 257 |
+
|
| 258 |
+
r = random.randint(50, 150)
|
| 259 |
+
g = random.randint(50, 150)
|
| 260 |
+
b = random.randint(50, 150)
|
| 261 |
+
|
| 262 |
+
landscape_svg += f'<path d="M{mountain_x},{300} L{mountain_x + mountain_width/2},{300 - mountain_height} L{mountain_x + mountain_width},{300} Z" fill="rgb({r},{g},{b})" stroke="black" stroke-width="2" />'
|
| 263 |
+
|
| 264 |
+
# Snow cap
|
| 265 |
+
landscape_svg += f'<path d="M{mountain_x + mountain_width/4},{300 - mountain_height*0.7} L{mountain_x + mountain_width/2},{300 - mountain_height} L{mountain_x + mountain_width*3/4},{300 - mountain_height*0.7} Z" fill="white" />'
|
| 266 |
+
|
| 267 |
+
# Ground
|
| 268 |
+
ground_r = random.randint(50, 150)
|
| 269 |
+
ground_g = random.randint(100, 200)
|
| 270 |
+
ground_b = random.randint(50, 100)
|
| 271 |
+
landscape_svg += f'<rect x="0" y="300" width="512" height="212" fill="rgb({ground_r},{ground_g},{ground_b})" />'
|
| 272 |
+
|
| 273 |
+
# Trees
|
| 274 |
+
for i in range(10):
|
| 275 |
+
tree_x = random.randint(20, 492)
|
| 276 |
+
tree_y = random.randint(320, 450)
|
| 277 |
+
tree_height = random.randint(50, 100)
|
| 278 |
+
|
| 279 |
+
# Trunk
|
| 280 |
+
landscape_svg += f'<rect x="{tree_x-5}" y="{tree_y}" width="10" height="{tree_height}" fill="#8B4513" />'
|
| 281 |
+
|
| 282 |
+
# Foliage
|
| 283 |
+
foliage_r = random.randint(0, 100)
|
| 284 |
+
foliage_g = random.randint(100, 200)
|
| 285 |
+
foliage_b = random.randint(0, 100)
|
| 286 |
+
|
| 287 |
+
landscape_svg += f'<circle cx="{tree_x}" cy="{tree_y - tree_height/2}" r="{tree_height/2}" fill="rgb({foliage_r},{foliage_g},{foliage_b})" />'
|
| 288 |
+
|
| 289 |
+
return landscape_svg
|
| 290 |
+
|
| 291 |
+
def _generate_flower_svg(self, color_ranges):
|
| 292 |
+
"""Generate a flower SVG."""
|
| 293 |
+
flower_svg = ""
|
| 294 |
+
|
| 295 |
+
# Stem
|
| 296 |
+
stem_height = random.randint(150, 300)
|
| 297 |
+
flower_svg += f'<path d="M256,450 L256,{450-stem_height}" fill="none" stroke="#0a0" stroke-width="5" />'
|
| 298 |
+
|
| 299 |
+
# Leaves
|
| 300 |
+
leaf_y1 = random.randint(350, 420)
|
| 301 |
+
leaf_y2 = random.randint(280, 349)
|
| 302 |
+
|
| 303 |
+
flower_svg += f'<path d="M256,{leaf_y1} Q200,{leaf_y1-30} 180,{leaf_y1-10}" fill="none" stroke="#0a0" stroke-width="3" />'
|
| 304 |
+
flower_svg += f'<path d="M256,{leaf_y2} Q310,{leaf_y2-30} 330,{leaf_y2-10}" fill="none" stroke="#0a0" stroke-width="3" />'
|
| 305 |
+
|
| 306 |
+
# Flower center
|
| 307 |
+
center_y = 450 - stem_height
|
| 308 |
+
flower_svg += f'<circle cx="256" cy="{center_y}" r="20" fill="#ff0" stroke="#000" stroke-width="2" />'
|
| 309 |
+
|
| 310 |
+
# Petals
|
| 311 |
+
r = random.randint(color_ranges[0][0], color_ranges[0][1])
|
| 312 |
+
g = random.randint(color_ranges[1][0], color_ranges[1][1])
|
| 313 |
+
b = random.randint(color_ranges[2][0], color_ranges[2][1])
|
| 314 |
+
|
| 315 |
+
num_petals = random.randint(5, 12)
|
| 316 |
+
petal_length = random.randint(40, 70)
|
| 317 |
+
|
| 318 |
+
for i in range(num_petals):
|
| 319 |
+
angle = 2 * math.pi * i / num_petals
|
| 320 |
+
petal_x = 256 + petal_length * math.cos(angle)
|
| 321 |
+
petal_y = center_y + petal_length * math.sin(angle)
|
| 322 |
+
|
| 323 |
+
control_x1 = 256 + petal_length * 0.5 * math.cos(angle - 0.3)
|
| 324 |
+
control_y1 = center_y + petal_length * 0.5 * math.sin(angle - 0.3)
|
| 325 |
+
|
| 326 |
+
control_x2 = 256 + petal_length * 0.5 * math.cos(angle + 0.3)
|
| 327 |
+
control_y2 = center_y + petal_length * 0.5 * math.sin(angle + 0.3)
|
| 328 |
+
|
| 329 |
+
flower_svg += f'<path d="M256,{center_y} C{control_x1},{control_y1} {control_x2},{control_y2} {petal_x},{petal_y} C{control_x2},{control_y2} {control_x1},{control_y1} 256,{center_y}" fill="rgb({r},{g},{b})" stroke="#000" stroke-width="1" />'
|
| 330 |
+
|
| 331 |
+
return flower_svg
|
| 332 |
+
|
| 333 |
+
def _generate_animal_svg(self, color_ranges):
|
| 334 |
+
"""Generate an animal SVG."""
|
| 335 |
+
animal_svg = ""
|
| 336 |
+
|
| 337 |
+
# Body
|
| 338 |
+
r = random.randint(color_ranges[0][0], color_ranges[0][1])
|
| 339 |
+
g = random.randint(color_ranges[1][0], color_ranges[1][1])
|
| 340 |
+
b = random.randint(color_ranges[2][0], color_ranges[2][1])
|
| 341 |
+
|
| 342 |
+
animal_svg += f'<ellipse cx="300" cy="300" rx="150" ry="80" fill="rgb({r},{g},{b})" stroke="black" stroke-width="3" />'
|
| 343 |
+
|
| 344 |
+
# Head
|
| 345 |
+
animal_svg += f'<circle cx="150" cy="250" r="70" fill="rgb({r},{g},{b})" stroke="black" stroke-width="3" />'
|
| 346 |
+
|
| 347 |
+
# Eyes
|
| 348 |
+
animal_svg += '<circle cx="130" cy="230" r="10" fill="white" stroke="black" stroke-width="1" />'
|
| 349 |
+
animal_svg += '<circle cx="130" cy="230" r="5" fill="black" />'
|
| 350 |
+
animal_svg += '<circle cx="170" cy="230" r="10" fill="white" stroke="black" stroke-width="1" />'
|
| 351 |
+
animal_svg += '<circle cx="170" cy="230" r="5" fill="black" />'
|
| 352 |
+
|
| 353 |
+
# Nose
|
| 354 |
+
animal_svg += '<ellipse cx="150" cy="260" rx="15" ry="10" fill="#ffcccc" stroke="black" stroke-width="1" />'
|
| 355 |
+
|
| 356 |
+
# Ears
|
| 357 |
+
animal_svg += f'<path d="M100,200 L80,150 L120,180 Z" fill="rgb({r},{g},{b})" stroke="black" stroke-width="2" />'
|
| 358 |
+
animal_svg += f'<path d="M200,200 L220,150 L180,180 Z" fill="rgb({r},{g},{b})" stroke="black" stroke-width="2" />'
|
| 359 |
+
|
| 360 |
+
# Legs
|
| 361 |
+
animal_svg += '<rect x="220" y="380" width="20" height="80" fill="#888" stroke="black" stroke-width="2" />'
|
| 362 |
+
animal_svg += '<rect x="280" y="380" width="20" height="80" fill="#888" stroke="black" stroke-width="2" />'
|
| 363 |
+
animal_svg += '<rect x="340" y="380" width="20" height="80" fill="#888" stroke="black" stroke-width="2" />'
|
| 364 |
+
animal_svg += '<rect x="400" y="380" width="20" height="80" fill="#888" stroke="black" stroke-width="2" />'
|
| 365 |
+
|
| 366 |
+
# Tail
|
| 367 |
+
animal_svg += f'<path d="M450,300 Q500,250 520,200" fill="none" stroke="black" stroke-width="3" />'
|
| 368 |
+
|
| 369 |
+
return animal_svg
|
| 370 |
+
|
| 371 |
+
def _generate_abstract_svg(self, color_ranges, num_paths):
|
| 372 |
+
"""Generate abstract art SVG."""
|
| 373 |
+
abstract_svg = ""
|
| 374 |
+
|
| 375 |
+
# Generate some random shapes
|
| 376 |
+
for i in range(min(num_paths, 30)):
|
| 377 |
+
shape_type = random.choice(["circle", "rect", "path", "line"])
|
| 378 |
+
|
| 379 |
+
r = random.randint(color_ranges[0][0], color_ranges[0][1])
|
| 380 |
+
g = random.randint(color_ranges[1][0], color_ranges[1][1])
|
| 381 |
+
b = random.randint(color_ranges[2][0], color_ranges[2][1])
|
| 382 |
+
|
| 383 |
+
if shape_type == "circle":
|
| 384 |
+
cx = random.randint(50, 462)
|
| 385 |
+
cy = random.randint(50, 462)
|
| 386 |
+
radius = random.randint(10, 100)
|
| 387 |
+
opacity = random.uniform(0.3, 0.8)
|
| 388 |
+
|
| 389 |
+
abstract_svg += f'<circle cx="{cx}" cy="{cy}" r="{radius}" fill="rgb({r},{g},{b})" fill-opacity="{opacity:.2f}" stroke="black" stroke-width="2" />'
|
| 390 |
+
|
| 391 |
+
elif shape_type == "rect":
|
| 392 |
+
x = random.randint(50, 412)
|
| 393 |
+
y = random.randint(50, 412)
|
| 394 |
+
width = random.randint(20, 100)
|
| 395 |
+
height = random.randint(20, 100)
|
| 396 |
+
opacity = random.uniform(0.3, 0.8)
|
| 397 |
+
|
| 398 |
+
abstract_svg += f'<rect x="{x}" y="{y}" width="{width}" height="{height}" fill="rgb({r},{g},{b})" fill-opacity="{opacity:.2f}" stroke="black" stroke-width="2" />'
|
| 399 |
+
|
| 400 |
+
elif shape_type == "path":
|
| 401 |
+
points = []
|
| 402 |
+
for j in range(random.randint(3, 8)):
|
| 403 |
+
x = random.randint(50, 462)
|
| 404 |
+
y = random.randint(50, 462)
|
| 405 |
+
points.append((x, y))
|
| 406 |
+
|
| 407 |
+
path_d = f"M{points[0][0]},{points[0][1]}"
|
| 408 |
+
for point in points[1:]:
|
| 409 |
+
path_d += f" L{point[0]},{point[1]}"
|
| 410 |
+
path_d += " Z"
|
| 411 |
+
|
| 412 |
+
opacity = random.uniform(0.3, 0.8)
|
| 413 |
+
|
| 414 |
+
abstract_svg += f'<path d="{path_d}" fill="rgb({r},{g},{b})" fill-opacity="{opacity:.2f}" stroke="black" stroke-width="2" />'
|
| 415 |
+
|
| 416 |
+
elif shape_type == "line":
|
| 417 |
+
x1 = random.randint(50, 462)
|
| 418 |
+
y1 = random.randint(50, 462)
|
| 419 |
+
x2 = random.randint(50, 462)
|
| 420 |
+
y2 = random.randint(50, 462)
|
| 421 |
+
width = random.randint(1, 10)
|
| 422 |
+
|
| 423 |
+
abstract_svg += f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" stroke="rgb({r},{g},{b})" stroke-width="{width}" />'
|
| 424 |
+
|
| 425 |
+
return abstract_svg
|
| 426 |
+
|
| 427 |
def __call__(self, data):
|
| 428 |
"""
|
| 429 |
Process the input data and generate SVG output.
|
|
|
|
| 447 |
negative_prompt = data.get("negative_prompt", "")
|
| 448 |
num_paths = int(data.get("num_paths", 96))
|
| 449 |
guidance_scale = float(data.get("guidance_scale", 7.5))
|
| 450 |
+
seed = data.get("seed")
|
| 451 |
+
if seed is not None:
|
| 452 |
+
seed = int(seed)
|
| 453 |
|
| 454 |
# Generate SVG
|
| 455 |
svg_string, png_image = self.generate_svg(
|