Spaces:
Sleeping
Sleeping
Ajjack4 commited on
Commit ·
3bd2b79
1
Parent(s): c1ff000
harden inference.py: catch all HTTP errors, retry on reset, guard main()
Browse files- inference.py +34 -14
inference.py
CHANGED
|
@@ -127,23 +127,39 @@ class EnvAdapter:
|
|
| 127 |
self.base_url = base_url.rstrip("/")
|
| 128 |
|
| 129 |
def reset(self, task_id: str) -> dict:
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
def step(self, action: dict) -> dict:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def grader(self, task_id: str) -> dict:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
def obs_to_text(obs: dict) -> str:
|
|
@@ -235,4 +251,8 @@ async def main() -> None:
|
|
| 235 |
|
| 236 |
|
| 237 |
if __name__ == "__main__":
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
self.base_url = base_url.rstrip("/")
|
| 128 |
|
| 129 |
def reset(self, task_id: str) -> dict:
|
| 130 |
+
import time
|
| 131 |
+
for attempt in range(5):
|
| 132 |
+
try:
|
| 133 |
+
resp = requests.post(
|
| 134 |
+
f"{self.base_url}/reset", params={"task_id": task_id}, timeout=60
|
| 135 |
+
)
|
| 136 |
+
resp.raise_for_status()
|
| 137 |
+
return resp.json()
|
| 138 |
+
except Exception as exc:
|
| 139 |
+
print(f"[DEBUG] reset() attempt {attempt+1} failed: {exc}", flush=True)
|
| 140 |
+
if attempt < 4:
|
| 141 |
+
time.sleep(5)
|
| 142 |
+
return {"step": 0, "services": {}, "last_action_result": {"success": False, "message": "env unreachable"}, "available_actions": [], "done": True}
|
| 143 |
|
| 144 |
def step(self, action: dict) -> dict:
|
| 145 |
+
try:
|
| 146 |
+
resp = requests.post(f"{self.base_url}/step", json=action, timeout=60)
|
| 147 |
+
resp.raise_for_status()
|
| 148 |
+
return resp.json()
|
| 149 |
+
except Exception as exc:
|
| 150 |
+
print(f"[DEBUG] step() failed: {exc}", flush=True)
|
| 151 |
+
return {"observation": {"step": 0, "services": {}, "last_action_result": {"success": False, "message": str(exc)}, "available_actions": [], "done": True}, "reward": 0.0, "done": True, "info": {}}
|
| 152 |
|
| 153 |
def grader(self, task_id: str) -> dict:
|
| 154 |
+
try:
|
| 155 |
+
resp = requests.get(
|
| 156 |
+
f"{self.base_url}/grader", params={"task_id": task_id}, timeout=60
|
| 157 |
+
)
|
| 158 |
+
resp.raise_for_status()
|
| 159 |
+
return resp.json()
|
| 160 |
+
except Exception as exc:
|
| 161 |
+
print(f"[DEBUG] grader() failed: {exc}", flush=True)
|
| 162 |
+
return {"grader_result": {"score": 0.0, "reason": str(exc)}}
|
| 163 |
|
| 164 |
|
| 165 |
def obs_to_text(obs: dict) -> str:
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|
| 254 |
+
try:
|
| 255 |
+
asyncio.run(main())
|
| 256 |
+
except Exception as exc:
|
| 257 |
+
print(f"[DEBUG] Fatal error: {exc}", flush=True)
|
| 258 |
+
sys.exit(1)
|