Update inference.py
Browse files- inference.py +126 -124
inference.py
CHANGED
|
@@ -1,128 +1,130 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
| 3 |
from env import TrafficEnv
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
|
| 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 |
-
Current state:
|
| 61 |
-
{state}
|
| 62 |
-
|
| 63 |
-
Available actions:
|
| 64 |
-
0 = keep current signal phase
|
| 65 |
-
1 = switch signal phase
|
| 66 |
-
|
| 67 |
-
Reply with only one number: 0 or 1
|
| 68 |
-
""".strip()
|
| 69 |
-
|
| 70 |
-
response = client.chat.completions.create(
|
| 71 |
-
model=model_name,
|
| 72 |
-
messages=[
|
| 73 |
-
{"role": "system", "content": "Reply with only 0 or 1."},
|
| 74 |
-
{"role": "user", "content": prompt},
|
| 75 |
-
],
|
| 76 |
-
temperature=0,
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
content = response.choices[0].message.content.strip()
|
| 80 |
-
|
| 81 |
-
try:
|
| 82 |
-
action = int(content)
|
| 83 |
-
if action not in (0, 1):
|
| 84 |
-
action = 0
|
| 85 |
-
except Exception:
|
| 86 |
-
action = 0
|
| 87 |
-
|
| 88 |
-
return action
|
| 89 |
-
|
| 90 |
-
def run_task(task_name, config, client, model_name, use_llm):
|
| 91 |
-
env = TrafficEnv(config)
|
| 92 |
state = env.reset()
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
("
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
for task_name
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
from env import TrafficEnv
|
| 5 |
+
from tasks import get_config
|
| 6 |
+
from baseline_agent import RuleBasedAgent
|
| 7 |
+
import os
|
| 8 |
+
import openai
|
| 9 |
+
|
| 10 |
+
class LLMAgent:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
try:
|
| 13 |
+
self.client = openai.OpenAI(
|
| 14 |
+
base_url=os.environ["API_BASE_URL"],
|
| 15 |
+
api_key=os.environ["API_KEY"]
|
| 16 |
+
)
|
| 17 |
+
except Exception:
|
| 18 |
+
self.client = None
|
| 19 |
+
self.fallback = RuleBasedAgent()
|
| 20 |
+
|
| 21 |
+
def select_action(self, state):
|
| 22 |
+
prompt = f"Traffic state: {state}. Reply with 1 to switch phase, 0 to keep phase. Output only 1 or 0."
|
| 23 |
+
try:
|
| 24 |
+
response = self.client.chat.completions.create(
|
| 25 |
+
model="gpt-3.5-turbo",
|
| 26 |
+
messages=[
|
| 27 |
+
{"role": "system", "content": "You are a traffic signal controller."},
|
| 28 |
+
{"role": "user", "content": prompt}
|
| 29 |
+
],
|
| 30 |
+
max_tokens=5,
|
| 31 |
+
temperature=0.0
|
| 32 |
+
)
|
| 33 |
+
content = response.choices[0].message.content.strip()
|
| 34 |
+
# Still call fallback to maintain its internal step counter
|
| 35 |
+
self.fallback.select_action(state)
|
| 36 |
+
|
| 37 |
+
if "1" in content:
|
| 38 |
+
return 1
|
| 39 |
+
else:
|
| 40 |
+
return 0
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return self.fallback.select_action(state)
|
| 43 |
+
|
| 44 |
+
def reset(self):
|
| 45 |
+
self.fallback.reset()
|
| 46 |
+
|
| 47 |
+
app = FastAPI()
|
| 48 |
+
env = TrafficEnv(get_config("medium"))
|
| 49 |
+
agent = LLMAgent()
|
| 50 |
+
|
| 51 |
+
class Action(BaseModel):
|
| 52 |
+
action: int
|
| 53 |
+
|
| 54 |
+
@app.get("/", response_class=HTMLResponse)
|
| 55 |
+
def root():
|
| 56 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 57 |
+
return f.read()
|
| 58 |
+
|
| 59 |
+
@app.post("/reset")
|
| 60 |
+
def reset():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
state = env.reset()
|
| 62 |
+
try:
|
| 63 |
+
state = state.tolist()
|
| 64 |
+
except:
|
| 65 |
+
pass
|
| 66 |
+
agent.reset()
|
| 67 |
+
return {"state":state}
|
| 68 |
+
|
| 69 |
+
@app.post("/step")
|
| 70 |
+
def step(data:Action):
|
| 71 |
+
state,reward,done,info = env.step(data.action)
|
| 72 |
+
try:
|
| 73 |
+
state = state.tolist()
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
return {
|
| 77 |
+
"state":state,
|
| 78 |
+
"reward":reward,
|
| 79 |
+
"done":done,
|
| 80 |
+
"info":info
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
@app.post("/auto_step")
|
| 84 |
+
def auto_step():
|
| 85 |
+
state_dict = env.get_state()
|
| 86 |
+
action = agent.select_action(state_dict)
|
| 87 |
+
state,reward,done,info = env.step(action)
|
| 88 |
+
try:
|
| 89 |
+
state = state.tolist()
|
| 90 |
+
except:
|
| 91 |
+
pass
|
| 92 |
+
return {
|
| 93 |
+
"state":state,
|
| 94 |
+
"reward":reward,
|
| 95 |
+
"done":done,
|
| 96 |
+
"info":info,
|
| 97 |
+
"action_taken": action
|
| 98 |
+
}
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
import sys
|
| 102 |
+
tasks_to_run = ["easy", "medium", "hard"]
|
| 103 |
+
if len(sys.argv) > 1:
|
| 104 |
+
# e.g., if validator optionally passes a task name as argument
|
| 105 |
+
task_arg = sys.argv[1].replace("--task=", "").replace("--task", "")
|
| 106 |
+
if task_arg in tasks_to_run:
|
| 107 |
+
tasks_to_run = [task_arg]
|
| 108 |
+
|
| 109 |
+
for task_name in tasks_to_run:
|
| 110 |
+
config = get_config(task_name)
|
| 111 |
+
eval_env = TrafficEnv(config)
|
| 112 |
+
eval_agent = LLMAgent()
|
| 113 |
+
|
| 114 |
+
state = eval_env.reset()
|
| 115 |
+
eval_agent.reset()
|
| 116 |
+
|
| 117 |
+
print("[START]", flush=True)
|
| 118 |
+
|
| 119 |
+
done = False
|
| 120 |
+
step_idx = 0
|
| 121 |
+
total_reward = 0.0
|
| 122 |
+
|
| 123 |
+
while not done:
|
| 124 |
+
action = eval_agent.select_action(state)
|
| 125 |
+
state, reward, done, info = eval_env.step(action)
|
| 126 |
+
print(f"[STEP] step={step_idx}, reward={reward}, done={done}", flush=True)
|
| 127 |
+
step_idx += 1
|
| 128 |
+
total_reward += reward
|
| 129 |
+
|
| 130 |
+
print("[END]", flush=True)
|