Commit ·
8991f32
1
Parent(s): 0736f7e
Fix: Use stdlib urllib instead of requests for zero dependencies
Browse files- inference.py +35 -35
inference.py
CHANGED
|
@@ -21,14 +21,18 @@ import os
|
|
| 21 |
import re
|
| 22 |
import sys
|
| 23 |
import time
|
|
|
|
|
|
|
| 24 |
from typing import Any, List, Optional
|
| 25 |
|
| 26 |
-
import requests
|
| 27 |
-
from dotenv import load_dotenv
|
| 28 |
from openai import OpenAI
|
| 29 |
|
| 30 |
-
# Load environment variables from .env file
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# ============================================================================
|
|
@@ -156,26 +160,38 @@ Remember: You're dealing with a production system. Be methodical and verify your
|
|
| 156 |
|
| 157 |
|
| 158 |
# ============================================================================
|
| 159 |
-
# Environment Client (HTTP-based)
|
| 160 |
# ============================================================================
|
| 161 |
|
| 162 |
class EnvClient:
|
| 163 |
-
"""HTTP client for interacting with the environment server."""
|
| 164 |
|
| 165 |
def __init__(self, base_url: str, timeout: float = 30.0):
|
| 166 |
self.base_url = base_url.rstrip("/")
|
| 167 |
self.timeout = timeout
|
| 168 |
self.session_id: Optional[str] = None
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
def reset(self, task_id: str) -> dict:
|
| 171 |
"""Reset the environment and start a new episode."""
|
| 172 |
-
|
| 173 |
-
f"{self.base_url}/reset",
|
| 174 |
-
json={"task_id": task_id},
|
| 175 |
-
timeout=self.timeout
|
| 176 |
-
)
|
| 177 |
-
response.raise_for_status()
|
| 178 |
-
data = response.json()
|
| 179 |
self.session_id = data["session_id"]
|
| 180 |
return data["observation"]
|
| 181 |
|
|
@@ -183,44 +199,28 @@ class EnvClient:
|
|
| 183 |
"""Take an action in the environment."""
|
| 184 |
if not self.session_id:
|
| 185 |
raise RuntimeError("Must call reset() before step()")
|
| 186 |
-
|
| 187 |
-
response = requests.post(
|
| 188 |
-
f"{self.base_url}/step",
|
| 189 |
-
json={"session_id": self.session_id, "action_str": action_str},
|
| 190 |
-
timeout=self.timeout
|
| 191 |
-
)
|
| 192 |
-
response.raise_for_status()
|
| 193 |
-
return response.json()
|
| 194 |
|
| 195 |
def grade(self) -> dict:
|
| 196 |
"""Get the final grade for the episode."""
|
| 197 |
if not self.session_id:
|
| 198 |
raise RuntimeError("Must call reset() before grade()")
|
| 199 |
-
|
| 200 |
-
response = requests.post(
|
| 201 |
-
f"{self.base_url}/grade",
|
| 202 |
-
json={"session_id": self.session_id},
|
| 203 |
-
timeout=self.timeout
|
| 204 |
-
)
|
| 205 |
-
response.raise_for_status()
|
| 206 |
-
return response.json()
|
| 207 |
|
| 208 |
def get_tasks(self) -> list:
|
| 209 |
"""Get list of available tasks."""
|
| 210 |
-
|
| 211 |
-
response.raise_for_status()
|
| 212 |
-
return response.json()["tasks"]
|
| 213 |
|
| 214 |
def health(self) -> bool:
|
| 215 |
"""Check if the environment is healthy."""
|
| 216 |
try:
|
| 217 |
-
|
| 218 |
-
return
|
| 219 |
except Exception:
|
| 220 |
return False
|
| 221 |
|
| 222 |
def close(self) -> None:
|
| 223 |
-
"""Close the HTTP client (no-op for
|
| 224 |
pass
|
| 225 |
|
| 226 |
|
|
|
|
| 21 |
import re
|
| 22 |
import sys
|
| 23 |
import time
|
| 24 |
+
import urllib.request
|
| 25 |
+
import urllib.error
|
| 26 |
from typing import Any, List, Optional
|
| 27 |
|
|
|
|
|
|
|
| 28 |
from openai import OpenAI
|
| 29 |
|
| 30 |
+
# Load environment variables from .env file if available
|
| 31 |
+
try:
|
| 32 |
+
from dotenv import load_dotenv
|
| 33 |
+
load_dotenv()
|
| 34 |
+
except ImportError:
|
| 35 |
+
pass # dotenv not required
|
| 36 |
|
| 37 |
|
| 38 |
# ============================================================================
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
# ============================================================================
|
| 163 |
+
# Environment Client (HTTP-based using stdlib)
|
| 164 |
# ============================================================================
|
| 165 |
|
| 166 |
class EnvClient:
|
| 167 |
+
"""HTTP client for interacting with the environment server using stdlib."""
|
| 168 |
|
| 169 |
def __init__(self, base_url: str, timeout: float = 30.0):
|
| 170 |
self.base_url = base_url.rstrip("/")
|
| 171 |
self.timeout = timeout
|
| 172 |
self.session_id: Optional[str] = None
|
| 173 |
|
| 174 |
+
def _request(self, method: str, path: str, data: dict = None) -> dict:
|
| 175 |
+
"""Make an HTTP request using urllib."""
|
| 176 |
+
url = f"{self.base_url}{path}"
|
| 177 |
+
headers = {"Content-Type": "application/json"}
|
| 178 |
+
|
| 179 |
+
if data is not None:
|
| 180 |
+
body = json.dumps(data).encode("utf-8")
|
| 181 |
+
req = urllib.request.Request(url, data=body, headers=headers, method=method)
|
| 182 |
+
else:
|
| 183 |
+
req = urllib.request.Request(url, headers=headers, method=method)
|
| 184 |
+
|
| 185 |
+
try:
|
| 186 |
+
with urllib.request.urlopen(req, timeout=self.timeout) as response:
|
| 187 |
+
return json.loads(response.read().decode("utf-8"))
|
| 188 |
+
except urllib.error.HTTPError as e:
|
| 189 |
+
error_body = e.read().decode("utf-8") if e.fp else str(e)
|
| 190 |
+
raise RuntimeError(f"HTTP {e.code}: {error_body}")
|
| 191 |
+
|
| 192 |
def reset(self, task_id: str) -> dict:
|
| 193 |
"""Reset the environment and start a new episode."""
|
| 194 |
+
data = self._request("POST", "/reset", {"task_id": task_id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
self.session_id = data["session_id"]
|
| 196 |
return data["observation"]
|
| 197 |
|
|
|
|
| 199 |
"""Take an action in the environment."""
|
| 200 |
if not self.session_id:
|
| 201 |
raise RuntimeError("Must call reset() before step()")
|
| 202 |
+
return self._request("POST", "/step", {"session_id": self.session_id, "action_str": action_str})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
def grade(self) -> dict:
|
| 205 |
"""Get the final grade for the episode."""
|
| 206 |
if not self.session_id:
|
| 207 |
raise RuntimeError("Must call reset() before grade()")
|
| 208 |
+
return self._request("POST", "/grade", {"session_id": self.session_id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
def get_tasks(self) -> list:
|
| 211 |
"""Get list of available tasks."""
|
| 212 |
+
return self._request("GET", "/tasks")["tasks"]
|
|
|
|
|
|
|
| 213 |
|
| 214 |
def health(self) -> bool:
|
| 215 |
"""Check if the environment is healthy."""
|
| 216 |
try:
|
| 217 |
+
self._request("GET", "/health")
|
| 218 |
+
return True
|
| 219 |
except Exception:
|
| 220 |
return False
|
| 221 |
|
| 222 |
def close(self) -> None:
|
| 223 |
+
"""Close the HTTP client (no-op for urllib)."""
|
| 224 |
pass
|
| 225 |
|
| 226 |
|