Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,198 +1,119 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import vtracer
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
""
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
colormode.change(handle_color_mode, inputs=colormode,outputs=output_text)
|
| 121 |
-
hierarchical.change(handle_color_mode, inputs=hierarchical,outputs=output_text)
|
| 122 |
-
mode.change(handle_color_mode, inputs=mode,outputs=output_text)
|
| 123 |
-
default_values = {
|
| 124 |
-
"color_precision": 6,
|
| 125 |
-
"layer_difference": 16
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
def clear_inputs():
|
| 129 |
-
return gr.Image(value=None), gr.Radio(value="color"), gr.Radio(value="stacked"), gr.Radio(value="spline"), gr.Slider(value=4), gr.Slider(value=6), gr.Slider(value=16), gr.Slider(value=60), gr.Slider(value=4.0), gr.Slider(value=10), gr.Slider(value=45), gr.Slider(value=3)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
|
| 133 |
-
is_color_mode = colormode == "color"
|
| 134 |
-
return (
|
| 135 |
-
gr.update(interactive=is_color_mode),
|
| 136 |
-
gr.update(interactive=is_color_mode),
|
| 137 |
-
gr.update(visible=is_color_mode) # Show/Hide Hierarchical Mode
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
colormode.change(
|
| 141 |
-
update_interactivity_and_visibility,
|
| 142 |
-
inputs=[colormode, color_precision, layer_difference],
|
| 143 |
-
outputs=[color_precision, layer_difference, hierarchical]
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
def update_interactivity_and_visibility_for_mode(mode):
|
| 147 |
-
is_spline_mode = mode == "spline"
|
| 148 |
-
return (
|
| 149 |
-
gr.update(interactive=is_spline_mode),
|
| 150 |
-
gr.update(interactive=is_spline_mode),
|
| 151 |
-
gr.update(interactive=is_spline_mode)
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
mode.change(
|
| 155 |
-
update_interactivity_and_visibility_for_mode,
|
| 156 |
-
inputs=[mode],
|
| 157 |
-
outputs=[corner_threshold,length_threshold,splice_threshold]
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
clear_button.click(
|
| 161 |
-
clear_inputs,
|
| 162 |
-
outputs=[
|
| 163 |
-
image_input,
|
| 164 |
-
colormode,
|
| 165 |
-
hierarchical,
|
| 166 |
-
mode,
|
| 167 |
-
filter_speckle,
|
| 168 |
-
color_precision,
|
| 169 |
-
layer_difference,
|
| 170 |
-
corner_threshold,
|
| 171 |
-
length_threshold,
|
| 172 |
-
max_iterations,
|
| 173 |
-
splice_threshold,
|
| 174 |
-
path_precision
|
| 175 |
-
]
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
-
convert_button.click(
|
| 179 |
-
convert_to_vector,
|
| 180 |
-
inputs=[
|
| 181 |
-
image_input,
|
| 182 |
-
colormode,
|
| 183 |
-
hierarchical,
|
| 184 |
-
mode,
|
| 185 |
-
filter_speckle,
|
| 186 |
-
color_precision,
|
| 187 |
-
layer_difference,
|
| 188 |
-
corner_threshold,
|
| 189 |
-
length_threshold,
|
| 190 |
-
max_iterations,
|
| 191 |
-
splice_threshold,
|
| 192 |
-
path_precision
|
| 193 |
-
],
|
| 194 |
-
outputs=[html,svg_output]
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
# Launch the app
|
| 198 |
-
app.launch(debug=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import vtracer
|
| 3 |
+
import os
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
# Initialize FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Configure CORS to allow requests from Figma plugin
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["https://www.figma.com"], # Restrict to Figma's domain
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Existing vtracer conversion function
|
| 23 |
+
def convert_to_vector(
|
| 24 |
+
image,
|
| 25 |
+
colormode="color",
|
| 26 |
+
hierarchical="stacked",
|
| 27 |
+
mode="spline",
|
| 28 |
+
filter_speckle=4,
|
| 29 |
+
color_precision=6,
|
| 30 |
+
layer_difference=16,
|
| 31 |
+
corner_threshold=60,
|
| 32 |
+
length_threshold=4.0,
|
| 33 |
+
max_iterations=10,
|
| 34 |
+
splice_threshold=45,
|
| 35 |
+
path_precision=3
|
| 36 |
+
):
|
| 37 |
+
input_path = "temp_input.jpg"
|
| 38 |
+
output_path = "svg_output.svg"
|
| 39 |
+
|
| 40 |
+
# Save the input image to a temporary file
|
| 41 |
+
image.save(input_path)
|
| 42 |
+
|
| 43 |
+
# Convert the image to SVG using VTracer
|
| 44 |
+
vtracer.convert_image_to_svg_py(
|
| 45 |
+
input_path,
|
| 46 |
+
output_path,
|
| 47 |
+
colormode=colormode,
|
| 48 |
+
hierarchical=hierarchical,
|
| 49 |
+
mode=mode,
|
| 50 |
+
filter_speckle=int(filter_speckle),
|
| 51 |
+
color_precision=int(color_precision),
|
| 52 |
+
layer_difference=int(layer_difference),
|
| 53 |
+
corner_threshold=int(corner_threshold),
|
| 54 |
+
length_threshold=float(length_threshold),
|
| 55 |
+
max_iterations=int(max_iterations),
|
| 56 |
+
splice_threshold=int(splice_threshold),
|
| 57 |
+
path_precision=int(path_precision)
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Read the SVG output
|
| 61 |
+
with open(output_path, "r") as f:
|
| 62 |
+
svg_content = f.read()
|
| 63 |
+
|
| 64 |
+
# Clean up temporary files
|
| 65 |
+
os.remove(input_path)
|
| 66 |
+
os.remove(output_path)
|
| 67 |
+
|
| 68 |
+
return svg_content
|
| 69 |
+
|
| 70 |
+
# FastAPI endpoint for vector conversion
|
| 71 |
+
@app.post("/convert")
|
| 72 |
+
async def convert_image(
|
| 73 |
+
file: UploadFile = File(...),
|
| 74 |
+
colormode: str = Form("color"),
|
| 75 |
+
hierarchical: str = Form("stacked"),
|
| 76 |
+
mode: str = Form("spline"),
|
| 77 |
+
filter_speckle: int = Form(4),
|
| 78 |
+
color_precision: int = Form(6),
|
| 79 |
+
layer_difference: int = Form(16),
|
| 80 |
+
corner_threshold: int = Form(60),
|
| 81 |
+
length_threshold: float = Form(4.0),
|
| 82 |
+
max_iterations: int = Form(10),
|
| 83 |
+
splice_threshold: int = Form(45),
|
| 84 |
+
path_precision: int = Form(3)
|
| 85 |
+
):
|
| 86 |
+
try:
|
| 87 |
+
# Read the uploaded image
|
| 88 |
+
image_data = await file.read()
|
| 89 |
+
image = Image.open(io.BytesIO(image_data))
|
| 90 |
+
|
| 91 |
+
# Convert to SVG
|
| 92 |
+
svg_content = convert_to_vector(
|
| 93 |
+
image,
|
| 94 |
+
colormode,
|
| 95 |
+
hierarchical,
|
| 96 |
+
mode,
|
| 97 |
+
filter_speckle,
|
| 98 |
+
color_precision,
|
| 99 |
+
layer_difference,
|
| 100 |
+
corner_threshold,
|
| 101 |
+
length_threshold,
|
| 102 |
+
max_iterations,
|
| 103 |
+
splice_threshold,
|
| 104 |
+
path_precision
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
return JSONResponse(content={"svg": svg_content})
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 110 |
+
|
| 111 |
+
# Existing Gradio interface (unchanged)
|
| 112 |
+
with gr.Blocks(css=...) as gradio_app:
|
| 113 |
+
# Your existing Gradio code here (omitted for brevity)
|
| 114 |
+
pass
|
| 115 |
+
|
| 116 |
+
# Mount Gradio app to FastAPI
|
| 117 |
+
app.mount("/", gradio_app)
|
| 118 |
+
|
| 119 |
+
# Note: app.launch() is not needed for Hugging Face Spaces; it will be handled by the platform
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|