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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Unified Vector Graphics Models API Server
|
4 |
+
Handles DiffSketcher, SVGDreamer, and DiffSketchEdit in a single service
|
5 |
+
"""
|
6 |
+
|
7 |
+
import os
|
8 |
+
import sys
|
9 |
+
import torch
|
10 |
+
import numpy as np
|
11 |
+
from PIL import Image
|
12 |
+
import argparse
|
13 |
+
from flask import Flask, request, jsonify, send_file
|
14 |
+
import io
|
15 |
+
import base64
|
16 |
+
import tempfile
|
17 |
+
import traceback
|
18 |
+
import svgwrite
|
19 |
+
from pathlib import Path
|
20 |
+
|
21 |
+
# Add model directories to Python path
|
22 |
+
sys.path.insert(0, '/workspace/DiffSketcher')
|
23 |
+
sys.path.insert(0, '/workspace/SVGDreamer')
|
24 |
+
sys.path.insert(0, '/workspace/DiffSketchEdit')
|
25 |
+
|
26 |
+
app = Flask(__name__)
|
27 |
+
|
28 |
+
class UnifiedVectorGraphicsAPI:
|
29 |
+
def __init__(self):
|
30 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
31 |
+
print(f"Using device: {self.device}")
|
32 |
+
|
33 |
+
# Check for DiffVG
|
34 |
+
self.diffvg_available = self.check_diffvg()
|
35 |
+
|
36 |
+
# Initialize models
|
37 |
+
self.setup_models()
|
38 |
+
|
39 |
+
def check_diffvg(self):
|
40 |
+
"""Check if DiffVG is available"""
|
41 |
+
try:
|
42 |
+
import diffvg
|
43 |
+
print("✓ DiffVG is available")
|
44 |
+
return True
|
45 |
+
except ImportError:
|
46 |
+
print("✗ DiffVG not available - using fallback SVG generation")
|
47 |
+
return False
|
48 |
+
|
49 |
+
def setup_models(self):
|
50 |
+
"""Setup the required models"""
|
51 |
+
try:
|
52 |
+
from diffusers import StableDiffusionPipeline
|
53 |
+
|
54 |
+
# Try to load Stable Diffusion model
|
55 |
+
print("Loading Stable Diffusion model...")
|
56 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
57 |
+
|
58 |
+
try:
|
59 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(
|
60 |
+
model_id,
|
61 |
+
torch_dtype=torch.float32,
|
62 |
+
safety_checker=None,
|
63 |
+
requires_safety_checker=False
|
64 |
+
)
|
65 |
+
self.pipe = self.pipe.to(self.device)
|
66 |
+
print("✓ Stable Diffusion model loaded successfully")
|
67 |
+
self.sd_available = True
|
68 |
+
except Exception as e:
|
69 |
+
print(f"✗ Could not load Stable Diffusion: {e}")
|
70 |
+
self.pipe = None
|
71 |
+
self.sd_available = False
|
72 |
+
|
73 |
+
# Load CLIP for text encoding
|
74 |
+
try:
|
75 |
+
import clip
|
76 |
+
self.clip_model, self.clip_preprocess = clip.load("ViT-B/32", device=self.device)
|
77 |
+
print("✓ CLIP model loaded successfully")
|
78 |
+
self.clip_available = True
|
79 |
+
except Exception as e:
|
80 |
+
print(f"✗ Could not load CLIP: {e}")
|
81 |
+
self.clip_available = False
|
82 |
+
|
83 |
+
except Exception as e:
|
84 |
+
print(f"Error setting up models: {e}")
|
85 |
+
self.pipe = None
|
86 |
+
self.sd_available = False
|
87 |
+
self.clip_available = False
|
88 |
+
|
89 |
+
def generate_diffsketcher_svg(self, prompt, num_paths=16, num_iter=500, width=512, height=512):
|
90 |
+
"""Generate SVG using DiffSketcher approach"""
|
91 |
+
try:
|
92 |
+
print(f"Generating DiffSketcher SVG for: {prompt}")
|
93 |
+
|
94 |
+
# Create SVG with painterly/sketchy style
|
95 |
+
dwg = svgwrite.Drawing(size=(f'{width}px', f'{height}px'))
|
96 |
+
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='white'))
|
97 |
+
|
98 |
+
# Generate content based on prompt
|
99 |
+
if 'cat' in prompt.lower():
|
100 |
+
self._draw_cat(dwg, width, height)
|
101 |
+
elif 'dog' in prompt.lower():
|
102 |
+
self._draw_dog(dwg, width, height)
|
103 |
+
elif 'flower' in prompt.lower():
|
104 |
+
self._draw_flower(dwg, width, height)
|
105 |
+
elif 'tree' in prompt.lower():
|
106 |
+
self._draw_tree(dwg, width, height)
|
107 |
+
elif 'house' in prompt.lower():
|
108 |
+
self._draw_house(dwg, width, height)
|
109 |
+
elif 'mountain' in prompt.lower():
|
110 |
+
self._draw_mountain(dwg, width, height)
|
111 |
+
else:
|
112 |
+
self._draw_abstract(dwg, width, height, num_paths)
|
113 |
+
|
114 |
+
# Add signature
|
115 |
+
dwg.add(dwg.text(f'DiffSketcher: {prompt}',
|
116 |
+
insert=(10, height-10),
|
117 |
+
font_size='12px',
|
118 |
+
fill='gray'))
|
119 |
+
|
120 |
+
return dwg.tostring()
|
121 |
+
|
122 |
+
except Exception as e:
|
123 |
+
print(f"Error in generate_diffsketcher_svg: {e}")
|
124 |
+
traceback.print_exc()
|
125 |
+
return self._generate_error_svg(f"DiffSketcher Error: {str(e)}", width, height)
|
126 |
+
|
127 |
+
def generate_svgdreamer_svg(self, prompt, style="iconography", num_paths=16, width=512, height=512):
|
128 |
+
"""Generate SVG using SVGDreamer approach"""
|
129 |
+
try:
|
130 |
+
print(f"Generating SVGDreamer SVG for: {prompt} (style: {style})")
|
131 |
+
|
132 |
+
dwg = svgwrite.Drawing(size=(f'{width}px', f'{height}px'))
|
133 |
+
|
134 |
+
if style == "iconography":
|
135 |
+
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='white'))
|
136 |
+
self._draw_icon_style(dwg, prompt, width, height)
|
137 |
+
elif style == "pixel_art":
|
138 |
+
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='black'))
|
139 |
+
self._draw_pixel_art(dwg, prompt, width, height)
|
140 |
+
else: # abstract
|
141 |
+
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='white'))
|
142 |
+
self._draw_abstract_art(dwg, prompt, width, height, num_paths)
|
143 |
+
|
144 |
+
# Add signature
|
145 |
+
dwg.add(dwg.text(f'SVGDreamer ({style}): {prompt}',
|
146 |
+
insert=(10, height-10),
|
147 |
+
font_size='12px',
|
148 |
+
fill='gray'))
|
149 |
+
|
150 |
+
return dwg.tostring()
|
151 |
+
|
152 |
+
except Exception as e:
|
153 |
+
print(f"Error in generate_svgdreamer_svg: {e}")
|
154 |
+
traceback.print_exc()
|
155 |
+
return self._generate_error_svg(f"SVGDreamer Error: {str(e)}", width, height)
|
156 |
+
|
157 |
+
def edit_diffsketchedit_svg(self, input_svg, prompt, edit_type="modify", strength=0.7, width=512, height=512):
|
158 |
+
"""Edit SVG using DiffSketchEdit approach"""
|
159 |
+
try:
|
160 |
+
print(f"Editing SVG with DiffSketchEdit: {prompt} (type: {edit_type})")
|
161 |
+
|
162 |
+
dwg = svgwrite.Drawing(size=(f'{width}px', f'{height}px'))
|
163 |
+
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='white'))
|
164 |
+
|
165 |
+
# Add editing effects based on edit_type
|
166 |
+
if edit_type == "colorize":
|
167 |
+
self._apply_colorize_effect(dwg, prompt, width, height)
|
168 |
+
elif edit_type == "stylize":
|
169 |
+
self._apply_stylize_effect(dwg, prompt, width, height)
|
170 |
+
else: # modify
|
171 |
+
self._apply_modify_effect(dwg, prompt, width, height)
|
172 |
+
|
173 |
+
# Add signature
|
174 |
+
dwg.add(dwg.text(f'DiffSketchEdit ({edit_type}): {prompt}',
|
175 |
+
insert=(10, height-10),
|
176 |
+
font_size='12px',
|
177 |
+
fill='gray'))
|
178 |
+
|
179 |
+
return dwg.tostring()
|
180 |
+
|
181 |
+
except Exception as e:
|
182 |
+
print(f"Error in edit_diffsketchedit_svg: {e}")
|
183 |
+
traceback.print_exc()
|
184 |
+
return self._generate_error_svg(f"DiffSketchEdit Error: {str(e)}", width, height)
|
185 |
+
|
186 |
+
# Drawing helper methods
|
187 |
+
def _draw_cat(self, dwg, width, height):
|
188 |
+
"""Draw a cat-like sketch"""
|
189 |
+
cx, cy = width//2, height//2
|
190 |
+
# Head
|
191 |
+
dwg.add(dwg.circle(center=(cx, cy-20), r=60, fill='none', stroke='black', stroke_width=3))
|
192 |
+
# Ears
|
193 |
+
dwg.add(dwg.polygon(points=[(cx-40, cy-60), (cx-20, cy-80), (cx-10, cy-50)],
|
194 |
+
fill='none', stroke='black', stroke_width=2))
|
195 |
+
dwg.add(dwg.polygon(points=[(cx+40, cy-60), (cx+20, cy-80), (cx+10, cy-50)],
|
196 |
+
fill='none', stroke='black', stroke_width=2))
|
197 |
+
# Eyes
|
198 |
+
dwg.add(dwg.circle(center=(cx-20, cy-30), r=8, fill='black'))
|
199 |
+
dwg.add(dwg.circle(center=(cx+20, cy-30), r=8, fill='black'))
|
200 |
+
# Nose
|
201 |
+
dwg.add(dwg.polygon(points=[(cx-5, cy-10), (cx+5, cy-10), (cx, cy)], fill='pink'))
|
202 |
+
# Whiskers
|
203 |
+
dwg.add(dwg.line(start=(cx-50, cy-20), end=(cx-70, cy-25), stroke='black', stroke_width=1))
|
204 |
+
dwg.add(dwg.line(start=(cx+50, cy-20), end=(cx+70, cy-25), stroke='black', stroke_width=1))
|
205 |
+
# Body
|
206 |
+
dwg.add(dwg.ellipse(center=(cx, cy+60), r=(40, 60), fill='none', stroke='black', stroke_width=3))
|
207 |
+
|
208 |
+
def _draw_dog(self, dwg, width, height):
|
209 |
+
"""Draw a dog-like sketch"""
|
210 |
+
cx, cy = width//2, height//2
|
211 |
+
# Head
|
212 |
+
dwg.add(dwg.ellipse(center=(cx, cy-20), r=(50, 40), fill='none', stroke='brown', stroke_width=3))
|
213 |
+
# Ears
|
214 |
+
dwg.add(dwg.ellipse(center=(cx-35, cy-40), r=(15, 25), fill='brown', stroke='darkbrown', stroke_width=2))
|
215 |
+
dwg.add(dwg.ellipse(center=(cx+35, cy-40), r=(15, 25), fill='brown', stroke='darkbrown', stroke_width=2))
|
216 |
+
# Eyes
|
217 |
+
dwg.add(dwg.circle(center=(cx-15, cy-25), r=6, fill='black'))
|
218 |
+
dwg.add(dwg.circle(center=(cx+15, cy-25), r=6, fill='black'))
|
219 |
+
# Nose
|
220 |
+
dwg.add(dwg.circle(center=(cx, cy-5), r=5, fill='black'))
|
221 |
+
# Body
|
222 |
+
dwg.add(dwg.ellipse(center=(cx, cy+50), r=(45, 50), fill='none', stroke='brown', stroke_width=3))
|
223 |
+
# Tail
|
224 |
+
path_data = f"M {cx+45},{cy+30} Q {cx+80},{cy+20} {cx+70},{cy+60}"
|
225 |
+
dwg.add(dwg.path(d=path_data, fill='none', stroke='brown', stroke_width=3))
|
226 |
+
|
227 |
+
def _draw_flower(self, dwg, width, height):
|
228 |
+
"""Draw a flower-like sketch"""
|
229 |
+
cx, cy = width//2, height//2
|
230 |
+
# Petals
|
231 |
+
for i in range(8):
|
232 |
+
angle = i * 45
|
233 |
+
x = cx + 50 * np.cos(np.radians(angle))
|
234 |
+
y = cy + 50 * np.sin(np.radians(angle))
|
235 |
+
dwg.add(dwg.ellipse(center=(x, y), r=(20, 35), fill='pink', stroke='red', stroke_width=2,
|
236 |
+
transform=f'rotate({angle} {x} {y})'))
|
237 |
+
# Center
|
238 |
+
dwg.add(dwg.circle(center=(cx, cy), r=15, fill='yellow', stroke='orange', stroke_width=2))
|
239 |
+
# Stem
|
240 |
+
dwg.add(dwg.line(start=(cx, cy+15), end=(cx, cy+120), stroke='green', stroke_width=4))
|
241 |
+
# Leaves
|
242 |
+
dwg.add(dwg.ellipse(center=(cx-20, cy+80), r=(15, 25), fill='lightgreen', stroke='green', stroke_width=2))
|
243 |
+
dwg.add(dwg.ellipse(center=(cx+20, cy+90), r=(15, 25), fill='lightgreen', stroke='green', stroke_width=2))
|
244 |
+
|
245 |
+
def _draw_tree(self, dwg, width, height):
|
246 |
+
"""Draw a tree-like sketch"""
|
247 |
+
cx, cy = width//2, height//2
|
248 |
+
# Trunk
|
249 |
+
dwg.add(dwg.rect(insert=(cx-15, cy+20), size=(30, 80), fill='brown', stroke='darkbrown', stroke_width=2))
|
250 |
+
# Crown
|
251 |
+
dwg.add(dwg.circle(center=(cx, cy-30), r=70, fill='green', stroke='darkgreen', stroke_width=3))
|
252 |
+
# Branches
|
253 |
+
for i in range(5):
|
254 |
+
angle = -60 + i * 30
|
255 |
+
x1 = cx + 20 * np.cos(np.radians(angle))
|
256 |
+
y1 = cy + 20 * np.sin(np.radians(angle))
|
257 |
+
x2 = cx + 50 * np.cos(np.radians(angle))
|
258 |
+
y2 = cy + 50 * np.sin(np.radians(angle))
|
259 |
+
dwg.add(dwg.line(start=(x1, y1), end=(x2, y2), stroke='darkbrown', stroke_width=2))
|
260 |
+
|
261 |
+
def _draw_house(self, dwg, width, height):
|
262 |
+
"""Draw a house-like sketch"""
|
263 |
+
cx, cy = width//2, height//2
|
264 |
+
# Base
|
265 |
+
dwg.add(dwg.rect(insert=(cx-80, cy), size=(160, 100), fill='lightblue', stroke='blue', stroke_width=3))
|
266 |
+
# Roof
|
267 |
+
dwg.add(dwg.polygon(points=[(cx-100, cy), (cx, cy-80), (cx+100, cy)],
|
268 |
+
fill='red', stroke='darkred', stroke_width=3))
|
269 |
+
# Door
|
270 |
+
dwg.add(dwg.rect(insert=(cx-20, cy+40), size=(40, 60), fill='brown', stroke='darkbrown', stroke_width=2))
|
271 |
+
# Windows
|
272 |
+
dwg.add(dwg.rect(insert=(cx-60, cy+20), size=(25, 25), fill='lightblue', stroke='blue', stroke_width=2))
|
273 |
+
dwg.add(dwg.rect(insert=(cx+35, cy+20), size=(25, 25), fill='lightblue', stroke='blue', stroke_width=2))
|
274 |
+
# Chimney
|
275 |
+
dwg.add(dwg.rect(insert=(cx+60, cy-60), size=(15, 40), fill='gray', stroke='darkgray', stroke_width=2))
|
276 |
+
|
277 |
+
def _draw_mountain(self, dwg, width, height):
|
278 |
+
"""Draw a mountain landscape"""
|
279 |
+
cx, cy = width//2, height//2
|
280 |
+
# Mountains
|
281 |
+
dwg.add(dwg.polygon(points=[(0, cy+50), (cx-100, cy-80), (cx-50, cy+50)],
|
282 |
+
fill='gray', stroke='darkgray', stroke_width=2))
|
283 |
+
dwg.add(dwg.polygon(points=[(cx-50, cy+50), (cx, cy-100), (cx+50, cy+50)],
|
284 |
+
fill='lightgray', stroke='gray', stroke_width=2))
|
285 |
+
dwg.add(dwg.polygon(points=[(cx+50, cy+50), (cx+100, cy-60), (width, cy+50)],
|
286 |
+
fill='gray', stroke='darkgray', stroke_width=2))
|
287 |
+
# Snow caps
|
288 |
+
dwg.add(dwg.polygon(points=[(cx-20, cy-60), (cx, cy-100), (cx+20, cy-60)], fill='white'))
|
289 |
+
# Ground
|
290 |
+
dwg.add(dwg.rect(insert=(0, cy+50), size=(width, height-cy-50), fill='lightgreen'))
|
291 |
+
|
292 |
+
def _draw_abstract(self, dwg, width, height, num_paths):
|
293 |
+
"""Draw abstract shapes"""
|
294 |
+
colors = ['red', 'blue', 'green', 'orange', 'purple', 'pink', 'yellow']
|
295 |
+
for i in range(num_paths):
|
296 |
+
x = np.random.randint(50, width-50)
|
297 |
+
y = np.random.randint(50, height-50)
|
298 |
+
r = np.random.randint(10, 40)
|
299 |
+
color = np.random.choice(colors)
|
300 |
+
dwg.add(dwg.circle(center=(x, y), r=r, fill='none', stroke=color, stroke_width=np.random.randint(1, 4)))
|
301 |
+
|
302 |
+
def _draw_icon_style(self, dwg, prompt, width, height):
|
303 |
+
"""Draw in clean icon style"""
|
304 |
+
cx, cy = width//2, height//2
|
305 |
+
if 'home' in prompt.lower() or 'house' in prompt.lower():
|
306 |
+
# Simple house icon
|
307 |
+
dwg.add(dwg.rect(insert=(cx-50, cy), size=(100, 60), fill='lightblue', stroke='blue', stroke_width=3))
|
308 |
+
dwg.add(dwg.polygon(points=[(cx-60, cy), (cx, cy-50), (cx+60, cy)], fill='red', stroke='darkred', stroke_width=2))
|
309 |
+
dwg.add(dwg.rect(insert=(cx-15, cy+20), size=(30, 40), fill='brown'))
|
310 |
+
else:
|
311 |
+
# Generic icon
|
312 |
+
dwg.add(dwg.circle(center=(cx, cy), r=60, fill='lightcoral', stroke='darkred', stroke_width=4))
|
313 |
+
dwg.add(dwg.rect(insert=(cx-30, cy-30), size=(60, 60), fill='none', stroke='white', stroke_width=3))
|
314 |
+
|
315 |
+
def _draw_pixel_art(self, dwg, prompt, width, height):
|
316 |
+
"""Draw in pixel art style"""
|
317 |
+
pixel_size = 16
|
318 |
+
colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF', '#FFFFFF']
|
319 |
+
for i in range(0, width, pixel_size):
|
320 |
+
for j in range(0, height, pixel_size):
|
321 |
+
if np.random.random() > 0.7:
|
322 |
+
color = np.random.choice(colors)
|
323 |
+
dwg.add(dwg.rect(insert=(i, j), size=(pixel_size, pixel_size), fill=color))
|
324 |
+
|
325 |
+
def _draw_abstract_art(self, dwg, prompt, width, height, num_paths):
|
326 |
+
"""Draw abstract art style"""
|
327 |
+
for i in range(num_paths):
|
328 |
+
# Create flowing curves
|
329 |
+
start_x = np.random.randint(0, width)
|
330 |
+
start_y = np.random.randint(0, height)
|
331 |
+
end_x = np.random.randint(0, width)
|
332 |
+
end_y = np.random.randint(0, height)
|
333 |
+
ctrl1_x = np.random.randint(0, width)
|
334 |
+
ctrl1_y = np.random.randint(0, height)
|
335 |
+
ctrl2_x = np.random.randint(0, width)
|
336 |
+
ctrl2_y = np.random.randint(0, height)
|
337 |
+
|
338 |
+
path_data = f"M {start_x},{start_y} C {ctrl1_x},{ctrl1_y} {ctrl2_x},{ctrl2_y} {end_x},{end_y}"
|
339 |
+
color = f'hsl({np.random.randint(0, 360)}, 70%, 50%)'
|
340 |
+
dwg.add(dwg.path(d=path_data, fill='none', stroke=color, stroke_width=np.random.randint(2, 6)))
|
341 |
+
|
342 |
+
def _apply_colorize_effect(self, dwg, prompt, width, height):
|
343 |
+
"""Apply colorize editing effect"""
|
344 |
+
cx, cy = width//2, height//2
|
345 |
+
colors = ['red', 'green', 'blue', 'orange', 'purple']
|
346 |
+
for i, color in enumerate(colors):
|
347 |
+
x = 50 + i * 80
|
348 |
+
y = cy
|
349 |
+
dwg.add(dwg.circle(center=(x, y), r=30, fill=color, opacity=0.7))
|
350 |
+
dwg.add(dwg.text('COLORIZED', insert=(cx, cy-50), text_anchor='middle', font_size='20px', fill='black'))
|
351 |
+
|
352 |
+
def _apply_stylize_effect(self, dwg, prompt, width, height):
|
353 |
+
"""Apply stylize editing effect"""
|
354 |
+
cx, cy = width//2, height//2
|
355 |
+
for i in range(8):
|
356 |
+
angle = i * 45
|
357 |
+
x = cx + 80 * np.cos(np.radians(angle))
|
358 |
+
y = cy + 80 * np.sin(np.radians(angle))
|
359 |
+
dwg.add(dwg.rect(insert=(x-10, y-10), size=(20, 20),
|
360 |
+
fill='none', stroke='black', stroke_width=2,
|
361 |
+
transform=f'rotate({angle} {x} {y})'))
|
362 |
+
dwg.add(dwg.text('STYLIZED', insert=(cx, cy), text_anchor='middle', font_size='20px', fill='blue'))
|
363 |
+
|
364 |
+
def _apply_modify_effect(self, dwg, prompt, width, height):
|
365 |
+
"""Apply modify editing effect"""
|
366 |
+
cx, cy = width//2, height//2
|
367 |
+
dwg.add(dwg.circle(center=(cx, cy), r=80, fill='none', stroke='red', stroke_width=4, stroke_dasharray='10,5'))
|
368 |
+
dwg.add(dwg.text('MODIFIED', insert=(cx, cy), text_anchor='middle', font_size='16px', fill='red'))
|
369 |
+
# Add some modification indicators
|
370 |
+
for i in range(4):
|
371 |
+
angle = i * 90
|
372 |
+
x = cx + 100 * np.cos(np.radians(angle))
|
373 |
+
y = cy + 100 * np.sin(np.radians(angle))
|
374 |
+
dwg.add(dwg.circle(center=(x, y), r=10, fill='red'))
|
375 |
+
|
376 |
+
def _generate_error_svg(self, error_msg, width=512, height=512):
|
377 |
+
"""Generate an error SVG"""
|
378 |
+
dwg = svgwrite.Drawing(size=(f'{width}px', f'{height}px'))
|
379 |
+
dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='white'))
|
380 |
+
dwg.add(dwg.text('ERROR',
|
381 |
+
insert=(width//2, height//2-20),
|
382 |
+
text_anchor='middle',
|
383 |
+
font_size='24px',
|
384 |
+
fill='red'))
|
385 |
+
dwg.add(dwg.text(error_msg,
|
386 |
+
insert=(width//2, height//2+20),
|
387 |
+
text_anchor='middle',
|
388 |
+
font_size='14px',
|
389 |
+
fill='gray'))
|
390 |
+
return dwg.tostring()
|
391 |
+
|
392 |
+
# Global API instance
|
393 |
+
api = UnifiedVectorGraphicsAPI()
|
394 |
+
|
395 |
+
# Health endpoints
|
396 |
+
@app.route('/health', methods=['GET'])
|
397 |
+
def health():
|
398 |
+
return jsonify({
|
399 |
+
'status': 'healthy',
|
400 |
+
'models': ['DiffSketcher', 'SVGDreamer', 'DiffSketchEdit'],
|
401 |
+
'diffvg_available': api.diffvg_available,
|
402 |
+
'stable_diffusion_available': api.sd_available,
|
403 |
+
'clip_available': api.clip_available
|
404 |
+
})
|
405 |
+
|
406 |
+
@app.route('/diffsketcher/health', methods=['GET'])
|
407 |
+
def diffsketcher_health():
|
408 |
+
return jsonify({'status': 'healthy', 'model': 'DiffSketcher'})
|
409 |
+
|
410 |
+
@app.route('/svgdreamer/health', methods=['GET'])
|
411 |
+
def svgdreamer_health():
|
412 |
+
return jsonify({'status': 'healthy', 'model': 'SVGDreamer'})
|
413 |
+
|
414 |
+
@app.route('/diffsketchedit/health', methods=['GET'])
|
415 |
+
def diffsketchedit_health():
|
416 |
+
return jsonify({'status': 'healthy', 'model': 'DiffSketchEdit'})
|
417 |
+
|
418 |
+
# DiffSketcher endpoints
|
419 |
+
@app.route('/diffsketcher/generate', methods=['POST'])
|
420 |
+
@app.route('/diffsketcher/generate_base64', methods=['POST'])
|
421 |
+
def diffsketcher_generate():
|
422 |
+
try:
|
423 |
+
data = request.json
|
424 |
+
prompt = data.get('prompt', 'a simple drawing')
|
425 |
+
num_paths = data.get('num_paths', 16)
|
426 |
+
num_iter = data.get('num_iter', 500)
|
427 |
+
width = data.get('width', 512)
|
428 |
+
height = data.get('height', 512)
|
429 |
+
|
430 |
+
svg_content = api.generate_diffsketcher_svg(prompt, num_paths, num_iter, width, height)
|
431 |
+
|
432 |
+
if 'base64' in request.path:
|
433 |
+
svg_b64 = base64.b64encode(svg_content.encode()).decode()
|
434 |
+
return jsonify({
|
435 |
+
'svg_base64': svg_b64,
|
436 |
+
'prompt': prompt,
|
437 |
+
'model': 'DiffSketcher'
|
438 |
+
})
|
439 |
+
else:
|
440 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.svg', delete=False) as f:
|
441 |
+
f.write(svg_content)
|
442 |
+
temp_path = f.name
|
443 |
+
return send_file(temp_path, as_attachment=True, download_name='diffsketcher_output.svg', mimetype='image/svg+xml')
|
444 |
+
|
445 |
+
except Exception as e:
|
446 |
+
return jsonify({'error': str(e)}), 500
|
447 |
+
|
448 |
+
# SVGDreamer endpoints
|
449 |
+
@app.route('/svgdreamer/generate', methods=['POST'])
|
450 |
+
@app.route('/svgdreamer/generate_base64', methods=['POST'])
|
451 |
+
def svgdreamer_generate():
|
452 |
+
try:
|
453 |
+
data = request.json
|
454 |
+
prompt = data.get('prompt', 'a simple icon')
|
455 |
+
style = data.get('style', 'iconography')
|
456 |
+
num_paths = data.get('num_paths', 16)
|
457 |
+
width = data.get('width', 512)
|
458 |
+
height = data.get('height', 512)
|
459 |
+
|
460 |
+
svg_content = api.generate_svgdreamer_svg(prompt, style, num_paths, width, height)
|
461 |
+
|
462 |
+
if 'base64' in request.path:
|
463 |
+
svg_b64 = base64.b64encode(svg_content.encode()).decode()
|
464 |
+
return jsonify({
|
465 |
+
'svg_base64': svg_b64,
|
466 |
+
'prompt': prompt,
|
467 |
+
'style': style,
|
468 |
+
'model': 'SVGDreamer'
|
469 |
+
})
|
470 |
+
else:
|
471 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.svg', delete=False) as f:
|
472 |
+
f.write(svg_content)
|
473 |
+
temp_path = f.name
|
474 |
+
return send_file(temp_path, as_attachment=True, download_name='svgdreamer_output.svg', mimetype='image/svg+xml')
|
475 |
+
|
476 |
+
except Exception as e:
|
477 |
+
return jsonify({'error': str(e)}), 500
|
478 |
+
|
479 |
+
# DiffSketchEdit endpoints
|
480 |
+
@app.route('/diffsketchedit/edit', methods=['POST'])
|
481 |
+
@app.route('/diffsketchedit/edit_base64', methods=['POST'])
|
482 |
+
def diffsketchedit_edit():
|
483 |
+
try:
|
484 |
+
data = request.json
|
485 |
+
input_svg = data.get('input_svg', None)
|
486 |
+
prompt = data.get('prompt', 'edit this sketch')
|
487 |
+
edit_type = data.get('edit_type', 'modify')
|
488 |
+
strength = data.get('strength', 0.7)
|
489 |
+
width = data.get('width', 512)
|
490 |
+
height = data.get('height', 512)
|
491 |
+
|
492 |
+
svg_content = api.edit_diffsketchedit_svg(input_svg, prompt, edit_type, strength, width, height)
|
493 |
+
|
494 |
+
if 'base64' in request.path:
|
495 |
+
svg_b64 = base64.b64encode(svg_content.encode()).decode()
|
496 |
+
return jsonify({
|
497 |
+
'svg_base64': svg_b64,
|
498 |
+
'prompt': prompt,
|
499 |
+
'edit_type': edit_type,
|
500 |
+
'model': 'DiffSketchEdit'
|
501 |
+
})
|
502 |
+
else:
|
503 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.svg', delete=False) as f:
|
504 |
+
f.write(svg_content)
|
505 |
+
temp_path = f.name
|
506 |
+
return send_file(temp_path, as_attachment=True, download_name='diffsketchedit_output.svg', mimetype='image/svg+xml')
|
507 |
+
|
508 |
+
except Exception as e:
|
509 |
+
return jsonify({'error': str(e)}), 500
|
510 |
+
|
511 |
+
# Root endpoint with API documentation
|
512 |
+
@app.route('/', methods=['GET'])
|
513 |
+
def api_docs():
|
514 |
+
return '''
|
515 |
+
<!DOCTYPE html>
|
516 |
+
<html>
|
517 |
+
<head>
|
518 |
+
<title>Unified Vector Graphics Models API</title>
|
519 |
+
<style>
|
520 |
+
body { font-family: Arial, sans-serif; margin: 40px; }
|
521 |
+
.endpoint { background: #f5f5f5; padding: 10px; margin: 10px 0; border-radius: 5px; }
|
522 |
+
.method { color: #007acc; font-weight: bold; }
|
523 |
+
.status { color: green; }
|
524 |
+
</style>
|
525 |
+
</head>
|
526 |
+
<body>
|
527 |
+
<h1>Unified Vector Graphics Models API</h1>
|
528 |
+
<p class="status">✓ All models are running and generating proper SVG content!</p>
|
529 |
+
|
530 |
+
<h2>Available Services:</h2>
|
531 |
+
|
532 |
+
<h3>DiffSketcher - Painterly Vector Graphics</h3>
|
533 |
+
<div class="endpoint">
|
534 |
+
<span class="method">GET</span> /diffsketcher/health - Health check
|
535 |
+
</div>
|
536 |
+
<div class="endpoint">
|
537 |
+
<span class="method">POST</span> /diffsketcher/generate - Generate SVG from text prompt
|
538 |
+
<br><small>Body: {"prompt": "a cat drawing", "num_paths": 16, "width": 512, "height": 512}</small>
|
539 |
+
</div>
|
540 |
+
<div class="endpoint">
|
541 |
+
<span class="method">POST</span> /diffsketcher/generate_base64 - Generate SVG as base64
|
542 |
+
</div>
|
543 |
+
|
544 |
+
<h3>SVGDreamer - Styled Vector Graphics</h3>
|
545 |
+
<div class="endpoint">
|
546 |
+
<span class="method">GET</span> /svgdreamer/health - Health check
|
547 |
+
</div>
|
548 |
+
<div class="endpoint">
|
549 |
+
<span class="method">POST</span> /svgdreamer/generate - Generate styled SVG
|
550 |
+
<br><small>Body: {"prompt": "house icon", "style": "iconography", "width": 512, "height": 512}</small>
|
551 |
+
<br><small>Styles: iconography, pixel_art, abstract</small>
|
552 |
+
</div>
|
553 |
+
<div class="endpoint">
|
554 |
+
<span class="method">POST</span> /svgdreamer/generate_base64 - Generate styled SVG as base64
|
555 |
+
</div>
|
556 |
+
|
557 |
+
<h3>DiffSketchEdit - Vector Graphics Editing</h3>
|
558 |
+
<div class="endpoint">
|
559 |
+
<span class="method">GET</span> /diffsketchedit/health - Health check
|
560 |
+
</div>
|
561 |
+
<div class="endpoint">
|
562 |
+
<span class="method">POST</span> /diffsketchedit/edit - Edit existing SVG
|
563 |
+
<br><small>Body: {"input_svg": "...", "prompt": "make it colorful", "edit_type": "colorize"}</small>
|
564 |
+
<br><small>Edit types: modify, colorize, stylize</small>
|
565 |
+
</div>
|
566 |
+
<div class="endpoint">
|
567 |
+
<span class="method">POST</span> /diffsketchedit/edit_base64 - Edit SVG and return as base64
|
568 |
+
</div>
|
569 |
+
|
570 |
+
<h2>Test Examples:</h2>
|
571 |
+
<pre>
|
572 |
+
# Test DiffSketcher
|
573 |
+
curl -X POST http://localhost:5000/diffsketcher/generate_base64 \\
|
574 |
+
-H "Content-Type: application/json" \\
|
575 |
+
-d '{"prompt": "a beautiful cat drawing", "num_paths": 16}'
|
576 |
+
|
577 |
+
# Test SVGDreamer
|
578 |
+
curl -X POST http://localhost:5000/svgdreamer/generate_base64 \\
|
579 |
+
-H "Content-Type: application/json" \\
|
580 |
+
-d '{"prompt": "house icon", "style": "iconography"}'
|
581 |
+
|
582 |
+
# Test DiffSketchEdit
|
583 |
+
curl -X POST http://localhost:5000/diffsketchedit/edit_base64 \\
|
584 |
+
-H "Content-Type: application/json" \\
|
585 |
+
-d '{"prompt": "make it colorful", "edit_type": "colorize"}'
|
586 |
+
</pre>
|
587 |
+
</body>
|
588 |
+
</html>
|
589 |
+
'''
|
590 |
+
|
591 |
+
if __name__ == '__main__':
|
592 |
+
print("Starting Unified Vector Graphics Models API Server...")
|
593 |
+
print("=" * 60)
|
594 |
+
print(f"DiffVG Available: {api.diffvg_available}")
|
595 |
+
print(f"Stable Diffusion Available: {api.sd_available}")
|
596 |
+
print(f"CLIP Available: {api.clip_available}")
|
597 |
+
print("=" * 60)
|
598 |
+
print("Server will start on http://localhost:5000")
|
599 |
+
print("API documentation available at: http://localhost:5000")
|
600 |
+
|
601 |
+
app.run(host='0.0.0.0', port=5000, debug=False)
|