amanmurari commited on
Commit
044eac2
·
verified ·
1 Parent(s): 63990c5

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. inference.py +16 -36
inference.py CHANGED
@@ -1,26 +1,17 @@
1
- """
2
- Inference Script — Autonomous Traffic Control OpenEnv Environment
3
- Strictly follows validator spec from hackathon guidelines.
4
- """
5
 
6
  import os
7
  import sys
8
  import json
9
  from typing import List, Optional
10
 
11
- # Allow running from repo root or from traffic_control/ subdirectory
12
  _HERE = os.path.dirname(os.path.abspath(__file__))
13
  _PARENT = os.path.dirname(_HERE)
14
  for _p in (_HERE, _PARENT):
15
  if _p not in sys.path:
16
  sys.path.insert(0, _p)
17
 
18
- try:
19
- from dotenv import load_dotenv
20
- load_dotenv()
21
- except ImportError:
22
- pass
23
-
24
  from openai import OpenAI
25
 
26
  try:
@@ -30,14 +21,10 @@ except ImportError:
30
  from client import TrafficControlEnv # type: ignore
31
  from models import TrafficAction, TrafficObservation # type: ignore
32
 
33
- # ---------------------------------------------------------------------------
34
- # Configuration - EXACTLY per spec: defaults for API_BASE_URL and MODEL_NAME
35
- # ---------------------------------------------------------------------------
36
-
37
  API_BASE_URL = os.environ["API_BASE_URL"]
38
- MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
39
  API_KEY = os.environ["API_KEY"]
40
-
41
  SERVER_URL = os.getenv("SERVER_URL", "http://localhost:7860")
42
  SEED = 42
43
  MAX_TOKENS = 64
@@ -91,10 +78,10 @@ def _parse_phase(raw: str) -> int:
91
  return int(m.group(1)) if m else 0
92
 
93
 
94
- def get_llm_action(client: OpenAI, obs: TrafficObservation, step: int, model: str) -> TrafficAction:
95
  """Call LLM for decision."""
96
  resp = client.chat.completions.create(
97
- model=model,
98
  messages=[
99
  {"role": "system", "content": SYSTEM_PROMPT},
100
  {"role": "user", "content": _build_prompt(obs, step)},
@@ -107,9 +94,9 @@ def get_llm_action(client: OpenAI, obs: TrafficObservation, step: int, model: st
107
  return TrafficAction(light_phase=phase)
108
 
109
 
110
- def run_task(task: str, client: OpenAI, model: str) -> dict:
111
  """Run a single task episode."""
112
- print(f'[START] task={task} env=traffic_control model={model}', flush=True)
113
 
114
  rewards: List[float] = []
115
  step = 0
@@ -122,7 +109,7 @@ def run_task(task: str, client: OpenAI, model: str) -> dict:
122
 
123
  while not obs.done:
124
  step += 1
125
- action = get_llm_action(client, obs, step, model)
126
  action_str = f"light_phase={action.light_phase}"
127
 
128
  try:
@@ -169,23 +156,16 @@ def run_task(task: str, client: OpenAI, model: str) -> dict:
169
  # ---------------------------------------------------------------------------
170
 
171
  def main():
172
- """Main entry point - reads env vars directly as validator requires."""
173
- # CRITICAL: Read environment variables directly here for validator detection
174
- api_base = os.environ["API_BASE_URL"]
175
- api_key = os.environ["API_KEY"]
176
- model = os.getenv("MODEL_NAME", "gpt-4o-mini")
177
-
178
- print(f"[INIT] API_BASE_URL={api_base[:30]}...", flush=True)
179
- print(f"[INIT] API_KEY present={bool(api_key)}", flush=True)
180
- print(f"[INIT] MODEL_NAME={model}", flush=True)
181
-
182
- # Initialize OpenAI client with directly-read env vars
183
- client = OpenAI(base_url=api_base, api_key=api_key)
184
- print(f"[INIT] Client ready", flush=True)
185
 
186
  tasks = ["basic_flow", "emergency_priority", "dynamic_scenarios"]
187
  for task in tasks:
188
- run_task(task, client, model)
189
 
190
 
191
  if __name__ == "__main__":
 
1
+ """Inference Script — Autonomous Traffic Control OpenEnv Environment"""
 
 
 
2
 
3
  import os
4
  import sys
5
  import json
6
  from typing import List, Optional
7
 
8
+ # Allow imports from repo root
9
  _HERE = os.path.dirname(os.path.abspath(__file__))
10
  _PARENT = os.path.dirname(_HERE)
11
  for _p in (_HERE, _PARENT):
12
  if _p not in sys.path:
13
  sys.path.insert(0, _p)
14
 
 
 
 
 
 
 
15
  from openai import OpenAI
16
 
17
  try:
 
21
  from client import TrafficControlEnv # type: ignore
22
  from models import TrafficAction, TrafficObservation # type: ignore
23
 
24
+ # Environment variables - read per validator spec
 
 
 
25
  API_BASE_URL = os.environ["API_BASE_URL"]
 
26
  API_KEY = os.environ["API_KEY"]
27
+ MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
28
  SERVER_URL = os.getenv("SERVER_URL", "http://localhost:7860")
29
  SEED = 42
30
  MAX_TOKENS = 64
 
78
  return int(m.group(1)) if m else 0
79
 
80
 
81
+ def get_llm_action(client: OpenAI, obs: TrafficObservation, step: int) -> TrafficAction:
82
  """Call LLM for decision."""
83
  resp = client.chat.completions.create(
84
+ model=MODEL_NAME,
85
  messages=[
86
  {"role": "system", "content": SYSTEM_PROMPT},
87
  {"role": "user", "content": _build_prompt(obs, step)},
 
94
  return TrafficAction(light_phase=phase)
95
 
96
 
97
+ def run_task(task: str, client: OpenAI) -> dict:
98
  """Run a single task episode."""
99
+ print(f'[START] task={task} env=traffic_control model={MODEL_NAME}', flush=True)
100
 
101
  rewards: List[float] = []
102
  step = 0
 
109
 
110
  while not obs.done:
111
  step += 1
112
+ action = get_llm_action(client, obs, step)
113
  action_str = f"light_phase={action.light_phase}"
114
 
115
  try:
 
156
  # ---------------------------------------------------------------------------
157
 
158
  def main():
159
+ """Main entry point."""
160
+ # Initialize OpenAI client with environment variables
161
+ client = OpenAI(
162
+ base_url=os.environ["API_BASE_URL"],
163
+ api_key=os.environ["API_KEY"]
164
+ )
 
 
 
 
 
 
 
165
 
166
  tasks = ["basic_flow", "emergency_priority", "dynamic_scenarios"]
167
  for task in tasks:
168
+ run_task(task, client)
169
 
170
 
171
  if __name__ == "__main__":