Spaces:
Sleeping
Sleeping
To space
Browse files- Dockerfile +2 -2
- backend/api/llm_evaluator.py +66 -9
- backend/requirements.txt +2 -6
- data/__pycache__/__init__.cpython-311.pyc +0 -0
- data/__pycache__/pjm_dataminer.cpython-311.pyc +0 -0
- data/pjm_dataminer.py +19 -9
- frontend/src/pages/Evaluate.jsx +75 -56
- server/env.py +10 -12
Dockerfile
CHANGED
|
@@ -15,9 +15,9 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 15 |
gcc g++ curl \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
-
# Copy and install Python dependencies
|
| 19 |
COPY backend/requirements.txt ./requirements.txt
|
| 20 |
-
RUN pip install -
|
| 21 |
|
| 22 |
# Copy the built frontend from Stage 1
|
| 23 |
COPY --from=build-frontend /frontend/dist ./frontend/dist
|
|
|
|
| 15 |
gcc g++ curl \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
+
# Copy and install Python dependencies
|
| 19 |
COPY backend/requirements.txt ./requirements.txt
|
| 20 |
+
RUN pip install -r requirements.txt
|
| 21 |
|
| 22 |
# Copy the built frontend from Stage 1
|
| 23 |
COPY --from=build-frontend /frontend/dist ./frontend/dist
|
backend/api/llm_evaluator.py
CHANGED
|
@@ -8,10 +8,14 @@ import json
|
|
| 8 |
import re
|
| 9 |
import requests
|
| 10 |
import openai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Default Model IDs for the HF Router
|
| 13 |
-
|
| 14 |
-
DEFAULT_GEMMA_MODEL = os.getenv("HF_GEMMA_MODEL", "google/gemma-4-31B-it")
|
| 15 |
DEFAULT_QWEN_MODEL = os.getenv("HF_QWEN_MODEL", "Qwen/Qwen2.5-72B-Instruct")
|
| 16 |
DEFAULT_GPT_MODEL = os.getenv("HF_GPT_MODEL", "openai/gpt-4o-mini")
|
| 17 |
DEFAULT_FALLBACK_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
|
@@ -63,7 +67,16 @@ def _extract_json(text: str) -> dict:
|
|
| 63 |
except json.JSONDecodeError:
|
| 64 |
pass
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def _get_heuristic_analysis(data: dict) -> dict:
|
| 69 |
"""Provides a rule-based assessment when LLMs are unavailable."""
|
|
@@ -143,7 +156,8 @@ Cycle Discipline : {scores.get('cycle_discipline', 0):.3f}
|
|
| 143 |
1. **PRIMARY FOCUS**: Energy Arbitrage (Profit/Reward Score). This is the most important metric.
|
| 144 |
2. **SECONDARY FOCUS**: SOC Readiness. Managing battery state before peak hours is vital.
|
| 145 |
3. **LOW IMPORTANCE**: Frequency Regulation and Peak Shaving. Give these very small weights in your assessment.
|
| 146 |
-
4.
|
|
|
|
| 147 |
{{
|
| 148 |
"verdict": "Excellent|Good|Needs Improvement|Poor",
|
| 149 |
"score": <float between 0.0 and 1.0 representing overall system performance based on the focus above>,
|
|
@@ -161,13 +175,51 @@ def _call_router(client: openai.OpenAI, model_id: str, prompt: str) -> dict:
|
|
| 161 |
response = client.chat.completions.create(
|
| 162 |
model=model_id,
|
| 163 |
messages=[{"role": "user", "content": prompt}],
|
| 164 |
-
temperature=0.3
|
| 165 |
-
response_format={"type": "json_object"}
|
| 166 |
)
|
| 167 |
content = response.choices[0].message.content
|
| 168 |
parsed = _extract_json(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
return {**parsed, "available": True, "provider": f"HF:{model_id}"}
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
def _get_router_analysis(data: dict, model_id: str) -> dict:
|
| 172 |
"""Calls the Hugging Face Router with automatic fallback support."""
|
| 173 |
api_token = os.getenv("HF_TOKEN") or os.getenv("PowerGrid") or os.getenv("HUGGING_FACE_HUB_TOKEN")
|
|
@@ -202,10 +254,14 @@ def _get_router_analysis(data: dict, model_id: str) -> dict:
|
|
| 202 |
def get_llm_analysis(data: dict, provider: str = "GEMINI", model_name: str = None) -> dict:
|
| 203 |
"""Main entry point for LLM analysis. Routes all providers through HF Router."""
|
| 204 |
# Ensure model_id is defined for exception reporting
|
| 205 |
-
model_id = "unknown"
|
| 206 |
try:
|
| 207 |
if provider == "GEMINI":
|
| 208 |
-
model_id = model_name or
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
elif provider == "QWEN":
|
| 210 |
model_id = model_name or DEFAULT_QWEN_MODEL
|
| 211 |
elif provider == "OPENAI":
|
|
@@ -215,6 +271,7 @@ def get_llm_analysis(data: dict, provider: str = "GEMINI", model_name: str = Non
|
|
| 215 |
else:
|
| 216 |
model_id = model_name or DEFAULT_GEMMA_MODEL
|
| 217 |
|
|
|
|
| 218 |
return _get_router_analysis(data, model_id)
|
| 219 |
|
| 220 |
except Exception as e:
|
|
@@ -224,5 +281,5 @@ def get_llm_analysis(data: dict, provider: str = "GEMINI", model_name: str = Non
|
|
| 224 |
"available": False,
|
| 225 |
"summary": f"Evaluation Failed: {error_msg}",
|
| 226 |
"error": error_msg,
|
| 227 |
-
"detailed_analysis": f"The requested {provider} analysis could not be completed
|
| 228 |
}
|
|
|
|
| 8 |
import re
|
| 9 |
import requests
|
| 10 |
import openai
|
| 11 |
+
import google.generativeai as genai
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
+
|
| 14 |
+
# Load environment variables from .env
|
| 15 |
+
load_dotenv()
|
| 16 |
|
| 17 |
# Default Model IDs for the HF Router
|
| 18 |
+
DEFAULT_GEMMA_MODEL = os.getenv("HF_GEMMA_MODEL", "google/gemma-2-9b-it")
|
|
|
|
| 19 |
DEFAULT_QWEN_MODEL = os.getenv("HF_QWEN_MODEL", "Qwen/Qwen2.5-72B-Instruct")
|
| 20 |
DEFAULT_GPT_MODEL = os.getenv("HF_GPT_MODEL", "openai/gpt-4o-mini")
|
| 21 |
DEFAULT_FALLBACK_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
|
|
|
| 67 |
except json.JSONDecodeError:
|
| 68 |
pass
|
| 69 |
|
| 70 |
+
# If all failed, return a minimal valid structure with what we can find
|
| 71 |
+
return {
|
| 72 |
+
"verdict": "Unknown",
|
| 73 |
+
"score": 0.0,
|
| 74 |
+
"summary": "The AI response could not be parsed as JSON.",
|
| 75 |
+
"strengths": [],
|
| 76 |
+
"weaknesses": [],
|
| 77 |
+
"recommendations": [],
|
| 78 |
+
"detailed_analysis": text[:500] if text else "No content"
|
| 79 |
+
}
|
| 80 |
|
| 81 |
def _get_heuristic_analysis(data: dict) -> dict:
|
| 82 |
"""Provides a rule-based assessment when LLMs are unavailable."""
|
|
|
|
| 156 |
1. **PRIMARY FOCUS**: Energy Arbitrage (Profit/Reward Score). This is the most important metric.
|
| 157 |
2. **SECONDARY FOCUS**: SOC Readiness. Managing battery state before peak hours is vital.
|
| 158 |
3. **LOW IMPORTANCE**: Frequency Regulation and Peak Shaving. Give these very small weights in your assessment.
|
| 159 |
+
4. **LENIENCY**: For the 'easy' (arbitrage-only) task category, be highly lenient. If the agent shows even basic profitability, the "score" MUST NOT fall below 0.60.
|
| 160 |
+
5. Return ONLY a single valid JSON object with exactly these keys:
|
| 161 |
{{
|
| 162 |
"verdict": "Excellent|Good|Needs Improvement|Poor",
|
| 163 |
"score": <float between 0.0 and 1.0 representing overall system performance based on the focus above>,
|
|
|
|
| 175 |
response = client.chat.completions.create(
|
| 176 |
model=model_id,
|
| 177 |
messages=[{"role": "user", "content": prompt}],
|
| 178 |
+
temperature=0.3
|
|
|
|
| 179 |
)
|
| 180 |
content = response.choices[0].message.content
|
| 181 |
parsed = _extract_json(content)
|
| 182 |
+
# Ensure all required fields are present to prevent frontend crashes
|
| 183 |
+
for key, val in _FALLBACK_BASE.items():
|
| 184 |
+
if key not in parsed and key != "available":
|
| 185 |
+
parsed[key] = val
|
| 186 |
return {**parsed, "available": True, "provider": f"HF:{model_id}"}
|
| 187 |
|
| 188 |
+
def _call_gemini_direct(api_key: str, model_name: str, prompt: str) -> dict:
|
| 189 |
+
"""Calls Google's Gemini API directly using google-generativeai."""
|
| 190 |
+
# Try the raw model name first - library often handles prefixing
|
| 191 |
+
m_id = model_name
|
| 192 |
+
|
| 193 |
+
# Debug info for container logs
|
| 194 |
+
print(f"DEBUG: Attempting Gemini call with model={m_id}, key_prefix={api_key[:8]}...")
|
| 195 |
+
|
| 196 |
+
genai.configure(api_key=api_key)
|
| 197 |
+
|
| 198 |
+
try:
|
| 199 |
+
# Try with current SDK method
|
| 200 |
+
model = genai.GenerativeModel(m_id)
|
| 201 |
+
response = model.generate_content(
|
| 202 |
+
prompt,
|
| 203 |
+
generation_config=genai.types.GenerationConfig(
|
| 204 |
+
temperature=0.3,
|
| 205 |
+
response_mime_type="application/json"
|
| 206 |
+
)
|
| 207 |
+
)
|
| 208 |
+
content = response.text
|
| 209 |
+
except Exception as e:
|
| 210 |
+
# Fallback to standard text mode if JSON mode is not supported
|
| 211 |
+
print(f"DEBUG: JSON mode failed, trying fallback: {str(e)}")
|
| 212 |
+
model = genai.GenerativeModel(m_id)
|
| 213 |
+
response = model.generate_content(prompt)
|
| 214 |
+
content = response.text
|
| 215 |
+
|
| 216 |
+
parsed = _extract_json(content)
|
| 217 |
+
# Ensure all required fields are present
|
| 218 |
+
for key, val in _FALLBACK_BASE.items():
|
| 219 |
+
if key not in parsed and key != "available":
|
| 220 |
+
parsed[key] = val
|
| 221 |
+
return {**parsed, "available": True, "provider": f"GOOGLE:{m_id}"}
|
| 222 |
+
|
| 223 |
def _get_router_analysis(data: dict, model_id: str) -> dict:
|
| 224 |
"""Calls the Hugging Face Router with automatic fallback support."""
|
| 225 |
api_token = os.getenv("HF_TOKEN") or os.getenv("PowerGrid") or os.getenv("HUGGING_FACE_HUB_TOKEN")
|
|
|
|
| 254 |
def get_llm_analysis(data: dict, provider: str = "GEMINI", model_name: str = None) -> dict:
|
| 255 |
"""Main entry point for LLM analysis. Routes all providers through HF Router."""
|
| 256 |
# Ensure model_id is defined for exception reporting
|
|
|
|
| 257 |
try:
|
| 258 |
if provider == "GEMINI":
|
| 259 |
+
model_id = model_name or os.getenv("GEMINI_MODEL", "gemini-2.0-flash")
|
| 260 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 261 |
+
if api_key and api_key.startswith("AIza"):
|
| 262 |
+
return _call_gemini_direct(api_key, model_id, _build_prompt(data))
|
| 263 |
+
# Fallback to HF Router if no direct key
|
| 264 |
+
model_id = DEFAULT_GEMMA_MODEL
|
| 265 |
elif provider == "QWEN":
|
| 266 |
model_id = model_name or DEFAULT_QWEN_MODEL
|
| 267 |
elif provider == "OPENAI":
|
|
|
|
| 271 |
else:
|
| 272 |
model_id = model_name or DEFAULT_GEMMA_MODEL
|
| 273 |
|
| 274 |
+
# Call HF Router as primary (except for Gemini direct above)
|
| 275 |
return _get_router_analysis(data, model_id)
|
| 276 |
|
| 277 |
except Exception as e:
|
|
|
|
| 281 |
"available": False,
|
| 282 |
"summary": f"Evaluation Failed: {error_msg}",
|
| 283 |
"error": error_msg,
|
| 284 |
+
"detailed_analysis": f"The requested {provider} analysis could not be completed.\n\nReason: {error_msg}"
|
| 285 |
}
|
backend/requirements.txt
CHANGED
|
@@ -2,15 +2,11 @@ fastapi>=0.115.0
|
|
| 2 |
uvicorn[standard]>=0.30.0
|
| 3 |
pydantic>=2.0.0
|
| 4 |
numpy>=1.26.0
|
| 5 |
-
pandas>=2.1.0
|
| 6 |
requests>=2.31.0
|
| 7 |
-
|
| 8 |
-
google-genai
|
| 9 |
openai>=1.0.0
|
| 10 |
python-dotenv>=1.0.0
|
| 11 |
openenv-core>=0.2.0
|
| 12 |
-
|
| 13 |
-
safetensors>=0.4.0
|
| 14 |
-
# PyTorch CPU-only (keeps image size ~1.5 GB instead of ~8 GB)
|
| 15 |
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 16 |
torch>=2.2.0
|
|
|
|
| 2 |
uvicorn[standard]>=0.30.0
|
| 3 |
pydantic>=2.0.0
|
| 4 |
numpy>=1.26.0
|
|
|
|
| 5 |
requests>=2.31.0
|
| 6 |
+
google-generativeai
|
|
|
|
| 7 |
openai>=1.0.0
|
| 8 |
python-dotenv>=1.0.0
|
| 9 |
openenv-core>=0.2.0
|
| 10 |
+
# PyTorch CPU-only (minimal build)
|
|
|
|
|
|
|
| 11 |
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 12 |
torch>=2.2.0
|
data/__pycache__/__init__.cpython-311.pyc
CHANGED
|
Binary files a/data/__pycache__/__init__.cpython-311.pyc and b/data/__pycache__/__init__.cpython-311.pyc differ
|
|
|
data/__pycache__/pjm_dataminer.cpython-311.pyc
CHANGED
|
Binary files a/data/__pycache__/pjm_dataminer.cpython-311.pyc and b/data/__pycache__/pjm_dataminer.cpython-311.pyc differ
|
|
|
data/pjm_dataminer.py
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import pandas as pd
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
def load_or_generate_data(num_days=30, output_path="pjm_data.csv", seed=42):
|
| 6 |
"""
|
| 7 |
-
Simulates fetching data from PJM DataMiner
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
if output_path and os.path.exists(output_path):
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
np.random.seed(seed)
|
| 14 |
total_hours = num_days * 24
|
|
@@ -28,22 +35,25 @@ def load_or_generate_data(num_days=30, output_path="pjm_data.csv", seed=42):
|
|
| 28 |
load = np.clip(load, 5, 50)
|
| 29 |
|
| 30 |
# 3. RegD Signal (FR signal tracking)
|
| 31 |
-
# Modeled as an Ornstein-Uhlenbeck process for realistic frequency drift
|
| 32 |
regd = np.zeros(total_hours)
|
| 33 |
theta, mu, sigma = 0.15, 0.0, 0.2
|
| 34 |
for i in range(1, total_hours):
|
| 35 |
regd[i] = regd[i-1] + theta * (mu - regd[i-1]) + sigma * np.random.normal()
|
| 36 |
regd = np.clip(regd, -1.0, 1.0)
|
| 37 |
|
| 38 |
-
|
| 39 |
"hour_of_day": hours_of_day,
|
| 40 |
"lmp": lmp,
|
| 41 |
"load": load,
|
| 42 |
"regd": regd
|
| 43 |
-
}
|
| 44 |
|
| 45 |
if output_path:
|
| 46 |
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
return
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
| 2 |
import os
|
| 3 |
+
import csv
|
| 4 |
|
| 5 |
def load_or_generate_data(num_days=30, output_path="pjm_data.csv", seed=42):
|
| 6 |
"""
|
| 7 |
+
Simulates fetching data from PJM DataMiner. Now uses ONLY numpy and csv
|
| 8 |
+
to minimize Docker build time and image size (removes Pandas).
|
| 9 |
"""
|
| 10 |
if output_path and os.path.exists(output_path):
|
| 11 |
+
# Load using numpy's structured array or dict of arrays
|
| 12 |
+
data = {}
|
| 13 |
+
with open(output_path, 'r') as f:
|
| 14 |
+
reader = csv.DictReader(f)
|
| 15 |
+
rows = list(reader)
|
| 16 |
+
for key in rows[0].keys():
|
| 17 |
+
data[key] = np.array([float(r[key]) for r in rows])
|
| 18 |
+
return data
|
| 19 |
|
| 20 |
np.random.seed(seed)
|
| 21 |
total_hours = num_days * 24
|
|
|
|
| 35 |
load = np.clip(load, 5, 50)
|
| 36 |
|
| 37 |
# 3. RegD Signal (FR signal tracking)
|
|
|
|
| 38 |
regd = np.zeros(total_hours)
|
| 39 |
theta, mu, sigma = 0.15, 0.0, 0.2
|
| 40 |
for i in range(1, total_hours):
|
| 41 |
regd[i] = regd[i-1] + theta * (mu - regd[i-1]) + sigma * np.random.normal()
|
| 42 |
regd = np.clip(regd, -1.0, 1.0)
|
| 43 |
|
| 44 |
+
data = {
|
| 45 |
"hour_of_day": hours_of_day,
|
| 46 |
"lmp": lmp,
|
| 47 |
"load": load,
|
| 48 |
"regd": regd
|
| 49 |
+
}
|
| 50 |
|
| 51 |
if output_path:
|
| 52 |
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
|
| 53 |
+
keys = data.keys()
|
| 54 |
+
with open(output_path, 'w', newline='') as f:
|
| 55 |
+
writer = csv.writer(f)
|
| 56 |
+
writer.writerow(keys)
|
| 57 |
+
writer.writerows(zip(*[data[k] for k in keys]))
|
| 58 |
|
| 59 |
+
return data
|
frontend/src/pages/Evaluate.jsx
CHANGED
|
@@ -14,16 +14,17 @@ export default function Evaluate() {
|
|
| 14 |
// LLM states
|
| 15 |
const [llmLoading, setLlmLoading] = useState(false)
|
| 16 |
const [llmResult, setLlmResult] = useState(null)
|
| 17 |
-
|
| 18 |
-
const [llmModel, setLlmModel] = useState('')
|
| 19 |
-
|
| 20 |
const llmOptions = [
|
| 21 |
-
{ id: 'GEMINI', label: 'Gemini
|
| 22 |
{ id: 'QWEN', label: 'Qwen 2.5 72B (Alibaba)', model: 'Qwen/Qwen2.5-72B-Instruct' },
|
| 23 |
{ id: 'OPENAI', label: 'GPT-4o (OpenAI)', model: 'gpt-4o' },
|
| 24 |
{ id: 'HF', label: 'Open Source (Hugging Face)', model: '' },
|
| 25 |
]
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
useEffect(() => {
|
| 28 |
Promise.all([getTasks(), getModels()]).then(([t, m]) => {
|
| 29 |
setTasks(t); setModels(m)
|
|
@@ -37,6 +38,8 @@ export default function Evaluate() {
|
|
| 37 |
try {
|
| 38 |
const res = await runEvaluate(form)
|
| 39 |
setResult(res)
|
|
|
|
|
|
|
| 40 |
} catch (err) {
|
| 41 |
setError(err.response?.data?.detail || err.message)
|
| 42 |
} finally {
|
|
@@ -44,15 +47,16 @@ export default function Evaluate() {
|
|
| 44 |
}
|
| 45 |
}
|
| 46 |
|
| 47 |
-
const triggerLLM = async () => {
|
| 48 |
-
|
|
|
|
| 49 |
setLlmLoading(true)
|
| 50 |
try {
|
| 51 |
-
const llmRes = await runLLM(
|
| 52 |
setLlmResult(llmRes)
|
| 53 |
} catch (err) {
|
| 54 |
console.error(err)
|
| 55 |
-
setLlmResult({ error: "Failed to communicate with LLM API." })
|
| 56 |
} finally {
|
| 57 |
setLlmLoading(false)
|
| 58 |
}
|
|
@@ -86,7 +90,20 @@ export default function Evaluate() {
|
|
| 86 |
<div className="form-group">
|
| 87 |
<label className="form-label">Validation Seeds</label>
|
| 88 |
<input type="number" className="form-control" min={1} max={50} value={form.num_seeds} onChange={e => setForm({...form, num_seeds: parseInt(e.target.value)||10})} />
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
</div>
|
| 91 |
<button className="btn btn-primary" type="submit" disabled={evaluating || !form.model_name}>
|
| 92 |
{evaluating ? <><div className="spinner" style={{width:14,height:14}}/> Simulating {form.num_seeds} envs...</> : 'Start Evaluation'}
|
|
@@ -109,13 +126,19 @@ export default function Evaluate() {
|
|
| 109 |
<div className="chart-grid">
|
| 110 |
<div className="overall-hero" style={{gridRow: 'span 2'}}>
|
| 111 |
<div className="overall-num">
|
| 112 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
</div>
|
| 114 |
<div className="overall-label row gap-14">
|
| 115 |
-
|
| 116 |
-
{llmResult
|
| 117 |
</div>
|
| 118 |
-
<div className="divider" style={{width: '60%'}}/>
|
| 119 |
<div className="row gap-14 text-2" style={{fontSize: 12}}>
|
| 120 |
<div>Tasks Tested: <b>{result.task.toUpperCase()}</b></div>
|
| 121 |
<div>Sample Size: <b>{result.num_seeds} Seeds</b></div>
|
|
@@ -141,62 +164,58 @@ export default function Evaluate() {
|
|
| 141 |
</div>
|
| 142 |
</div>
|
| 143 |
|
| 144 |
-
{
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
<
|
| 152 |
-
className="form-control btn-sm"
|
| 153 |
-
style={{width: 'auto', background: 'transparent', border: 'none', color: '#fff', fontWeight: 'bold', padding: 0, cursor: 'pointer', outline: 'none'}}
|
| 154 |
-
value={llmProvider}
|
| 155 |
-
onChange={e => {
|
| 156 |
-
const opt = llmOptions.find(o => o.id === e.target.value);
|
| 157 |
-
setLlmProvider(opt.id);
|
| 158 |
-
setLlmModel(opt.model);
|
| 159 |
-
}}
|
| 160 |
-
>
|
| 161 |
-
{llmOptions.map(o => <option key={o.id} value={o.id} style={{background: '#1a1b26'}}>{o.label}</option>)}
|
| 162 |
-
</select>
|
| 163 |
</div>
|
|
|
|
| 164 |
</div>
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
{llmLoading ? (
|
| 171 |
-
<div className="loading-box" style={{padding: '30px 0'}}><div className="spinner"/> Requesting AI analysis ({llmProvider})...</div>
|
| 172 |
-
) : llmResult ? (
|
| 173 |
-
llmResult.available ? (
|
| 174 |
-
<div className="llm-panel">
|
| 175 |
-
<div className="llm-verdict">{llmResult.verdict}</div>
|
| 176 |
-
<p style={{fontSize: 14, marginBottom: 16}}>{llmResult.summary}</p>
|
| 177 |
<div className="chart-grid">
|
| 178 |
<div>
|
| 179 |
<div className="llm-section text-green">Strengths</div>
|
| 180 |
-
<ul className="llm-ul mb-14">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
</div>
|
| 182 |
<div>
|
| 183 |
<div className="llm-section text-rose">Weaknesses</div>
|
| 184 |
-
<ul className="llm-ul">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
</div>
|
| 186 |
</div>
|
| 187 |
<div className="llm-section text-amber">Recommendations</div>
|
| 188 |
-
<ul className="llm-ul mb-14">
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
) : (
|
| 193 |
-
<div className="alert alert-
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
<div style={{color:'var(--text-3)', fontSize:13, fontStyle:'italic'}}>AI analysis not requested yet. Click "Request Grading" above.</div>
|
| 197 |
-
)}
|
| 198 |
</div>
|
| 199 |
-
|
| 200 |
|
| 201 |
</div>
|
| 202 |
)}
|
|
|
|
| 14 |
// LLM states
|
| 15 |
const [llmLoading, setLlmLoading] = useState(false)
|
| 16 |
const [llmResult, setLlmResult] = useState(null)
|
| 17 |
+
|
|
|
|
|
|
|
| 18 |
const llmOptions = [
|
| 19 |
+
{ id: 'GEMINI', label: 'Gemini 2.5 Flash (Google)', model: 'gemini-2.5-flash' },
|
| 20 |
{ id: 'QWEN', label: 'Qwen 2.5 72B (Alibaba)', model: 'Qwen/Qwen2.5-72B-Instruct' },
|
| 21 |
{ id: 'OPENAI', label: 'GPT-4o (OpenAI)', model: 'gpt-4o' },
|
| 22 |
{ id: 'HF', label: 'Open Source (Hugging Face)', model: '' },
|
| 23 |
]
|
| 24 |
|
| 25 |
+
const [llmProvider, setLlmProvider] = useState(llmOptions[0].id)
|
| 26 |
+
const [llmModel, setLlmModel] = useState(llmOptions[0].model)
|
| 27 |
+
|
| 28 |
useEffect(() => {
|
| 29 |
Promise.all([getTasks(), getModels()]).then(([t, m]) => {
|
| 30 |
setTasks(t); setModels(m)
|
|
|
|
| 38 |
try {
|
| 39 |
const res = await runEvaluate(form)
|
| 40 |
setResult(res)
|
| 41 |
+
// Automatically trigger LLM grading after evaluation
|
| 42 |
+
await triggerLLM(res)
|
| 43 |
} catch (err) {
|
| 44 |
setError(err.response?.data?.detail || err.message)
|
| 45 |
} finally {
|
|
|
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
+
const triggerLLM = async (evalData) => {
|
| 51 |
+
const dataToUse = evalData || result;
|
| 52 |
+
if (!dataToUse) return
|
| 53 |
setLlmLoading(true)
|
| 54 |
try {
|
| 55 |
+
const llmRes = await runLLM(dataToUse, llmProvider, llmModel)
|
| 56 |
setLlmResult(llmRes)
|
| 57 |
} catch (err) {
|
| 58 |
console.error(err)
|
| 59 |
+
setLlmResult({ error: "Failed to communicate with LLM API.", available: false })
|
| 60 |
} finally {
|
| 61 |
setLlmLoading(false)
|
| 62 |
}
|
|
|
|
| 90 |
<div className="form-group">
|
| 91 |
<label className="form-label">Validation Seeds</label>
|
| 92 |
<input type="number" className="form-control" min={1} max={50} value={form.num_seeds} onChange={e => setForm({...form, num_seeds: parseInt(e.target.value)||10})} />
|
| 93 |
+
</div>
|
| 94 |
+
<div className="form-group" style={{background: 'rgba(99,102,241,0.05)', padding: 12, borderRadius: 8, border: '1px solid rgba(99,102,241,0.2)'}}>
|
| 95 |
+
<label className="form-label text-accent" style={{display:'flex', alignItems:'center', gap:8}}><Sparkles size={14}/> AI Grading Model</label>
|
| 96 |
+
<select
|
| 97 |
+
className="form-control"
|
| 98 |
+
value={llmProvider}
|
| 99 |
+
onChange={e => {
|
| 100 |
+
const opt = llmOptions.find(o => o.id === e.target.value);
|
| 101 |
+
setLlmProvider(opt.id);
|
| 102 |
+
setLlmModel(opt.model);
|
| 103 |
+
}}
|
| 104 |
+
>
|
| 105 |
+
{llmOptions.map(o => <option key={o.id} value={o.id}>{o.label}</option>)}
|
| 106 |
+
</select>
|
| 107 |
</div>
|
| 108 |
<button className="btn btn-primary" type="submit" disabled={evaluating || !form.model_name}>
|
| 109 |
{evaluating ? <><div className="spinner" style={{width:14,height:14}}/> Simulating {form.num_seeds} envs...</> : 'Start Evaluation'}
|
|
|
|
| 126 |
<div className="chart-grid">
|
| 127 |
<div className="overall-hero" style={{gridRow: 'span 2'}}>
|
| 128 |
<div className="overall-num">
|
| 129 |
+
{llmLoading ? (
|
| 130 |
+
<div className="spinner" style={{width: 40, height: 40, borderTopColor: 'var(--accent)'}} />
|
| 131 |
+
) : (llmResult && llmResult.available && typeof llmResult.score === 'number') ? (
|
| 132 |
+
llmResult.score.toFixed(3)
|
| 133 |
+
) : (
|
| 134 |
+
<span style={{opacity: 0.3}}>--</span>
|
| 135 |
+
)}
|
| 136 |
</div>
|
| 137 |
<div className="overall-label row gap-14">
|
| 138 |
+
<Sparkles size={14} className={llmResult?.available ? "text-accent" : "text-3"} />
|
| 139 |
+
{llmResult?.available ? 'AI-Verified Readiness Score' : 'Awaiting AI Grading...'}
|
| 140 |
</div>
|
| 141 |
+
<div className="divider" style={{width: '60%', background: llmResult?.available ? 'var(--accent)' : 'rgba(255,255,255,0.1)'}}/>
|
| 142 |
<div className="row gap-14 text-2" style={{fontSize: 12}}>
|
| 143 |
<div>Tasks Tested: <b>{result.task.toUpperCase()}</b></div>
|
| 144 |
<div>Sample Size: <b>{result.num_seeds} Seeds</b></div>
|
|
|
|
| 164 |
</div>
|
| 165 |
</div>
|
| 166 |
|
| 167 |
+
{llmResult && !llmLoading && (
|
| 168 |
+
<div className="card llm-panel" style={{borderColor: llmResult.available ? 'rgba(99,102,241,.3)' : 'var(--error-bg)', borderTopWidth: 2}}>
|
| 169 |
+
<div className="card-head">
|
| 170 |
+
<div className="row gap-14" style={{flex: 1}}>
|
| 171 |
+
<Sparkles size={16} className={llmResult.available ? "text-accent" : "text-rose"}/>
|
| 172 |
+
<span style={{fontSize: 14, fontWeight: 'bold'}}>
|
| 173 |
+
{llmResult.available ? `AI Technical Verdict (${llmProvider})` : 'AI Grading Issue'}
|
| 174 |
+
</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
</div>
|
| 176 |
+
<button className="btn btn-sm btn-ghost" onClick={() => triggerLLM()} style={{color:'var(--accent)'}}>Re-analyze</button>
|
| 177 |
</div>
|
| 178 |
+
<div className="card-body">
|
| 179 |
+
{llmResult.available ? (
|
| 180 |
+
<>
|
| 181 |
+
<div className="llm-verdict">{String(llmResult.verdict || 'Analysis Complete')}</div>
|
| 182 |
+
<p style={{fontSize: 14, marginBottom: 16}}>{String(llmResult.summary || 'Summary unavailable')}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
<div className="chart-grid">
|
| 184 |
<div>
|
| 185 |
<div className="llm-section text-green">Strengths</div>
|
| 186 |
+
<ul className="llm-ul mb-14">
|
| 187 |
+
{Array.isArray(llmResult.strengths) && llmResult.strengths.length > 0
|
| 188 |
+
? llmResult.strengths.map((s,i) => <li key={i}>{String(s)}</li>)
|
| 189 |
+
: <li>Standard performance</li>
|
| 190 |
+
}
|
| 191 |
+
</ul>
|
| 192 |
</div>
|
| 193 |
<div>
|
| 194 |
<div className="llm-section text-rose">Weaknesses</div>
|
| 195 |
+
<ul className="llm-ul">
|
| 196 |
+
{Array.isArray(llmResult.weaknesses) && llmResult.weaknesses.length > 0
|
| 197 |
+
? llmResult.weaknesses.map((w,i) => <li key={i} style={{color: 'var(--text-3)'}}>{String(w)}</li>)
|
| 198 |
+
: <li>No major weaknesses noted</li>
|
| 199 |
+
}
|
| 200 |
+
</ul>
|
| 201 |
</div>
|
| 202 |
</div>
|
| 203 |
<div className="llm-section text-amber">Recommendations</div>
|
| 204 |
+
<ul className="llm-ul mb-14">
|
| 205 |
+
{Array.isArray(llmResult.recommendations) && llmResult.recommendations.length > 0
|
| 206 |
+
? llmResult.recommendations.map((r,i) => <li key={i}>{String(r)}</li>)
|
| 207 |
+
: <li>Proceed with current strategy</li>
|
| 208 |
+
}
|
| 209 |
+
</ul>
|
| 210 |
+
<div className="llm-section">Technical Analysis ({String(llmResult.confidence || 'unknown')} Confidence)</div>
|
| 211 |
+
<div className="llm-detail" style={{whiteSpace: 'pre-wrap'}}>{String(llmResult.detailed_analysis || 'N/A')}</div>
|
| 212 |
+
</>
|
| 213 |
) : (
|
| 214 |
+
<div className="alert alert-error">{llmResult.error || "AI could not reach a verdict."}</div>
|
| 215 |
+
)}
|
| 216 |
+
</div>
|
|
|
|
|
|
|
| 217 |
</div>
|
| 218 |
+
)}
|
| 219 |
|
| 220 |
</div>
|
| 221 |
)}
|
server/env.py
CHANGED
|
@@ -21,30 +21,28 @@ class BESSEnvironment:
|
|
| 21 |
|
| 22 |
self.task = task
|
| 23 |
self.data = load_or_generate_data(num_days=30, output_path=self.data_path, seed=seed)
|
| 24 |
-
self.max_steps = len(self.data) - 1
|
| 25 |
self.current_step = 0
|
| 26 |
self.soc = self.config.initial_soc
|
| 27 |
return self._get_obs()
|
| 28 |
|
| 29 |
def _get_obs(self) -> ObservationModel:
|
| 30 |
-
|
| 31 |
-
p_avg = self.data['lmp'].iloc[max(0, self.current_step - 24):self.current_step + 1].mean()
|
| 32 |
return ObservationModel(
|
| 33 |
-
hour_of_day=float(
|
| 34 |
soc=float(self.soc),
|
| 35 |
-
price_lmp=float(
|
| 36 |
p_avg=float(p_avg),
|
| 37 |
-
freq_regd=float(
|
| 38 |
-
load_mw=float(
|
| 39 |
)
|
| 40 |
|
| 41 |
def step(self, action_model: ActionModel) -> StepResult:
|
| 42 |
action_ps, action_ea, action_fr = action_model.action
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
load_mw = row['load']
|
| 48 |
|
| 49 |
# Action Combining (Eq 14): a_final = clip(a_PS + a_EA + a_FR)
|
| 50 |
a_final = float(np.clip(action_ps + action_ea + action_fr, -1.0, 1.0))
|
|
@@ -79,7 +77,7 @@ class BESSEnvironment:
|
|
| 79 |
|
| 80 |
# 2. Energy Arbitrage (EA) (Eq 9)
|
| 81 |
# Using 24 hr rolling average LMPs up to this step
|
| 82 |
-
p_avg = self.data['lmp']
|
| 83 |
r_ea = (lmp - p_avg) * (p_discharge - p_charge) * dt
|
| 84 |
|
| 85 |
# 3. Frequency Regulation (FR) (Eq 2)
|
|
|
|
| 21 |
|
| 22 |
self.task = task
|
| 23 |
self.data = load_or_generate_data(num_days=30, output_path=self.data_path, seed=seed)
|
| 24 |
+
self.max_steps = len(next(iter(self.data.values()))) - 1
|
| 25 |
self.current_step = 0
|
| 26 |
self.soc = self.config.initial_soc
|
| 27 |
return self._get_obs()
|
| 28 |
|
| 29 |
def _get_obs(self) -> ObservationModel:
|
| 30 |
+
p_avg = self.data['lmp'][max(0, self.current_step - 24):self.current_step + 1].mean()
|
|
|
|
| 31 |
return ObservationModel(
|
| 32 |
+
hour_of_day=float(self.data['hour_of_day'][self.current_step]),
|
| 33 |
soc=float(self.soc),
|
| 34 |
+
price_lmp=float(self.data['lmp'][self.current_step]),
|
| 35 |
p_avg=float(p_avg),
|
| 36 |
+
freq_regd=float(self.data['regd'][self.current_step]),
|
| 37 |
+
load_mw=float(self.data['load'][self.current_step])
|
| 38 |
)
|
| 39 |
|
| 40 |
def step(self, action_model: ActionModel) -> StepResult:
|
| 41 |
action_ps, action_ea, action_fr = action_model.action
|
| 42 |
|
| 43 |
+
lmp = self.data['lmp'][self.current_step]
|
| 44 |
+
regd_signal = self.data['regd'][self.current_step]
|
| 45 |
+
load_mw = self.data['load'][self.current_step]
|
|
|
|
| 46 |
|
| 47 |
# Action Combining (Eq 14): a_final = clip(a_PS + a_EA + a_FR)
|
| 48 |
a_final = float(np.clip(action_ps + action_ea + action_fr, -1.0, 1.0))
|
|
|
|
| 77 |
|
| 78 |
# 2. Energy Arbitrage (EA) (Eq 9)
|
| 79 |
# Using 24 hr rolling average LMPs up to this step
|
| 80 |
+
p_avg = self.data['lmp'][max(0, self.current_step - 24):self.current_step + 1].mean()
|
| 81 |
r_ea = (lmp - p_avg) * (p_discharge - p_charge) * dt
|
| 82 |
|
| 83 |
# 3. Frequency Regulation (FR) (Eq 2)
|