risunobushi commited on
Commit
076e877
·
1 Parent(s): cd22e75

Support remote image URLs in API by fetching images server-side

Browse files

- Add fetch_image_if_url helper to download images if input is a URL string
- Use this helper for base_img, garment_img, and mask_img in generate function
- Allows both file uploads and remote URLs for all image fields in API calls
- No change to UI behavior

Files changed (1) hide show
  1. app.py +18 -0
app.py CHANGED
@@ -331,6 +331,19 @@ def is_nsfw_content(img: Image.Image) -> bool:
331
  # Main generate function
332
  # -----------------------------------------------------------------------------
333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  def generate(
335
  base_img: Image.Image,
336
  garment_img: Image.Image,
@@ -338,6 +351,11 @@ def generate(
338
  mask_img: Optional[Image.Image], # NEW: Optional mask parameter
339
  request: gr.Request
340
  ) -> Image.Image:
 
 
 
 
 
341
  if base_img is None or garment_img is None:
342
  raise gr.Error("Please provide both images.")
343
 
 
331
  # Main generate function
332
  # -----------------------------------------------------------------------------
333
 
334
+ def fetch_image_if_url(img):
335
+ """
336
+ If img is a string and looks like a URL, download and return as PIL.Image.
337
+ Otherwise, return as-is (assume already PIL.Image).
338
+ """
339
+ if isinstance(img, str) and (img.startswith("http://") or img.startswith("https://")):
340
+ print(f"[FETCH] Downloading image from URL: {img}")
341
+ resp = requests.get(img)
342
+ resp.raise_for_status()
343
+ from PIL import Image
344
+ return Image.open(io.BytesIO(resp.content)).convert("RGB")
345
+ return img
346
+
347
  def generate(
348
  base_img: Image.Image,
349
  garment_img: Image.Image,
 
351
  mask_img: Optional[Image.Image], # NEW: Optional mask parameter
352
  request: gr.Request
353
  ) -> Image.Image:
354
+ base_img = fetch_image_if_url(base_img)
355
+ garment_img = fetch_image_if_url(garment_img)
356
+ if mask_img is not None:
357
+ mask_img = fetch_image_if_url(mask_img)
358
+
359
  if base_img is None or garment_img is None:
360
  raise gr.Error("Please provide both images.")
361