Spaces:
Sleeping
Sleeping
Afifa Kureshi commited on
Commit ·
8f89f9b
1
Parent(s): d02e2f9
Fix Space startup health endpoint
Browse files- inference.py +52 -12
inference.py
CHANGED
|
@@ -1,24 +1,64 @@
|
|
| 1 |
-
from environment.cloud_env import CloudDataJanitorEnv
|
| 2 |
-
from agent.rule_agent import RuleBasedAgent
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
TASK_NAME = os.getenv("MY_ENV_V4_TASK", "easy")
|
| 11 |
MAX_STEPS = 10
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
rewards = []
|
| 18 |
-
|
| 19 |
-
# IMPORTANT — define success before try
|
| 20 |
success = False
|
| 21 |
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
|
| 24 |
print(
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from environment.cloud_env import CloudDataJanitorEnv
|
| 5 |
|
| 6 |
+
# 1. PULL THE EXACT VARIABLES INJECTED BY THE JUDGES
|
| 7 |
+
API_BASE_URL = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 8 |
+
API_KEY = os.environ.get("API_KEY", "dummy-key")
|
| 9 |
+
MODEL_NAME = os.environ.get("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 10 |
+
TASK_NAME = os.environ.get("MY_ENV_V4_TASK", "easy")
|
|
|
|
| 11 |
MAX_STEPS = 10
|
| 12 |
|
| 13 |
+
# 2. INITIALIZE THE CLIENT EXACTLY AS REQUESTED
|
| 14 |
+
client = OpenAI(
|
| 15 |
+
base_url=API_BASE_URL,
|
| 16 |
+
api_key=API_KEY
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
# 3. REPLACE THE RULE-BASED AGENT WITH A REAL LLM AGENT
|
| 20 |
+
class BaselineLLMAgent:
|
| 21 |
+
def choose_action(self, state):
|
| 22 |
+
prompt = f"""
|
| 23 |
+
You are a Data Janitor managing a cloud drive.
|
| 24 |
+
Current State: {state}
|
| 25 |
+
|
| 26 |
+
Choose one action. You MUST reply with ONLY raw, valid JSON. Do not use markdown blocks.
|
| 27 |
+
Valid actions formats:
|
| 28 |
+
{{"action_type": "move", "file_id": "f1", "destination_folder": "Images"}}
|
| 29 |
+
{{"action_type": "delete", "file_id": "f2"}}
|
| 30 |
+
{{"action_type": "zip", "file_id": "f3"}}
|
| 31 |
+
{{"action_type": "done"}}
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# This is the API call the proxy is waiting to see!
|
| 36 |
+
response = client.chat.completions.create(
|
| 37 |
+
model=MODEL_NAME,
|
| 38 |
+
messages=[{"role": "user", "content": prompt}],
|
| 39 |
+
temperature=0.1
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Clean up the response and parse it
|
| 43 |
+
raw_output = response.choices[0].message.content.strip("` \n")
|
| 44 |
+
if raw_output.startswith("json"):
|
| 45 |
+
raw_output = raw_output[4:]
|
| 46 |
+
|
| 47 |
+
return json.loads(raw_output)
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
# If the LLM breaks or hallucinates, safely end the loop
|
| 51 |
+
return {"action_type": "done"}
|
| 52 |
+
|
| 53 |
+
# Initialize the environment and the NEW agent
|
| 54 |
+
env = CloudDataJanitorEnv(task_name=TASK_NAME)
|
| 55 |
+
agent = BaselineLLMAgent()
|
| 56 |
|
| 57 |
rewards = []
|
|
|
|
|
|
|
| 58 |
success = False
|
| 59 |
|
| 60 |
+
# ... KEEP YOUR EXISTING try/except LOOP EXACTLY THE SAME BELOW THIS LINE ...
|
| 61 |
+
|
| 62 |
try:
|
| 63 |
|
| 64 |
print(
|