Spaces:
Running
Running
noahlee1234 commited on
Commit ·
159c23a
1
Parent(s): 116d93d
fix: raise backend timeout default and show clear timeout errors
Browse files- apps/gradio-demo/app/main.py +43 -1
apps/gradio-demo/app/main.py
CHANGED
|
@@ -23,7 +23,7 @@ import trimesh
|
|
| 23 |
BUILD123D_PYTHON = os.getenv("BUILD123D_PYTHON", sys.executable)
|
| 24 |
BACKEND_URL = os.getenv("NATURALCAD_BACKEND_URL", os.getenv("NL_CAD_BACKEND_URL", "")).strip()
|
| 25 |
BACKEND_API_KEY = os.getenv("NATURALCAD_API_KEY", os.getenv("NL_CAD_API_KEY", ""))
|
| 26 |
-
BACKEND_TIMEOUT_SECONDS = float(os.getenv("NATURALCAD_BACKEND_TIMEOUT", "
|
| 27 |
SHOW_GENERATED_CODE = os.getenv("NATURALCAD_SHOW_CODE", "false").strip().lower() in {"1", "true", "yes", "on"}
|
| 28 |
VERBOSE_LOGS = os.getenv("NATURALCAD_VERBOSE_LOGS", "false").strip().lower() in {"1", "true", "yes", "on"}
|
| 29 |
ARTIFACTS_DIR = Path(__file__).parent.parent / "artifacts"
|
|
@@ -334,6 +334,24 @@ def create_job(prompt: str, mode: str, output_type: str) -> tuple[dict | None, s
|
|
| 334 |
except error.HTTPError as exc:
|
| 335 |
detail = exc.read().decode() if exc.fp else str(exc)
|
| 336 |
return None, json.dumps({"error": f"backend http {exc.code}", "detail": detail}, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
except Exception as exc: # noqa: BLE001
|
| 338 |
return None, json.dumps({"error": f"backend unavailable: {exc}"}, indent=2)
|
| 339 |
|
|
@@ -613,6 +631,30 @@ def generate_from_prompt(prompt: str, mode: str, output_type: str):
|
|
| 613 |
except Exception:
|
| 614 |
detail = body or str(exc)
|
| 615 |
return None, None, None, f"Backend HTTP {exc.code}: {detail}", "Generation failed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
except Exception as exc:
|
| 617 |
return None, None, None, f"Backend error: {exc}", "Generation failed."
|
| 618 |
|
|
|
|
| 23 |
BUILD123D_PYTHON = os.getenv("BUILD123D_PYTHON", sys.executable)
|
| 24 |
BACKEND_URL = os.getenv("NATURALCAD_BACKEND_URL", os.getenv("NL_CAD_BACKEND_URL", "")).strip()
|
| 25 |
BACKEND_API_KEY = os.getenv("NATURALCAD_API_KEY", os.getenv("NL_CAD_API_KEY", ""))
|
| 26 |
+
BACKEND_TIMEOUT_SECONDS = float(os.getenv("NATURALCAD_BACKEND_TIMEOUT", "180"))
|
| 27 |
SHOW_GENERATED_CODE = os.getenv("NATURALCAD_SHOW_CODE", "false").strip().lower() in {"1", "true", "yes", "on"}
|
| 28 |
VERBOSE_LOGS = os.getenv("NATURALCAD_VERBOSE_LOGS", "false").strip().lower() in {"1", "true", "yes", "on"}
|
| 29 |
ARTIFACTS_DIR = Path(__file__).parent.parent / "artifacts"
|
|
|
|
| 334 |
except error.HTTPError as exc:
|
| 335 |
detail = exc.read().decode() if exc.fp else str(exc)
|
| 336 |
return None, json.dumps({"error": f"backend http {exc.code}", "detail": detail}, indent=2)
|
| 337 |
+
except error.URLError as exc:
|
| 338 |
+
if isinstance(exc.reason, TimeoutError) or "timed out" in str(exc.reason).lower():
|
| 339 |
+
return None, json.dumps(
|
| 340 |
+
{
|
| 341 |
+
"error": f"backend timeout after {BACKEND_TIMEOUT_SECONDS:.0f}s",
|
| 342 |
+
"detail": "Try a shorter prompt or retry.",
|
| 343 |
+
},
|
| 344 |
+
indent=2,
|
| 345 |
+
)
|
| 346 |
+
return None, json.dumps({"error": f"backend unavailable: {exc}"}, indent=2)
|
| 347 |
+
except TimeoutError:
|
| 348 |
+
return None, json.dumps(
|
| 349 |
+
{
|
| 350 |
+
"error": f"backend timeout after {BACKEND_TIMEOUT_SECONDS:.0f}s",
|
| 351 |
+
"detail": "Try a shorter prompt or retry.",
|
| 352 |
+
},
|
| 353 |
+
indent=2,
|
| 354 |
+
)
|
| 355 |
except Exception as exc: # noqa: BLE001
|
| 356 |
return None, json.dumps({"error": f"backend unavailable: {exc}"}, indent=2)
|
| 357 |
|
|
|
|
| 631 |
except Exception:
|
| 632 |
detail = body or str(exc)
|
| 633 |
return None, None, None, f"Backend HTTP {exc.code}: {detail}", "Generation failed."
|
| 634 |
+
except error.URLError as exc:
|
| 635 |
+
if isinstance(exc.reason, TimeoutError) or "timed out" in str(exc.reason).lower():
|
| 636 |
+
return (
|
| 637 |
+
None,
|
| 638 |
+
None,
|
| 639 |
+
None,
|
| 640 |
+
(
|
| 641 |
+
f"Backend timeout after {BACKEND_TIMEOUT_SECONDS:.0f}s. "
|
| 642 |
+
"Try a shorter prompt (fewer clauses), or retry."
|
| 643 |
+
),
|
| 644 |
+
"Generation timed out.",
|
| 645 |
+
)
|
| 646 |
+
return None, None, None, f"Backend error: {exc}", "Generation failed."
|
| 647 |
+
except TimeoutError:
|
| 648 |
+
return (
|
| 649 |
+
None,
|
| 650 |
+
None,
|
| 651 |
+
None,
|
| 652 |
+
(
|
| 653 |
+
f"Backend timeout after {BACKEND_TIMEOUT_SECONDS:.0f}s. "
|
| 654 |
+
"Try a shorter prompt (fewer clauses), or retry."
|
| 655 |
+
),
|
| 656 |
+
"Generation timed out.",
|
| 657 |
+
)
|
| 658 |
except Exception as exc:
|
| 659 |
return None, None, None, f"Backend error: {exc}", "Generation failed."
|
| 660 |
|