incendies commited on
Commit
cda4df7
·
1 Parent(s): 0b1be36

Fix No image: postprocess bg-removal, URL->path for Daggr display

Browse files
Files changed (1) hide show
  1. app.py +49 -16
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import base64
2
  import tempfile
 
3
  import gradio as gr
4
  import spaces
5
  from gradio_client import Client, handle_file
@@ -8,13 +9,35 @@ from daggr import FnNode, GradioNode, Graph
8
  # ==================== FREE TOOLS COLLECTION ====================
9
  # These tools are free to use and don't consume credits
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # 1. Background Removal
12
- # API returns 1 value. hf-applications/background-removal runs on GPU (Zero)
13
  bg_remover = GradioNode(
14
  "hf-applications/background-removal",
15
  api_name="/image",
16
  run_locally=False,
17
  inputs={"image": gr.Image()},
 
18
  outputs={
19
  "final_image": gr.Image(label="Background Removed"),
20
  },
@@ -82,16 +105,18 @@ def run_upscaler(image, model_selection="4xBHI_dat2_real"):
82
  except Exception as e:
83
  raise RuntimeError(f"Upscaler API error: {e}") from e
84
  # Returns tuple of 2: [comparison, filepath]; we need the upscaled image path (second element)
 
85
  if result and len(result) >= 2 and result[1]:
86
- return result[1]
87
- if result and len(result) >= 1 and result[0]:
88
- # result[0] can be tuple (orig, upscaled) - take second
89
  r0 = result[0]
90
  if isinstance(r0, (list, tuple)) and len(r0) >= 2 and r0[1]:
91
- return r0[1]
92
- if isinstance(r0, str):
93
- return r0
94
- return None
 
 
95
 
96
 
97
  upscaler = FnNode(
@@ -125,13 +150,17 @@ def run_z_image_turbo(prompt, height=1024, width=1024, seed=42):
125
  )
126
  except Exception as e:
127
  raise RuntimeError(f"Z-Image-Turbo API error: {e}") from e
128
- # Returns tuple of 2: [0] dict(path=..., url=..., ...), [1] seed used
129
  if result and len(result) >= 1 and result[0]:
130
  img = result[0]
 
131
  if isinstance(img, dict):
132
- return img.get("url") or img.get("path")
133
- if isinstance(img, str):
134
- return img
 
 
 
135
  return None
136
 
137
 
@@ -177,13 +206,17 @@ def run_flux_klein(
177
  )
178
  except Exception as e:
179
  raise RuntimeError(f"FLUX.2-klein-9B API error: {e}") from e
180
- # Returns tuple of 2: [0] dict(path=..., url=...), [1] seed used
181
  if result and len(result) >= 1 and result[0]:
182
  img = result[0]
 
183
  if isinstance(img, dict):
184
- return img.get("url") or img.get("path")
185
- if isinstance(img, str):
186
- return img
 
 
 
187
  return None
188
 
189
 
 
1
  import base64
2
  import tempfile
3
+ import urllib.request
4
  import gradio as gr
5
  import spaces
6
  from gradio_client import Client, handle_file
 
9
  # ==================== FREE TOOLS COLLECTION ====================
10
  # These tools are free to use and don't consume credits
11
 
12
+
13
+ def _url_to_path(url):
14
+ """Download image from URL to a temp file; Daggr needs file paths to display images."""
15
+ if not url or not isinstance(url, str) or not url.startswith("http"):
16
+ return url
17
+ try:
18
+ ext = "png"
19
+ if ".jpg" in url or ".jpeg" in url:
20
+ ext = "jpg"
21
+ elif ".webp" in url:
22
+ ext = "webp"
23
+ f = tempfile.NamedTemporaryFile(suffix=f".{ext}", delete=False)
24
+ req = urllib.request.Request(url, headers={"User-Agent": "Gradio-Daggr/1.0"})
25
+ with urllib.request.urlopen(req, timeout=60) as r:
26
+ f.write(r.read())
27
+ f.close()
28
+ return f.name
29
+ except Exception:
30
+ return url
31
+
32
+
33
  # 1. Background Removal
34
+ # API may return (original, result); we only need the result for "final_image".
35
  bg_remover = GradioNode(
36
  "hf-applications/background-removal",
37
  api_name="/image",
38
  run_locally=False,
39
  inputs={"image": gr.Image()},
40
+ postprocess=lambda *vals: vals[-1] if vals else None,
41
  outputs={
42
  "final_image": gr.Image(label="Background Removed"),
43
  },
 
105
  except Exception as e:
106
  raise RuntimeError(f"Upscaler API error: {e}") from e
107
  # Returns tuple of 2: [comparison, filepath]; we need the upscaled image path (second element)
108
+ out = None
109
  if result and len(result) >= 2 and result[1]:
110
+ out = result[1]
111
+ elif result and len(result) >= 1 and result[0]:
 
112
  r0 = result[0]
113
  if isinstance(r0, (list, tuple)) and len(r0) >= 2 and r0[1]:
114
+ out = r0[1]
115
+ elif isinstance(r0, str):
116
+ out = r0
117
+ if out and isinstance(out, str) and out.startswith("http"):
118
+ return _url_to_path(out)
119
+ return out
120
 
121
 
122
  upscaler = FnNode(
 
150
  )
151
  except Exception as e:
152
  raise RuntimeError(f"Z-Image-Turbo API error: {e}") from e
153
+ # Returns tuple of 2: [0] dict(path=..., url=..., ...), [1] seed used. Daggr needs local path.
154
  if result and len(result) >= 1 and result[0]:
155
  img = result[0]
156
+ out = None
157
  if isinstance(img, dict):
158
+ out = img.get("url") or img.get("path")
159
+ elif isinstance(img, str):
160
+ out = img
161
+ if out and isinstance(out, str) and out.startswith("http"):
162
+ return _url_to_path(out)
163
+ return out
164
  return None
165
 
166
 
 
206
  )
207
  except Exception as e:
208
  raise RuntimeError(f"FLUX.2-klein-9B API error: {e}") from e
209
+ # Returns tuple of 2: [0] dict(path=..., url=...), [1] seed used. Daggr needs local path.
210
  if result and len(result) >= 1 and result[0]:
211
  img = result[0]
212
+ out = None
213
  if isinstance(img, dict):
214
+ out = img.get("url") or img.get("path")
215
+ elif isinstance(img, str):
216
+ out = img
217
+ if out and isinstance(out, str) and out.startswith("http"):
218
+ return _url_to_path(out)
219
+ return out
220
  return None
221
 
222