Spaces:
Runtime error
Runtime error
Fix export temp files to use Gradio cache for privacy consistency
Browse filesWrite PNG and SVG export files into Gradio's /tmp/gradio/ cache via
save_pil_to_cache / save_bytes_to_cache instead of bare /tmp, so
delete_cache=(3600,3600) sweeps them after 1 hour — consistent with
the generated image auto-delete policy stated in the README.
app.py
CHANGED
|
@@ -26,7 +26,12 @@ import kornia.color # For RGB→HSV conversion in Stable Cascade filter
|
|
| 26 |
import base64
|
| 27 |
import io
|
| 28 |
import re
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def _make_qr_stem(text_input: str, seed: int) -> str:
|
|
@@ -36,24 +41,25 @@ def _make_qr_stem(text_input: str, seed: int) -> str:
|
|
| 36 |
|
| 37 |
|
| 38 |
def _write_png(img: Image.Image, stem: str) -> str:
|
| 39 |
-
"""
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return tmp.name
|
| 48 |
|
| 49 |
|
| 50 |
def _write_svg(img: Image.Image, stem: str) -> str:
|
| 51 |
-
"""
|
| 52 |
|
| 53 |
The SVG wraps the raster image as a base64-encoded PNG inside a valid SVG
|
| 54 |
container. Confirmed scannable and opens correctly in Figma, Illustrator,
|
| 55 |
-
Safari, and Chrome.
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
buf = io.BytesIO()
|
| 59 |
img.convert("RGB").save(buf, "PNG")
|
|
@@ -69,10 +75,7 @@ def _write_svg(img: Image.Image, stem: str) -> str:
|
|
| 69 |
f'x="0" y="0" width="{w}" height="{h}" '
|
| 70 |
f'preserveAspectRatio="xMidYMid meet"/></svg>'
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
tmp.write(svg.encode("utf-8"))
|
| 74 |
-
tmp.close()
|
| 75 |
-
return tmp.name
|
| 76 |
|
| 77 |
|
| 78 |
def _download_png(image, text_input, seed):
|
|
|
|
| 26 |
import base64
|
| 27 |
import io
|
| 28 |
import re
|
| 29 |
+
|
| 30 |
+
from gradio.processing_utils import (
|
| 31 |
+
get_upload_folder,
|
| 32 |
+
save_bytes_to_cache,
|
| 33 |
+
save_pil_to_cache,
|
| 34 |
+
)
|
| 35 |
|
| 36 |
|
| 37 |
def _make_qr_stem(text_input: str, seed: int) -> str:
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def _write_png(img: Image.Image, stem: str) -> str:
|
| 44 |
+
"""Save PNG into Gradio's cache dir. Returns path.
|
| 45 |
|
| 46 |
+
Written into /tmp/gradio/ so delete_cache=(3600, 3600) sweeps it after
|
| 47 |
+
1 hour — consistent with the generated image privacy policy in the README.
|
| 48 |
"""
|
| 49 |
+
return save_pil_to_cache(
|
| 50 |
+
img.convert("RGB"), get_upload_folder(), name=stem, format="png"
|
| 51 |
+
)
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def _write_svg(img: Image.Image, stem: str) -> str:
|
| 55 |
+
"""Save a PNG-embedded SVG into Gradio's cache dir. Returns path.
|
| 56 |
|
| 57 |
The SVG wraps the raster image as a base64-encoded PNG inside a valid SVG
|
| 58 |
container. Confirmed scannable and opens correctly in Figma, Illustrator,
|
| 59 |
+
Safari, and Chrome.
|
| 60 |
+
|
| 61 |
+
Written into /tmp/gradio/ so delete_cache=(3600, 3600) sweeps it after
|
| 62 |
+
1 hour — consistent with the generated image privacy policy in the README.
|
| 63 |
"""
|
| 64 |
buf = io.BytesIO()
|
| 65 |
img.convert("RGB").save(buf, "PNG")
|
|
|
|
| 75 |
f'x="0" y="0" width="{w}" height="{h}" '
|
| 76 |
f'preserveAspectRatio="xMidYMid meet"/></svg>'
|
| 77 |
)
|
| 78 |
+
return save_bytes_to_cache(svg.encode("utf-8"), f"{stem}.svg", get_upload_folder())
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
def _download_png(image, text_input, seed):
|