Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,19 @@ def image_to_svg(image, colormode, hierarchical, filter_speckle, color_precision
|
|
| 7 |
"""
|
| 8 |
Converts an input image to an SVG string using vtracer.
|
| 9 |
This function is triggered automatically when any of the input controls change.
|
|
|
|
| 10 |
"""
|
| 11 |
if image is None:
|
| 12 |
return None, "Upload an image to see the SVG preview and code.", None
|
| 13 |
|
| 14 |
input_path = image
|
| 15 |
|
|
|
|
| 16 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".svg", mode='w', encoding='utf-8') as temp_output_file:
|
| 17 |
output_path = temp_output_file.name
|
| 18 |
|
| 19 |
try:
|
|
|
|
| 20 |
vtracer.convert_image_to_svg_py(
|
| 21 |
input_path,
|
| 22 |
output_path,
|
|
@@ -33,13 +36,24 @@ def image_to_svg(image, colormode, hierarchical, filter_speckle, color_precision
|
|
| 33 |
max_iterations=10
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
with open(output_path, "r", encoding='utf-8') as f:
|
| 37 |
svg_content = f.read()
|
| 38 |
|
| 39 |
return output_path, svg_content, output_path
|
| 40 |
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return None, error_message, None
|
| 44 |
|
| 45 |
|
|
@@ -71,9 +85,10 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 71 |
hierarchical = gr.Radio(["Stacked", "Cutout"], value="Stacked", label="Hierarchical Mode")
|
| 72 |
filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle (Cleaner)")
|
| 73 |
|
| 74 |
-
# **THE FIX
|
| 75 |
-
# The
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step (Less layers)")
|
| 79 |
|
|
|
|
| 7 |
"""
|
| 8 |
Converts an input image to an SVG string using vtracer.
|
| 9 |
This function is triggered automatically when any of the input controls change.
|
| 10 |
+
It now includes robust error handling to prevent the app from freezing.
|
| 11 |
"""
|
| 12 |
if image is None:
|
| 13 |
return None, "Upload an image to see the SVG preview and code.", None
|
| 14 |
|
| 15 |
input_path = image
|
| 16 |
|
| 17 |
+
# Create a persistent temporary file that Gradio can access.
|
| 18 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".svg", mode='w', encoding='utf-8') as temp_output_file:
|
| 19 |
output_path = temp_output_file.name
|
| 20 |
|
| 21 |
try:
|
| 22 |
+
# vtracer conversion call with all parameters from the UI.
|
| 23 |
vtracer.convert_image_to_svg_py(
|
| 24 |
input_path,
|
| 25 |
output_path,
|
|
|
|
| 36 |
max_iterations=10
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# If successful, read the generated SVG content to display.
|
| 40 |
with open(output_path, "r", encoding='utf-8') as f:
|
| 41 |
svg_content = f.read()
|
| 42 |
|
| 43 |
return output_path, svg_content, output_path
|
| 44 |
|
| 45 |
+
# **THE FIX for Error Handling:**
|
| 46 |
+
# Catch any exception from the vtracer process.
|
| 47 |
except Exception as e:
|
| 48 |
+
# Create a user-friendly error message.
|
| 49 |
+
error_message = (
|
| 50 |
+
f"⚠️ Conversion Failed\n\n"
|
| 51 |
+
f"An error occurred in the backend. This can happen with certain image types or extreme parameter values.\n\n"
|
| 52 |
+
f"Please try adjusting the settings or use a different image.\n\n"
|
| 53 |
+
f"Details: {str(e)}"
|
| 54 |
+
)
|
| 55 |
+
# Return clean outputs and display the error message in the code block.
|
| 56 |
+
# This prevents the app from getting stuck.
|
| 57 |
return None, error_message, None
|
| 58 |
|
| 59 |
|
|
|
|
| 85 |
hierarchical = gr.Radio(["Stacked", "Cutout"], value="Stacked", label="Hierarchical Mode")
|
| 86 |
filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle (Cleaner)")
|
| 87 |
|
| 88 |
+
# **THE FIX for Parameter Range:**
|
| 89 |
+
# The vtracer backend panics if color_precision is 0.
|
| 90 |
+
# The valid range is 1-8. This slider now enforces that constraint.
|
| 91 |
+
color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision (More accurate)")
|
| 92 |
|
| 93 |
layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step (Less layers)")
|
| 94 |
|