Spaces:
Runtime error
Runtime error
Update inference.py
Browse files- inference.py +29 -8
inference.py
CHANGED
|
@@ -4,6 +4,15 @@ import json
|
|
| 4 |
import time
|
| 5 |
from openai import OpenAI
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# 1. Point the OpenAI client to Google's Gemini servers!
|
| 8 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/")
|
| 9 |
|
|
@@ -23,15 +32,22 @@ client = OpenAI(
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def run_inference():
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
print(f"[START] task={task_name}", flush=True)
|
| 28 |
|
| 29 |
-
print("Initializing environment...")
|
| 30 |
|
| 31 |
try:
|
| 32 |
state_resp = requests.post(f"{ENV_URL}/reset").json()
|
| 33 |
except Exception as e:
|
| 34 |
-
print(f"Error connecting to environment: {e}")
|
| 35 |
print(f"[END] task={task_name} score=0.0 steps=0", flush=True)
|
| 36 |
return
|
| 37 |
|
|
@@ -62,7 +78,7 @@ def run_inference():
|
|
| 62 |
|
| 63 |
action_str = response.choices[0].message.content
|
| 64 |
action = json.loads(action_str)
|
| 65 |
-
print(f"Agent Action: {action}")
|
| 66 |
|
| 67 |
# Take step in environment
|
| 68 |
step_resp = requests.post(f"{ENV_URL}/step", json=action).json()
|
|
@@ -72,7 +88,12 @@ def run_inference():
|
|
| 72 |
|
| 73 |
scores = step_resp.get('scores', {})
|
| 74 |
if isinstance(scores, dict):
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
elif isinstance(scores, (int, float)):
|
| 77 |
current_reward = float(scores)
|
| 78 |
else:
|
|
@@ -81,16 +102,16 @@ def run_inference():
|
|
| 81 |
total_current_score += current_reward
|
| 82 |
|
| 83 |
print(f"[STEP] step={step} reward={current_reward}", flush=True)
|
| 84 |
-
print(f"Current Scores: {scores}\n")
|
| 85 |
|
| 86 |
# Pause to prevent rate limits
|
| 87 |
time.sleep(8)
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
print(f"API Error: {e}")
|
| 91 |
break
|
| 92 |
|
| 93 |
print(f"[END] task={task_name} score={total_current_score} steps={step}", flush=True)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
run_inference()
|
|
|
|
| 4 |
import time
|
| 5 |
from openai import OpenAI
|
| 6 |
|
| 7 |
+
# Load variables from .env file if it exists
|
| 8 |
+
if os.path.exists(".env"):
|
| 9 |
+
with open(".env") as f:
|
| 10 |
+
for line in f:
|
| 11 |
+
line = line.strip()
|
| 12 |
+
if line and not line.startswith("#") and "=" in line:
|
| 13 |
+
key, val = line.split("=", 1)
|
| 14 |
+
os.environ[key.strip()] = val.strip()
|
| 15 |
+
|
| 16 |
# 1. Point the OpenAI client to Google's Gemini servers!
|
| 17 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai/")
|
| 18 |
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
def run_inference():
|
| 35 |
+
import argparse
|
| 36 |
+
import os
|
| 37 |
+
parser = argparse.ArgumentParser()
|
| 38 |
+
parser.add_argument("--task", type=str, default=os.environ.get("TASK", "task_1_easy"))
|
| 39 |
+
parser.add_argument("--task-id", type=str, dest="task_id", default=None)
|
| 40 |
+
args, _ = parser.parse_known_args()
|
| 41 |
+
task_name = args.task_id or args.task
|
| 42 |
+
|
| 43 |
print(f"[START] task={task_name}", flush=True)
|
| 44 |
|
| 45 |
+
print("Initializing environment...", flush=True)
|
| 46 |
|
| 47 |
try:
|
| 48 |
state_resp = requests.post(f"{ENV_URL}/reset").json()
|
| 49 |
except Exception as e:
|
| 50 |
+
print(f"Error connecting to environment: {e}", flush=True)
|
| 51 |
print(f"[END] task={task_name} score=0.0 steps=0", flush=True)
|
| 52 |
return
|
| 53 |
|
|
|
|
| 78 |
|
| 79 |
action_str = response.choices[0].message.content
|
| 80 |
action = json.loads(action_str)
|
| 81 |
+
print(f"Agent Action: {action}", flush=True)
|
| 82 |
|
| 83 |
# Take step in environment
|
| 84 |
step_resp = requests.post(f"{ENV_URL}/step", json=action).json()
|
|
|
|
| 88 |
|
| 89 |
scores = step_resp.get('scores', {})
|
| 90 |
if isinstance(scores, dict):
|
| 91 |
+
score_key = "easy"
|
| 92 |
+
if "medium" in task_name.lower():
|
| 93 |
+
score_key = "medium"
|
| 94 |
+
elif "hard" in task_name.lower():
|
| 95 |
+
score_key = "hard"
|
| 96 |
+
current_reward = float(scores.get(score_key, 0.0))
|
| 97 |
elif isinstance(scores, (int, float)):
|
| 98 |
current_reward = float(scores)
|
| 99 |
else:
|
|
|
|
| 102 |
total_current_score += current_reward
|
| 103 |
|
| 104 |
print(f"[STEP] step={step} reward={current_reward}", flush=True)
|
| 105 |
+
print(f"Current Scores: {scores}\n", flush=True)
|
| 106 |
|
| 107 |
# Pause to prevent rate limits
|
| 108 |
time.sleep(8)
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
+
print(f"API Error: {e}", flush=True)
|
| 112 |
break
|
| 113 |
|
| 114 |
print(f"[END] task={task_name} score={total_current_score} steps={step}", flush=True)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
run_inference()
|