Spaces:
Paused
Paused
api mode
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import base64
|
| 2 |
import io
|
|
|
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
from typing import Union, Any, Optional
|
|
@@ -45,6 +46,7 @@ os.environ["GRADIO_TEMP_DIR"] = temp_dir
|
|
| 45 |
|
| 46 |
ADAPTER_NAME = "subject"
|
| 47 |
MODEL_PATH = model_path
|
|
|
|
| 48 |
|
| 49 |
def get_gpu_memory_gb() -> float:
|
| 50 |
return torch.cuda.get_device_properties(0).total_memory / 1024**3
|
|
@@ -296,12 +298,12 @@ def generate_background_local(styled_image: Image.Image, prompt: str, steps: int
|
|
| 296 |
return result_img
|
| 297 |
|
| 298 |
def image_to_base64(image: Image.Image) -> str:
|
| 299 |
-
"""Convert PIL Image to base64 string"""
|
| 300 |
-
if image.mode != "
|
| 301 |
-
image = image.convert("
|
| 302 |
|
| 303 |
buffer = io.BytesIO()
|
| 304 |
-
image.save(buffer, format="
|
| 305 |
img_bytes = buffer.getvalue()
|
| 306 |
return base64.b64encode(img_bytes).decode("utf-8")
|
| 307 |
|
|
@@ -311,7 +313,7 @@ def generate_background_api(
|
|
| 311 |
steps: int = 4,
|
| 312 |
api_key: str = "",
|
| 313 |
email: str = "",
|
| 314 |
-
|
| 315 |
) -> Image.Image:
|
| 316 |
"""Generate background using API"""
|
| 317 |
if styled_image is None:
|
|
@@ -321,44 +323,79 @@ def generate_background_api(
|
|
| 321 |
return Image.new("RGB", styled_image.size, (255, 200, 200)) # Red tint to indicate error
|
| 322 |
|
| 323 |
try:
|
| 324 |
-
base64_image = image_to_base64(styled_image)
|
| 325 |
width, height = styled_image.size
|
| 326 |
-
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
payload = {
|
| 330 |
-
"
|
| 331 |
-
"mode": mode,
|
| 332 |
-
"base64_image": base64_image,
|
| 333 |
"prompt": prompt.strip() if prompt else "professional product photography background",
|
| 334 |
-
"
|
| 335 |
-
"
|
| 336 |
-
"
|
| 337 |
-
"
|
| 338 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
}
|
| 340 |
|
| 341 |
headers = {
|
| 342 |
"x-api-key": api_key,
|
| 343 |
"x-email": email,
|
|
|
|
| 344 |
}
|
| 345 |
|
| 346 |
response = requests.post(
|
| 347 |
-
|
| 348 |
headers=headers,
|
| 349 |
json=payload,
|
| 350 |
-
timeout=
|
| 351 |
)
|
| 352 |
|
| 353 |
if response.status_code == 200:
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
return Image.new("RGB", styled_image.size, (255, 200, 200))
|
| 363 |
except Exception as e:
|
| 364 |
print(f"API Error: {e}")
|
|
@@ -377,7 +414,9 @@ def generate_background(
|
|
| 377 |
) -> Image.Image:
|
| 378 |
"""Generate background using either API or local model"""
|
| 379 |
if use_api:
|
| 380 |
-
return generate_background_api(
|
|
|
|
|
|
|
| 381 |
return generate_background_local(styled_image, prompt, steps, width, height)
|
| 382 |
|
| 383 |
@spaces.GPU
|
|
@@ -499,9 +538,10 @@ def create_simple_app():
|
|
| 499 |
placeholder="Enter your registered email"
|
| 500 |
)
|
| 501 |
mode = gr.Radio(
|
| 502 |
-
choices=["
|
| 503 |
-
value="
|
| 504 |
-
label="API
|
|
|
|
| 505 |
)
|
| 506 |
|
| 507 |
# Generation steps
|
|
|
|
| 1 |
import base64
|
| 2 |
import io
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
from typing import Union, Any, Optional
|
|
|
|
| 46 |
|
| 47 |
ADAPTER_NAME = "subject"
|
| 48 |
MODEL_PATH = model_path
|
| 49 |
+
ZEN_BG_ENDPOINT = "https://zen-inpaint-1066271267292.europe-west1.run.app/"
|
| 50 |
|
| 51 |
def get_gpu_memory_gb() -> float:
|
| 52 |
return torch.cuda.get_device_properties(0).total_memory / 1024**3
|
|
|
|
| 298 |
return result_img
|
| 299 |
|
| 300 |
def image_to_base64(image: Image.Image) -> str:
|
| 301 |
+
"""Convert PIL Image to base64 string (PNG to preserve transparency)"""
|
| 302 |
+
if image.mode != "RGBA":
|
| 303 |
+
image = image.convert("RGBA")
|
| 304 |
|
| 305 |
buffer = io.BytesIO()
|
| 306 |
+
image.save(buffer, format="PNG")
|
| 307 |
img_bytes = buffer.getvalue()
|
| 308 |
return base64.b64encode(img_bytes).decode("utf-8")
|
| 309 |
|
|
|
|
| 313 |
steps: int = 4,
|
| 314 |
api_key: str = "",
|
| 315 |
email: str = "",
|
| 316 |
+
zen_mode: str = "bg_generation",
|
| 317 |
) -> Image.Image:
|
| 318 |
"""Generate background using API"""
|
| 319 |
if styled_image is None:
|
|
|
|
| 323 |
return Image.new("RGB", styled_image.size, (255, 200, 200)) # Red tint to indicate error
|
| 324 |
|
| 325 |
try:
|
|
|
|
| 326 |
width, height = styled_image.size
|
| 327 |
+
base64_image = image_to_base64(styled_image)
|
| 328 |
+
# Ensure padding so the API always receives valid Base64 chunks
|
| 329 |
+
subject_base64 = base64_image + "=" * (-len(base64_image) % 4)
|
| 330 |
+
|
| 331 |
+
# Map legacy UI modes to documented gen_mode values
|
| 332 |
+
gen_mode = {
|
| 333 |
+
"subject": "bg_generation",
|
| 334 |
+
"canny": "bg_generation",
|
| 335 |
+
"bg_generation": "bg_generation",
|
| 336 |
+
}.get(zen_mode, "bg_generation")
|
| 337 |
+
|
| 338 |
+
max_dim = max(width, height)
|
| 339 |
+
if max_dim <= 1024:
|
| 340 |
+
upscale = "1k"
|
| 341 |
+
elif max_dim <= 1536:
|
| 342 |
+
upscale = "1.5k"
|
| 343 |
+
else:
|
| 344 |
+
upscale = "2k"
|
| 345 |
|
| 346 |
payload = {
|
| 347 |
+
"gen_mode": gen_mode,
|
|
|
|
|
|
|
| 348 |
"prompt": prompt.strip() if prompt else "professional product photography background",
|
| 349 |
+
"subject": subject_base64,
|
| 350 |
+
"subject_format": "base64",
|
| 351 |
+
"background": "",
|
| 352 |
+
"negative_prompt": "",
|
| 353 |
+
"steps": int(steps),
|
| 354 |
+
"seed": 42,
|
| 355 |
+
"randomize_seed": True,
|
| 356 |
+
"bg_upscale_choice": upscale,
|
| 357 |
+
"max_bg_side_px": int(max_dim),
|
| 358 |
+
"output_image_format": "base64",
|
| 359 |
+
"use_bg_size_for_output": True,
|
| 360 |
}
|
| 361 |
|
| 362 |
headers = {
|
| 363 |
"x-api-key": api_key,
|
| 364 |
"x-email": email,
|
| 365 |
+
"Content-Type": "application/json",
|
| 366 |
}
|
| 367 |
|
| 368 |
response = requests.post(
|
| 369 |
+
ZEN_BG_ENDPOINT,
|
| 370 |
headers=headers,
|
| 371 |
json=payload,
|
| 372 |
+
timeout=90,
|
| 373 |
)
|
| 374 |
|
| 375 |
if response.status_code == 200:
|
| 376 |
+
try:
|
| 377 |
+
result_data = response.json()
|
| 378 |
+
except Exception:
|
| 379 |
+
print(f"[API] Unable to parse response JSON: {response.text[:200]}")
|
| 380 |
+
result_data = {}
|
| 381 |
+
image_field = result_data.get("image")
|
| 382 |
+
if image_field:
|
| 383 |
+
if image_field.startswith("http"):
|
| 384 |
+
try:
|
| 385 |
+
img_resp = requests.get(image_field, timeout=60)
|
| 386 |
+
img_resp.raise_for_status()
|
| 387 |
+
return Image.open(io.BytesIO(img_resp.content))
|
| 388 |
+
except Exception as download_err:
|
| 389 |
+
print(f"[API] Failed to download image URL: {download_err}")
|
| 390 |
+
else:
|
| 391 |
+
try:
|
| 392 |
+
img_data = base64.b64decode(image_field)
|
| 393 |
+
return Image.open(io.BytesIO(img_data))
|
| 394 |
+
except Exception as decode_err:
|
| 395 |
+
print(f"[API] Failed to decode base64 response: {decode_err}")
|
| 396 |
+
print(f"[API] 200 response without image: {result_data}")
|
| 397 |
+
else:
|
| 398 |
+
print(f"[API] Non-200 response ({response.status_code}): {response.text[:500]}")
|
| 399 |
return Image.new("RGB", styled_image.size, (255, 200, 200))
|
| 400 |
except Exception as e:
|
| 401 |
print(f"API Error: {e}")
|
|
|
|
| 414 |
) -> Image.Image:
|
| 415 |
"""Generate background using either API or local model"""
|
| 416 |
if use_api:
|
| 417 |
+
return generate_background_api(
|
| 418 |
+
styled_image, prompt, steps, api_key, email, zen_mode=mode
|
| 419 |
+
)
|
| 420 |
return generate_background_local(styled_image, prompt, steps, width, height)
|
| 421 |
|
| 422 |
@spaces.GPU
|
|
|
|
| 538 |
placeholder="Enter your registered email"
|
| 539 |
)
|
| 540 |
mode = gr.Radio(
|
| 541 |
+
choices=["bg_generation"],
|
| 542 |
+
value="bg_generation",
|
| 543 |
+
label="API gen_mode",
|
| 544 |
+
interactive=False
|
| 545 |
)
|
| 546 |
|
| 547 |
# Generation steps
|