Update app.py
Browse files
app.py
CHANGED
|
@@ -33,8 +33,8 @@ def encode_image_to_gallery_dict(image_path):
|
|
| 33 |
if not image_path or not isinstance(image_path, str):
|
| 34 |
return None
|
| 35 |
try:
|
| 36 |
-
# Gallery with type="filepath" expects
|
| 37 |
-
return (
|
| 38 |
except Exception as e:
|
| 39 |
print(f"无法读取图片: {image_path}: {e}")
|
| 40 |
return None
|
|
@@ -49,10 +49,10 @@ def infer(
|
|
| 49 |
steps,
|
| 50 |
progress=gr.Progress(track_tqdm=True),
|
| 51 |
):
|
| 52 |
-
#
|
| 53 |
images_input = []
|
| 54 |
if image is not None:
|
| 55 |
-
if isinstance(image, list):
|
| 56 |
for im in image:
|
| 57 |
img_obj = encode_image_to_gallery_dict(im)
|
| 58 |
if img_obj:
|
|
@@ -84,7 +84,7 @@ def infer(
|
|
| 84 |
print(f" steps: {steps}")
|
| 85 |
|
| 86 |
try:
|
| 87 |
-
#
|
| 88 |
result = space_client.predict(
|
| 89 |
images=images_input,
|
| 90 |
prompt=prompt,
|
|
@@ -98,7 +98,6 @@ def infer(
|
|
| 98 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 99 |
# result可能不是元组形式,需增加健壮性
|
| 100 |
if isinstance(result, dict):
|
| 101 |
-
# 新API只返回dict
|
| 102 |
image_info = result
|
| 103 |
seed_used = result.get("seed", seed)
|
| 104 |
elif isinstance(result, (tuple, list)) and len(result) == 2:
|
|
@@ -107,12 +106,9 @@ def infer(
|
|
| 107 |
print(f"[错误] space_client.predict 返回类型未知: {type(result)},内容: {result}")
|
| 108 |
return None, seed
|
| 109 |
|
| 110 |
-
# 检查image_info是否包含url/path
|
| 111 |
if isinstance(image_info, dict):
|
| 112 |
img_url = image_info.get("url") or image_info.get("path") or None
|
| 113 |
-
# 获取base64图片(如果有)
|
| 114 |
if img_url is None and "data" in image_info:
|
| 115 |
-
# 返回base64图片(data格式)
|
| 116 |
return f"data:image/png;base64,{image_info['data']}", seed_used
|
| 117 |
return img_url, seed_used
|
| 118 |
else:
|
|
@@ -127,6 +123,11 @@ def infer(
|
|
| 127 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 128 |
"可以尝试重新选择图片或修改图片格式。"
|
| 129 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
else:
|
| 131 |
print("\n[错误] 调用推理服务失败,请稍后重试或联系开发者。")
|
| 132 |
return None, seed
|
|
|
|
| 33 |
if not image_path or not isinstance(image_path, str):
|
| 34 |
return None
|
| 35 |
try:
|
| 36 |
+
# Gallery with type="filepath" expects a dictionary with file path
|
| 37 |
+
return {"path": image_path, "orig_name": os.path.basename(image_path), "meta": {"_type": "gradio.FileData"}}
|
| 38 |
except Exception as e:
|
| 39 |
print(f"无法读取图片: {image_path}: {e}")
|
| 40 |
return None
|
|
|
|
| 49 |
steps,
|
| 50 |
progress=gr.Progress(track_tqdm=True),
|
| 51 |
):
|
| 52 |
+
# Prepare images_input as a list of dicts of file data (not as tuples)
|
| 53 |
images_input = []
|
| 54 |
if image is not None:
|
| 55 |
+
if isinstance(image, list):
|
| 56 |
for im in image:
|
| 57 |
img_obj = encode_image_to_gallery_dict(im)
|
| 58 |
if img_obj:
|
|
|
|
| 84 |
print(f" steps: {steps}")
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
# Use plain list of dicts as images parameter
|
| 88 |
result = space_client.predict(
|
| 89 |
images=images_input,
|
| 90 |
prompt=prompt,
|
|
|
|
| 98 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 99 |
# result可能不是元组形式,需增加健壮性
|
| 100 |
if isinstance(result, dict):
|
|
|
|
| 101 |
image_info = result
|
| 102 |
seed_used = result.get("seed", seed)
|
| 103 |
elif isinstance(result, (tuple, list)) and len(result) == 2:
|
|
|
|
| 106 |
print(f"[错误] space_client.predict 返回类型未知: {type(result)},内容: {result}")
|
| 107 |
return None, seed
|
| 108 |
|
|
|
|
| 109 |
if isinstance(image_info, dict):
|
| 110 |
img_url = image_info.get("url") or image_info.get("path") or None
|
|
|
|
| 111 |
if img_url is None and "data" in image_info:
|
|
|
|
| 112 |
return f"data:image/png;base64,{image_info['data']}", seed_used
|
| 113 |
return img_url, seed_used
|
| 114 |
else:
|
|
|
|
| 123 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 124 |
"可以尝试重新选择图片或修改图片格式。"
|
| 125 |
)
|
| 126 |
+
elif "validation errors for GalleryData" in str(e):
|
| 127 |
+
print(
|
| 128 |
+
"\n[错误] 图片数据上传格式错误。请检查图片输入的数据结构,"
|
| 129 |
+
"确保为文件路径字符串或有效图片文件。"
|
| 130 |
+
)
|
| 131 |
else:
|
| 132 |
print("\n[错误] 调用推理服务失败,请稍后重试或联系开发者。")
|
| 133 |
return None, seed
|