Spaces:
Sleeping
Sleeping
Subhash Vadaparthi commited on
Commit Β·
bd38df2
1
Parent(s): 975ea1e
Fix inference output formatting and environment defaults for Hackathon compliance
Browse files29 unmodified lines303132333435363722 unmodified lines606162636465666768697076 unmodified lines14714814915015115215315466 unmodified lines22122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329 unmodified linesfrom tasks import TaskGrader# ββ Environment variables (per submission guidelines) ββββββββββββββββAPI_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")HF_TOKEN = os.getenv("HF_TOKEN")if HF_TOKEN is None:22 unmodified lines)def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:"""Emit [END] line after episode ends."""rewards_str = ",".join(f"{r:.2f}" for r in rewards)print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}",flush=True,)76 unmodified linestext = text.strip()return json.loads(text)except Exception as e:print(f"[DEBUG] LLM error: {e}", flush=True)# Smart fallback: try to find an available slot for a waiting patientreturn _fallback_action(obs)66 unmodified lines"current_step": obs.current_step,}# ββ Run the agent loop and collect step data ββsteps_taken = 0rewards: List[float] = []actions: List[str] = []dones: List[bool] = []errors: List[Optional[str]] = []action_history: List[Dict[str, Any]] = [] # memory for the LLMwhile not env.done and steps_taken < MAX_STEPS:action_dict = get_action_from_llm(obs_dict, action_history)# Step using typed actionaction = HealthcareAction(type=action_dict.get("type", "waitlist"),patient_id=action_dict.get("patient_id", 0),doctor_id=action_dict.get("doctor_id"),slot_id=action_dict.get("slot_id"),)obs = env.step(action)obs_dict = {"doctor_slots": obs.doctor_slots,"patients": obs.patients,"waiting_queue": obs.waiting_queue,"current_step": obs.current_step,}step_error = obs.info.get("error") if obs.info else Nonestep_action_type = action_dict.get("type", "waitlist")steps_taken += 1rewards.append(obs.reward)actions.append(step_action_type)dones.append(obs.done)errors.append(step_error)# Track history so LLM can learn from mistakesaction_history.append({"action_str": action_to_str(action_dict),"reward": obs.reward,"error": step_error,})# ββ Grade all tasks ββgrader = TaskGrader(env)# ββ Emit one structured [START]/[STEP]/[END] block per task ββfor task_def in task_defs:task_name = task_def["name"]grade_fn = getattr(grader, task_def["grader"])task_score = min(max(grade_fn(), 0.01), 0.99) # strictly between (0, 1)success = task_score > 0.0log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)for i in range(steps_taken):log_step(step=i + 1,action=actions[i],reward=rewards[i],done=dones[i],error=errors[i],)log_end(success=success, steps=steps_taken, score=task_score, rewards=rewards)sys.exit(0)if __name__ == "__main__":29 unmodified lines303132333435363722 unmodified lines606162636465666768697076 unmodified lines14714814915015115215315466 unmodified lines22122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829929 unmodified linesfrom tasks import TaskGrader# ββ Environment variables (per submission guidelines) ββββββββββββββββAPI_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4.1-mini")HF_TOKEN = os.getenv("HF_TOKEN")if HF_TOKEN is None:22 unmodified lines)def log_end(success: bool, steps: int, rewards: List[float]) -> None:"""Emit [END] line after episode ends."""rewards_str = ",".join(f"{r:.2f}" for r in rewards)print(f"[END] success={str(success).lower()} steps={steps} rewards={rewards_str}",flush=True,)76 unmodified linestext = text.strip()return json.loads(text)except Exception:# Silently log as fallback; do not print debug info to STDOUT# Smart fallback: try to find an available slot for a waiting patientreturn _fallback_action(obs)66 unmodified lines"current_step": obs.current_step,}try:# ββ Run the agent loop and collect step data ββsteps_taken = 0rewards: List[float] = []actions: List[str] = []dones: List[bool] = []errors: List[Optional[str]] = []action_history: List[Dict[str, Any]] = [] # memory for the LLMwhile not env.done and steps_taken < MAX_STEPS:action_dict = get_action_from_llm(obs_dict, action_history)# Step using typed actionaction = HealthcareAction(type=action_dict.get("type", "waitlist"),patient_id=action_dict.get("patient_id", 0),doctor_id=action_dict.get("doctor_id"),slot_id=action_dict.get("slot_id"),)obs = env.step(action)obs_dict = {"doctor_slots": obs.doctor_slots,"patients": obs.patients,"waiting_queue": obs.waiting_queue,"current_step": obs.current_step,}step_error = obs.info.get("error") if obs.info else Nonestep_action_type = action_dict.get("type", "waitlist")steps_taken += 1rewards.append(obs.reward)actions.append(step_action_type)dones.append(obs.done)errors.append(step_error)# Track history so LLM can learn from mistakesaction_history.append({"action_str": action_to_str(action_dict),"reward": obs.reward,"error": step_error,})# ββ Grade all tasks ββgrader = TaskGrader(env)# ββ Emit one structured [START]/[STEP]/[END] block per task ββfor task_def in task_defs:task_name = task_def["name"]grade_fn = getattr(grader, task_def["grader"])task_score = min(max(grade_fn(), 0.01), 0.99) # strictly between (0, 1)success = task_score > 0.0log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)for i in range(steps_taken):log_step(step=i + 1,action=actions[i],reward=rewards[i],done=dones[i],error=errors[i],)log_end(success=success, steps=steps_taken, rewards=rewards)except Exception as e:# Ensure at least one [END] line if everything crashes before reporting# This is a safety measure to meet the "always emitted" requirementlog_end(success=False, steps=0, rewards=[0.0])finally:sys.exit(0)if __name__ == "__main__":
inference.py+77-71