Update app.py
Browse files
app.py
CHANGED
|
@@ -103,19 +103,39 @@ def infer(
|
|
| 103 |
api_name="/infer",
|
| 104 |
)
|
| 105 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
import traceback
|
| 112 |
traceback.print_exc()
|
| 113 |
print(f"[调用API] 调用接口异常: {e}")
|
| 114 |
-
if
|
| 115 |
print(
|
| 116 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 117 |
"可以尝试重新选择图片或修改图片格式。"
|
| 118 |
)
|
|
|
|
|
|
|
| 119 |
return None, seed
|
| 120 |
|
| 121 |
examples = [
|
|
|
|
| 103 |
api_name="/infer",
|
| 104 |
)
|
| 105 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 106 |
+
# result可能不是元组形式,需增加健壮性
|
| 107 |
+
if isinstance(result, dict):
|
| 108 |
+
# 新API只返回dict
|
| 109 |
+
image_info = result
|
| 110 |
+
seed_used = result.get("seed", seed)
|
| 111 |
+
elif isinstance(result, (tuple, list)) and len(result) == 2:
|
| 112 |
+
image_info, seed_used = result
|
| 113 |
+
else:
|
| 114 |
+
print(f"[错误] space_client.predict 返回类型未知: {type(result)},内容: {result}")
|
| 115 |
+
return None, seed
|
| 116 |
+
|
| 117 |
+
# 检查image_info是否包含url/path
|
| 118 |
+
if isinstance(image_info, dict):
|
| 119 |
+
img_url = image_info.get("url") or image_info.get("path") or None
|
| 120 |
+
# 获取base64图片(如果有)
|
| 121 |
+
if img_url is None and "data" in image_info:
|
| 122 |
+
# 返回base64图片(data格式)
|
| 123 |
+
return f"data:image/png;base64,{image_info['data']}", seed_used
|
| 124 |
+
return img_url, seed_used
|
| 125 |
+
else:
|
| 126 |
+
print(f"[错误] image_info 格式异常: {image_info}")
|
| 127 |
+
return None, seed
|
| 128 |
except Exception as e:
|
| 129 |
import traceback
|
| 130 |
traceback.print_exc()
|
| 131 |
print(f"[调用API] 调用接口异常: {e}")
|
| 132 |
+
if "Could not process uploaded images" in str(e):
|
| 133 |
print(
|
| 134 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 135 |
"可以尝试重新选择图片或修改图片格式。"
|
| 136 |
)
|
| 137 |
+
else:
|
| 138 |
+
print("\n[错误] 调用推理服务失败,请稍后重试或联系开发者。")
|
| 139 |
return None, seed
|
| 140 |
|
| 141 |
examples = [
|