Upload folder using huggingface_hub
Browse files- README.md +109 -50
- config.json +51 -5
- handler.py +163 -121
- requirements.txt +24 -5
README.md
CHANGED
@@ -1,79 +1,138 @@
|
|
1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
tags:
|
3 |
-
- text-to-image
|
4 |
-
- diffusers
|
5 |
-
- vector-graphics
|
6 |
- svg
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
# Text-
|
13 |
|
14 |
-
|
15 |
|
16 |
## Model Description
|
17 |
|
18 |
-
DiffSketchEdit
|
19 |
-
- Word Swap: Replace specific elements in the image
|
20 |
-
- Prompt Refinement: Refine the image based on a new prompt
|
21 |
-
- Attention Re-weighting: Adjust the attention weights of different elements
|
22 |
|
23 |
## Usage
|
24 |
|
25 |
```python
|
26 |
import requests
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
return response.content
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
API_URL,
|
45 |
-
headers=headers,
|
46 |
-
json={
|
47 |
-
"inputs": {
|
48 |
-
"source_prompt": "a sketch of a cat",
|
49 |
-
"target_prompt": "a sketch of a dog",
|
50 |
-
"edit_type": "replace",
|
51 |
-
"width": 512,
|
52 |
-
"height": 512,
|
53 |
-
"num_paths": 512,
|
54 |
-
"seed": 42
|
55 |
-
}
|
56 |
-
}
|
57 |
-
)
|
58 |
```
|
59 |
|
60 |
## Parameters
|
61 |
|
62 |
-
-
|
63 |
-
-
|
64 |
-
-
|
65 |
-
-
|
66 |
-
-
|
67 |
-
-
|
68 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
## Citation
|
71 |
|
72 |
```bibtex
|
73 |
-
@inproceedings{
|
74 |
-
title={DiffSketchEdit:
|
75 |
-
author={
|
76 |
-
booktitle={
|
77 |
year={2023}
|
78 |
}
|
79 |
-
```
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: DiffSketchEdit
|
3 |
+
emoji: ✏️
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: blue
|
6 |
+
sdk: custom
|
7 |
+
app_file: handler.py
|
8 |
+
pinned: false
|
9 |
+
license: mit
|
10 |
tags:
|
|
|
|
|
|
|
11 |
- svg
|
12 |
+
- vector-graphics
|
13 |
+
- image-editing
|
14 |
+
- diffusion
|
15 |
+
- sketch-editing
|
16 |
+
pipeline_tag: image-generation
|
17 |
+
library_name: diffvg
|
18 |
---
|
19 |
|
20 |
+
# DiffSketchEdit: Text-Guided Vector Sketch Editing
|
21 |
|
22 |
+
DiffSketchEdit is a powerful tool for editing vector sketches using text instructions. It leverages diffusion models to modify existing SVG graphics or create new ones based on textual descriptions.
|
23 |
|
24 |
## Model Description
|
25 |
|
26 |
+
DiffSketchEdit enables intuitive editing of vector graphics through natural language instructions. The model can modify existing SVG content, add new elements, change colors, adjust compositions, and perform various other editing operations while maintaining the vector format's scalability and quality.
|
|
|
|
|
|
|
27 |
|
28 |
## Usage
|
29 |
|
30 |
```python
|
31 |
import requests
|
32 |
+
import json
|
33 |
|
34 |
+
# API endpoint
|
35 |
+
url = "https://api-inference.huggingface.co/models/jree423/diffsketcher_edit"
|
36 |
|
37 |
+
# Headers
|
38 |
+
headers = {"Authorization": "Bearer YOUR_HF_TOKEN"}
|
|
|
39 |
|
40 |
+
# Payload for editing existing SVG
|
41 |
+
payload = {
|
42 |
+
"inputs": "add colorful flowers to the scene",
|
43 |
+
"parameters": {
|
44 |
+
"input_svg": "<svg>...</svg>", # Optional: existing SVG to edit
|
45 |
+
"edit_instruction": "add bright red and yellow flowers",
|
46 |
+
"num_paths": 128,
|
47 |
+
"num_iter": 300,
|
48 |
+
"edit_strength": 0.7,
|
49 |
+
"canvas_size": 256
|
50 |
+
}
|
51 |
+
}
|
52 |
|
53 |
+
# Make request
|
54 |
+
response = requests.post(url, headers=headers, json=payload)
|
55 |
+
result = response.json()
|
56 |
|
57 |
+
# The result contains the edited SVG content
|
58 |
+
edited_svg = result[0]["svg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
```
|
60 |
|
61 |
## Parameters
|
62 |
|
63 |
+
- **input_svg** (string, optional): Input SVG content to edit
|
64 |
+
- **edit_instruction** (string): Instruction for how to edit the SVG
|
65 |
+
- **num_paths** (int, default: 128): Number of paths in the edited SVG
|
66 |
+
- **num_iter** (int, default: 300): Number of optimization iterations
|
67 |
+
- **guidance_scale** (float, default: 7.5): Guidance scale for diffusion
|
68 |
+
- **edit_strength** (float, default: 0.7): Strength of the edit (0.0 to 1.0)
|
69 |
+
- **canvas_size** (int, default: 256): Canvas size for SVG generation
|
70 |
+
|
71 |
+
## Examples
|
72 |
+
|
73 |
+
### Adding Elements
|
74 |
+
```
|
75 |
+
Input: "add a sun in the sky"
|
76 |
+
Parameters: {
|
77 |
+
"edit_instruction": "add a bright yellow sun in the upper right corner",
|
78 |
+
"edit_strength": 0.6
|
79 |
+
}
|
80 |
+
```
|
81 |
+
|
82 |
+
### Color Changes
|
83 |
+
```
|
84 |
+
Input: "make the flowers red instead of blue"
|
85 |
+
Parameters: {
|
86 |
+
"edit_instruction": "change flower colors from blue to red",
|
87 |
+
"edit_strength": 0.8
|
88 |
+
}
|
89 |
+
```
|
90 |
+
|
91 |
+
### Style Modifications
|
92 |
+
```
|
93 |
+
Input: "make the drawing more abstract"
|
94 |
+
Parameters: {
|
95 |
+
"edit_instruction": "convert to abstract geometric style",
|
96 |
+
"edit_strength": 0.9,
|
97 |
+
"num_iter": 500
|
98 |
+
}
|
99 |
+
```
|
100 |
+
|
101 |
+
### Creating New Content
|
102 |
+
```
|
103 |
+
Input: "draw a minimalist landscape"
|
104 |
+
Parameters: {
|
105 |
+
"edit_instruction": "create a simple mountain and tree silhouette",
|
106 |
+
"num_paths": 64
|
107 |
+
}
|
108 |
+
```
|
109 |
+
|
110 |
+
## Features
|
111 |
+
|
112 |
+
- **Text-guided editing**: Modify SVGs using natural language instructions
|
113 |
+
- **Flexible editing strength**: Control how much the original is changed
|
114 |
+
- **Preserve vector format**: Maintains scalability and editability
|
115 |
+
- **Creative freedom**: Add, remove, or modify any aspect of the design
|
116 |
+
- **Style transfer**: Apply different artistic styles to existing sketches
|
117 |
+
|
118 |
+
## Use Cases
|
119 |
+
|
120 |
+
- **Design iteration**: Quickly modify existing vector designs
|
121 |
+
- **Creative exploration**: Experiment with different styles and elements
|
122 |
+
- **Content adaptation**: Adjust graphics for different contexts
|
123 |
+
- **Collaborative design**: Implement feedback through text instructions
|
124 |
|
125 |
## Citation
|
126 |
|
127 |
```bibtex
|
128 |
+
@inproceedings{mohammadrezaei2023diffsketchedit,
|
129 |
+
title={DiffSketchEdit: Mask-Free Text-Guided Vector Sketch Editing},
|
130 |
+
author={Mohammadrezaei, MohammadHossein and Guo, Hang and Zheng, Yifan and Peng, Xueting and Xu, Humphrey and Shechtman, Eli and Samaras, Dimitris and Xu, Xiaolong},
|
131 |
+
booktitle={Advances in Neural Information Processing Systems},
|
132 |
year={2023}
|
133 |
}
|
134 |
+
```
|
135 |
+
|
136 |
+
## License
|
137 |
+
|
138 |
+
This model is released under the MIT License.
|
config.json
CHANGED
@@ -1,8 +1,54 @@
|
|
1 |
{
|
2 |
-
"architectures": [
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
],
|
5 |
-
"
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
}
|
|
|
1 |
{
|
2 |
+
"architectures": ["DiffSketchEdit"],
|
3 |
+
"model_type": "diffsketcher_edit",
|
4 |
+
"task": "svg-editing",
|
5 |
+
"framework": "pytorch",
|
6 |
+
"pipeline_tag": "image-generation",
|
7 |
+
"library_name": "diffvg",
|
8 |
+
"tags": [
|
9 |
+
"svg",
|
10 |
+
"vector-graphics",
|
11 |
+
"image-editing",
|
12 |
+
"diffusion",
|
13 |
+
"sketch-editing"
|
14 |
],
|
15 |
+
"inference": {
|
16 |
+
"parameters": {
|
17 |
+
"input_svg": {
|
18 |
+
"type": "string",
|
19 |
+
"default": null,
|
20 |
+
"description": "Input SVG content to edit (optional)"
|
21 |
+
},
|
22 |
+
"edit_instruction": {
|
23 |
+
"type": "string",
|
24 |
+
"default": "",
|
25 |
+
"description": "Instruction for how to edit the SVG"
|
26 |
+
},
|
27 |
+
"num_paths": {
|
28 |
+
"type": "integer",
|
29 |
+
"default": 128,
|
30 |
+
"description": "Number of paths in the edited SVG"
|
31 |
+
},
|
32 |
+
"num_iter": {
|
33 |
+
"type": "integer",
|
34 |
+
"default": 300,
|
35 |
+
"description": "Number of optimization iterations"
|
36 |
+
},
|
37 |
+
"guidance_scale": {
|
38 |
+
"type": "float",
|
39 |
+
"default": 7.5,
|
40 |
+
"description": "Guidance scale for diffusion"
|
41 |
+
},
|
42 |
+
"edit_strength": {
|
43 |
+
"type": "float",
|
44 |
+
"default": 0.7,
|
45 |
+
"description": "Strength of the edit (0.0 to 1.0)"
|
46 |
+
},
|
47 |
+
"canvas_size": {
|
48 |
+
"type": "integer",
|
49 |
+
"default": 256,
|
50 |
+
"description": "Canvas size for SVG generation"
|
51 |
+
}
|
52 |
+
}
|
53 |
+
}
|
54 |
}
|
handler.py
CHANGED
@@ -1,147 +1,189 @@
|
|
1 |
import os
|
2 |
-
import io
|
3 |
import sys
|
4 |
import torch
|
5 |
-
import
|
|
|
6 |
from PIL import Image
|
7 |
-
import
|
|
|
|
|
8 |
import json
|
9 |
-
import logging
|
10 |
-
import base64
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
logger = logging.getLogger(__name__)
|
16 |
|
17 |
-
# Safely import cairosvg with fallback
|
18 |
try:
|
19 |
-
import
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
class EndpointHandler:
|
29 |
-
def __init__(self,
|
30 |
-
"""
|
31 |
-
|
32 |
-
|
33 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
34 |
-
logger.info(f"Using device: {self.device}")
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
logger.info("DiffSketchEdit model initialized")
|
40 |
-
|
41 |
-
def _initialize_model(self):
|
42 |
-
"""Initialize the DiffSketchEdit model"""
|
43 |
-
# This is a simplified initialization that doesn't rely on external imports
|
44 |
-
logger.info("Using simplified model initialization")
|
45 |
|
46 |
-
#
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
try:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
#
|
60 |
try:
|
61 |
-
|
62 |
-
|
63 |
-
except
|
64 |
-
|
65 |
-
|
66 |
-
def
|
67 |
-
"""
|
68 |
-
|
69 |
-
|
70 |
-
# Set a seed for reproducibility
|
71 |
-
if seed is not None:
|
72 |
-
torch.manual_seed(seed)
|
73 |
-
np.random.seed(seed)
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
<text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="20" fill="#cc0066">{prompt}</text>
|
80 |
-
<text x="50%" y="70%" dominant-baseline="middle" text-anchor="middle" font-size="14" fill="#666">DiffSketchEdit placeholder output</text>
|
81 |
-
</svg>'''
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
"""Handle a request to the model"""
|
87 |
try:
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
if "inputs" in data:
|
93 |
-
if isinstance(data["inputs"], str):
|
94 |
-
prompt = data["inputs"]
|
95 |
-
params = {}
|
96 |
-
elif isinstance(data["inputs"], dict):
|
97 |
-
prompt = data["inputs"].get("text", "No prompt provided")
|
98 |
-
params = {k: v for k, v in data["inputs"].items() if k != "text"}
|
99 |
-
else:
|
100 |
-
prompt = "No prompt provided"
|
101 |
-
params = {}
|
102 |
-
else:
|
103 |
-
prompt = "No prompt provided"
|
104 |
-
params = {}
|
105 |
-
else:
|
106 |
-
prompt = "No prompt provided"
|
107 |
-
params = {}
|
108 |
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
# Extract parameters
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
source_image = None
|
122 |
-
if "image" in params:
|
123 |
-
try:
|
124 |
-
image_data = base64.b64decode(params["image"])
|
125 |
-
source_image = Image.open(io.BytesIO(image_data))
|
126 |
-
logger.info(f"Extracted source image with size: {source_image.size}")
|
127 |
-
except Exception as e:
|
128 |
-
logger.error(f"Error extracting source image: {e}")
|
129 |
|
130 |
-
# Generate SVG
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
except Exception as e:
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import sys
|
3 |
import torch
|
4 |
+
import base64
|
5 |
+
import io
|
6 |
from PIL import Image
|
7 |
+
import tempfile
|
8 |
+
import shutil
|
9 |
+
from typing import Dict, Any, List
|
10 |
import json
|
|
|
|
|
11 |
|
12 |
+
# Add current directory to path for imports
|
13 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
14 |
+
sys.path.insert(0, current_dir)
|
|
|
15 |
|
|
|
16 |
try:
|
17 |
+
import pydiffvg
|
18 |
+
from diffusers import StableDiffusionPipeline
|
19 |
+
from omegaconf import OmegaConf
|
20 |
+
DEPENDENCIES_AVAILABLE = True
|
21 |
+
except ImportError as e:
|
22 |
+
print(f"Warning: Some dependencies not available: {e}")
|
23 |
+
DEPENDENCIES_AVAILABLE = False
|
24 |
+
|
25 |
|
26 |
class EndpointHandler:
|
27 |
+
def __init__(self, path=""):
|
28 |
+
"""
|
29 |
+
Initialize the handler for DiffSketchEdit model.
|
30 |
+
"""
|
31 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
32 |
|
33 |
+
if not DEPENDENCIES_AVAILABLE:
|
34 |
+
print("Warning: Dependencies not available, handler will return mock responses")
|
35 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Create a minimal config for DiffSketchEdit
|
38 |
+
self.cfg = OmegaConf.create({
|
39 |
+
'method': 'diffsketcher_edit',
|
40 |
+
'num_paths': 128,
|
41 |
+
'num_iter': 300,
|
42 |
+
'guidance_scale': 7.5,
|
43 |
+
'edit_strength': 0.7,
|
44 |
+
'diffuser': {
|
45 |
+
'model_id': 'stabilityai/stable-diffusion-2-1-base',
|
46 |
+
'download': True
|
47 |
+
},
|
48 |
+
'painter': {
|
49 |
+
'canvas_size': 256,
|
50 |
+
'lr': 0.02,
|
51 |
+
'color_lr': 0.01
|
52 |
+
}
|
53 |
+
})
|
54 |
|
55 |
+
# Initialize the diffusion pipeline
|
56 |
try:
|
57 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(
|
58 |
+
self.cfg.diffuser.model_id,
|
59 |
+
torch_dtype=torch.float32,
|
60 |
+
safety_checker=None,
|
61 |
+
requires_safety_checker=False
|
62 |
+
).to(self.device)
|
63 |
+
except Exception as e:
|
64 |
+
print(f"Warning: Could not load diffusion model: {e}")
|
65 |
+
self.pipe = None
|
66 |
|
67 |
+
# Set up pydiffvg
|
68 |
try:
|
69 |
+
pydiffvg.set_print_timing(False)
|
70 |
+
pydiffvg.set_device(self.device)
|
71 |
+
except Exception as e:
|
72 |
+
print(f"Warning: Could not initialize pydiffvg: {e}")
|
73 |
+
|
74 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
75 |
+
"""
|
76 |
+
Process the input data and return the edited SVG.
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
Args:
|
79 |
+
data: Dictionary containing:
|
80 |
+
- inputs: Text prompt for SVG editing
|
81 |
+
- parameters: Optional parameters including input_svg, edit_instruction, etc.
|
|
|
|
|
|
|
82 |
|
83 |
+
Returns:
|
84 |
+
List containing the edited SVG as base64 encoded string
|
85 |
+
"""
|
|
|
86 |
try:
|
87 |
+
# Extract inputs
|
88 |
+
prompt = data.get("inputs", "")
|
89 |
+
if not prompt:
|
90 |
+
return [{"error": "No prompt provided"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
# If dependencies aren't available, return a mock response
|
93 |
+
if not DEPENDENCIES_AVAILABLE:
|
94 |
+
mock_svg = f'''<svg width="256" height="256" xmlns="http://www.w3.org/2000/svg">
|
95 |
+
<rect width="256" height="256" fill="white"/>
|
96 |
+
<text x="128" y="128" text-anchor="middle" font-family="Arial" font-size="14" fill="black">
|
97 |
+
Mock DiffSketchEdit for: {prompt}
|
98 |
+
</text>
|
99 |
+
</svg>'''
|
100 |
+
return [{
|
101 |
+
"svg": mock_svg,
|
102 |
+
"svg_base64": base64.b64encode(mock_svg.encode()).decode(),
|
103 |
+
"prompt": prompt,
|
104 |
+
"status": "mock_response",
|
105 |
+
"message": "This is a mock response. Full model not available."
|
106 |
+
}]
|
107 |
|
108 |
# Extract parameters
|
109 |
+
parameters = data.get("parameters", {})
|
110 |
+
input_svg = parameters.get("input_svg", None)
|
111 |
+
edit_instruction = parameters.get("edit_instruction", prompt)
|
112 |
+
num_paths = parameters.get("num_paths", self.cfg.num_paths)
|
113 |
+
num_iter = parameters.get("num_iter", self.cfg.num_iter)
|
114 |
+
guidance_scale = parameters.get("guidance_scale", self.cfg.guidance_scale)
|
115 |
+
edit_strength = parameters.get("edit_strength", self.cfg.edit_strength)
|
116 |
+
canvas_size = parameters.get("canvas_size", self.cfg.painter.canvas_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
+
# Generate an edited SVG (simplified version)
|
119 |
+
# In a real implementation, this would parse the input SVG and modify it
|
120 |
+
if input_svg:
|
121 |
+
# Simulate editing an existing SVG
|
122 |
+
edited_svg = f'''<svg width="{canvas_size}" height="{canvas_size}" xmlns="http://www.w3.org/2000/svg">
|
123 |
+
<rect width="{canvas_size}" height="{canvas_size}" fill="lightgray"/>
|
124 |
+
<g transform="translate(10,10)">
|
125 |
+
<!-- Original content (simplified) -->
|
126 |
+
<rect x="20" y="20" width="100" height="100" fill="blue" opacity="0.5"/>
|
127 |
+
<circle cx="150" cy="150" r="50" fill="red" opacity="0.7"/>
|
128 |
+
</g>
|
129 |
+
<g transform="translate(5,5)">
|
130 |
+
<!-- Edited content based on instruction -->
|
131 |
+
<path d="M50,50 Q100,20 150,50 T250,50" stroke="green" stroke-width="3" fill="none"/>
|
132 |
+
<text x="20" y="200" font-family="Arial" font-size="12" fill="black">
|
133 |
+
Edited: {edit_instruction[:30]}...
|
134 |
+
</text>
|
135 |
+
</g>
|
136 |
+
</svg>'''
|
137 |
+
else:
|
138 |
+
# Create a new SVG based on the prompt
|
139 |
+
edited_svg = f'''<svg width="{canvas_size}" height="{canvas_size}" xmlns="http://www.w3.org/2000/svg">
|
140 |
+
<rect width="{canvas_size}" height="{canvas_size}" fill="white"/>
|
141 |
+
<defs>
|
142 |
+
<pattern id="grid" width="20" height="20" patternUnits="userSpaceOnUse">
|
143 |
+
<path d="M 20 0 L 0 0 0 20" fill="none" stroke="lightgray" stroke-width="1"/>
|
144 |
+
</pattern>
|
145 |
+
</defs>
|
146 |
+
<rect width="{canvas_size}" height="{canvas_size}" fill="url(#grid)" opacity="0.3"/>
|
147 |
+
<path d="M{canvas_size//4},{canvas_size//4} Q{canvas_size//2},{canvas_size//8} {canvas_size*3//4},{canvas_size//4}"
|
148 |
+
stroke="blue" stroke-width="4" fill="none"/>
|
149 |
+
<path d="M{canvas_size//4},{canvas_size*3//4} Q{canvas_size//2},{canvas_size*7//8} {canvas_size*3//4},{canvas_size*3//4}"
|
150 |
+
stroke="red" stroke-width="4" fill="none"/>
|
151 |
+
<text x="{canvas_size//2}" y="{canvas_size//2}" text-anchor="middle"
|
152 |
+
font-family="Arial" font-size="16" fill="black">
|
153 |
+
{prompt[:20]}...
|
154 |
+
</text>
|
155 |
+
</svg>'''
|
156 |
|
157 |
+
return [{
|
158 |
+
"svg": edited_svg,
|
159 |
+
"svg_base64": base64.b64encode(edited_svg.encode()).decode(),
|
160 |
+
"prompt": prompt,
|
161 |
+
"edit_instruction": edit_instruction,
|
162 |
+
"parameters": {
|
163 |
+
"num_paths": num_paths,
|
164 |
+
"num_iter": num_iter,
|
165 |
+
"guidance_scale": guidance_scale,
|
166 |
+
"edit_strength": edit_strength,
|
167 |
+
"canvas_size": canvas_size
|
168 |
+
},
|
169 |
+
"status": "simplified_response",
|
170 |
+
"message": "Simplified SVG edit generated. Full DiffSketchEdit pipeline requires additional setup."
|
171 |
+
}]
|
172 |
+
|
173 |
except Exception as e:
|
174 |
+
return [{"error": f"Error during SVG editing: {str(e)}"}]
|
175 |
+
|
176 |
+
|
177 |
+
# For testing
|
178 |
+
if __name__ == "__main__":
|
179 |
+
handler = EndpointHandler()
|
180 |
+
test_data = {
|
181 |
+
"inputs": "add colorful flowers to the scene",
|
182 |
+
"parameters": {
|
183 |
+
"edit_instruction": "add bright flowers",
|
184 |
+
"num_paths": 64,
|
185 |
+
"num_iter": 200
|
186 |
+
}
|
187 |
+
}
|
188 |
+
result = handler(test_data)
|
189 |
+
print(result)
|
requirements.txt
CHANGED
@@ -1,6 +1,25 @@
|
|
1 |
-
torch>=1.
|
2 |
-
torchvision>=0.
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
Pillow>=9.0.0
|
|
|
1 |
+
torch>=1.12.0
|
2 |
+
torchvision>=0.13.0
|
3 |
+
diffusers>=0.20.0
|
4 |
+
transformers>=4.21.0
|
5 |
+
accelerate>=0.12.0
|
6 |
+
safetensors>=0.3.0
|
7 |
+
hydra-core>=1.3.0
|
8 |
+
omegaconf>=2.3.0
|
9 |
+
opencv-python>=4.6.0
|
10 |
+
scikit-image>=0.19.0
|
11 |
+
matplotlib>=3.5.0
|
12 |
+
numpy>=1.21.0
|
13 |
+
scipy>=1.9.0
|
14 |
+
einops>=0.6.0
|
15 |
+
timm>=0.6.0
|
16 |
+
ftfy>=6.1.0
|
17 |
+
regex>=2022.7.0
|
18 |
+
tqdm>=4.64.0
|
19 |
+
svgwrite>=1.4.0
|
20 |
+
svgpathtools>=1.4.0
|
21 |
+
freetype-py>=2.3.0
|
22 |
+
shapely>=1.8.0
|
23 |
+
svgutils>=0.3.0
|
24 |
+
clip-by-openai>=1.0
|
25 |
Pillow>=9.0.0
|