incendies Cursor commited on
Commit
183468a
·
1 Parent(s): 80974ab

fix(upscaler): support data URL (base64) input — write to temp file for handle_file

Browse files
Files changed (1) hide show
  1. app.py +29 -1
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
  from daggr import FnNode, GradioNode, Graph
@@ -35,13 +37,39 @@ def _image_to_filepath(image):
35
  return None
36
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # 2. Image Upscaler (FnNode so we pass a single filepath string; GradioNode was receiving a list from Daggr)
39
  def run_upscaler(image, model_selection="4xBHI_dat2_real"):
40
  """Call Phips/Upscaler API with a single filepath string. Returns path to upscaled image."""
41
  path = _image_to_filepath(image)
42
  if not path:
43
  return None
44
- # Phips/Upscaler runs on a different server - send file content via handle_file so it can read the image (local path is not visible there).
 
 
 
45
  try:
46
  image_arg = handle_file(path)
47
  except Exception as e:
 
1
+ import base64
2
+ import tempfile
3
  import gradio as gr
4
  from gradio_client import Client, handle_file
5
  from daggr import FnNode, GradioNode, Graph
 
37
  return None
38
 
39
 
40
+ def _path_for_api(path):
41
+ """Convert path (file path, URL, or data URL) to a path handle_file can use. Data URLs become temp files."""
42
+ if not path or not isinstance(path, str):
43
+ return None
44
+ if path.startswith("data:"):
45
+ # data:image/jpeg;base64,... -> decode and write to temp file (handle_file can't use data URL as filename)
46
+ try:
47
+ header, b64 = path.split(",", 1)
48
+ ext = "png"
49
+ if "jpeg" in header or "jpg" in header:
50
+ ext = "jpg"
51
+ elif "webp" in header:
52
+ ext = "webp"
53
+ data = base64.b64decode(b64)
54
+ f = tempfile.NamedTemporaryFile(suffix=f".{ext}", delete=False)
55
+ f.write(data)
56
+ f.close()
57
+ return f.name
58
+ except Exception as e:
59
+ raise RuntimeError(f"Upscaler: could not decode data URL: {e}") from e
60
+ return path
61
+
62
+
63
  # 2. Image Upscaler (FnNode so we pass a single filepath string; GradioNode was receiving a list from Daggr)
64
  def run_upscaler(image, model_selection="4xBHI_dat2_real"):
65
  """Call Phips/Upscaler API with a single filepath string. Returns path to upscaled image."""
66
  path = _image_to_filepath(image)
67
  if not path:
68
  return None
69
+ path = _path_for_api(path)
70
+ if not path:
71
+ return None
72
+ # Phips/Upscaler runs on a different server - send file content via handle_file so it can read the image.
73
  try:
74
  image_arg = handle_file(path)
75
  except Exception as e: