Update app.py
Browse files
app.py
CHANGED
|
@@ -25,11 +25,22 @@ def upload_file(image_path):
|
|
| 25 |
headers = {'Authorization': f'Bearer {HF_TOKEN}'}
|
| 26 |
resp = requests.post(UPLOAD_URL, files=files, headers=headers)
|
| 27 |
resp.raise_for_status()
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
if isinstance(res, list) and len(res) > 0:
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def call_infer(payload):
|
| 35 |
headers = {'Authorization': f'Bearer {HF_TOKEN}'}
|
|
@@ -79,7 +90,10 @@ def infer(
|
|
| 79 |
# 1. 上传文件
|
| 80 |
try:
|
| 81 |
uploaded_info = upload_file(image)
|
| 82 |
-
#
|
|
|
|
|
|
|
|
|
|
| 83 |
img_obj = {
|
| 84 |
"path": uploaded_info["path"],
|
| 85 |
"meta": {"_type": "gradio.FileData"},
|
|
@@ -96,7 +110,7 @@ def infer(
|
|
| 96 |
"lora_adapter": lora_adapter,
|
| 97 |
"seed": int(seed),
|
| 98 |
"randomize_seed": bool(randomize_seed),
|
| 99 |
-
"guidance_scale":
|
| 100 |
"steps": int(steps),
|
| 101 |
}
|
| 102 |
|
|
|
|
| 25 |
headers = {'Authorization': f'Bearer {HF_TOKEN}'}
|
| 26 |
resp = requests.post(UPLOAD_URL, files=files, headers=headers)
|
| 27 |
resp.raise_for_status()
|
| 28 |
+
# 修复: 解析结果时先检查返回类型, 如果非dict或list则报错
|
| 29 |
+
try:
|
| 30 |
+
res = resp.json()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
raise RuntimeError("解析API响应的JSON失败: %s" % e)
|
| 33 |
if isinstance(res, list) and len(res) > 0:
|
| 34 |
+
# 取第一个有效项目, 如果不是dict也报错
|
| 35 |
+
if isinstance(res[0], dict):
|
| 36 |
+
return res[0]
|
| 37 |
+
else:
|
| 38 |
+
raise RuntimeError("返回结果不是字典类型: %s" % str(res[0]))
|
| 39 |
+
elif isinstance(res, dict):
|
| 40 |
+
return res
|
| 41 |
+
else:
|
| 42 |
+
# 返回格式不明, 直接异常
|
| 43 |
+
raise RuntimeError("API响应格式异常,期望是dict或list: %s" % str(res))
|
| 44 |
|
| 45 |
def call_infer(payload):
|
| 46 |
headers = {'Authorization': f'Bearer {HF_TOKEN}'}
|
|
|
|
| 90 |
# 1. 上传文件
|
| 91 |
try:
|
| 92 |
uploaded_info = upload_file(image)
|
| 93 |
+
# 检查uploaded_info类型
|
| 94 |
+
if not isinstance(uploaded_info, dict) or "path" not in uploaded_info:
|
| 95 |
+
print(f"[图片上传] 失败: 返回结果无'path'字段: {uploaded_info}")
|
| 96 |
+
return None, seed
|
| 97 |
img_obj = {
|
| 98 |
"path": uploaded_info["path"],
|
| 99 |
"meta": {"_type": "gradio.FileData"},
|
|
|
|
| 110 |
"lora_adapter": lora_adapter,
|
| 111 |
"seed": int(seed),
|
| 112 |
"randomize_seed": bool(randomize_seed),
|
| 113 |
+
"guidance_scale": int(guidance_scale),
|
| 114 |
"steps": int(steps),
|
| 115 |
}
|
| 116 |
|