Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -168,80 +168,105 @@ def _extract_mask_array(mask_value: Union[np.ndarray, Dict[str, Any], None]) ->
|
|
168 |
|
169 |
|
170 |
def edit_image(
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
):
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
-
try:
|
197 |
-
client = _client(api_key)
|
198 |
-
common_args = _common_kwargs(prompt, n, size, quality, out_fmt, compression, transparent_bg)
|
199 |
-
api_kwargs = {"image": img_bytes, **common_args}
|
200 |
-
if mask_bytes is not None:
|
201 |
-
api_kwargs["mask"] = mask_bytes
|
202 |
-
resp = client.images.edit(**api_kwargs)
|
203 |
-
imgs = _img_list(resp)
|
204 |
-
if out_fmt in {"jpeg", "webp"}:
|
205 |
-
imgs = [convert_to_format(img, out_fmt, compression) for img in imgs]
|
206 |
-
return imgs
|
207 |
-
except (openai.APIError, openai.OpenAIError) as e:
|
208 |
-
raise gr.Error(_format_openai_error(e))
|
209 |
-
except Exception as e:
|
210 |
-
print(f"Unexpected error during edit: {type(e).__name__}: {e}")
|
211 |
-
raise gr.Error("An unexpected application error occurred. Please check logs.")
|
212 |
|
213 |
|
214 |
# ---------- Variations ---------- #
|
215 |
def variation_image(
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
):
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
|
229 |
-
img_bytes = _bytes_from_numpy(image_numpy)
|
230 |
-
try:
|
231 |
-
client = _client(api_key)
|
232 |
-
var_args: Dict[str, Any] = {"model": MODEL, "n": n}
|
233 |
-
if size != "auto":
|
234 |
-
var_args["size"] = size
|
235 |
-
resp = client.images.create_variation(image=img_bytes, **var_args)
|
236 |
-
imgs = _img_list(resp)
|
237 |
-
if out_fmt in {"jpeg", "webp"}:
|
238 |
-
imgs = [convert_to_format(img, out_fmt, compression) for img in imgs]
|
239 |
-
return imgs
|
240 |
-
except (openai.APIError, openai.OpenAIError) as e:
|
241 |
-
raise gr.Error(_format_openai_error(e))
|
242 |
-
except Exception as e:
|
243 |
-
print(f"Unexpected error during variation: {type(e).__name__}: {e}")
|
244 |
-
raise gr.Error("An unexpected application error occurred. Please check logs.")
|
245 |
|
246 |
|
247 |
# ---------- UI ---------- #
|
@@ -294,3 +319,4 @@ def build_ui():
|
|
294 |
if __name__ == "__main__":
|
295 |
app = build_ui()
|
296 |
app.launch(share=os.getenv("GRADIO_SHARE") == "true", debug=os.getenv("GRADIO_DEBUG") == "true")
|
|
|
|
168 |
|
169 |
|
170 |
def edit_image(
|
171 |
+
api_key: str,
|
172 |
+
image_numpy: Optional[np.ndarray],
|
173 |
+
mask_dict: Optional[Dict[str, Any]],
|
174 |
+
prompt: str,
|
175 |
+
n: int,
|
176 |
+
size: str,
|
177 |
+
quality: str,
|
178 |
+
out_fmt: str,
|
179 |
+
compression: int,
|
180 |
+
transparent_bg: bool,
|
181 |
+
):
|
182 |
+
if image_numpy is None:
|
183 |
+
raise gr.Error("Please upload an image.")
|
184 |
+
if not prompt:
|
185 |
+
raise gr.Error("Please enter an edit prompt.")
|
186 |
+
|
187 |
+
img_bytes = _bytes_from_numpy(image_numpy)
|
188 |
+
mask_bytes: Optional[bytes] = None
|
189 |
+
mask_numpy = _extract_mask_array(mask_dict)
|
190 |
+
|
191 |
+
# (Mask handling code unchanged - Note: the current code doesn't actually
|
192 |
+
# convert mask_numpy to mask_bytes. If you implement this, you'll need
|
193 |
+
# to apply the tuple format to the mask as well.)
|
194 |
+
if mask_numpy is not None:
|
195 |
+
# Assuming you implement mask conversion similar to image:
|
196 |
+
# mask_bytes = _bytes_from_numpy(mask_numpy) # Example implementation needed here
|
197 |
+
pass # Placeholder - current code doesn't set mask_bytes
|
198 |
|
199 |
+
try:
|
200 |
+
client = _client(api_key)
|
201 |
+
common_args = _common_kwargs(prompt, n, size, quality, out_fmt, compression, transparent_bg)
|
202 |
|
203 |
+
# --- FIX: Provide image data as a tuple ---
|
204 |
+
image_tuple = ("image.png", img_bytes, "image/png")
|
205 |
+
api_kwargs = {"image": image_tuple, **common_args}
|
206 |
+
# ------------------------------------------
|
207 |
+
|
208 |
+
if mask_bytes is not None:
|
209 |
+
# --- FIX: Provide mask data as a tuple if used ---
|
210 |
+
mask_tuple = ("mask.png", mask_bytes, "image/png")
|
211 |
+
api_kwargs["mask"] = mask_tuple
|
212 |
+
# -------------------------------------------------
|
213 |
+
|
214 |
+
resp = client.images.edit(**api_kwargs) # This line caused the error
|
215 |
+
imgs = _img_list(resp)
|
216 |
+
if out_fmt in {"jpeg", "webp"}:
|
217 |
+
imgs = [convert_to_format(img, out_fmt, compression) for img in imgs]
|
218 |
+
return imgs
|
219 |
+
except (openai.APIError, openai.OpenAIError) as e:
|
220 |
+
raise gr.Error(_format_openai_error(e))
|
221 |
+
except Exception as e:
|
222 |
+
print(f"Unexpected error during edit: {type(e).__name__}: {e}")
|
223 |
+
raise gr.Error("An unexpected application error occurred. Please check logs.")
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
|
227 |
# ---------- Variations ---------- #
|
228 |
def variation_image(
|
229 |
+
api_key: str,
|
230 |
+
image_numpy: Optional[np.ndarray],
|
231 |
+
n: int,
|
232 |
+
size: str,
|
233 |
+
quality: str,
|
234 |
+
out_fmt: str,
|
235 |
+
compression: int,
|
236 |
+
transparent_bg: bool, # Note: transparent_bg is passed but not used by variations API
|
237 |
+
):
|
238 |
+
gr.Warning("Note: Image Variations are officially supported for DALL路E 2/3, not gpt-image-1. This may fail.")
|
239 |
+
if image_numpy is None:
|
240 |
+
raise gr.Error("Please upload an image.")
|
241 |
+
|
242 |
+
img_bytes = _bytes_from_numpy(image_numpy)
|
243 |
+
try:
|
244 |
+
client = _client(api_key)
|
245 |
+
var_args: Dict[str, Any] = {"model": MODEL, "n": n}
|
246 |
+
if size != "auto":
|
247 |
+
var_args["size"] = size
|
248 |
+
|
249 |
+
# --- FIX: Provide image data as a tuple ---
|
250 |
+
image_tuple = ("image.png", img_bytes, "image/png")
|
251 |
+
# ------------------------------------------
|
252 |
+
|
253 |
+
# Pass the tuple to the image parameter
|
254 |
+
resp = client.images.create_variation(image=image_tuple, **var_args) # This line would have the same error
|
255 |
+
|
256 |
+
imgs = _img_list(resp)
|
257 |
+
if out_fmt in {"jpeg", "webp"}:
|
258 |
+
imgs = [convert_to_format(img, out_fmt, compression) for img in imgs]
|
259 |
+
return imgs
|
260 |
+
except (openai.APIError, openai.OpenAIError) as e:
|
261 |
+
# Add specific check for variation incompatibility
|
262 |
+
err_msg = _format_openai_error(e)
|
263 |
+
if isinstance(e, openai.BadRequestError) and "model does not support variations" in err_msg.lower():
|
264 |
+
raise gr.Error("As warned, the selected model (gpt-image-1) does not support the variations endpoint.")
|
265 |
+
raise gr.Error(err_msg)
|
266 |
+
except Exception as e:
|
267 |
+
print(f"Unexpected error during variation: {type(e).__name__}: {e}")
|
268 |
+
raise gr.Error("An unexpected application error occurred. Please check logs.")
|
269 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
|
272 |
# ---------- UI ---------- #
|
|
|
319 |
if __name__ == "__main__":
|
320 |
app = build_ui()
|
321 |
app.launch(share=os.getenv("GRADIO_SHARE") == "true", debug=os.getenv("GRADIO_DEBUG") == "true")
|
322 |
+
|