Rohannk commited on
Commit
f4e7deb
·
verified ·
1 Parent(s): a57d4eb

Update inference.py

Browse files
Files changed (1) hide show
  1. 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
- # Required Environment Variables
8
- API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
9
- MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
10
- HF_TOKEN = os.getenv("HF_TOKEN", "your-default-token-here")
11
- ENV_URL = os.getenv("ENV_URL", "http://localhost:7860") # Points to the FastAPI app
12
-
13
- client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
14
-
15
- def run_inference():
16
- print("Initializing environment...")
17
- state_resp = requests.post(f"{ENV_URL}/reset").json()
18
-
19
- done = False
20
- while not done:
21
- # 1. Prepare prompt with current state
22
- prompt = f"""
23
- You are an AI managing a Data Center Cooling System.
24
- Current State:
25
- - Rack Temperatures (Goal: get all < 25.0 C): {state_resp['rack_temps']}
26
- - Fan Speeds (Scale 1-5): {state_resp['fan_speeds']}
27
- - Total Power (Goal: keep <= 500 W): {state_resp['power_usage']}W
28
-
29
- Provide your action as a JSON object with keys: 'fan_change_rack1', 'fan_change_rack2', 'fan_change_rack3'.
30
- Values must be integers: -1 (decrease speed), 0 (keep speed), or 1 (increase speed).
31
- """
32
-
33
- # 2. Call LLM
34
- response = client.chat.completions.create(
35
- model=MODEL_NAME,
36
- messages=[{"role": "user", "content": prompt}],
37
- response_format={ "type": "json_object" }
38
- )
39
-
40
- action_str = response.choices[0].message.content
41
- action = json.loads(action_str)
42
- print(f"Agent Action: {action}")
43
-
44
- # 3. Take step in environment
45
- step_resp = requests.post(f"{ENV_URL}/step", json=action).json()
46
-
47
- state_resp = step_resp['state']
48
- done = step_resp['done']
49
- print(f"Current Scores: {step_resp['scores']}\n")
50
- time.sleep(1) # Prevent rate limits
51
-
52
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()