Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import time
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from handwriting_api import InputData, validate_input
|
| 8 |
from hand import Hand
|
|
|
|
| 9 |
|
| 10 |
# Create img directory if it doesn't exist
|
| 11 |
os.makedirs("img", exist_ok=True)
|
|
@@ -78,7 +79,7 @@ def generate_handwriting(
|
|
| 78 |
return f"Error: {str(e)}"
|
| 79 |
|
| 80 |
def export_to_png(svg_content):
|
| 81 |
-
"""Convert SVG to PNG using CairoSVG"""
|
| 82 |
try:
|
| 83 |
import cairosvg
|
| 84 |
|
|
@@ -89,11 +90,17 @@ def export_to_png(svg_content):
|
|
| 89 |
with open("img/temp.svg", "w") as f:
|
| 90 |
f.write(svg_content)
|
| 91 |
|
| 92 |
-
# Convert SVG to PNG
|
| 93 |
-
cairosvg.svg2png(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
return "img/output.png"
|
| 96 |
except Exception as e:
|
|
|
|
| 97 |
return None
|
| 98 |
|
| 99 |
def generate_lyrics_sample():
|
|
@@ -110,7 +117,8 @@ def generate_handwriting_wrapper(
|
|
| 110 |
multiline=True
|
| 111 |
):
|
| 112 |
svg = generate_handwriting(text, style, bias, color, stroke_width, multiline)
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
css = """
|
| 116 |
.container {max-width: 900px; margin: auto;}
|
|
@@ -173,10 +181,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 173 |
sample_btn = gr.Button("Insert Sample Text")
|
| 174 |
|
| 175 |
with gr.Column(scale=3):
|
| 176 |
-
output_svg = gr.HTML(label="Generated Handwriting", elem_classes=["output-container"])
|
|
|
|
| 177 |
|
| 178 |
with gr.Row():
|
| 179 |
-
|
|
|
|
| 180 |
|
| 181 |
gr.Markdown("""
|
| 182 |
### Tips:
|
|
@@ -185,6 +195,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 185 |
- Each line should be 75 characters or less
|
| 186 |
- The model works best for English text
|
| 187 |
- Forward slashes (/) and backslashes (\\) will be replaced with dashes (-)
|
|
|
|
| 188 |
""")
|
| 189 |
|
| 190 |
gr.Markdown("""
|
|
@@ -197,7 +208,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 197 |
generate_btn.click(
|
| 198 |
fn=generate_handwriting_wrapper,
|
| 199 |
inputs=[text_input, style_select, bias_slider, color_picker, stroke_width],
|
| 200 |
-
outputs=[output_svg]
|
| 201 |
)
|
| 202 |
|
| 203 |
clear_btn.click(
|
|
@@ -212,13 +223,28 @@ with gr.Blocks(css=css) as demo:
|
|
| 212 |
outputs=[text_input]
|
| 213 |
)
|
| 214 |
|
| 215 |
-
|
| 216 |
fn=lambda x: x,
|
| 217 |
inputs=[output_svg],
|
| 218 |
outputs=[gr.File(label="Download SVG", file_count="single", file_types=[".svg"])]
|
| 219 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
| 222 |
# Set port based on environment variable or default to 7860
|
| 223 |
port = int(os.environ.get("PORT", 7860))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
demo.launch(server_name="0.0.0.0", server_port=port)
|
|
|
|
| 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)
|
|
|
|
| 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 |
|
|
|
|
| 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/output.png",
|
| 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)}")
|
| 104 |
return None
|
| 105 |
|
| 106 |
def generate_lyrics_sample():
|
|
|
|
| 117 |
multiline=True
|
| 118 |
):
|
| 119 |
svg = generate_handwriting(text, style, bias, color, stroke_width, multiline)
|
| 120 |
+
png_path = export_to_png(svg)
|
| 121 |
+
return svg, png_path
|
| 122 |
|
| 123 |
css = """
|
| 124 |
.container {max-width: 900px; margin: auto;}
|
|
|
|
| 181 |
sample_btn = gr.Button("Insert Sample Text")
|
| 182 |
|
| 183 |
with gr.Column(scale=3):
|
| 184 |
+
output_svg = gr.HTML(label="Generated Handwriting (SVG)", elem_classes=["output-container"])
|
| 185 |
+
output_png = gr.Image(type="filepath", label="Generated Handwriting (PNG)", elem_classes=["output-container"])
|
| 186 |
|
| 187 |
with gr.Row():
|
| 188 |
+
download_svg_btn = gr.Button("Download SVG")
|
| 189 |
+
download_png_btn = gr.Button("Download PNG")
|
| 190 |
|
| 191 |
gr.Markdown("""
|
| 192 |
### Tips:
|
|
|
|
| 195 |
- Each line should be 75 characters or less
|
| 196 |
- The model works best for English text
|
| 197 |
- Forward slashes (/) and backslashes (\\) will be replaced with dashes (-)
|
| 198 |
+
- PNG output has transparency for easy integration into other documents
|
| 199 |
""")
|
| 200 |
|
| 201 |
gr.Markdown("""
|
|
|
|
| 208 |
generate_btn.click(
|
| 209 |
fn=generate_handwriting_wrapper,
|
| 210 |
inputs=[text_input, style_select, bias_slider, color_picker, stroke_width],
|
| 211 |
+
outputs=[output_svg, output_png]
|
| 212 |
)
|
| 213 |
|
| 214 |
clear_btn.click(
|
|
|
|
| 223 |
outputs=[text_input]
|
| 224 |
)
|
| 225 |
|
| 226 |
+
download_svg_btn.click(
|
| 227 |
fn=lambda x: x,
|
| 228 |
inputs=[output_svg],
|
| 229 |
outputs=[gr.File(label="Download SVG", file_count="single", file_types=[".svg"])]
|
| 230 |
)
|
| 231 |
+
|
| 232 |
+
download_png_btn.click(
|
| 233 |
+
fn=lambda x: x,
|
| 234 |
+
inputs=[output_png],
|
| 235 |
+
outputs=[gr.File(label="Download PNG", file_count="single", file_types=[".png"])]
|
| 236 |
+
)
|
| 237 |
|
| 238 |
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 CairoSVG is installed
|
| 243 |
+
try:
|
| 244 |
+
import cairosvg
|
| 245 |
+
print("CairoSVG is installed and ready for PNG export")
|
| 246 |
+
except ImportError:
|
| 247 |
+
print("WARNING: CairoSVG is not installed. PNG export will not work.")
|
| 248 |
+
print("Please install it using: pip install cairosvg")
|
| 249 |
+
|
| 250 |
demo.launch(server_name="0.0.0.0", server_port=port)
|