Spaces:
Runtime error
Runtime error
Update inference.py
Browse files- inference.py +74 -52
inference.py
CHANGED
|
@@ -1,53 +1,75 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
import json
|
| 4 |
-
import time
|
| 5 |
-
from openai import OpenAI
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
API_BASE_URL = os.getenv("API_BASE_URL", "https://
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
""
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
run_inference()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
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 |
+
|
| 10 |
+
# We'll hook this up to the powerful Pro model for the best logic
|
| 11 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "gemini-2.5-flash")
|
| 12 |
+
|
| 13 |
+
# 2. Put your REAL Gemini key right here (starts with AIza...)
|
| 14 |
+
GEMINI_API_KEY = os.getenv("HF_TOKEN", "GEMINI_API_KEYS")
|
| 15 |
+
|
| 16 |
+
# 3. Your live Hugging Face Space URL
|
| 17 |
+
ENV_URL = os.getenv("ENV_URL", "https://rohannk-datacenter-openenv.hf.space")
|
| 18 |
+
|
| 19 |
+
# We are strictly using the OpenAI client to satisfy the hackathon rules
|
| 20 |
+
client = OpenAI(
|
| 21 |
+
base_url=API_BASE_URL,
|
| 22 |
+
api_key=GEMINI_API_KEY
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def run_inference():
|
| 26 |
+
print("Initializing environment...")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
state_resp = requests.post(f"{ENV_URL}/reset").json()
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Error connecting to environment: {e}")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
done = False
|
| 35 |
+
while not done:
|
| 36 |
+
# Prepare prompt with current state
|
| 37 |
+
prompt = f"""
|
| 38 |
+
You are an AI managing a Data Center Cooling System.
|
| 39 |
+
Current State:
|
| 40 |
+
- Rack Temperatures (Goal: get all < 25.0 C): {state_resp['rack_temps']}
|
| 41 |
+
- Fan Speeds (Scale 1-5): {state_resp['fan_speeds']}
|
| 42 |
+
- Total Power (Goal: keep <= 500 W): {state_resp['power_usage']}W
|
| 43 |
+
|
| 44 |
+
Provide your action as a JSON object with keys: 'fan_change_rack1', 'fan_change_rack2', 'fan_change_rack3'.
|
| 45 |
+
Values must be integers: -1 (decrease speed), 0 (keep speed), or 1 (increase speed).
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
# Call Gemini (disguised as an OpenAI call)
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model=MODEL_NAME,
|
| 52 |
+
messages=[{"role": "user", "content": prompt}],
|
| 53 |
+
response_format={ "type": "json_object" }
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
action_str = response.choices[0].message.content
|
| 57 |
+
action = json.loads(action_str)
|
| 58 |
+
print(f"Agent Action: {action}")
|
| 59 |
+
|
| 60 |
+
# Take step in environment
|
| 61 |
+
step_resp = requests.post(f"{ENV_URL}/step", json=action).json()
|
| 62 |
+
|
| 63 |
+
state_resp = step_resp['state']
|
| 64 |
+
done = step_resp['done']
|
| 65 |
+
print(f"Current Scores: {step_resp['scores']}\n")
|
| 66 |
+
|
| 67 |
+
# Pause to prevent rate limits
|
| 68 |
+
time.sleep(8)
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"API Error: {e}")
|
| 72 |
+
break
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
run_inference()
|