Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ from gradio_client import Client, handle_file
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# API client for the external Space
|
| 8 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
|
@@ -30,24 +32,62 @@ LORA_STYLES = [
|
|
| 30 |
]
|
| 31 |
MAX_SEED = 2**31 - 1
|
| 32 |
|
| 33 |
-
def encode_image_to_gallery_dict(
|
| 34 |
"""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def infer(
|
| 53 |
image,
|
|
@@ -62,19 +102,20 @@ def infer(
|
|
| 62 |
# Prepare images_input as a list of dicts containing "image" (binary data)
|
| 63 |
images_input = []
|
| 64 |
if image is not None:
|
|
|
|
| 65 |
if isinstance(image, list):
|
| 66 |
for im in image:
|
| 67 |
img_obj = encode_image_to_gallery_dict(im)
|
| 68 |
if img_obj:
|
| 69 |
images_input.append(img_obj)
|
| 70 |
else:
|
| 71 |
-
print(f"警告:
|
| 72 |
else:
|
| 73 |
img_obj = encode_image_to_gallery_dict(image)
|
| 74 |
if img_obj:
|
| 75 |
images_input.append(img_obj)
|
| 76 |
else:
|
| 77 |
-
print(f"警告:
|
| 78 |
|
| 79 |
if len(images_input) == 0:
|
| 80 |
print("未检测到有效图片,未能上传图片。")
|
|
@@ -94,7 +135,6 @@ def infer(
|
|
| 94 |
print(f" steps: {steps}")
|
| 95 |
|
| 96 |
try:
|
| 97 |
-
# Use plain list of dicts with "image" key as images parameter
|
| 98 |
result = space_client.predict(
|
| 99 |
images=images_input,
|
| 100 |
prompt=prompt,
|
|
@@ -106,7 +146,6 @@ def infer(
|
|
| 106 |
api_name="/infer",
|
| 107 |
)
|
| 108 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 109 |
-
# result可能不是元组形式,需增加健壮性
|
| 110 |
if isinstance(result, dict):
|
| 111 |
image_info = result
|
| 112 |
seed_used = result.get("seed", seed)
|
|
@@ -162,7 +201,7 @@ with gr.Blocks() as demo:
|
|
| 162 |
image = gr.Image(
|
| 163 |
label="上传图片",
|
| 164 |
sources=["upload"],
|
| 165 |
-
type="
|
| 166 |
elem_id="input-image"
|
| 167 |
)
|
| 168 |
with gr.Row():
|
|
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
from PIL import Image
|
| 6 |
+
import tempfile
|
| 7 |
+
import shutil
|
| 8 |
|
| 9 |
# API client for the external Space
|
| 10 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
|
|
|
| 32 |
]
|
| 33 |
MAX_SEED = 2**31 - 1
|
| 34 |
|
| 35 |
+
def encode_image_to_gallery_dict(image_data):
|
| 36 |
"""
|
| 37 |
+
Support both file path or np.ndarray/PIL.Image as input.
|
| 38 |
+
Return the dict structure required for GalleryData, containing "image" (binary data) and "orig_name".
|
| 39 |
"""
|
| 40 |
+
# Direct path (legacy): works locally but not on HF
|
| 41 |
+
if isinstance(image_data, str) and os.path.isfile(image_data):
|
| 42 |
+
try:
|
| 43 |
+
with open(image_data, "rb") as f:
|
| 44 |
+
image_bytes = f.read()
|
| 45 |
+
return {"image": image_bytes, "orig_name": os.path.basename(image_data)}
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"无法读取图片: {image_data}: {e}")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
# Gradio on Huggingface likely to pass PIL.Image or ndarray/bytes
|
| 51 |
+
# Try handling PIL.Image.Image
|
| 52 |
+
if hasattr(image_data, "save"):
|
| 53 |
+
try:
|
| 54 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 55 |
+
image_data.save(tmp, format="PNG")
|
| 56 |
+
tmp.flush()
|
| 57 |
+
tmp_name = tmp.name
|
| 58 |
+
with open(tmp_name, "rb") as f:
|
| 59 |
+
image_bytes = f.read()
|
| 60 |
+
result = {"image": image_bytes, "orig_name": os.path.basename(tmp_name)}
|
| 61 |
+
os.remove(tmp_name)
|
| 62 |
+
return result
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"无法处理PIL.Image: {e}")
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
# If numpy image, treat as PIL then to bytes
|
| 68 |
try:
|
| 69 |
+
import numpy as np
|
| 70 |
+
if isinstance(image_data, np.ndarray):
|
| 71 |
+
im_pil = Image.fromarray(image_data)
|
| 72 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 73 |
+
im_pil.save(tmp, format="PNG")
|
| 74 |
+
tmp.flush()
|
| 75 |
+
tmp_name = tmp.name
|
| 76 |
+
with open(tmp_name, "rb") as f:
|
| 77 |
+
image_bytes = f.read()
|
| 78 |
+
result = {"image": image_bytes, "orig_name": os.path.basename(tmp_name)}
|
| 79 |
+
os.remove(tmp_name)
|
| 80 |
+
return result
|
| 81 |
+
except ImportError:
|
| 82 |
+
pass
|
| 83 |
+
|
| 84 |
+
# If already bytes-like, accept (for more robust future-proofing)
|
| 85 |
+
if isinstance(image_data, (bytes, bytearray)):
|
| 86 |
+
return {"image": image_data, "orig_name": "uploaded.png"}
|
| 87 |
+
|
| 88 |
+
# Nothing worked
|
| 89 |
+
print(f"encode_image_to_gallery_dict: 无法处理输入: {type(image_data)}, value={image_data}")
|
| 90 |
+
return None
|
| 91 |
|
| 92 |
def infer(
|
| 93 |
image,
|
|
|
|
| 102 |
# Prepare images_input as a list of dicts containing "image" (binary data)
|
| 103 |
images_input = []
|
| 104 |
if image is not None:
|
| 105 |
+
# gr.Image may return ndarray, PIL.Image, path(str), or list thereof!
|
| 106 |
if isinstance(image, list):
|
| 107 |
for im in image:
|
| 108 |
img_obj = encode_image_to_gallery_dict(im)
|
| 109 |
if img_obj:
|
| 110 |
images_input.append(img_obj)
|
| 111 |
else:
|
| 112 |
+
print(f"警告: 输入图片类型无法处理: {im}")
|
| 113 |
else:
|
| 114 |
img_obj = encode_image_to_gallery_dict(image)
|
| 115 |
if img_obj:
|
| 116 |
images_input.append(img_obj)
|
| 117 |
else:
|
| 118 |
+
print(f"警告: 输入图片类型无法处理: {image}")
|
| 119 |
|
| 120 |
if len(images_input) == 0:
|
| 121 |
print("未检测到有效图片,未能上传图片。")
|
|
|
|
| 135 |
print(f" steps: {steps}")
|
| 136 |
|
| 137 |
try:
|
|
|
|
| 138 |
result = space_client.predict(
|
| 139 |
images=images_input,
|
| 140 |
prompt=prompt,
|
|
|
|
| 146 |
api_name="/infer",
|
| 147 |
)
|
| 148 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
|
|
|
| 149 |
if isinstance(result, dict):
|
| 150 |
image_info = result
|
| 151 |
seed_used = result.get("seed", seed)
|
|
|
|
| 201 |
image = gr.Image(
|
| 202 |
label="上传图片",
|
| 203 |
sources=["upload"],
|
| 204 |
+
type="numpy", # 修改为 numpy,自动适配array/PIL/PATH,兼容HF Spaces
|
| 205 |
elem_id="input-image"
|
| 206 |
)
|
| 207 |
with gr.Row():
|