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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw
|
| 2 |
+
import math
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
class EndpointHandler:
|
| 6 |
+
def __init__(self, path=""):
|
| 7 |
+
"""Initialize SVGDreamer handler for Hugging Face Inference API"""
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
def __call__(self, data):
|
| 11 |
+
"""Generate SVG-style graphics from text prompt"""
|
| 12 |
+
# Extract prompt
|
| 13 |
+
inputs = data.get("inputs", "")
|
| 14 |
+
if isinstance(inputs, dict):
|
| 15 |
+
prompt = inputs.get("prompt", inputs.get("text", ""))
|
| 16 |
+
else:
|
| 17 |
+
prompt = str(inputs)
|
| 18 |
+
|
| 19 |
+
if not prompt:
|
| 20 |
+
prompt = "geometric pattern"
|
| 21 |
+
|
| 22 |
+
# Generate SVG-style image
|
| 23 |
+
image = self.generate_svg(prompt)
|
| 24 |
+
|
| 25 |
+
# Return PIL Image directly for HF Inference API
|
| 26 |
+
return image
|
| 27 |
+
|
| 28 |
+
def generate_svg(self, prompt):
|
| 29 |
+
"""Generate SVG-style graphics based on the prompt"""
|
| 30 |
+
# Create 224x224 white canvas
|
| 31 |
+
img = Image.new('RGB', (224, 224), 'white')
|
| 32 |
+
draw = ImageDraw.Draw(img)
|
| 33 |
+
|
| 34 |
+
prompt_lower = prompt.lower()
|
| 35 |
+
|
| 36 |
+
if any(word in prompt_lower for word in ['mandala', 'pattern', 'geometric']):
|
| 37 |
+
self._draw_mandala(draw)
|
| 38 |
+
elif any(word in prompt_lower for word in ['flower', 'floral', 'bloom']):
|
| 39 |
+
self._draw_floral_pattern(draw)
|
| 40 |
+
elif any(word in prompt_lower for word in ['star', 'celestial', 'cosmic']):
|
| 41 |
+
self._draw_star_pattern(draw)
|
| 42 |
+
elif any(word in prompt_lower for word in ['spiral', 'swirl', 'curve']):
|
| 43 |
+
self._draw_spiral_pattern(draw)
|
| 44 |
+
elif any(word in prompt_lower for word in ['grid', 'tile', 'mosaic']):
|
| 45 |
+
self._draw_grid_pattern(draw)
|
| 46 |
+
else:
|
| 47 |
+
self._draw_abstract_svg(draw, prompt)
|
| 48 |
+
|
| 49 |
+
return img
|
| 50 |
+
|
| 51 |
+
def _draw_mandala(self, draw):
|
| 52 |
+
"""Draw mandala pattern"""
|
| 53 |
+
center = (112, 112)
|
| 54 |
+
|
| 55 |
+
# Outer circles
|
| 56 |
+
for r in range(20, 100, 15):
|
| 57 |
+
draw.ellipse([center[0]-r, center[1]-r, center[0]+r, center[1]+r],
|
| 58 |
+
outline=(100, 150, 200), width=2)
|
| 59 |
+
|
| 60 |
+
# Radial lines
|
| 61 |
+
for angle in range(0, 360, 30):
|
| 62 |
+
x1 = center[0] + 30 * math.cos(math.radians(angle))
|
| 63 |
+
y1 = center[1] + 30 * math.sin(math.radians(angle))
|
| 64 |
+
x2 = center[0] + 80 * math.cos(math.radians(angle))
|
| 65 |
+
y2 = center[1] + 80 * math.sin(math.radians(angle))
|
| 66 |
+
draw.line([x1, y1, x2, y2], fill=(50, 100, 150), width=2)
|
| 67 |
+
|
| 68 |
+
# Inner pattern
|
| 69 |
+
for angle in range(0, 360, 45):
|
| 70 |
+
x = center[0] + 40 * math.cos(math.radians(angle))
|
| 71 |
+
y = center[1] + 40 * math.sin(math.radians(angle))
|
| 72 |
+
draw.ellipse([x-8, y-8, x+8, y+8], fill=(200, 100, 150))
|
| 73 |
+
|
| 74 |
+
# Center
|
| 75 |
+
draw.ellipse([center[0]-15, center[1]-15, center[0]+15, center[1]+15],
|
| 76 |
+
fill=(255, 200, 100), outline=(200, 150, 50), width=2)
|
| 77 |
+
|
| 78 |
+
def _draw_floral_pattern(self, draw):
|
| 79 |
+
"""Draw floral SVG pattern"""
|
| 80 |
+
center = (112, 112)
|
| 81 |
+
|
| 82 |
+
# Main flower
|
| 83 |
+
for angle in range(0, 360, 45):
|
| 84 |
+
x = center[0] + 35 * math.cos(math.radians(angle))
|
| 85 |
+
y = center[1] + 35 * math.sin(math.radians(angle))
|
| 86 |
+
draw.ellipse([x-12, y-8, x+12, y+8], fill=(255, 100, 150), outline=(200, 50, 100))
|
| 87 |
+
|
| 88 |
+
# Center
|
| 89 |
+
draw.ellipse([center[0]-10, center[1]-10, center[0]+10, center[1]+10],
|
| 90 |
+
fill=(255, 255, 0), outline=(200, 200, 0))
|
| 91 |
+
|
| 92 |
+
# Corner flowers
|
| 93 |
+
corners = [(50, 50), (174, 50), (50, 174), (174, 174)]
|
| 94 |
+
for cx, cy in corners:
|
| 95 |
+
for angle in range(0, 360, 60):
|
| 96 |
+
x = cx + 15 * math.cos(math.radians(angle))
|
| 97 |
+
y = cy + 15 * math.sin(math.radians(angle))
|
| 98 |
+
draw.ellipse([x-5, y-3, x+5, y+3], fill=(150, 200, 100))
|
| 99 |
+
draw.ellipse([cx-4, cy-4, cx+4, cy+4], fill=(255, 200, 50))
|
| 100 |
+
|
| 101 |
+
# Connecting vines
|
| 102 |
+
draw.arc([30, 30, 90, 90], 0, 90, fill=(100, 200, 100), width=3)
|
| 103 |
+
draw.arc([134, 30, 194, 90], 90, 180, fill=(100, 200, 100), width=3)
|
| 104 |
+
draw.arc([30, 134, 90, 194], 270, 360, fill=(100, 200, 100), width=3)
|
| 105 |
+
draw.arc([134, 134, 194, 194], 180, 270, fill=(100, 200, 100), width=3)
|
| 106 |
+
|
| 107 |
+
def _draw_star_pattern(self, draw):
|
| 108 |
+
"""Draw star pattern"""
|
| 109 |
+
center = (112, 112)
|
| 110 |
+
|
| 111 |
+
# Main star
|
| 112 |
+
star_points = []
|
| 113 |
+
for i in range(10):
|
| 114 |
+
angle = i * 36
|
| 115 |
+
if i % 2 == 0:
|
| 116 |
+
r = 50
|
| 117 |
+
else:
|
| 118 |
+
r = 25
|
| 119 |
+
x = center[0] + r * math.cos(math.radians(angle - 90))
|
| 120 |
+
y = center[1] + r * math.sin(math.radians(angle - 90))
|
| 121 |
+
star_points.append((x, y))
|
| 122 |
+
draw.polygon(star_points, fill=(255, 255, 100), outline=(200, 200, 50), width=2)
|
| 123 |
+
|
| 124 |
+
# Smaller stars
|
| 125 |
+
small_stars = [(60, 60), (164, 60), (60, 164), (164, 164), (112, 40), (40, 112), (184, 112), (112, 184)]
|
| 126 |
+
for sx, sy in small_stars:
|
| 127 |
+
small_star_points = []
|
| 128 |
+
for i in range(8):
|
| 129 |
+
angle = i * 45
|
| 130 |
+
r = 8 if i % 2 == 0 else 4
|
| 131 |
+
x = sx + r * math.cos(math.radians(angle))
|
| 132 |
+
y = sy + r * math.sin(math.radians(angle))
|
| 133 |
+
small_star_points.append((x, y))
|
| 134 |
+
draw.polygon(small_star_points, fill=(200, 200, 255))
|
| 135 |
+
|
| 136 |
+
def _draw_spiral_pattern(self, draw):
|
| 137 |
+
"""Draw spiral pattern"""
|
| 138 |
+
center = (112, 112)
|
| 139 |
+
|
| 140 |
+
# Main spiral
|
| 141 |
+
points = []
|
| 142 |
+
for t in range(0, 720, 5):
|
| 143 |
+
r = t * 0.1
|
| 144 |
+
x = center[0] + r * math.cos(math.radians(t))
|
| 145 |
+
y = center[1] + r * math.sin(math.radians(t))
|
| 146 |
+
if 0 <= x <= 224 and 0 <= y <= 224:
|
| 147 |
+
points.append((x, y))
|
| 148 |
+
|
| 149 |
+
for i in range(len(points)-1):
|
| 150 |
+
color_intensity = int(255 * (i / len(points)))
|
| 151 |
+
draw.line([points[i], points[i+1]],
|
| 152 |
+
fill=(color_intensity, 100, 255-color_intensity), width=2)
|
| 153 |
+
|
| 154 |
+
# Counter spiral
|
| 155 |
+
points2 = []
|
| 156 |
+
for t in range(0, 720, 5):
|
| 157 |
+
r = t * 0.08
|
| 158 |
+
x = center[0] + r * math.cos(math.radians(-t + 180))
|
| 159 |
+
y = center[1] + r * math.sin(math.radians(-t + 180))
|
| 160 |
+
if 0 <= x <= 224 and 0 <= y <= 224:
|
| 161 |
+
points2.append((x, y))
|
| 162 |
+
|
| 163 |
+
for i in range(len(points2)-1):
|
| 164 |
+
color_intensity = int(255 * (i / len(points2)))
|
| 165 |
+
draw.line([points2[i], points2[i+1]],
|
| 166 |
+
fill=(255-color_intensity, 150, color_intensity), width=2)
|
| 167 |
+
|
| 168 |
+
def _draw_grid_pattern(self, draw):
|
| 169 |
+
"""Draw grid/tile pattern"""
|
| 170 |
+
tile_size = 28
|
| 171 |
+
|
| 172 |
+
for i in range(0, 224, tile_size):
|
| 173 |
+
for j in range(0, 224, tile_size):
|
| 174 |
+
# Alternate pattern
|
| 175 |
+
if (i // tile_size + j // tile_size) % 2 == 0:
|
| 176 |
+
# Circle tile
|
| 177 |
+
draw.ellipse([i+4, j+4, i+tile_size-4, j+tile_size-4],
|
| 178 |
+
fill=(150, 200, 255), outline=(100, 150, 200), width=2)
|
| 179 |
+
else:
|
| 180 |
+
# Diamond tile
|
| 181 |
+
cx, cy = i + tile_size//2, j + tile_size//2
|
| 182 |
+
diamond_points = [
|
| 183 |
+
(cx, cy - tile_size//3),
|
| 184 |
+
(cx + tile_size//3, cy),
|
| 185 |
+
(cx, cy + tile_size//3),
|
| 186 |
+
(cx - tile_size//3, cy)
|
| 187 |
+
]
|
| 188 |
+
draw.polygon(diamond_points, fill=(255, 150, 150), outline=(200, 100, 100), width=2)
|
| 189 |
+
|
| 190 |
+
def _draw_abstract_svg(self, draw, prompt):
|
| 191 |
+
"""Draw abstract SVG pattern"""
|
| 192 |
+
prompt_hash = hash(prompt) % 100
|
| 193 |
+
|
| 194 |
+
# Create geometric shapes based on prompt
|
| 195 |
+
for i in range(6):
|
| 196 |
+
x = (i * 50 + prompt_hash) % 180 + 22
|
| 197 |
+
y = (i * 40 + prompt_hash) % 160 + 32
|
| 198 |
+
size = 20 + (i * 10) % 30
|
| 199 |
+
|
| 200 |
+
shape_type = (i + prompt_hash) % 4
|
| 201 |
+
color = (
|
| 202 |
+
(i * 60 + prompt_hash) % 255,
|
| 203 |
+
(i * 80 + prompt_hash) % 255,
|
| 204 |
+
(i * 100 + prompt_hash) % 255
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
if shape_type == 0:
|
| 208 |
+
# Circle
|
| 209 |
+
draw.ellipse([x-size, y-size, x+size, y+size],
|
| 210 |
+
fill=color, outline=tuple(c//2 for c in color), width=2)
|
| 211 |
+
elif shape_type == 1:
|
| 212 |
+
# Rectangle
|
| 213 |
+
draw.rectangle([x-size, y-size, x+size, y+size],
|
| 214 |
+
fill=color, outline=tuple(c//2 for c in color), width=2)
|
| 215 |
+
elif shape_type == 2:
|
| 216 |
+
# Triangle
|
| 217 |
+
points = [
|
| 218 |
+
(x, y-size),
|
| 219 |
+
(x-size, y+size),
|
| 220 |
+
(x+size, y+size)
|
| 221 |
+
]
|
| 222 |
+
draw.polygon(points, fill=color, outline=tuple(c//2 for c in color), width=2)
|
| 223 |
+
else:
|
| 224 |
+
# Hexagon
|
| 225 |
+
hex_points = []
|
| 226 |
+
for angle in range(0, 360, 60):
|
| 227 |
+
hx = x + size * math.cos(math.radians(angle))
|
| 228 |
+
hy = y + size * math.sin(math.radians(angle))
|
| 229 |
+
hex_points.append((hx, hy))
|
| 230 |
+
draw.polygon(hex_points, fill=color, outline=tuple(c//2 for c in color), width=2)
|