Oysiyl commited on
Commit
b03ddfc
·
1 Parent(s): 2be8221

Fix export temp files to use Gradio cache for privacy consistency

Browse files

Write 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.

Files changed (1) hide show
  1. app.py +18 -15
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
- import tempfile
 
 
 
 
 
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
- """Write PNG to a named temp file outside Gradio's cache. Returns path.
40
 
41
- Uses /tmp (OS-managed) rather than Gradio's /tmp/gradio/ cache so the
42
- file is not subject to the delete_cache=(3600, 3600) sweep on the Space.
43
  """
44
- tmp = tempfile.NamedTemporaryFile(suffix=".png", prefix=f"{stem}-", delete=False)
45
- img.convert("RGB").save(tmp.name, "PNG")
46
- tmp.close()
47
- return tmp.name
48
 
49
 
50
  def _write_svg(img: Image.Image, stem: str) -> str:
51
- """Write a PNG-embedded SVG to a named temp file. Returns path.
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. Written outside Gradio's cache for the same reason as
56
- _write_png — not subject to the 1-hour cache sweep.
 
 
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
- tmp = tempfile.NamedTemporaryFile(suffix=".svg", prefix=f"{stem}-", delete=False)
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):