Vishvam10 commited on
Commit ·
17c0543
1
Parent(s): 6a68c85
debug: (inference) yeah, given up fully part 5
Browse files- inference.py +28 -58
inference.py
CHANGED
|
@@ -9,9 +9,6 @@ import httpx
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from openai import OpenAI
|
| 11 |
|
| 12 |
-
# ----------------------------
|
| 13 |
-
# PATH SETUP
|
| 14 |
-
# ----------------------------
|
| 15 |
ROOT = Path(__file__).resolve().parent
|
| 16 |
REPO_ROOT = ROOT.parent
|
| 17 |
|
|
@@ -20,9 +17,6 @@ if str(REPO_ROOT) not in sys.path:
|
|
| 20 |
|
| 21 |
load_dotenv()
|
| 22 |
|
| 23 |
-
# ----------------------------
|
| 24 |
-
# CONFIG
|
| 25 |
-
# ----------------------------
|
| 26 |
TASK_DIR = ROOT / "tasks"
|
| 27 |
SYSTEM_PROMPT = (ROOT / "system_prompt.txt").read_text().strip()
|
| 28 |
|
|
@@ -31,7 +25,7 @@ MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o")
|
|
| 31 |
|
| 32 |
BASE_URL = "https://vishvam10-cloudenv.hf.space"
|
| 33 |
|
| 34 |
-
TASK_ORDER = ["easy-task"]
|
| 35 |
|
| 36 |
MAX_PARSE_RETRIES = 3
|
| 37 |
MAX_STEPS = 5
|
|
@@ -46,7 +40,6 @@ def log_start(task: str, env: str, model: str) -> None:
|
|
| 46 |
def log_step(
|
| 47 |
step: int, action: str, reward: float, done: bool, error: Optional[str]
|
| 48 |
) -> None:
|
| 49 |
-
|
| 50 |
error_val = error if error else "null"
|
| 51 |
done_val = str(done).lower()
|
| 52 |
action_safe = action.replace("\n", " ").replace("\r", "")[:120]
|
|
@@ -68,32 +61,38 @@ def log_end(
|
|
| 68 |
)
|
| 69 |
|
| 70 |
|
| 71 |
-
# ----------------------------
|
| 72 |
-
# LOAD TASK
|
| 73 |
-
# ----------------------------
|
| 74 |
def load_task(name: str):
|
| 75 |
return json.loads((TASK_DIR / f"{name}.json").read_text())
|
| 76 |
|
| 77 |
|
| 78 |
-
|
| 79 |
-
# RESET
|
| 80 |
-
# ----------------------------
|
| 81 |
-
async def env_reset(task_id: str, episode_id: str):
|
| 82 |
async with httpx.AsyncClient(timeout=30) as http:
|
| 83 |
r = await http.post(
|
| 84 |
-
f"{BASE_URL}/
|
| 85 |
json={
|
| 86 |
-
"
|
| 87 |
-
"
|
|
|
|
| 88 |
},
|
| 89 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
r.raise_for_status()
|
| 91 |
return r.json()
|
| 92 |
|
| 93 |
|
| 94 |
-
# ----------------------------
|
| 95 |
-
# STEP
|
| 96 |
-
# ----------------------------
|
| 97 |
async def env_step(action: dict):
|
| 98 |
async with httpx.AsyncClient(timeout=30) as http:
|
| 99 |
r = await http.post(
|
|
@@ -104,9 +103,6 @@ async def env_step(action: dict):
|
|
| 104 |
return r.json()
|
| 105 |
|
| 106 |
|
| 107 |
-
# ----------------------------
|
| 108 |
-
# SAFE JSON CLEANING
|
| 109 |
-
# ----------------------------
|
| 110 |
def clean_json(raw: str) -> str:
|
| 111 |
raw = raw.strip()
|
| 112 |
raw = re.sub(r"^```(?:json)?\s*", "", raw)
|
|
@@ -119,9 +115,6 @@ def clean_json(raw: str) -> str:
|
|
| 119 |
return raw
|
| 120 |
|
| 121 |
|
| 122 |
-
# ----------------------------
|
| 123 |
-
# BUILD ACTION (DICT ONLY)
|
| 124 |
-
# ----------------------------
|
| 125 |
def build_action(raw: str, task_id: str, episode_id: str):
|
| 126 |
try:
|
| 127 |
raw = clean_json(raw)
|
|
@@ -162,9 +155,6 @@ def build_action(raw: str, task_id: str, episode_id: str):
|
|
| 162 |
return None
|
| 163 |
|
| 164 |
|
| 165 |
-
# ----------------------------
|
| 166 |
-
# LLM CALL
|
| 167 |
-
# ----------------------------
|
| 168 |
def call_model(step, state, broken, ideal, history):
|
| 169 |
prompt = f"""
|
| 170 |
Return ONLY valid JSON.
|
|
@@ -189,15 +179,14 @@ HISTORY {history[-5:]}
|
|
| 189 |
return (res.choices[0].message.content or "").strip()
|
| 190 |
|
| 191 |
|
| 192 |
-
# ----------------------------
|
| 193 |
-
# EPISODE LOOP
|
| 194 |
-
# ----------------------------
|
| 195 |
async def run_episode(task):
|
| 196 |
task_id = task["pipeline_id"]
|
| 197 |
episode_id = f"{task_id}-episode-1"
|
| 198 |
|
| 199 |
log_start(task=task_id, env="http", model=MODEL_NAME)
|
| 200 |
|
|
|
|
|
|
|
| 201 |
await env_step(
|
| 202 |
{
|
| 203 |
"task_id": task_id,
|
|
@@ -208,15 +197,14 @@ async def run_episode(task):
|
|
| 208 |
"payload": {},
|
| 209 |
}
|
| 210 |
)
|
| 211 |
-
result = await env_reset(task_id, episode_id)
|
| 212 |
-
|
| 213 |
-
obs = result["observation"]
|
| 214 |
|
|
|
|
|
|
|
|
|
|
| 215 |
history = []
|
| 216 |
rewards = []
|
| 217 |
step_count = 0
|
| 218 |
|
| 219 |
-
|
| 220 |
for step in range(MAX_STEPS):
|
| 221 |
if obs.get("done"):
|
| 222 |
break
|
|
@@ -226,7 +214,7 @@ async def run_episode(task):
|
|
| 226 |
try:
|
| 227 |
raw = call_model(
|
| 228 |
step,
|
| 229 |
-
obs
|
| 230 |
task["broken_pipeline"],
|
| 231 |
task["ideal_pipeline"],
|
| 232 |
history,
|
|
@@ -260,36 +248,18 @@ async def run_episode(task):
|
|
| 260 |
except Exception as e:
|
| 261 |
error = str(e)
|
| 262 |
|
| 263 |
-
log_step(
|
| 264 |
-
step=step,
|
| 265 |
-
action="ERROR",
|
| 266 |
-
reward=0.0,
|
| 267 |
-
done=False,
|
| 268 |
-
error=error,
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
break
|
| 272 |
|
| 273 |
if obs.get("done"):
|
| 274 |
break
|
| 275 |
|
| 276 |
-
# ----------------------------
|
| 277 |
-
# END LOG
|
| 278 |
-
# ----------------------------
|
| 279 |
success = obs.get("done", False)
|
| 280 |
score = sum(rewards) / (len(rewards) or 1)
|
| 281 |
|
| 282 |
-
log_end(
|
| 283 |
-
success=success,
|
| 284 |
-
steps=step_count,
|
| 285 |
-
score=score,
|
| 286 |
-
rewards=rewards,
|
| 287 |
-
)
|
| 288 |
|
| 289 |
|
| 290 |
-
# ----------------------------
|
| 291 |
-
# MAIN
|
| 292 |
-
# ----------------------------
|
| 293 |
async def main():
|
| 294 |
for name in TASK_ORDER:
|
| 295 |
task = load_task(name)
|
|
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from openai import OpenAI
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
ROOT = Path(__file__).resolve().parent
|
| 13 |
REPO_ROOT = ROOT.parent
|
| 14 |
|
|
|
|
| 17 |
|
| 18 |
load_dotenv()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
TASK_DIR = ROOT / "tasks"
|
| 21 |
SYSTEM_PROMPT = (ROOT / "system_prompt.txt").read_text().strip()
|
| 22 |
|
|
|
|
| 25 |
|
| 26 |
BASE_URL = "https://vishvam10-cloudenv.hf.space"
|
| 27 |
|
| 28 |
+
TASK_ORDER = ["easy-task", "medium-task", "hard-task"]
|
| 29 |
|
| 30 |
MAX_PARSE_RETRIES = 3
|
| 31 |
MAX_STEPS = 5
|
|
|
|
| 40 |
def log_step(
|
| 41 |
step: int, action: str, reward: float, done: bool, error: Optional[str]
|
| 42 |
) -> None:
|
|
|
|
| 43 |
error_val = error if error else "null"
|
| 44 |
done_val = str(done).lower()
|
| 45 |
action_safe = action.replace("\n", " ").replace("\r", "")[:120]
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
def load_task(name: str):
|
| 65 |
return json.loads((TASK_DIR / f"{name}.json").read_text())
|
| 66 |
|
| 67 |
|
| 68 |
+
async def register_pipeline(task: dict):
|
|
|
|
|
|
|
|
|
|
| 69 |
async with httpx.AsyncClient(timeout=30) as http:
|
| 70 |
r = await http.post(
|
| 71 |
+
f"{BASE_URL}/api/pipelines",
|
| 72 |
json={
|
| 73 |
+
"pipeline_id": task["pipeline_id"],
|
| 74 |
+
"broken_pipeline": task["broken_pipeline"],
|
| 75 |
+
"ideal_pipeline": task["ideal_pipeline"],
|
| 76 |
},
|
| 77 |
)
|
| 78 |
+
|
| 79 |
+
if r.status_code not in (200, 201, 400):
|
| 80 |
+
print(
|
| 81 |
+
f"[PIPELINE REGISTER ERROR] {r.status_code} {r.text}",
|
| 82 |
+
flush=True,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
async def env_reset(task_id: str, episode_id: str):
|
| 87 |
+
async with httpx.AsyncClient(timeout=30) as http:
|
| 88 |
+
r = await http.post(
|
| 89 |
+
f"{BASE_URL}/reset",
|
| 90 |
+
json={"task_id": task_id, "episode_id": episode_id},
|
| 91 |
+
)
|
| 92 |
r.raise_for_status()
|
| 93 |
return r.json()
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
async def env_step(action: dict):
|
| 97 |
async with httpx.AsyncClient(timeout=30) as http:
|
| 98 |
r = await http.post(
|
|
|
|
| 103 |
return r.json()
|
| 104 |
|
| 105 |
|
|
|
|
|
|
|
|
|
|
| 106 |
def clean_json(raw: str) -> str:
|
| 107 |
raw = raw.strip()
|
| 108 |
raw = re.sub(r"^```(?:json)?\s*", "", raw)
|
|
|
|
| 115 |
return raw
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
| 118 |
def build_action(raw: str, task_id: str, episode_id: str):
|
| 119 |
try:
|
| 120 |
raw = clean_json(raw)
|
|
|
|
| 155 |
return None
|
| 156 |
|
| 157 |
|
|
|
|
|
|
|
|
|
|
| 158 |
def call_model(step, state, broken, ideal, history):
|
| 159 |
prompt = f"""
|
| 160 |
Return ONLY valid JSON.
|
|
|
|
| 179 |
return (res.choices[0].message.content or "").strip()
|
| 180 |
|
| 181 |
|
|
|
|
|
|
|
|
|
|
| 182 |
async def run_episode(task):
|
| 183 |
task_id = task["pipeline_id"]
|
| 184 |
episode_id = f"{task_id}-episode-1"
|
| 185 |
|
| 186 |
log_start(task=task_id, env="http", model=MODEL_NAME)
|
| 187 |
|
| 188 |
+
result = await env_reset(task_id, episode_id)
|
| 189 |
+
|
| 190 |
await env_step(
|
| 191 |
{
|
| 192 |
"task_id": task_id,
|
|
|
|
| 197 |
"payload": {},
|
| 198 |
}
|
| 199 |
)
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
await register_pipeline(task)
|
| 202 |
+
|
| 203 |
+
obs = {}
|
| 204 |
history = []
|
| 205 |
rewards = []
|
| 206 |
step_count = 0
|
| 207 |
|
|
|
|
| 208 |
for step in range(MAX_STEPS):
|
| 209 |
if obs.get("done"):
|
| 210 |
break
|
|
|
|
| 214 |
try:
|
| 215 |
raw = call_model(
|
| 216 |
step,
|
| 217 |
+
obs,
|
| 218 |
task["broken_pipeline"],
|
| 219 |
task["ideal_pipeline"],
|
| 220 |
history,
|
|
|
|
| 248 |
except Exception as e:
|
| 249 |
error = str(e)
|
| 250 |
|
| 251 |
+
log_step(step, "ERROR", 0.0, False, error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
break
|
| 253 |
|
| 254 |
if obs.get("done"):
|
| 255 |
break
|
| 256 |
|
|
|
|
|
|
|
|
|
|
| 257 |
success = obs.get("done", False)
|
| 258 |
score = sum(rewards) / (len(rewards) or 1)
|
| 259 |
|
| 260 |
+
log_end(success=success, steps=step_count, score=score, rewards=rewards)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
|
|
|
|
|
|
|
|
|
|
| 263 |
async def main():
|
| 264 |
for name in TASK_ORDER:
|
| 265 |
task = load_task(name)
|