Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- inference.py +17 -16
inference.py
CHANGED
|
@@ -91,10 +91,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) -> TrafficAction:
|
| 95 |
"""Call LLM for decision."""
|
| 96 |
resp = client.chat.completions.create(
|
| 97 |
-
model=
|
| 98 |
messages=[
|
| 99 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 100 |
{"role": "user", "content": _build_prompt(obs, step)},
|
|
@@ -107,9 +107,9 @@ def get_llm_action(client: OpenAI, obs: TrafficObservation, step: int) -> Traffi
|
|
| 107 |
return TrafficAction(light_phase=phase)
|
| 108 |
|
| 109 |
|
| 110 |
-
def run_task(task: str, client: OpenAI) -> dict:
|
| 111 |
"""Run a single task episode."""
|
| 112 |
-
print(f'[START] task={task} env=traffic_control model={
|
| 113 |
|
| 114 |
rewards: List[float] = []
|
| 115 |
step = 0
|
|
@@ -122,7 +122,7 @@ def run_task(task: str, client: OpenAI) -> dict:
|
|
| 122 |
|
| 123 |
while not obs.done:
|
| 124 |
step += 1
|
| 125 |
-
action = get_llm_action(client, obs, step)
|
| 126 |
action_str = f"light_phase={action.light_phase}"
|
| 127 |
|
| 128 |
try:
|
|
@@ -169,22 +169,23 @@ def run_task(task: str, client: OpenAI) -> dict:
|
|
| 169 |
# ---------------------------------------------------------------------------
|
| 170 |
|
| 171 |
def main():
|
| 172 |
-
"""Main entry point."""
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
| 183 |
print(f"[INIT] Client ready", flush=True)
|
| 184 |
|
| 185 |
tasks = ["basic_flow", "emergency_priority", "dynamic_scenarios"]
|
| 186 |
for task in tasks:
|
| 187 |
-
run_task(task, client)
|
| 188 |
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
|
|
|
| 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 |
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 |
|
| 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 |
# ---------------------------------------------------------------------------
|
| 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__":
|