Roshan818 commited on
Commit
16b706d
Β·
1 Parent(s): 4c41e84

fix: reconnect per task to avoid closed WebSocket on second episode

Browse files
Files changed (1) hide show
  1. inference.py +20 -16
inference.py CHANGED
@@ -221,26 +221,30 @@ async def run_task(env_client: FactoryEnvClient,
221
 
222
 
223
  # ── Main ──────────────────────────────────────────────────────────────────────
224
- async def main() -> None:
225
- llm_client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
226
-
227
  if IMAGE_NAME:
228
  print(f"[DEBUG] Spinning up Docker image: {IMAGE_NAME}", flush=True)
229
- env_client = await FactoryEnvClient.from_docker_image(IMAGE_NAME)
230
- else:
231
- url = ENV_URL or "http://localhost:7860"
232
- print(f"[DEBUG] Connecting to: {url}", flush=True)
233
- env_client = FactoryEnvClient(base_url=url)
234
- await env_client.connect()
235
 
236
- try:
237
- for task in TASKS:
238
- await run_task(env_client, llm_client, task)
239
- finally:
 
 
240
  try:
241
- await env_client.close()
242
- except Exception as exc:
243
- print(f"[DEBUG] env.close() error: {exc}", flush=True)
 
 
 
244
 
245
 
246
  if __name__ == "__main__":
 
221
 
222
 
223
  # ── Main ──────────────────────────────────────────────────────────────────────
224
+ async def _make_client() -> FactoryEnvClient:
225
+ """Create and connect a fresh client for one task episode."""
 
226
  if IMAGE_NAME:
227
  print(f"[DEBUG] Spinning up Docker image: {IMAGE_NAME}", flush=True)
228
+ return await FactoryEnvClient.from_docker_image(IMAGE_NAME)
229
+ url = ENV_URL or "http://localhost:7860"
230
+ print(f"[DEBUG] Connecting to: {url}", flush=True)
231
+ client = FactoryEnvClient(base_url=url)
232
+ await client.connect()
233
+ return client
234
 
235
+
236
+ async def main() -> None:
237
+ llm_client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
238
+
239
+ for task in TASKS:
240
+ env_client = await _make_client()
241
  try:
242
+ await run_task(env_client, llm_client, task)
243
+ finally:
244
+ try:
245
+ await env_client.close()
246
+ except Exception as exc:
247
+ print(f"[DEBUG] env.close() error: {exc}", flush=True)
248
 
249
 
250
  if __name__ == "__main__":