TanujInsane commited on
Commit ·
53c6dcf
1
Parent(s): 1b64cdd
fix: add wait_for_server retry loop for phase 2
Browse files- inference.py +30 -67
inference.py
CHANGED
|
@@ -1,20 +1,26 @@
|
|
| 1 |
-
import os
|
| 2 |
import sys
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
"
|
| 10 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def run_inference(difficulty="easy", seed=42):
|
| 13 |
-
print(f"Connecting to
|
| 14 |
-
|
| 15 |
start_time = time.time()
|
| 16 |
-
|
| 17 |
-
# 1. Reset the environment
|
| 18 |
reset_payload = {"difficulty": difficulty, "seed": seed}
|
| 19 |
try:
|
| 20 |
reset_response = requests.post(f"{API_BASE_URL}/reset", json=reset_payload, timeout=60)
|
|
@@ -22,14 +28,8 @@ def run_inference(difficulty="easy", seed=42):
|
|
| 22 |
obs = reset_response.json().get("observation") or {}
|
| 23 |
except Exception as e:
|
| 24 |
print(f"Error during reset: {e}")
|
| 25 |
-
return {
|
| 26 |
-
|
| 27 |
-
"score": 0.0,
|
| 28 |
-
"time": time.time() - start_time,
|
| 29 |
-
"error": str(e)
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
# Load model for intelligent classification
|
| 33 |
try:
|
| 34 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 35 |
from baseline_inference import load_or_train
|
|
@@ -37,13 +37,11 @@ def run_inference(difficulty="easy", seed=42):
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Failed to load model: {e}")
|
| 39 |
model = None
|
| 40 |
-
|
| 41 |
done = False
|
| 42 |
total_reward = 0.0
|
| 43 |
-
|
| 44 |
-
# 2. Step through the environment
|
| 45 |
while not done:
|
| 46 |
-
# Fetch current state as requested before each step
|
| 47 |
try:
|
| 48 |
state_response = requests.get(f"{API_BASE_URL}/state", timeout=60)
|
| 49 |
state_response.raise_for_status()
|
|
@@ -52,75 +50,40 @@ def run_inference(difficulty="easy", seed=42):
|
|
| 52 |
print(f"Error fetching state: {e}")
|
| 53 |
state_data = {}
|
| 54 |
|
| 55 |
-
# Use the actual document text; fallback to obs if not strictly inside /state
|
| 56 |
content = state_data.get("content")
|
| 57 |
-
|
| 58 |
if not content:
|
| 59 |
-
|
| 60 |
-
content = obs_data.get("content")
|
| 61 |
-
|
| 62 |
if not content:
|
| 63 |
content = obs.get("content", "")
|
| 64 |
-
|
| 65 |
-
# Intelligent classification decision
|
| 66 |
action = 0
|
| 67 |
if model is not None and content:
|
| 68 |
try:
|
| 69 |
action = int(model.predict([str(content)])[0])
|
| 70 |
except Exception as e:
|
| 71 |
-
print(f"
|
| 72 |
action = 0
|
| 73 |
-
|
| 74 |
-
step_payload = {"action": action}
|
| 75 |
try:
|
| 76 |
-
step_response = requests.post(f"{API_BASE_URL}/step", json=
|
| 77 |
step_response.raise_for_status()
|
| 78 |
result = step_response.json() or {}
|
| 79 |
except Exception as e:
|
| 80 |
print(f"Error during step: {e}")
|
| 81 |
-
return {
|
| 82 |
-
|
| 83 |
-
"score": total_reward,
|
| 84 |
-
"time": time.time() - start_time,
|
| 85 |
-
"error": str(e)
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
obs = result.get("observation") or {}
|
| 89 |
reward = result.get("reward", 0.0)
|
| 90 |
done = result.get("done", False)
|
| 91 |
-
info = result.get("info") or {}
|
| 92 |
-
|
| 93 |
total_reward += reward
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
return {
|
| 98 |
-
"difficulty": difficulty,
|
| 99 |
-
"score": total_reward,
|
| 100 |
-
"time": elapsed
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
def wait_for_server(url, max_retries=30, delay=2):
|
| 104 |
-
print(f"Waiting for server at {url} to become reachable...")
|
| 105 |
-
for i in range(max_retries):
|
| 106 |
-
try:
|
| 107 |
-
response = requests.get(url, timeout=5)
|
| 108 |
-
print("Server is up and connected!")
|
| 109 |
-
return True
|
| 110 |
-
except Exception as e:
|
| 111 |
-
print(f"Server not ready, retrying in {delay} seconds... ({i+1}/{max_retries})")
|
| 112 |
-
time.sleep(delay)
|
| 113 |
-
|
| 114 |
-
print("Max retries reached. Server did not start in time.")
|
| 115 |
-
return False
|
| 116 |
|
| 117 |
def main():
|
| 118 |
try:
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
print("Exiting because server failed to start.")
|
| 122 |
sys.exit(1)
|
| 123 |
-
|
| 124 |
result = run_inference()
|
| 125 |
print("Inference completed:", result)
|
| 126 |
except Exception as e:
|
|
|
|
| 1 |
+
import os
|
| 2 |
import sys
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
API_BASE_URL = os.environ.get("API_BASE_URL", "http://127.0.0.1:7860")
|
| 7 |
+
|
| 8 |
+
def wait_for_server(url, max_retries=30, delay=2):
|
| 9 |
+
print(f"Waiting for server at {url}...")
|
| 10 |
+
for i in range(max_retries):
|
| 11 |
+
try:
|
| 12 |
+
requests.get(url, timeout=5)
|
| 13 |
+
print("Server is up!")
|
| 14 |
+
return True
|
| 15 |
+
except Exception:
|
| 16 |
+
print(f"Retrying... ({i+1}/{max_retries})")
|
| 17 |
+
time.sleep(delay)
|
| 18 |
+
print("Server did not start in time.")
|
| 19 |
+
return False
|
| 20 |
|
| 21 |
def run_inference(difficulty="easy", seed=42):
|
| 22 |
+
print(f"Connecting to {API_BASE_URL}...")
|
|
|
|
| 23 |
start_time = time.time()
|
|
|
|
|
|
|
| 24 |
reset_payload = {"difficulty": difficulty, "seed": seed}
|
| 25 |
try:
|
| 26 |
reset_response = requests.post(f"{API_BASE_URL}/reset", json=reset_payload, timeout=60)
|
|
|
|
| 28 |
obs = reset_response.json().get("observation") or {}
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Error during reset: {e}")
|
| 31 |
+
return {"difficulty": difficulty, "score": 0.0, "time": time.time() - start_time, "error": str(e)}
|
| 32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 35 |
from baseline_inference import load_or_train
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Failed to load model: {e}")
|
| 39 |
model = None
|
| 40 |
+
|
| 41 |
done = False
|
| 42 |
total_reward = 0.0
|
| 43 |
+
|
|
|
|
| 44 |
while not done:
|
|
|
|
| 45 |
try:
|
| 46 |
state_response = requests.get(f"{API_BASE_URL}/state", timeout=60)
|
| 47 |
state_response.raise_for_status()
|
|
|
|
| 50 |
print(f"Error fetching state: {e}")
|
| 51 |
state_data = {}
|
| 52 |
|
|
|
|
| 53 |
content = state_data.get("content")
|
|
|
|
| 54 |
if not content:
|
| 55 |
+
content = (state_data.get("observation") or {}).get("content")
|
|
|
|
|
|
|
| 56 |
if not content:
|
| 57 |
content = obs.get("content", "")
|
| 58 |
+
|
|
|
|
| 59 |
action = 0
|
| 60 |
if model is not None and content:
|
| 61 |
try:
|
| 62 |
action = int(model.predict([str(content)])[0])
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"Prediction error: {e}")
|
| 65 |
action = 0
|
| 66 |
+
|
|
|
|
| 67 |
try:
|
| 68 |
+
step_response = requests.post(f"{API_BASE_URL}/step", json={"action": action}, timeout=60)
|
| 69 |
step_response.raise_for_status()
|
| 70 |
result = step_response.json() or {}
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Error during step: {e}")
|
| 73 |
+
return {"difficulty": difficulty, "score": total_reward, "time": time.time() - start_time, "error": str(e)}
|
| 74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
obs = result.get("observation") or {}
|
| 76 |
reward = result.get("reward", 0.0)
|
| 77 |
done = result.get("done", False)
|
|
|
|
|
|
|
| 78 |
total_reward += reward
|
| 79 |
|
| 80 |
+
return {"difficulty": difficulty, "score": total_reward, "time": time.time() - start_time}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def main():
|
| 83 |
try:
|
| 84 |
+
if not wait_for_server(f"{API_BASE_URL}/reset", max_retries=30, delay=2):
|
| 85 |
+
print("Exiting: server not ready.")
|
|
|
|
| 86 |
sys.exit(1)
|
|
|
|
| 87 |
result = run_inference()
|
| 88 |
print("Inference completed:", result)
|
| 89 |
except Exception as e:
|