Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- inference.py +21 -8
inference.py
CHANGED
|
@@ -51,6 +51,7 @@ except ImportError:
|
|
| 51 |
# Configuration — read from env at import time (matches sample script pattern)
|
| 52 |
# ---------------------------------------------------------------------------
|
| 53 |
|
|
|
|
| 54 |
API_BASE_URL = os.environ["API_BASE_URL"]
|
| 55 |
API_KEY = os.environ["API_KEY"]
|
| 56 |
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4.1-mini")
|
|
@@ -161,6 +162,7 @@ def _rule_based_action(obs: TrafficObservation) -> TrafficAction:
|
|
| 161 |
# ---------------------------------------------------------------------------
|
| 162 |
|
| 163 |
def get_llm_action(client: OpenAI, obs: TrafficObservation) -> TrafficAction:
|
|
|
|
| 164 |
resp = client.chat.completions.create(
|
| 165 |
model=MODEL_NAME,
|
| 166 |
messages=[
|
|
@@ -171,6 +173,7 @@ def get_llm_action(client: OpenAI, obs: TrafficObservation) -> TrafficAction:
|
|
| 171 |
max_tokens=MAX_TOKENS,
|
| 172 |
stream=False,
|
| 173 |
)
|
|
|
|
| 174 |
data_str = (resp.choices[0].message.content or "").strip()
|
| 175 |
match = re.search(r'\{[^}]*\}', data_str.replace('\n', ' '))
|
| 176 |
data = json.loads(match.group(0) if match else data_str)
|
|
@@ -275,20 +278,30 @@ def run_task(task_id: str, client: OpenAI) -> None:
|
|
| 275 |
# ---------------------------------------------------------------------------
|
| 276 |
|
| 277 |
def main() -> None:
|
|
|
|
| 278 |
print(
|
| 279 |
-
f"[CONFIG] API_BASE_URL={API_BASE_URL}
|
| 280 |
-
f"
|
|
|
|
| 281 |
flush=True,
|
| 282 |
)
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
"The validator must inject API_KEY as an environment variable."
|
| 288 |
-
)
|
| 289 |
|
| 290 |
# Create the OpenAI client once using module-level env vars
|
| 291 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
for task in ["basic_flow", "emergency_priority", "dynamic_scenarios"]:
|
| 294 |
run_task(task, client)
|
|
|
|
| 51 |
# Configuration — read from env at import time (matches sample script pattern)
|
| 52 |
# ---------------------------------------------------------------------------
|
| 53 |
|
| 54 |
+
# CRITICAL: Use exact syntax validator requires for static analysis
|
| 55 |
API_BASE_URL = os.environ["API_BASE_URL"]
|
| 56 |
API_KEY = os.environ["API_KEY"]
|
| 57 |
MODEL_NAME = os.environ.get("MODEL_NAME", "gpt-4.1-mini")
|
|
|
|
| 162 |
# ---------------------------------------------------------------------------
|
| 163 |
|
| 164 |
def get_llm_action(client: OpenAI, obs: TrafficObservation) -> TrafficAction:
|
| 165 |
+
print(f"[DEBUG] Making LLM call to {API_BASE_URL} with model {MODEL_NAME}", flush=True)
|
| 166 |
resp = client.chat.completions.create(
|
| 167 |
model=MODEL_NAME,
|
| 168 |
messages=[
|
|
|
|
| 173 |
max_tokens=MAX_TOKENS,
|
| 174 |
stream=False,
|
| 175 |
)
|
| 176 |
+
print(f"[DEBUG] LLM response received", flush=True)
|
| 177 |
data_str = (resp.choices[0].message.content or "").strip()
|
| 178 |
match = re.search(r'\{[^}]*\}', data_str.replace('\n', ' '))
|
| 179 |
data = json.loads(match.group(0) if match else data_str)
|
|
|
|
| 278 |
# ---------------------------------------------------------------------------
|
| 279 |
|
| 280 |
def main() -> None:
|
| 281 |
+
# Debug: show env var status with explicit length check
|
| 282 |
print(
|
| 283 |
+
f"[CONFIG] API_BASE_URL={API_BASE_URL} (len={len(API_BASE_URL)}) "
|
| 284 |
+
f"API_KEY={API_KEY[:10]}... (len={len(API_KEY)}) "
|
| 285 |
+
f"MODEL_NAME={MODEL_NAME}",
|
| 286 |
flush=True,
|
| 287 |
)
|
| 288 |
+
|
| 289 |
+
# Ensure env vars are not empty
|
| 290 |
+
if not API_BASE_URL or not API_KEY:
|
| 291 |
+
raise SystemExit(f"[FATAL] Empty env vars: API_BASE_URL='{API_BASE_URL}', API_KEY empty={not API_KEY}")
|
|
|
|
|
|
|
| 292 |
|
| 293 |
# Create the OpenAI client once using module-level env vars
|
| 294 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 295 |
+
|
| 296 |
+
# Verify the client is configured correctly by making a test call
|
| 297 |
+
try:
|
| 298 |
+
_ = client.chat.completions.create(
|
| 299 |
+
model=MODEL_NAME,
|
| 300 |
+
messages=[{"role": "user", "content": "test"}],
|
| 301 |
+
max_tokens=1,
|
| 302 |
+
)
|
| 303 |
+
except Exception as e:
|
| 304 |
+
print(f"[WARN] Test call failed: {e}", flush=True)
|
| 305 |
|
| 306 |
for task in ["basic_flow", "emergency_priority", "dynamic_scenarios"]:
|
| 307 |
run_task(task, client)
|