TanujInsane commited on
Commit
676fd8d
·
1 Parent(s): 3a86e09

fix: add wait_for_server and remove sys.exit(1)

Browse files
Files changed (1) hide show
  1. inference.py +29 -4
inference.py CHANGED
@@ -2,13 +2,38 @@ import requests
2
  import traceback
3
  import sys
4
  import os
 
5
 
6
- BASE_URL = os.environ.get("ENV_URL", "https://tanujinsane-document-classification-env.hf.space")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  def main():
9
  print("[START] task=DocumentClassification-v1", flush=True)
 
 
 
 
10
  try:
11
- # Reset the environment
12
  reset_resp = requests.post(f"{BASE_URL}/reset", json={}, timeout=60)
13
  reset_resp.raise_for_status()
14
  obs = reset_resp.json()
@@ -17,7 +42,7 @@ def main():
17
  num_steps = 5
18
 
19
  for step in range(1, num_steps + 1):
20
- # Take a step with a dummy action
21
  step_resp = requests.post(f"{BASE_URL}/step", json={"action": 0}, timeout=60)
22
  step_resp.raise_for_status()
23
  result = step_resp.json()
@@ -38,7 +63,7 @@ def main():
38
  # On failure, ensure we print the END tag with zero score to satisfy the grader
39
  print(f"[END] task=DocumentClassification-v1 score=0.0 steps=0", flush=True)
40
  traceback.print_exc()
41
- sys.exit(1)
42
 
43
  if __name__ == "__main__":
44
  main()
 
2
  import traceback
3
  import sys
4
  import os
5
+ import time
6
 
7
+ BASE_URL = os.environ.get("ENV_URL", "http://127.0.0.1:7860")
8
+
9
+ def wait_for_server():
10
+ """Retries GET /reset to ensure server is ready before proceeding."""
11
+ print(f"Waiting for server at {BASE_URL}...")
12
+ for i in range(20):
13
+ try:
14
+ # Perform GET /reset as requested
15
+ resp = requests.get(f"{BASE_URL}/reset", timeout=5)
16
+ # If the server responds (even with 405 Method Not Allowed), it's online
17
+ if resp.status_code < 500:
18
+ print("Server online.")
19
+ return True
20
+ except requests.exceptions.RequestException:
21
+ pass
22
+
23
+ print(f"Server not ready, retrying ({i+1}/20) in 3s...")
24
+ time.sleep(3)
25
+
26
+ print("Warning: Server did not respond after 20 retries. Proceeding anyway...")
27
+ return False
28
 
29
  def main():
30
  print("[START] task=DocumentClassification-v1", flush=True)
31
+
32
+ # Wait for the service to be healthy
33
+ wait_for_server()
34
+
35
  try:
36
+ # Reset the environment (POST /reset is the valid action endpoint)
37
  reset_resp = requests.post(f"{BASE_URL}/reset", json={}, timeout=60)
38
  reset_resp.raise_for_status()
39
  obs = reset_resp.json()
 
42
  num_steps = 5
43
 
44
  for step in range(1, num_steps + 1):
45
+ # Take a step with a dummy action (0)
46
  step_resp = requests.post(f"{BASE_URL}/step", json={"action": 0}, timeout=60)
47
  step_resp.raise_for_status()
48
  result = step_resp.json()
 
63
  # On failure, ensure we print the END tag with zero score to satisfy the grader
64
  print(f"[END] task=DocumentClassification-v1 score=0.0 steps=0", flush=True)
65
  traceback.print_exc()
66
+ # sys.exit(1) removed - returning gracefully
67
 
68
  if __name__ == "__main__":
69
  main()