CoolFace
Apppublic

Forest-Fire/gridworld-openenv

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes
inference.py56 linesDownload Raw Back to root
1import os
2import requests
3import numpy as np
4
5API_BASE_URL = os.getenv("API_BASE_URL")
6
7tasks = ["easy", "medium", "hard"]
8
9print("[START]")
10
11for task in tasks:
12
13    # Reset env
14    res = requests.post(f"{API_BASE_URL}/reset", params={"task": task})
15
16    if res.status_code != 200:
17        print("[ERROR]", res.text)
18        continue
19
20    state = res.json()["state"]
21
22    total_steps = 0
23    reached_goal = False
24
25    for step in range(100):
26
27        action = np.random.randint(4)
28
29        res = requests.post(
30            f"{API_BASE_URL}/step",
31            params={"task": task},
32            json={"action": action}
33        )
34
35        # ✅ Safety check (VERY IMPORTANT)
36        if res.status_code != 200:
37            print("[ERROR]", res.text)
38            break
39
40        data = res.json()
41
42        print(f"[STEP] task={task} step={step} action={action} reward={data['reward']}")
43
44        state = data["state"]
45        total_steps += 1
46
47        # ✅ Handle BOTH cases
48        if data["terminated"] or data["truncated"]:
49            if data["terminated"]:
50                reached_goal = True
51            break
52
53    print(f"[STEP] task={task} completed steps={total_steps}")
54
55print("[END]")
56