SpandanM110 commited on
Commit
4e71d7a
·
1 Parent(s): 59d6790

Fix inference.py: correct default port to 7860, add retry logic

Browse files

- Changed ENV_URL default from localhost:8000 to localhost:7860
- Added wait_for_env() with 90s timeout to handle container startup
- Made HF_TOKEN non-fatal (warning instead of crash)
- Fixes Phase 2 validation: connection refused error

Submission by SpandanM110

Files changed (1) hide show
  1. inference.py +26 -3
inference.py CHANGED
@@ -8,12 +8,13 @@ Environment variables:
8
  API_BASE_URL - LLM API endpoint (default: https://api-inference.huggingface.co/v1)
9
  MODEL_NAME - Model identifier (default: Qwen/Qwen2.5-72B-Instruct)
10
  HF_TOKEN - API key (required, no default)
11
- ENV_URL - Environment server URL (default: http://localhost:8000)
12
  """
13
  from __future__ import annotations
14
  import os
15
  import json
16
  import sys
 
17
  import requests
18
  from openai import OpenAI
19
 
@@ -24,13 +25,14 @@ from openai import OpenAI
24
  API_BASE_URL = os.getenv("API_BASE_URL", "https://api-inference.huggingface.co/v1")
25
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
26
  HF_TOKEN = os.getenv("HF_TOKEN")
27
- ENV_URL = os.getenv("ENV_URL", "http://localhost:8000")
28
 
29
  # Optional - if you use from_docker_image():
30
  LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
31
 
32
  if HF_TOKEN is None:
33
- raise ValueError("HF_TOKEN environment variable is required")
 
34
 
35
  # All LLM calls use the OpenAI client configured via these variables
36
  client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
@@ -108,6 +110,22 @@ def call_agent(obs: dict, history: list, step_num: int, task: str) -> dict:
108
  return {"action_type": "finalize"}
109
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  def env_reset(task: str) -> dict:
112
  resp = requests.post(f"{ENV_URL}/reset", json={"task": task}, timeout=30)
113
  resp.raise_for_status()
@@ -168,6 +186,11 @@ def run_task(task: str) -> tuple[bool, int, list[float]]:
168
 
169
 
170
  def main():
 
 
 
 
 
171
  all_success = True
172
  for task in TASKS:
173
  try:
 
8
  API_BASE_URL - LLM API endpoint (default: https://api-inference.huggingface.co/v1)
9
  MODEL_NAME - Model identifier (default: Qwen/Qwen2.5-72B-Instruct)
10
  HF_TOKEN - API key (required, no default)
11
+ ENV_URL - Environment server URL (default: http://localhost:7860)
12
  """
13
  from __future__ import annotations
14
  import os
15
  import json
16
  import sys
17
+ import time
18
  import requests
19
  from openai import OpenAI
20
 
 
25
  API_BASE_URL = os.getenv("API_BASE_URL", "https://api-inference.huggingface.co/v1")
26
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
27
  HF_TOKEN = os.getenv("HF_TOKEN")
28
+ ENV_URL = os.getenv("ENV_URL", "http://localhost:7860")
29
 
30
  # Optional - if you use from_docker_image():
31
  LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
32
 
33
  if HF_TOKEN is None:
34
+ print("WARNING: HF_TOKEN not set, using placeholder", file=sys.stderr)
35
+ HF_TOKEN = "hf_placeholder"
36
 
37
  # All LLM calls use the OpenAI client configured via these variables
38
  client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
 
110
  return {"action_type": "finalize"}
111
 
112
 
113
+ def wait_for_env(url: str, timeout: int = 60):
114
+ """Wait for the environment server to be reachable."""
115
+ start = time.time()
116
+ while time.time() - start < timeout:
117
+ try:
118
+ resp = requests.get(f"{url}/health", timeout=5)
119
+ if resp.status_code == 200:
120
+ return True
121
+ except requests.ConnectionError:
122
+ pass
123
+ except Exception:
124
+ pass
125
+ time.sleep(2)
126
+ return False
127
+
128
+
129
  def env_reset(task: str) -> dict:
130
  resp = requests.post(f"{ENV_URL}/reset", json={"task": task}, timeout=30)
131
  resp.raise_for_status()
 
186
 
187
 
188
  def main():
189
+ # Wait for environment server to be ready
190
+ print(f"Waiting for environment at {ENV_URL} ...", file=sys.stderr)
191
+ if not wait_for_env(ENV_URL, timeout=90):
192
+ print(f"WARNING: Environment at {ENV_URL} not reachable, proceeding anyway", file=sys.stderr)
193
+
194
  all_success = True
195
  for task in TASKS:
196
  try: