Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +116 -115
inference.py
CHANGED
|
@@ -1,116 +1,117 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import
|
| 4 |
-
from openai import OpenAI
|
| 5 |
-
|
| 6 |
-
# ==========================================
|
| 7 |
-
# 1. MANDATORY ENVIRONMENT VARIABLES
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
print(f"[
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
action_json =
|
| 83 |
-
action_log =
|
| 84 |
-
error =
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
reward = 0.0
|
| 94 |
-
done =
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
run_inference()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import urllib.request
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
# ==========================================
|
| 7 |
+
# 1. MANDATORY ENVIRONMENT VARIABLES
|
| 8 |
+
# ==========================================
|
| 9 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/")
|
| 10 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "gemini-2.5-flash")
|
| 11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
# ==========================================
|
| 14 |
+
# 2. ENVIRONMENT SETUP
|
| 15 |
+
# ==========================================
|
| 16 |
+
ENV_URL = "https://mark012-logisticsflow-openenv.hf.space"
|
| 17 |
+
TASK_NAME = "hard"
|
| 18 |
+
BENCHMARK = "LogisticsFlow-OpenEnv"
|
| 19 |
+
|
| 20 |
+
client = OpenAI(api_key=HF_TOKEN, base_url=API_BASE_URL)
|
| 21 |
+
|
| 22 |
+
# ==========================================
|
| 23 |
+
# 3. STRICT LOGGING FORMATTERS
|
| 24 |
+
# ==========================================
|
| 25 |
+
def log_start(task: str, env: str, model: str) -> None:
|
| 26 |
+
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 27 |
+
|
| 28 |
+
def log_step(step: int, action: str, reward: float, done: bool, error: str) -> None:
|
| 29 |
+
error_val = error if error else "null"
|
| 30 |
+
done_val = str(done).lower()
|
| 31 |
+
print(f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}", flush=True)
|
| 32 |
+
|
| 33 |
+
def log_end(success: bool, steps: int, score: float, rewards: list) -> None:
|
| 34 |
+
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 35 |
+
print(f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}", flush=True)
|
| 36 |
+
|
| 37 |
+
# ==========================================
|
| 38 |
+
# 4. NETWORK HELPER (No 'requests' library needed)
|
| 39 |
+
# ==========================================
|
| 40 |
+
def send_post_request(url: str, payload: dict) -> dict:
|
| 41 |
+
data = json.dumps(payload).encode('utf-8')
|
| 42 |
+
req = urllib.request.Request(url, data=data, headers={'Content-Type': 'application/json'})
|
| 43 |
+
with urllib.request.urlopen(req) as response:
|
| 44 |
+
return json.loads(response.read().decode('utf-8'))
|
| 45 |
+
|
| 46 |
+
# ==========================================
|
| 47 |
+
# 5. INFERENCE LOOP
|
| 48 |
+
# ==========================================
|
| 49 |
+
def run_inference():
|
| 50 |
+
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 51 |
+
|
| 52 |
+
# Reset Environment
|
| 53 |
+
try:
|
| 54 |
+
obs = send_post_request(f"{ENV_URL}/reset", {"level": TASK_NAME})
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Failed to connect to environment: {e}")
|
| 57 |
+
# The prompt instructed us to wrap risky operations. If it fails, we gracefully exit.
|
| 58 |
+
return
|
| 59 |
+
|
| 60 |
+
rewards = []
|
| 61 |
+
steps_taken = 0
|
| 62 |
+
success = False
|
| 63 |
+
|
| 64 |
+
for step in range(1, 21): # Max 20 steps
|
| 65 |
+
current_state = obs.get("observation", obs) if isinstance(obs, dict) else obs
|
| 66 |
+
|
| 67 |
+
# Prepare prompt
|
| 68 |
+
system_prompt = "You are an AI logistics agent. Analyze the state. You must output exactly valid JSON. Actions: {'command': 'ship', 'params': {'order_id': 'ORD-XYZ', 'carrier': 'Standard'}} OR {'command': 'restock', 'params': {'item': 'Electronics'}}."
|
| 69 |
+
user_prompt = f"Current State: {json.dumps(current_state)}"
|
| 70 |
+
|
| 71 |
+
# Get Model Action
|
| 72 |
+
try:
|
| 73 |
+
response = client.chat.completions.create(
|
| 74 |
+
model=MODEL_NAME,
|
| 75 |
+
messages=[
|
| 76 |
+
{"role": "system", "content": system_prompt},
|
| 77 |
+
{"role": "user", "content": user_prompt}
|
| 78 |
+
],
|
| 79 |
+
response_format={"type": "json_object"} # Forces valid JSON
|
| 80 |
+
)
|
| 81 |
+
action_str = response.choices[0].message.content.strip()
|
| 82 |
+
action_json = json.loads(action_str)
|
| 83 |
+
action_log = action_str.replace('\n', '').replace(' ', '') # Flatten for logging
|
| 84 |
+
error = None
|
| 85 |
+
except Exception as e:
|
| 86 |
+
action_json = {"command": "wait", "params": {}}
|
| 87 |
+
action_log = "error"
|
| 88 |
+
error = str(e)
|
| 89 |
+
|
| 90 |
+
# Step the Environment
|
| 91 |
+
try:
|
| 92 |
+
obs = send_post_request(f"{ENV_URL}/step", action_json)
|
| 93 |
+
reward = obs.get("reward", 0.0)
|
| 94 |
+
done = obs.get("done", False)
|
| 95 |
+
except Exception as e:
|
| 96 |
+
reward = 0.0
|
| 97 |
+
done = True
|
| 98 |
+
error = str(e)
|
| 99 |
+
|
| 100 |
+
rewards.append(reward)
|
| 101 |
+
steps_taken = step
|
| 102 |
+
|
| 103 |
+
# Log strictly
|
| 104 |
+
log_step(step=step, action=action_log, reward=reward, done=done, error=error)
|
| 105 |
+
|
| 106 |
+
if done:
|
| 107 |
+
break
|
| 108 |
+
|
| 109 |
+
# Calculate final score (clamp between 0 and 1 for judges)
|
| 110 |
+
score = sum(rewards)
|
| 111 |
+
normalized_score = min(max(score, 0.0), 1.0)
|
| 112 |
+
success = normalized_score > 0.0
|
| 113 |
+
|
| 114 |
+
log_end(success=success, steps=steps_taken, score=normalized_score, rewards=rewards)
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
run_inference()
|