wkea commited on
Commit
eacd242
·
verified ·
1 Parent(s): b8d2078

Improve error messages for client calls

Browse files
Files changed (2) hide show
  1. app.py +13 -3
  2. call_space.py +19 -14
app.py CHANGED
@@ -93,7 +93,11 @@ def _check_api_key(user_key: str) -> None:
93
  """
94
  伪私有鉴权:对外只暴露“成功/失败”,避免泄漏真实 Key。
95
  """
96
- expected = _require_space_api_key()
 
 
 
 
97
  user_key = (user_key or "").strip()
98
  if not user_key or not hmac.compare_digest(user_key, expected):
99
  raise gr.Error("未授权:API_KEY错误")
@@ -256,7 +260,13 @@ def build_demo() -> gr.Blocks:
256
  gallery = gr.Gallery(label="单张结果")
257
 
258
  def _run(*args):
259
- return _infer(*args)
 
 
 
 
 
 
260
 
261
  run.click(
262
  _run,
@@ -289,7 +299,7 @@ def main() -> None:
289
  """
290
  port = int(os.getenv("PORT", "7860"))
291
  # 监听 0.0.0.0 才能被 Space 反向代理访问到
292
- demo.launch(server_name="0.0.0.0", server_port=port, share=False)
293
 
294
 
295
  if __name__ == "__main__":
 
93
  """
94
  伪私有鉴权:对外只暴露“成功/失败”,避免泄漏真实 Key。
95
  """
96
+ # 若 Space 端未配置 Key,给出明确错误,避免客户端只能看到“上游异常但无verbose”
97
+ try:
98
+ expected = _require_space_api_key()
99
+ except Exception:
100
+ raise gr.Error("服务端未配置SPACEAPIKEY/SPACE_API_KEY:请到Space Settings->Secrets 新建后重启")
101
  user_key = (user_key or "").strip()
102
  if not user_key or not hmac.compare_digest(user_key, expected):
103
  raise gr.Error("未授权:API_KEY错误")
 
260
  gallery = gr.Gallery(label="单张结果")
261
 
262
  def _run(*args):
263
+ # 把非 gr.Error 的异常也转换成可见错误信息(不暴露堆栈)
264
+ try:
265
+ return _infer(*args)
266
+ except gr.Error:
267
+ raise
268
+ except Exception as e:
269
+ raise gr.Error(f"推理失败:{e}")
270
 
271
  run.click(
272
  _run,
 
299
  """
300
  port = int(os.getenv("PORT", "7860"))
301
  # 监听 0.0.0.0 才能被 Space 反向代理访问到
302
+ demo.launch(server_name="0.0.0.0", server_port=port, share=False, show_error=False)
303
 
304
 
305
  if __name__ == "__main__":
call_space.py CHANGED
@@ -120,6 +120,7 @@ def _call_space(
120
  """
121
  try:
122
  from gradio_client import Client # type: ignore
 
123
  except Exception as e:
124
  raise RuntimeError("缺少依赖:gradio_client(建议安装 gradio 时一并安装)") from e
125
 
@@ -127,20 +128,24 @@ def _call_space(
127
  client = Client(space)
128
 
129
  # 输入顺序必须与 Space 的 gradio inputs 一致(见 app.py run.click(inputs=...))
130
- res = client.predict(
131
- api_key,
132
- prompt,
133
- float(cfg_scale),
134
- bool(use_ema),
135
- bool(use_ddim),
136
- int(ddim_steps),
137
- float(ddim_eta),
138
- int(n_images),
139
- int(batch),
140
- int(seed),
141
- int(preview_upscale),
142
- fn_index=0,
143
- )
 
 
 
 
144
 
145
  # 期望输出:(info, grid, gallery)
146
  if not isinstance(res, (list, tuple)) or len(res) != 3:
 
120
  """
121
  try:
122
  from gradio_client import Client # type: ignore
123
+ from gradio_client.exceptions import AppError # type: ignore
124
  except Exception as e:
125
  raise RuntimeError("缺少依赖:gradio_client(建议安装 gradio 时一并安装)") from e
126
 
 
128
  client = Client(space)
129
 
130
  # 输入顺序必须与 Space 的 gradio inputs 一致(见 app.py run.click(inputs=...))
131
+ try:
132
+ res = client.predict(
133
+ api_key,
134
+ prompt,
135
+ float(cfg_scale),
136
+ bool(use_ema),
137
+ bool(use_ddim),
138
+ int(ddim_steps),
139
+ float(ddim_eta),
140
+ int(n_images),
141
+ int(batch),
142
+ int(seed),
143
+ int(preview_upscale),
144
+ fn_index=0,
145
+ )
146
+ except AppError as e:
147
+ # 让报错更可读:通常是 Space 端 gr.Error 抛出的信息
148
+ raise RuntimeError(f"Space调用失败:{e}") from e
149
 
150
  # 期望输出:(info, grid, gallery)
151
  if not isinstance(res, (list, tuple)) or len(res) != 3: