Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ import os
|
|
| 3 |
import threading
|
| 4 |
import subprocess
|
| 5 |
import time
|
|
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from handwriting_api import InputData, validate_input
|
| 8 |
from hand import Hand
|
| 9 |
-
import cairosvg
|
| 10 |
|
| 11 |
# Create img directory if it doesn't exist
|
| 12 |
os.makedirs("img", exist_ok=True)
|
|
@@ -21,7 +21,8 @@ def generate_handwriting(
|
|
| 21 |
bias=0.75,
|
| 22 |
color="#000000",
|
| 23 |
stroke_width=2,
|
| 24 |
-
multiline=True
|
|
|
|
| 25 |
):
|
| 26 |
"""Generate handwritten text using the model"""
|
| 27 |
try:
|
|
@@ -74,30 +75,78 @@ def generate_handwriting(
|
|
| 74 |
with open("img/output.svg", "r") as f:
|
| 75 |
svg_content = f.read()
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return svg_content
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error: {str(e)}"
|
| 80 |
|
| 81 |
def export_to_png(svg_content):
|
| 82 |
-
"""Convert SVG to transparent PNG using CairoSVG"""
|
| 83 |
try:
|
| 84 |
import cairosvg
|
|
|
|
| 85 |
|
| 86 |
if not svg_content or svg_content.startswith("Error:"):
|
| 87 |
return None
|
| 88 |
-
|
| 89 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with open("img/temp.svg", "w") as f:
|
| 91 |
f.write(svg_content)
|
| 92 |
|
| 93 |
-
# Convert SVG to PNG with transparency
|
| 94 |
cairosvg.svg2png(
|
| 95 |
url="img/temp.svg",
|
| 96 |
-
write_to="img/
|
| 97 |
scale=2.0,
|
| 98 |
background_color="none" # This ensures transparency
|
| 99 |
)
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
return "img/output.png"
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error converting to PNG: {str(e)}")
|
|
@@ -239,12 +288,22 @@ if __name__ == "__main__":
|
|
| 239 |
# Set port based on environment variable or default to 7860
|
| 240 |
port = int(os.environ.get("PORT", 7860))
|
| 241 |
|
| 242 |
-
# Check if
|
|
|
|
| 243 |
try:
|
| 244 |
import cairosvg
|
| 245 |
-
print("CairoSVG is installed and ready for PNG export")
|
| 246 |
except ImportError:
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
demo.launch(server_name="0.0.0.0", server_port=port)
|
|
|
|
| 3 |
import threading
|
| 4 |
import subprocess
|
| 5 |
import time
|
| 6 |
+
import re
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
from handwriting_api import InputData, validate_input
|
| 9 |
from hand import Hand
|
|
|
|
| 10 |
|
| 11 |
# Create img directory if it doesn't exist
|
| 12 |
os.makedirs("img", exist_ok=True)
|
|
|
|
| 21 |
bias=0.75,
|
| 22 |
color="#000000",
|
| 23 |
stroke_width=2,
|
| 24 |
+
multiline=True,
|
| 25 |
+
transparent_background=True
|
| 26 |
):
|
| 27 |
"""Generate handwritten text using the model"""
|
| 28 |
try:
|
|
|
|
| 75 |
with open("img/output.svg", "r") as f:
|
| 76 |
svg_content = f.read()
|
| 77 |
|
| 78 |
+
# If transparent background is requested, modify the SVG
|
| 79 |
+
if transparent_background:
|
| 80 |
+
# Remove the background rectangle or make it transparent
|
| 81 |
+
pattern = r'<rect[^>]*?fill="white"[^>]*?>'
|
| 82 |
+
if re.search(pattern, svg_content):
|
| 83 |
+
svg_content = re.sub(pattern, '', svg_content)
|
| 84 |
+
|
| 85 |
+
# Write the modified SVG back
|
| 86 |
+
with open("img/output.svg", "w") as f:
|
| 87 |
+
f.write(svg_content)
|
| 88 |
+
|
| 89 |
return svg_content
|
| 90 |
except Exception as e:
|
| 91 |
return f"Error: {str(e)}"
|
| 92 |
|
| 93 |
def export_to_png(svg_content):
|
| 94 |
+
"""Convert SVG to transparent PNG using CairoSVG and Pillow for robust transparency"""
|
| 95 |
try:
|
| 96 |
import cairosvg
|
| 97 |
+
from PIL import Image
|
| 98 |
|
| 99 |
if not svg_content or svg_content.startswith("Error:"):
|
| 100 |
return None
|
| 101 |
+
|
| 102 |
+
# Modify the SVG to ensure the background is transparent
|
| 103 |
+
# Remove any white background rectangle
|
| 104 |
+
pattern = r'<rect[^>]*?fill="white"[^>]*?>'
|
| 105 |
+
if re.search(pattern, svg_content):
|
| 106 |
+
svg_content = re.sub(pattern, '', svg_content)
|
| 107 |
+
|
| 108 |
+
# Save the modified SVG to a temporary file
|
| 109 |
with open("img/temp.svg", "w") as f:
|
| 110 |
f.write(svg_content)
|
| 111 |
|
| 112 |
+
# Convert SVG to PNG with transparency using CairoSVG
|
| 113 |
cairosvg.svg2png(
|
| 114 |
url="img/temp.svg",
|
| 115 |
+
write_to="img/output_temp.png",
|
| 116 |
scale=2.0,
|
| 117 |
background_color="none" # This ensures transparency
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Additional processing with Pillow to ensure transparency
|
| 121 |
+
img = Image.open("img/output_temp.png")
|
| 122 |
+
|
| 123 |
+
# Convert to RGBA if not already
|
| 124 |
+
if img.mode != 'RGBA':
|
| 125 |
+
img = img.convert('RGBA')
|
| 126 |
+
|
| 127 |
+
# Create a transparent canvas
|
| 128 |
+
transparent_img = Image.new('RGBA', img.size, (0, 0, 0, 0))
|
| 129 |
+
|
| 130 |
+
# Process the image data to ensure white is transparent
|
| 131 |
+
datas = img.getdata()
|
| 132 |
+
new_data = []
|
| 133 |
+
|
| 134 |
+
for item in datas:
|
| 135 |
+
# If pixel is white or near-white, make it transparent
|
| 136 |
+
if item[0] > 240 and item[1] > 240 and item[2] > 240:
|
| 137 |
+
new_data.append((255, 255, 255, 0)) # Transparent
|
| 138 |
+
else:
|
| 139 |
+
new_data.append(item) # Keep original color
|
| 140 |
+
|
| 141 |
+
transparent_img.putdata(new_data)
|
| 142 |
+
transparent_img.save("img/output.png", "PNG")
|
| 143 |
+
|
| 144 |
+
# Clean up the temporary file
|
| 145 |
+
try:
|
| 146 |
+
os.remove("img/output_temp.png")
|
| 147 |
+
except:
|
| 148 |
+
pass
|
| 149 |
+
|
| 150 |
return "img/output.png"
|
| 151 |
except Exception as e:
|
| 152 |
print(f"Error converting to PNG: {str(e)}")
|
|
|
|
| 288 |
# Set port based on environment variable or default to 7860
|
| 289 |
port = int(os.environ.get("PORT", 7860))
|
| 290 |
|
| 291 |
+
# Check if required packages are installed
|
| 292 |
+
missing_packages = []
|
| 293 |
try:
|
| 294 |
import cairosvg
|
|
|
|
| 295 |
except ImportError:
|
| 296 |
+
missing_packages.append("cairosvg")
|
| 297 |
+
|
| 298 |
+
try:
|
| 299 |
+
from PIL import Image
|
| 300 |
+
except ImportError:
|
| 301 |
+
missing_packages.append("pillow")
|
| 302 |
+
|
| 303 |
+
if missing_packages:
|
| 304 |
+
print(f"WARNING: The following packages are missing and required for transparent PNG export: {', '.join(missing_packages)}")
|
| 305 |
+
print("Please install them using: pip install " + " ".join(missing_packages))
|
| 306 |
+
else:
|
| 307 |
+
print("All required packages are installed and ready for transparent PNG export")
|
| 308 |
|
| 309 |
demo.launch(server_name="0.0.0.0", server_port=port)
|