Shreeraj Mummidivarapu commited on
Please God, Help Me Pass !!!
Browse files- inference.py +80 -118
inference.py
CHANGED
|
@@ -4,172 +4,134 @@ import urllib.request
|
|
| 4 |
import urllib.error
|
| 5 |
from typing import List, Optional
|
| 6 |
|
| 7 |
-
try:
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
-
load_dotenv()
|
| 10 |
-
except ImportError:
|
| 11 |
-
pass
|
| 12 |
-
|
| 13 |
-
# /// script
|
| 14 |
-
# requires-python = ">=3.11"
|
| 15 |
-
# dependencies = [
|
| 16 |
-
# "openai",
|
| 17 |
-
# ]
|
| 18 |
-
# ///
|
| 19 |
-
|
| 20 |
from openai import OpenAI
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return json.loads(res.read().decode("utf-8"))
|
| 28 |
-
except urllib.error.HTTPError as e:
|
| 29 |
-
raise Exception(f"HTTP Error {e.code}: {e.read().decode('utf-8')}")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
if not API_BASE_URL or not API_KEY:
|
| 35 |
-
raise RuntimeError("[FATAL] API_BASE_URL and API_KEY must be injected by the validator.")
|
| 36 |
-
ENV_BASE_URL = os.getenv("ENV_BASE_URL", "http://localhost:7860")
|
| 37 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 38 |
-
HF_TOKEN = os.getenv("HF_TOKEN") # kept for local dev / backward compat
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def log_start(task: str, env: str, model: str) -> None:
|
| 46 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 47 |
|
| 48 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 49 |
-
|
| 50 |
-
done_val = str(done).lower()
|
| 51 |
-
print(
|
| 52 |
-
f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}",
|
| 53 |
-
flush=True,
|
| 54 |
-
)
|
| 55 |
|
| 56 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
| 57 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 58 |
print(f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}", flush=True)
|
| 59 |
|
|
|
|
| 60 |
def main():
|
| 61 |
-
# Initialize
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Initialize Environment
|
| 66 |
task_id = os.getenv("CLM_LEVEL", "hard")
|
| 67 |
-
|
| 68 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 69 |
-
|
| 70 |
-
# 1. Reset Environment
|
| 71 |
try:
|
| 72 |
data = post_json(f"{ENV_BASE_URL}/reset", {"task_id": task_id})
|
| 73 |
except Exception as e:
|
| 74 |
-
log_step(step=0, action="reset", reward=0.0, done=True, error=str(e)[:
|
| 75 |
log_end(success=False, steps=0, score=0.0, rewards=[])
|
| 76 |
return
|
| 77 |
-
|
| 78 |
-
session_id
|
| 79 |
observation = data["observation"]
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
while not done and step < MAX_STEPS:
|
| 88 |
step += 1
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
""
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
Current Observation:
|
| 106 |
-
{json.dumps(observation, indent=2)}
|
| 107 |
-
|
| 108 |
-
What is your next action JSON?
|
| 109 |
-
"""
|
| 110 |
-
action = None
|
| 111 |
error_msg = None
|
| 112 |
-
|
| 113 |
try:
|
| 114 |
-
completion
|
| 115 |
model=MODEL_NAME,
|
| 116 |
messages=[
|
| 117 |
-
{"role": "system", "content": system_prompt
|
| 118 |
-
{"role": "user",
|
| 119 |
],
|
| 120 |
temperature=0.1,
|
| 121 |
-
max_tokens=150
|
| 122 |
)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
action = json.loads(json_str)
|
| 133 |
-
except Exception as e:
|
| 134 |
-
error_msg = str(e)[:50]
|
| 135 |
-
|
| 136 |
-
# Fallback heuristic logic if action could not be parsed
|
| 137 |
if not action:
|
| 138 |
-
tasks
|
| 139 |
-
incomp
|
| 140 |
-
|
|
|
|
| 141 |
action = {"type": "break"}
|
| 142 |
elif incomp:
|
| 143 |
action = {"type": "work", "task_id": incomp[0]["id"]}
|
| 144 |
else:
|
| 145 |
action = {"type": "delay"}
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# 3. Process action in Env
|
| 151 |
try:
|
| 152 |
-
step_data
|
| 153 |
-
"session_id": session_id,
|
| 154 |
-
"action": action
|
| 155 |
-
})
|
| 156 |
-
|
| 157 |
observation = step_data["observation"]
|
| 158 |
-
reward
|
| 159 |
-
done
|
| 160 |
-
info
|
| 161 |
-
except Exception as
|
| 162 |
-
reward
|
| 163 |
-
done
|
| 164 |
-
error_msg = error_msg or str(
|
| 165 |
-
|
| 166 |
rewards.append(reward)
|
| 167 |
-
history.append(f"Step {step}
|
| 168 |
log_step(step=step, action=action_str, reward=reward, done=done, error=error_msg)
|
| 169 |
|
| 170 |
-
score
|
| 171 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 172 |
log_end(success=success, steps=step, score=score, rewards=rewards)
|
| 173 |
|
|
|
|
| 174 |
if __name__ == "__main__":
|
| 175 |
main()
|
|
|
|
| 4 |
import urllib.error
|
| 5 |
from typing import List, Optional
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from openai import OpenAI
|
| 8 |
|
| 9 |
+
# ββ CRITICAL: Use ONLY validator-injected credentials. No fallbacks. ββββββββββ
|
| 10 |
+
API_BASE_URL = os.environ["API_BASE_URL"] # raises KeyError if not injected β intentional
|
| 11 |
+
API_KEY = os.environ["API_KEY"] # raises KeyError if not injected β intentional
|
| 12 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 13 |
+
ENV_BASE_URL = os.getenv("ENV_BASE_URL", "http://localhost:7860")
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
TASK_NAME = "schedule-optimization"
|
| 16 |
+
BENCHMARK = "cognitive-load-manager"
|
| 17 |
+
MAX_STEPS = 50
|
| 18 |
+
SUCCESS_SCORE_THRESHOLD = 0.5
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def post_json(url: str, payload: dict) -> dict:
|
| 22 |
+
data = json.dumps(payload).encode("utf-8")
|
| 23 |
+
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})
|
| 24 |
+
with urllib.request.urlopen(req) as res:
|
| 25 |
+
return json.loads(res.read().decode("utf-8"))
|
| 26 |
+
|
| 27 |
|
| 28 |
def log_start(task: str, env: str, model: str) -> None:
|
| 29 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 30 |
|
| 31 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 32 |
+
print(f"[STEP] step={step} action={action} reward={reward:.2f} done={str(done).lower()} error={error or 'null'}", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
| 35 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 36 |
print(f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}", flush=True)
|
| 37 |
|
| 38 |
+
|
| 39 |
def main():
|
| 40 |
+
# Initialize client using ONLY the validator-provided proxy credentials
|
| 41 |
+
client = OpenAI(
|
| 42 |
+
base_url=API_BASE_URL,
|
| 43 |
+
api_key=API_KEY,
|
| 44 |
+
)
|
| 45 |
|
|
|
|
| 46 |
task_id = os.getenv("CLM_LEVEL", "hard")
|
|
|
|
| 47 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 48 |
+
|
|
|
|
| 49 |
try:
|
| 50 |
data = post_json(f"{ENV_BASE_URL}/reset", {"task_id": task_id})
|
| 51 |
except Exception as e:
|
| 52 |
+
log_step(step=0, action="reset", reward=0.0, done=True, error=str(e)[:80])
|
| 53 |
log_end(success=False, steps=0, score=0.0, rewards=[])
|
| 54 |
return
|
| 55 |
+
|
| 56 |
+
session_id = data["session_id"]
|
| 57 |
observation = data["observation"]
|
| 58 |
+
done = False
|
| 59 |
+
step = 0
|
| 60 |
+
rewards = []
|
| 61 |
+
history = []
|
| 62 |
+
info = {}
|
| 63 |
+
|
|
|
|
| 64 |
while not done and step < MAX_STEPS:
|
| 65 |
step += 1
|
| 66 |
+
history_str = "\n".join(history[-5:]) if history else "No previous actions."
|
| 67 |
+
system_prompt = (
|
| 68 |
+
"You are an AI task scheduler managing cognitive load.\n"
|
| 69 |
+
"RULES:\n"
|
| 70 |
+
"1. If fatigue_level is 'high' or 'medium', output {\"type\": \"break\"}.\n"
|
| 71 |
+
"2. If stress_warning is true, output {\"type\": \"break\"}.\n"
|
| 72 |
+
"3. Find tasks where progress < 1.0 and output {\"type\": \"work\", \"task_id\": \"<id>\"}.\n"
|
| 73 |
+
"4. Respond ONLY with raw JSON. No markdown."
|
| 74 |
+
)
|
| 75 |
+
user_prompt = (
|
| 76 |
+
f"Previous 5 steps:\n{history_str}\n\n"
|
| 77 |
+
f"Observation:\n{json.dumps(observation, indent=2)}\n\n"
|
| 78 |
+
"Next action JSON?"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
action = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
error_msg = None
|
| 83 |
+
|
| 84 |
try:
|
| 85 |
+
completion = client.chat.completions.create(
|
| 86 |
model=MODEL_NAME,
|
| 87 |
messages=[
|
| 88 |
+
{"role": "system", "content": system_prompt},
|
| 89 |
+
{"role": "user", "content": user_prompt},
|
| 90 |
],
|
| 91 |
temperature=0.1,
|
| 92 |
+
max_tokens=150,
|
| 93 |
)
|
| 94 |
+
text = (completion.choices[0].message.content or "").strip()
|
| 95 |
+
text = text.removeprefix("```json").removesuffix("```").strip()
|
| 96 |
+
s, e = text.find("{"), text.rfind("}")
|
| 97 |
+
if s != -1 and e != -1:
|
| 98 |
+
action = json.loads(text[s:e+1])
|
| 99 |
+
except Exception as ex:
|
| 100 |
+
error_msg = str(ex)[:80]
|
| 101 |
+
|
| 102 |
+
# Fallback if LLM call failed or returned unparseable output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if not action:
|
| 104 |
+
tasks = observation.get("tasks", [])
|
| 105 |
+
incomp = [t for t in tasks if t.get("progress", 0.0) < 1.0]
|
| 106 |
+
fatigue = observation.get("visible_state", {}).get("fatigue_level", "low")
|
| 107 |
+
if fatigue in ("high", "medium"):
|
| 108 |
action = {"type": "break"}
|
| 109 |
elif incomp:
|
| 110 |
action = {"type": "work", "task_id": incomp[0]["id"]}
|
| 111 |
else:
|
| 112 |
action = {"type": "delay"}
|
| 113 |
|
| 114 |
+
action_str = json.dumps(action, separators=(",", ":"))
|
| 115 |
+
|
|
|
|
|
|
|
| 116 |
try:
|
| 117 |
+
step_data = post_json(f"{ENV_BASE_URL}/step", {"session_id": session_id, "action": action})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
observation = step_data["observation"]
|
| 119 |
+
reward = step_data.get("reward", 0.0)
|
| 120 |
+
done = step_data.get("done", False)
|
| 121 |
+
info = step_data.get("info", {})
|
| 122 |
+
except Exception as ex:
|
| 123 |
+
reward = 0.0
|
| 124 |
+
done = True
|
| 125 |
+
error_msg = error_msg or str(ex)[:80]
|
| 126 |
+
|
| 127 |
rewards.append(reward)
|
| 128 |
+
history.append(f"Step {step}: {action_str} -> reward={reward:.2f}")
|
| 129 |
log_step(step=step, action=action_str, reward=reward, done=done, error=error_msg)
|
| 130 |
|
| 131 |
+
score = info.get("final_score", 0.0)
|
| 132 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 133 |
log_end(success=success, steps=step, score=score, rewards=rewards)
|
| 134 |
|
| 135 |
+
|
| 136 |
if __name__ == "__main__":
|
| 137 |
main()
|