|
|
|
import base64 |
|
import io |
|
from PIL import Image, ImageDraw |
|
import json |
|
|
|
class VectorGraphicsModel: |
|
def __init__(self, config=None): |
|
self.config = config or {} |
|
self.initialized = False |
|
|
|
def initialize(self): |
|
"""Initialize the model.""" |
|
self.initialized = True |
|
|
|
def generate(self, prompt, **kwargs): |
|
"""Generate vector graphics from a prompt.""" |
|
|
|
|
|
|
|
|
|
svg = f""" |
|
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512"> |
|
<rect width="512" height="512" fill="#f0f0f0"/> |
|
<text x="256" y="50" font-family="Arial" font-size="20" text-anchor="middle" fill="#333">Generated from: "{prompt}"</text> |
|
<g transform="translate(256, 256)"> |
|
<circle cx="0" cy="0" r="100" fill="#3498db" opacity="0.7"/> |
|
<rect x="-50" y="-50" width="100" height="100" fill="#e74c3c" opacity="0.7"/> |
|
<path d="M-100,-100 L100,100 M-100,100 L100,-100" stroke="#2c3e50" stroke-width="5"/> |
|
</g> |
|
</svg> |
|
""" |
|
|
|
|
|
img = Image.new("RGB", (512, 512), color="#f0f0f0") |
|
draw = ImageDraw.Draw(img) |
|
draw.ellipse((156, 156, 356, 356), fill="#3498db", outline="#3498db") |
|
draw.rectangle((206, 206, 306, 306), fill="#e74c3c", outline="#e74c3c") |
|
draw.line((156, 156, 356, 356), fill="#2c3e50", width=5) |
|
draw.line((156, 356, 356, 156), fill="#2c3e50", width=5) |
|
|
|
|
|
|
|
|
|
|
|
|
|
buffered = io.BytesIO() |
|
img.save(buffered, format="PNG") |
|
img_str = base64.b64encode(buffered.getvalue()).decode() |
|
|
|
return {"svg": svg, "image": img_str} |
|
|